Shuffling functions into new Xcode source organization

This commit is contained in:
starkos 2009-11-03 21:35:24 +00:00
parent 45d6a1d568
commit 03b9eb3deb
9 changed files with 250 additions and 165 deletions

View File

@ -60,7 +60,7 @@
-- Xcode action
"actions/xcode/_xcode.lua",
"actions/xcode/xcode_sections.lua",
"actions/xcode/xcode_common.lua",
"actions/xcode/xcode_solution.lua",
"actions/xcode/xcode_project.lua",
"actions/xcode/xcode_pbxproj.lua",

View File

@ -0,0 +1,86 @@
--
-- xcode_common.lua
-- Functions to generate the different sections of an Xcode project.
-- Copyright (c) 2009 Jason Perkins and the Premake project
--
local xcode = premake.xcode
local tree = premake.tree
--
-- Return the Xcode build category for a given file, based on the file extension.
--
-- @param node
-- The node to identify.
-- @returns
-- An Xcode build category, one of "Sources", "Resources", "Frameworks", or nil.
--
function xcode.getbuildcategory(node)
local categories = {
[".c"] = "Sources",
[".cc"] = "Sources",
[".cpp"] = "Sources",
[".cxx"] = "Sources",
[".framework"] = "Frameworks",
[".m"] = "Sources",
[".strings"] = "Resources",
[".nib"] = "Resources",
[".xib"] = "Resources",
}
return categories[path.getextension(node.name)]
end
--
-- Retrieves a unique 12 byte ID for an object. This function accepts and ignores two
-- parameters 'node' and 'usage', which are used by an alternative implementation of
-- this function for testing.
--
-- @returns
-- A 24-character string representing the 12 byte ID.
--
function xcode.newid()
return string.format("%04X%04X%04X%012d", math.random(0, 32767), math.random(0, 32767), math.random(0, 32767), os.time())
end
---------------------------------------------------------------------------
-- Section generator functions, in the same order in which they appear
-- in the .pbxproj file
---------------------------------------------------------------------------
function xcode.Header()
_p('// !$*UTF8*$!')
_p('{')
_p('\tarchiveVersion = 1;')
_p('\tclasses = {')
_p('\t};')
_p('\tobjectVersion = 45;')
_p('\tobjects = {')
_p('')
end
function xcode.PBXBuildFile(tr)
_p('/* Begin PBXBuildFile section */')
tree.traverse(tr, {
onnode = function(node)
if node.buildid then
_p(2,'%s /* %s in %s */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };',
node.buildid, node.name, xcode.getbuildcategory(node), node.id, node.name)
end
end
})
_p('/* End PBXBuildFile section */')
_p('')
end
function xcode.Footer()
_p(1,'};')
_p('\trootObject = 08FB7793FE84155DC02AAC07 /* Project object */;')
_p('}')
end

View File

@ -153,31 +153,6 @@
end
--
-- Return the Xcode build category for a given file, based on the file extension.
--
-- @param node
-- The node to identify.
-- @returns
-- An Xcode build category, one of "Sources", "Resources", "Frameworks", or nil.
--
function xcode.getbuildcategory(node)
local categories = {
[".c"] = "Sources",
[".cc"] = "Sources",
[".cpp"] = "Sources",
[".cxx"] = "Sources",
[".framework"] = "Frameworks",
[".m"] = "Sources",
[".strings"] = "Resources",
[".nib"] = "Resources",
[".xib"] = "Resources",
}
return categories[path.getextension(node.name)]
end
--
-- Return the Xcode type for a given file, based on the file extension.
--
@ -261,20 +236,6 @@
end
--
-- Retrieves a unique 12 byte ID for an object. This function accepts and ignores two
-- parameters 'node' and 'usage', which are used by an alternative implementation of
-- this function for testing.
--
-- @returns
-- A 24-character string representing the 12 byte ID.
--
function xcode.newid()
return string.format("%04X%04X%04X%012d", math.random(0, 32767), math.random(0, 32767), math.random(0, 32767), os.time())
end
--
-- Converts a path or list of paths from project-relative to solution-relative.
--
@ -306,21 +267,6 @@
---------------------------------------------------------------------------
function xcode.PBXBuildFile(tr)
_p('/* Begin PBXBuildFile section */')
tree.traverse(tr, {
onnode = function(node)
if node.buildid then
_p(2,'%s /* %s in %s */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };',
node.buildid, node.name, xcode.getbuildcategory(node), node.id, node.name)
end
end
})
_p('/* End PBXBuildFile section */')
_p('')
end
function xcode.PBXFileReference(tr)
_p('/* Begin PBXFileReference section */')

