Added support for localized .xib files
This commit is contained in:
parent
da2f037d9c
commit
c04c4ddf3d
@ -32,39 +32,54 @@
|
||||
local prjnode = premake.project.buildsourcetree(prj)
|
||||
tree.insert(ctx.root, prjnode)
|
||||
|
||||
-- localized files need an extra ID (see below)
|
||||
prjnode.variantgroups = { }
|
||||
|
||||
-- first pass over the tree to handle resource files like MainMenu.xib. If present,
|
||||
-- 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, {
|
||||
-- assign IDs to all nodes in the tree
|
||||
onnode = function(node)
|
||||
node.id = xcode.newid()
|
||||
end,
|
||||
|
||||
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,
|
||||
-- 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.
|
||||
if node.path then
|
||||
node.path = xcode.rebase(prj, node.path)
|
||||
end
|
||||
|
||||
end,
|
||||
|
||||
onleaf = function(node)
|
||||
-- assign a build ID to buildable files
|
||||
local category = xcode.getfilecategory(node.name)
|
||||
if category then
|
||||
if xcode.getfilecategory(node.name) then
|
||||
node.buildid = xcode.newid()
|
||||
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
|
||||
}, true)
|
||||
end
|
||||
@ -119,6 +134,7 @@
|
||||
end
|
||||
|
||||
|
||||
|
||||
--
|
||||
-- Return the Xcode category (for lack of a better term) for a file, such
|
||||
-- as "Sources" or "Resources".
|
||||
@ -130,7 +146,6 @@
|
||||
--
|
||||
|
||||
function xcode.getfilecategory(fname)
|
||||
print("Category for ", fname)
|
||||
local categories = {
|
||||
[".c" ] = "Sources",
|
||||
[".cc" ] = "Sources",
|
||||
@ -210,6 +225,9 @@
|
||||
--
|
||||
|
||||
function xcode.islocalized(node)
|
||||
if path.getextension(node.name) == ".lproj" then
|
||||
return true
|
||||
end
|
||||
if xcode.getfilecategory(node.name) ~= "Resources" then
|
||||
return false
|
||||
end
|
||||
@ -281,30 +299,12 @@
|
||||
function xcode.PBXBuildFile(ctx)
|
||||
local resources
|
||||
_p('/* Begin PBXBuildFile section */')
|
||||
local prjnode
|
||||
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)
|
||||
if node.buildid then
|
||||
local id = node.id
|
||||
|
||||
-- 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
|
||||
|
||||
if xcode.islocalized(node) then return end
|
||||
_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
|
||||
})
|
||||
@ -317,7 +317,6 @@
|
||||
_p('/* Begin PBXFileReference section */')
|
||||
tree.traverse(ctx.root, {
|
||||
onleaf = function(node)
|
||||
-- is this a project with no files?
|
||||
if not node.path then return end
|
||||
|
||||
local nodename, nodepath, encoding
|
||||
@ -347,13 +346,19 @@
|
||||
|
||||
function xcode.PBXGroup(ctx)
|
||||
_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, {
|
||||
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\tisa = PBXGroup;')
|
||||
_p('\t\t\tchildren = (')
|
||||
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
|
||||
_p('\t\t\t);')
|
||||
_p('\t\t\tname = %s;', node.name)
|
||||
@ -364,6 +369,7 @@
|
||||
_p('\t\t};')
|
||||
end
|
||||
}, true)
|
||||
|
||||
_p('/* End PBXGroup section */')
|
||||
_p('')
|
||||
end
|
||||
@ -372,17 +378,19 @@
|
||||
function xcode.PBXVariantGroup(ctx)
|
||||
_p('/* Begin PBXVariantGroup section */')
|
||||
for _, prjnode in ipairs(ctx.root.children) do
|
||||
for name, group in pairs(prjnode.variantgroups) do
|
||||
_p('\t\t%s /* %s */ = {', group.id, name)
|
||||
_p('\t\t\tisa = PBXVariantGroup;')
|
||||
_p('\t\t\tchildren = (')
|
||||
for _, node in ipairs(group) do
|
||||
_p('\t\t\t\t%s /* %s */,', node.id, path.getbasename(node.parent.name))
|
||||
if prjnode.resources then
|
||||
for _, node in ipairs(prjnode.resources.children) do
|
||||
_p('\t\t%s /* %s */ = {', node.id, node.name)
|
||||
_p('\t\t\tisa = PBXVariantGroup;')
|
||||
_p('\t\t\tchildren = (')
|
||||
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
|
||||
_p('\t\t\t);')
|
||||
_p('\t\t\tname = %s;', name)
|
||||
_p('\t\t\tsourceTree = "<group>";')
|
||||
_p('\t\t};')
|
||||
end
|
||||
end
|
||||
_p('/* End PBXVariantGroup section */')
|
||||
|
@ -50,6 +50,15 @@
|
||||
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
|
||||
-- returns the result.
|
||||
|
@ -108,21 +108,7 @@
|
||||
xcode.PBXBuildFile(ctx)
|
||||
test.capture [[
|
||||
/* Begin PBXBuildFile section */
|
||||
000000000005 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000006 /* 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 */; };
|
||||
000000000011 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000010 /* MainMenu.xib */; };
|
||||
/* End PBXBuildFile section */
|
||||
]]
|
||||
end
|
||||
@ -136,8 +122,8 @@
|
||||
xcode.PBXBuildFile(ctx)
|
||||
test.capture [[
|
||||
/* Begin PBXBuildFile section */
|
||||
000000000006 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000007 /* MainMenu.xib */; };
|
||||
000000000015 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000016 /* MainMenu.xib */; };
|
||||
000000000012 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000011 /* MainMenu.xib */; };
|
||||
000000000023 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 000000000022 /* MainMenu.xib */; };
|
||||
/* End PBXBuildFile section */
|
||||
]]
|
||||
end
|
||||
@ -182,7 +168,7 @@
|
||||
test.capture [[
|
||||
/* Begin PBXFileReference section */
|
||||
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
|
||||
|
||||
@ -196,9 +182,9 @@
|
||||
test.capture [[
|
||||
/* Begin PBXFileReference section */
|
||||
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>"; };
|
||||
000000000014 /* 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>"; };
|
||||
000000000008 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; };
|
||||
000000000016 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
|
||||
000000000019 /* French */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = French; path = French.lproj/MainMenu.xib; sourceTree = "<group>"; };
|
||||
]]
|
||||
end
|
||||
|
||||
@ -264,6 +250,33 @@
|
||||
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
|
||||
@ -277,11 +290,11 @@
|
||||
xcode.PBXVariantGroup(ctx)
|
||||
test.capture [[
|
||||
/* Begin PBXVariantGroup section */
|
||||
000000000006 /* MainMenu.xib */ = {
|
||||
000000000010 /* MainMenu.xib */ = {
|
||||
isa = PBXVariantGroup;
|
||||
children = (
|
||||
000000000007 /* French */,
|
||||
000000000004 /* English */,
|
||||
000000000008 /* French */,
|
||||
);
|
||||
name = MainMenu.xib;
|
||||
sourceTree = "<group>";
|
||||
|
@ -31,3 +31,16 @@
|
||||
t = { "one", "two", "three", "four" }
|
||||
test.isequal("[one], [two], [three], [four]", table.implode(t, "[", "]", ", "))
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user