Skip to content

Commit e842a9e

Browse files
committed
added PBXVariantGroup for localization and nodeunit test cases
1 parent 4cca2f6 commit e842a9e

File tree

5 files changed

+915
-35
lines changed

5 files changed

+915
-35
lines changed

lib/pbxFile.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var FILETYPE_BY_EXTENSION = {
2929
xcdatamodel: 'wrapper.xcdatamodel',
3030
xcodeproj: 'wrapper.pb-project',
3131
xctest: 'wrapper.cfbundle',
32-
xib: 'file.xib'
32+
xib: 'file.xib',
33+
strings: 'text.plist.strings'
3334
},
3435
GROUP_BY_FILETYPE = {
3536
'archive.ar': 'Frameworks',
@@ -59,7 +60,8 @@ var FILETYPE_BY_EXTENSION = {
5960
'text': 4,
6061
'text.plist.xml': 4,
6162
'text.script.sh': 4,
62-
'text.xcconfig': 4
63+
'text.xcconfig': 4,
64+
'text.plist.strings': 4
6365
};
6466

6567

lib/pbxProject.js

Lines changed: 129 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,21 @@ pbxProject.prototype.addResourceFile = function(path, opt, group) {
241241
correctForResourcesPath(file, this);
242242
file.fileRef = this.generateUuid();
243243
}
244-
245-
this.addToPbxBuildFileSection(file); // PBXBuildFile
246-
this.addToPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase
247-
244+
245+
if (!opt.variantGroup) {
246+
this.addToPbxBuildFileSection(file); // PBXBuildFile
247+
this.addToPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase
248+
}
249+
248250
if (!opt.plugin) {
249251
this.addToPbxFileReferenceSection(file); // PBXFileReference
250252
if (group) {
251-
this.addToPbxGroup(file, group); //Group other than Resources (i.e. 'splash')
253+
if (this.getPBXGroupByKey(group)) {
254+
this.addToPbxGroup(file, group); //Group other than Resources (i.e. 'splash')
255+
}
256+
else if (this.getPBXVariantGroupByKey(group)) {
257+
this.addToPbxVariantGroup(file, group); // PBXVariantGroup
258+
}
252259
}
253260
else {
254261
this.addToResourcesPbxGroup(file); // PBXGroup
@@ -275,7 +282,12 @@ pbxProject.prototype.removeResourceFile = function(path, opt, group) {
275282
this.removeFromPbxBuildFileSection(file); // PBXBuildFile
276283
this.removeFromPbxFileReferenceSection(file); // PBXFileReference
277284
if (group) {
278-
this.removeFromPbxGroup(file, group); //Group other than Resources (i.e. 'splash')
285+
if (this.getPBXGroupByKey(group)) {
286+
this.removeFromPbxGroup(file, group); //Group other than Resources (i.e. 'splash')
287+
}
288+
else if (this.getPBXVariantGroupByKey(group)) {
289+
this.removeFromPbxVariantGroup(file, group); // PBXVariantGroup
290+
}
279291
}
280292
else {
281293
this.removeFromResourcesPbxGroup(file); // PBXGroup
@@ -1696,15 +1708,20 @@ pbxProject.prototype.getFirstTarget = function() {
16961708

16971709
/*** NEW ***/
16981710

1699-
pbxProject.prototype.addToPbxGroup = function (file, groupKey) {
1700-
var group = this.getPBXGroupByKey(groupKey);
1711+
pbxProject.prototype.addToPbxGroupType = function (file, groupKey, groupType) {
1712+
var group = this.getPBXGroupByKeyAndType(groupKey, groupType);
17011713
if (group && group.children !== undefined) {
17021714
if (typeof file === 'string') {
17031715
//Group Key
17041716
var childGroup = {
17051717
value:file,
1706-
comment: this.getPBXGroupByKey(file).name
17071718
};
1719+
if (this.getPBXGroupByKey(file)) {
1720+
childGroup.comment = this.getPBXGroupByKey(file).name;
1721+
}
1722+
else if (this.getPBXVariantGroupByKey(file)) {
1723+
childGroup.comment = this.getPBXVariantGroupByKey(file).name;
1724+
}
17081725

17091726
group.children.push(childGroup);
17101727
}
@@ -1715,8 +1732,53 @@ pbxProject.prototype.addToPbxGroup = function (file, groupKey) {
17151732
}
17161733
}
17171734

1718-
pbxProject.prototype.removeFromPbxGroup = function (file, groupKey) {
1719-
var group = this.getPBXGroupByKey(groupKey);
1735+
pbxProject.prototype.addToPbxVariantGroup = function (file, groupKey) {
1736+
this.addToPbxGroupType(file, groupKey, 'PBXVariantGroup');
1737+
}
1738+
1739+
pbxProject.prototype.addToPbxGroup = function (file, groupKey) {
1740+
this.addToPbxGroupType(file, groupKey, 'PBXGroup');
1741+
}
1742+
1743+
1744+
1745+
pbxProject.prototype.pbxCreateGroupWithType = function(name, pathName, groupType) {
1746+
//Create object
1747+
var model = {
1748+
isa: '"' + groupType + '"',
1749+
children: [],
1750+
name: name,
1751+
sourceTree: '"<group>"'
1752+
};
1753+
if (pathName) model.path = pathName;
1754+
var key = this.generateUuid();
1755+
1756+
//Create comment
1757+
var commendId = key + '_comment';
1758+
1759+
//add obj and commentObj to groups;
1760+
var groups = this.hash.project.objects[groupType];
1761+
if (!groups) {
1762+
groups = this.hash.project.objects[groupType] = new Object();
1763+
}
1764+
groups[commendId] = name;
1765+
groups[key] = model;
1766+
1767+
return key;
1768+
}
1769+
1770+
pbxProject.prototype.pbxCreateVariantGroup = function(name) {
1771+
return this.pbxCreateGroupWithType(name, undefined, 'PBXVariantGroup')
1772+
}
1773+
1774+
pbxProject.prototype.pbxCreateGroup = function(name, pathName) {
1775+
return this.pbxCreateGroupWithType(name, pathName, 'PBXGroup');
1776+
}
1777+
1778+
1779+
1780+
pbxProject.prototype.removeFromPbxGroupAndType = function (file, groupKey, groupType) {
1781+
var group = this.getPBXGroupByKeyAndType(groupKey, groupType);
17201782
if (group) {
17211783
var groupChildren = group.children, i;
17221784
for(i in groupChildren) {
@@ -1729,12 +1791,32 @@ pbxProject.prototype.removeFromPbxGroup = function (file, groupKey) {
17291791
}
17301792
}
17311793

1794+
pbxProject.prototype.removeFromPbxGroup = function (file, groupKey) {
1795+
this.removeFromPbxGroupAndType(file, groupKey, 'PBXGroup');
1796+
}
1797+
1798+
pbxProject.prototype.removeFromPbxVariantGroup = function (file, groupKey) {
1799+
this.removeFromPbxGroupAndType(file, groupKey, 'PBXVariantGroup');
1800+
}
1801+
1802+
1803+
1804+
pbxProject.prototype.getPBXGroupByKeyAndType = function(key, groupType) {
1805+
return this.hash.project.objects[groupType][key];
1806+
};
1807+
17321808
pbxProject.prototype.getPBXGroupByKey = function(key) {
17331809
return this.hash.project.objects['PBXGroup'][key];
17341810
};
17351811

1736-
pbxProject.prototype.findPBXGroupKey = function(criteria) {
1737-
var groups = this.hash.project.objects['PBXGroup'];
1812+
pbxProject.prototype.getPBXVariantGroupByKey = function(key) {
1813+
return this.hash.project.objects['PBXVariantGroup'][key];
1814+
};
1815+
1816+
1817+
1818+
pbxProject.prototype.findPBXGroupKeyAndType = function(criteria, groupType) {
1819+
var groups = this.hash.project.objects[groupType];
17381820
var target;
17391821

17401822
for (var key in groups) {
@@ -1765,28 +1847,30 @@ pbxProject.prototype.findPBXGroupKey = function(criteria) {
17651847
return target;
17661848
}
17671849

1768-
pbxProject.prototype.pbxCreateGroup = function(name, pathName) {
1850+
pbxProject.prototype.findPBXGroupKey = function(criteria) {
1851+
return this.findPBXGroupKeyAndType(criteria, 'PBXGroup');
1852+
}
17691853

1770-
//Create object
1771-
var model = {
1772-
isa:"PBXGroup",
1773-
children: [],
1774-
name: name,
1775-
path: pathName,
1776-
sourceTree: '"<group>"'
1777-
};
1778-
var key = this.generateUuid();
1854+
pbxProject.prototype.findPBXVariantGroupKey = function(criteria) {
1855+
return this.findPBXGroupKeyAndType(criteria, 'PBXVariantGroup');
1856+
}
17791857

1780-
//Create comment
1781-
var commendId = key + '_comment';
1858+
pbxProject.prototype.addLocalizationVariantGroup = function(name) {
1859+
var groupKey = this.pbxCreateVariantGroup(name);
17821860

1783-
//add obj and commentObj to groups;
1784-
var groups = this.hash.project.objects['PBXGroup'];
1785-
groups[commendId] = name;
1786-
groups[key] = model;
1861+
var resourceGroupKey = this.findPBXGroupKey({name: 'Resources'});
1862+
this.addToPbxGroup(groupKey, resourceGroupKey);
17871863

1788-
return key;
1789-
}
1864+
var localizationVariantGroup = {
1865+
uuid: this.generateUuid(),
1866+
fileRef: groupKey,
1867+
basename: name
1868+
}
1869+
this.addToPbxBuildFileSection(localizationVariantGroup); // PBXBuildFile
1870+
this.addToPbxResourcesBuildPhase(localizationVariantGroup); //PBXResourcesBuildPhase
1871+
1872+
return localizationVariantGroup;
1873+
};
17901874

17911875

17921876
pbxProject.prototype.getPBXObject = function(name) {
@@ -1804,7 +1888,13 @@ pbxProject.prototype.addFile = function (path, group, opt) {
18041888
file.fileRef = this.generateUuid();
18051889

18061890
this.addToPbxFileReferenceSection(file); // PBXFileReference
1807-
this.addToPbxGroup(file, group); // PBXGroup
1891+
1892+
if (this.getPBXGroupByKey(group)) {
1893+
this.addToPbxGroup(file, group); // PBXGroup
1894+
}
1895+
else if (this.getPBXVariantGroupByKey(group)) {
1896+
this.addToPbxVariantGroup(file, group); // PBXVariantGroup
1897+
}
18081898

18091899
return file;
18101900
}
@@ -1813,7 +1903,13 @@ pbxProject.prototype.removeFile = function (path, group, opt) {
18131903
var file = new pbxFile(path, opt);
18141904

18151905
this.removeFromPbxFileReferenceSection(file); // PBXFileReference
1816-
this.removeFromPbxGroup(file, group); // PBXGroup
1906+
1907+
if (this.getPBXGroupByKey(group)) {
1908+
this.removeFromPbxGroup(file, group); // PBXGroup
1909+
}
1910+
else if (this.getPBXVariantGroupByKey(group)) {
1911+
this.removeFromPbxVariantGroup(file, group); // PBXVariantGroup
1912+
}
18171913

18181914
return file;
18191915
}

test/addResourceFile.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ exports.addResourceFile = {
210210
test.done();
211211
}
212212
},
213+
'when added with { variantGroup: true }': {
214+
215+
'should not add to the PBXResourcesBuildPhase and PBXBuildFile': function (test) {
216+
var newFile = proj.addResourceFile('en.lproj/Localization.strings',
217+
{ variantGroup: true });
218+
219+
var sources = proj.pbxResourcesBuildPhaseObj();
220+
test.equal(sources.files.length, 12);
221+
222+
var buildFileSection = proj.pbxBuildFileSection();
223+
test.ok(buildFileSection[newFile.uuid] === undefined);
224+
225+
test.done();
226+
},
227+
228+
},
213229
'duplicate entries': {
214230
'should return false': function (test) {
215231
var newFile = proj.addResourceFile('Plugins/assets.bundle');

0 commit comments

Comments
 (0)