View File

@ -17,8 +17,22 @@
-- A tree, loaded with metadata, which mirrors Xcode's view of the project.
--
local function buildtree(prj)
local tr = tree.new(prj.name)
function xcode.buildprjtree(prj)
local tr = premake.project.buildsourcetree(prj)
-- Final setup
tree.traverse(tr, {
onnode = function(node)
-- assign IDs to every node in the tree
node.id = xcode.newid(node)
-- assign build IDs to buildable files
if xcode.getbuildcategory(node) then
node.buildid = xcode.newid(node, "build")
end
end
}, true)
return tr
end
@ -31,7 +45,8 @@
--
function premake.xcode.project(prj)
tr = buildtree(prj)
local tr = xcode.buildprjtree(prj)
xcode.Header(tr)
xcode.PBXBuildFile(tr)
xcode.Footer(tr)
end

View File

@ -1,25 +0,0 @@
--
-- xcode_sections.lua
-- Functions to generate the different sections of an Xcode project.
-- Copyright (c) 2009 Jason Perkins and the Premake project
--
local xcode = premake.xcode
function xcode.Header()
_p('// !$*UTF8*$!')
_p('{')
_p('\tarchiveVersion = 1;')
_p('\tclasses = {')
_p('\t};')
_p('\tobjectVersion = 45;')
_p('\tobjects = {')
_p('')
end
function xcode.Footer()
_p(1,'};')
_p('\trootObject = 08FB7793FE84155DC02AAC07 /* Project object */;')
_p('}')
end

View File

@ -8,43 +8,6 @@
local xcode = premake.xcode
--
-- Replacement for xcode.newid(). Creates a synthetic ID based on the node name,
-- it's intended usage (file ID, build ID, etc.) and its place in the tree. This
-- makes it easier to tell if the right ID is being used in the right places.
--
local used_ids = {}
xcode.newid = function(node, usage)
-- assign special usages depending on where this node lives in the tree,
-- to help distinguish nodes that are likely to have the same name
if not usage and node.parent then
local grandparent = node.parent.parent
if grandparent then
if node.parent == grandparent.products then
usage = "product"
end
end
end
local name = node.name
if usage then
name = name .. ":" .. usage
end
if used_ids[name] then
local count = used_ids[name] + 1
used_ids[name] = count
name = name .. "(" .. count .. ")"
else
used_ids[name] = 1
end
return "[" .. name .. "]"
end
--
-- Setup
--
@ -53,7 +16,7 @@
function T.xcode3.setup()
premake.action.set("xcode3")
-- reset the list of generated IDs
used_ids = { }
xcode.used_ids = { }
sln = test.createsolution()
end
@ -64,53 +27,10 @@
end
---------------------------------------------------------------------------
-- Header/footer tests
---------------------------------------------------------------------------
function T.xcode3.Header_IsCorrect()
prepare()
xcode.Header()
test.capture [[
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 45;
objects = {
]]
end
function T.xcode3.Footer()
prepare()
xcode.Footer()
test.capture [[
};
rootObject = 08FB7793FE84155DC02AAC07 /* Project object */;
}
]]
end
---------------------------------------------------------------------------
-- PBXBuildFile tests
---------------------------------------------------------------------------
function T.xcode3.PBXBuildFile_ListsBuildableSources()
files { "source.h", "source.c", "source.cpp", "Info.plist" }
prepare()
xcode.PBXBuildFile(tr)
test.capture [[
/* Begin PBXBuildFile section */
[source.c:build] /* source.c in Sources */ = {isa = PBXBuildFile; fileRef = [source.c] /* source.c */; };
[source.cpp:build] /* source.cpp in Sources */ = {isa = PBXBuildFile; fileRef = [source.cpp] /* source.cpp */; };
/* End PBXBuildFile section */
]]
end
function T.xcode3.PBXBuildFile_ListsResourceFilesOnlyOnceWithGroupID()
files { "English.lproj/MainMenu.xib", "French.lproj/MainMenu.xib" }

