Added support for localized .xib files

This commit is contained in:
starkos 2009-08-27 13:27:47 +00:00
parent da2f037d9c
commit c04c4ddf3d
4 changed files with 122 additions and 79 deletions

View File

@ -32,39 +32,54 @@
local prjnode = premake.project.buildsourcetree(prj) local prjnode = premake.project.buildsourcetree(prj)
tree.insert(ctx.root, prjnode) tree.insert(ctx.root, prjnode)
-- localized files need an extra ID (see below) -- first pass over the tree to handle resource files like MainMenu.xib. If present,
prjnode.variantgroups = { } -- create a new project-level group named "Resources" to contain the files, and
-- virtual groups to contain each language variant. I'm converting how these files
-- are stored on disk (English.lproj/MainMenu.xib) to how they are shown in Xcode
-- (Resources/MainMenu.xib/English).
tree.traverse(prjnode, { tree.traverse(prjnode, {
-- assign IDs to all nodes in the tree
onnode = function(node)
node.id = xcode.newid()
end,
onleaf = function(node) onleaf = function(node)
if xcode.islocalized(node) then
-- create a "Resources" folder if necessary
if not prjnode.resources then
prjnode.resources = tree.new("Resources")
tree.insert(prjnode, prjnode.resources)
end
-- create node to represent the file; will become a virtual group later
if not prjnode.resources.children[node.name] then
local group = tree.new(node.name)
group.languages = { }
prjnode.resources.children[node.name] = group
tree.insert(prjnode.resources, group)
end
-- add this language to the group
local lang = path.getbasename(node.parent.name)
prjnode.resources.children[node.name].languages[lang] = node
end
end
})
-- second pass: finish configuring things
tree.traverse(prjnode, {
onnode = function(node)
-- assign IDs to all nodes in the tree
node.id = xcode.newid()
-- Premake is setup for the idea of a solution file referencing multiple project files, -- Premake is setup for the idea of a solution file referencing multiple project files,
-- but Xcode uses a single file for everything. Convert the file paths from project -- but Xcode uses a single file for everything. Convert the file paths from project
-- location relative to solution (the one Xcode file) location relative to compensate. -- location relative to solution (the one Xcode file) location relative to compensate.
if node.path then if node.path then
node.path = xcode.rebase(prj, node.path) node.path = xcode.rebase(prj, node.path)
end end
end,
onleaf = function(node)
-- assign a build ID to buildable files -- assign a build ID to buildable files
local category = xcode.getfilecategory(node.name) if xcode.getfilecategory(node.name) then
if category then
node.buildid = xcode.newid() node.buildid = xcode.newid()
end end
-- localized files are stored in "variant groups". The file, such as 'MainMenu.xib',
-- only appears once in the project tree with each language listed underneath it.
-- (this is the reverse of how it actually appears on the filesystem). This file
-- has an extra ID to represent this group, in addition to its file ID.
if xcode.islocalized(node) then
if not prjnode.variantgroups[node.name] then
prjnode.variantgroups[node.name] = { id = xcode.newid() }
end
table.insert(prjnode.variantgroups[node.name], node)
end
end end
}, true) }, true)
end end
@ -119,6 +134,7 @@
end end
-- --
-- Return the Xcode category (for lack of a better term) for a file, such -- Return the Xcode category (for lack of a better term) for a file, such
-- as "Sources" or "Resources". -- as "Sources" or "Resources".
@ -130,7 +146,6 @@
-- --
function xcode.getfilecategory(fname) function xcode.getfilecategory(fname)
print("Category for ", fname)
local categories = { local categories = {
[".c" ] = "Sources", [".c" ] = "Sources",
[".cc" ] = "Sources", [".cc" ] = "Sources",
@ -210,6 +225,9 @@
-- --
function xcode.islocalized(node) function xcode.islocalized(node)
if path.getextension(node.name) == ".lproj" then
return true
end
if xcode.getfilecategory(node.name) ~= "Resources" then if xcode.getfilecategory(node.name) ~= "Resources" then
return false return false
end end
@ -281,30 +299,12 @@
function xcode.PBXBuildFile(ctx) function xcode.PBXBuildFile(ctx)
local resources local resources
_p('/* Begin PBXBuildFile section */') _p('/* Begin PBXBuildFile section */')
local prjnode
tree.traverse(ctx.root, { tree.traverse(ctx.root, {
onnode = function(node)
-- remember the project node, for variant groups below
if node.project then
prjnode = node
resources = { }
end
end,
onleaf = function(node) onleaf = function(node)
if node.buildid then if node.buildid then
local id = node.id if xcode.islocalized(node) then return end
-- localized resources are only listed once, add use their variant group ID
-- rather than a file ID, so all languages are referenced at once.
if xcode.islocalized(node) then
if resources[node.name] then return end
id = prjnode.variantgroups[node.name].id
resources[node.name] = true
end
_p('\t\t%s /* %s in %s */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };', _p('\t\t%s /* %s in %s */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };',
node.buildid, node.name, xcode.getfilecategory(node.name), id, node.name) node.buildid, node.name, xcode.getfilecategory(node.name), node.id, node.name)
end end
end end
}) })
@ -317,7 +317,6 @@
_p('/* Begin PBXFileReference section */') _p('/* Begin PBXFileReference section */')
tree.traverse(ctx.root, { tree.traverse(ctx.root, {
onleaf = function(node) onleaf = function(node)
-- is this a project with no files?
if not node.path then return end if not node.path then return end
local nodename, nodepath, encoding local nodename, nodepath, encoding
@ -347,13 +346,19 @@
function xcode.PBXGroup(ctx) function xcode.PBXGroup(ctx)
_p('/* Begin PBXGroup section */') _p('/* Begin PBXGroup section */')
-- create groups for each branch node in the tree, skipping over localization
-- groups which get flipped around and put in a special "Resources" group.
tree.traverse(ctx.prjroot, { tree.traverse(ctx.prjroot, {
onbranch = function(node, depth) onbranch = function(node)
if xcode.islocalized(node) then return end
_p('\t\t%s /* %s */ = {', node.id, node.name) _p('\t\t%s /* %s */ = {', node.id, node.name)
_p('\t\t\tisa = PBXGroup;') _p('\t\t\tisa = PBXGroup;')
_p('\t\t\tchildren = (') _p('\t\t\tchildren = (')
for _, child in ipairs(node.children) do for _, child in ipairs(node.children) do
_p('\t\t\t\t%s /* %s */,', child.id, child.name) if not xcode.islocalized(child) then
_p('\t\t\t\t%s /* %s */,', child.id, child.name)
end
end end
_p('\t\t\t);') _p('\t\t\t);')
_p('\t\t\tname = %s;', node.name) _p('\t\t\tname = %s;', node.name)
@ -364,6 +369,7 @@
_p('\t\t};') _p('\t\t};')
end end
}, true) }, true)
_p('/* End PBXGroup section */') _p('/* End PBXGroup section */')
_p('') _p('')
end end
@ -372,17 +378,19 @@
function xcode.PBXVariantGroup(ctx) function xcode.PBXVariantGroup(ctx)
_p('/* Begin PBXVariantGroup section */') _p('/* Begin PBXVariantGroup section */')
for _, prjnode in ipairs(ctx.root.children) do for _, prjnode in ipairs(ctx.root.children) do
for name, group in pairs(prjnode.variantgroups) do if prjnode.resources then
_p('\t\t%s /* %s */ = {', group.id, name) for _, node in ipairs(prjnode.resources.children) do
_p('\t\t\tisa = PBXVariantGroup;') _p('\t\t%s /* %s */ = {', node.id, node.name)
_p('\t\t\tchildren = (') _p('\t\t\tisa = PBXVariantGroup;')
for _, node in ipairs(group) do _p('\t\t\tchildren = (')
_p('\t\t\t\t%s /* %s */,', node.id, path.getbasename(node.parent.name)) for lang, file in pairs(node.languages) do
_p('\t\t\t\t%s /* %s */,', file.id, lang)
end
_p('\t\t\t);')
_p('\t\t\tname = %s;', node.name)
_p('\t\t\tsourceTree = "<group>";')
_p('\t\t};')
end end
_p('\t\t\t);')
_p('\t\t\tname = %s;', name)
_p('\t\t\tsourceTree = "<group>";')
_p('\t\t};')
end end
end end
_p('/* End PBXVariantGroup section */') _p('/* End PBXVariantGroup section */')

View File

@ -50,6 +50,15 @@
end end
--
-- Returns true if the table is empty, and contains no indexed or keyed values.
--
function table.isempty(t)
return not next(t)
end
-- --
-- Adds the values from one array to the end of another and -- Adds the values from one array to the end of another and
-- returns the result. -- returns the result.

View File

@ -108,21 +108,7 @@
xcode.PBXBuildFile(ctx) xcode.PBXBuildFile(ctx)
test.capture [[ test.capture [[
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
000000000005 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000006 /* MainMenu.xib */; }; 000000000011 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000010 /* MainMenu.xib */; };
/* End PBXBuildFile section */
]]
end
function T.xcode3.PBXBuildFile_ListsResourceFilesOnlyOnceWithGroupID()
files {
"English.lproj/MainMenu.xib", "French.lproj/MainMenu.xib"
}
prepare()
xcode.PBXBuildFile(ctx)
test.capture [[
/* Begin PBXBuildFile section */
000000000005 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000006 /* MainMenu.xib */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
]] ]]
end end
@ -136,8 +122,8 @@
xcode.PBXBuildFile(ctx) xcode.PBXBuildFile(ctx)
test.capture [[ test.capture [[
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
000000000006 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000007 /* MainMenu.xib */; }; 000000000012 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000011 /* MainMenu.xib */; };
000000000015 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000016 /* MainMenu.xib */; }; 000000000023 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000022 /* MainMenu.xib */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
]] ]]
end end
@ -182,7 +168,7 @@
test.capture [[ test.capture [[
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
000000000004 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; }; 000000000004 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
000000000008 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; }; 000000000007 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; };
]] ]]
end end
@ -196,9 +182,9 @@
test.capture [[ test.capture [[
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
000000000005 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; }; 000000000005 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
000000000009 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; }; 000000000008 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; };
000000000014 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; }; 000000000016 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
000000000018 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; }; 000000000019 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; };
]] ]]
end end
@ -264,6 +250,33 @@
end end
function T.xcode3.PBXGroup_CreatesResourceSubgroup()
files { "English.lproj/MainMenu.xib", "French.lproj/MainMenu.xib" }
prepare()
xcode.PBXGroup(ctx)
test.capture [[
/* Begin PBXGroup section */
000000000002 /* MyProject */ = {
isa = PBXGroup;
children = (
000000000009 /* Resources */,
);
name = MyProject;
sourceTree = "<group>";
};
000000000009 /* Resources */ = {
isa = PBXGroup;
children = (
000000000010 /* MainMenu.xib */,
);
name = Resources;
sourceTree = "<group>";
};
/* End PBXGroup section */
]]
end
-- --
-- PBXVariantGroup section tests -- PBXVariantGroup section tests
@ -277,11 +290,11 @@
xcode.PBXVariantGroup(ctx) xcode.PBXVariantGroup(ctx)
test.capture [[ test.capture [[
/* Begin PBXVariantGroup section */ /* Begin PBXVariantGroup section */
000000000006 /* MainMenu.xib */ = { 000000000010 /* MainMenu.xib */ = {
isa = PBXVariantGroup; isa = PBXVariantGroup;
children = ( children = (
000000000007 /* French */,
000000000004 /* English */, 000000000004 /* English */,
000000000008 /* French */,
); );
name = MainMenu.xib; name = MainMenu.xib;
sourceTree = "<group>"; sourceTree = "<group>";

View File

@ -31,3 +31,16 @@
t = { "one", "two", "three", "four" } t = { "one", "two", "three", "four" }
test.isequal("[one], [two], [three], [four]", table.implode(t, "[", "]", ", ")) test.isequal("[one], [two], [three], [four]", table.implode(t, "[", "]", ", "))
end end
--
-- table.isempty() tests
--
function T.table.isempty_ReturnsTrueOnEmpty()
test.istrue(table.isempty({}))
end
function T.table.isempty_ReturnsFalseOnNotEmpty()
test.isfalse(table.isempty({ 1 }))
end