View File

@ -0,0 +1,87 @@
--
-- tests/actions/xcode/test_xcode_common.lua
-- Automated test suite for functions shared between Xcode projects and solutions
-- Copyright (c) 2009 Jason Perkins and the Premake project
--
T.xcode3common = { }
local suite = T.xcode3common
local xcode = premake.xcode
--
-- Replacement for xcode.newid(). Creates a synthetic ID based on the node name,
-- it's intended usage (file ID, build ID, etc.) and its place in the tree. This
-- makes it easier to tell if the right ID is being used in the right places.
--
xcode.used_ids = {}
xcode.newid = function(node, usage)
-- assign special usages depending on where this node lives in the tree,
-- to help distinguish nodes that are likely to have the same name
if not usage and node.parent then
local grandparent = node.parent.parent
if grandparent then
if node.parent == grandparent.products then
usage = "product"
end
end
end
local name = node.name
if usage then
name = name .. ":" .. usage
end
if xcode.used_ids[name] then
local count = xcode.used_ids[name] + 1
xcode.used_ids[name] = count
name = name .. "(" .. count .. ")"
else
xcode.used_ids[name] = 1
end
return "[" .. name .. "]"
end
---------------------------------------------------------------------------
-- Setup
---------------------------------------------------------------------------
function suite.setup()
io.capture()
end
---------------------------------------------------------------------------
-- Header/footer tests
---------------------------------------------------------------------------
function suite.Header_IsCorrect()
xcode.Header()
test.capture [[
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 45;
objects = {
]]
end
function suite.Footer()
xcode.Footer()
test.capture [[
};
rootObject = 08FB7793FE84155DC02AAC07 /* Project object */;
}
]]
end

View File

@ -0,0 +1,51 @@
--
-- tests/actions/xcode/test_xcode_project.lua
-- Automated test suite for Xcode project generation.
-- Copyright (c) 2009 Jason Perkins and the Premake project
--
T.xcode3_project = { }
local suite = T.xcode3_project
local xcode = premake.xcode
---------------------------------------------------------------------------
-- Setup/Teardown
---------------------------------------------------------------------------
local sln, tr
function suite.setup()
premake.action.set("xcode3")
xcode.used_ids = { } -- reset the list of generated IDs
sln = test.createsolution()
end
local function prepare()
io.capture()
premake.buildconfigs()
local prj = sln.projects[1]
local cfg = premake.getconfig(prj)
cfg.name = prj.name
cfg.blocks = prj.blocks
tr = xcode.buildprjtree(cfg)
end
---------------------------------------------------------------------------
-- PBXBuildFile tests
---------------------------------------------------------------------------
function suite.PBXBuildFile_ListsBuildableSources()
files { "source.h", "source.c", "source.cpp", "Info.plist" }
prepare()
xcode.PBXBuildFile(tr)
test.capture [[
/* Begin PBXBuildFile section */
[source.c:build] /* source.c in Sources */ = {isa = PBXBuildFile; fileRef = [source.c] /* source.c */; };
[source.cpp:build] /* source.cpp in Sources */ = {isa = PBXBuildFile; fileRef = [source.cpp] /* source.cpp */; };
/* End PBXBuildFile section */
]]
end

View File

@ -60,6 +60,11 @@
dofile("base/test_path.lua")
dofile("base/test_tree.lua")
dofile("actions/test_clean.lua")
-- Xcode tests
dofile("actions/xcode/test_xcode_common.lua")
dofile("actions/xcode/test_xcode_project.lua")
dofile("actions/test_xcode.lua")