Skip to content

Commit 3489006

Browse files
committed
feature: Add groups created by addPbxGroup() to the meta PBXGroup
Current implementation doesn't add created groups to the main/meta PBXGroup and therefore they don't show up in the file tree.
1 parent 08846bb commit 3489006

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

lib/pbxFile.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ var path = require('path'),
1111
DEFAULT_SOURCE_TREE = '"<group>"',
1212
DEFAULT_FILE_ENCODING = 4;
1313

14+
function fileTypes() {
15+
return {
16+
SOURCE_FILE: SOURCE_FILE,
17+
HEADER_FILE: HEADER_FILE,
18+
BUNDLE: BUNDLE,
19+
XIB_FILE: XIB_FILE,
20+
FRAMEWORK: FRAMEWORK,
21+
DYLIB: DYLIB,
22+
ARCHIVE: ARCHIVE,
23+
PNG_IMAGE: PNG_IMAGE
24+
}
25+
}
26+
1427
function detectLastType(path) {
1528
if (M_EXTENSION.test(path))
1629
return SOURCE_FILE;
@@ -102,4 +115,7 @@ function pbxFile(filepath, opt) {
102115
}
103116
}
104117

105-
module.exports = pbxFile;
118+
module.exports = {
119+
pbxFile: pbxFile,
120+
fileTypes: fileTypes
121+
}

lib/pbxProject.js

+43-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var util = require('util'),
55
uuid = require('node-uuid'),
66
fork = require('child_process').fork,
77
pbxWriter = require('./pbxWriter'),
8-
pbxFile = require('./pbxFile'),
8+
pbxFile = require('./pbxFile').pbxFile,
9+
pbxFileTypes = require('./pbxFile').fileTypes,
910
fs = require('fs'),
1011
parser = require('./parser/pbxproj'),
1112
COMMENT_KEY = /_comment$/,
@@ -286,6 +287,21 @@ pbxProject.prototype.removeFromPbxBuildFileSection = function (file) {
286287
}
287288
}
288289

290+
pbxProject.prototype.findMainPbxGroup = function () {
291+
var groups = this.hash.project.objects['PBXGroup'];
292+
var candidates = [];
293+
for (var key in groups) {
294+
if (groups[key].path == undefined && groups[key].name == undefined && groups[key].isa) {
295+
candidates.push(groups[key]);
296+
}
297+
}
298+
if (candidates.length == 1) {
299+
return candidates[0];
300+
}
301+
302+
return null;
303+
}
304+
289305
pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceTree) {
290306
var groups = this.hash.project.objects['PBXGroup'],
291307
pbxGroupUuid = this.generateUuid(),
@@ -320,20 +336,39 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT
320336
pbxGroup.children.push(pbxGroupChild(filePathToReference[filePathQuoted]));
321337
continue;
322338
}
323-
339+
324340
var file = new pbxFile(filePath);
325-
file.uuid = this.generateUuid();
326-
file.fileRef = this.generateUuid();
327-
this.addToPbxFileReferenceSection(file); // PBXFileReference
328-
this.addToPbxBuildFileSection(file); // PBXBuildFile
329-
pbxGroup.children.push(pbxGroupChild(file));
341+
if (file.lastType == pbxFileTypes().SOURCE_FILE || file.lastType == pbxFileTypes().HEADER_FILE || fs.lstatSync(file.path).isDirectory()) {
342+
file.uuid = this.generateUuid();
343+
file.fileRef = this.generateUuid();
344+
this.addToPbxFileReferenceSection(file); // PBXFileReference
345+
this.addToPbxBuildFileSection(file); // PBXBuildFile
346+
if (file.lastType == pbxFileTypes().SOURCE_FILE) {
347+
this.addToPbxSourcesBuildPhase(file);
348+
}
349+
pbxGroup.children.push(pbxGroupChild(file));
350+
}
351+
330352
}
331353

332354
if (groups) {
333355
groups[pbxGroupUuid] = pbxGroup;
334356
groups[commentKey] = name;
335357
}
336-
358+
359+
let mainGroup = this.findMainPbxGroup();
360+
if (mainGroup) {
361+
var file = new pbxFile(pbxGroup.path);
362+
file.fileRef = pbxGroupUuid;
363+
// Following check is just for readability. 'file.basename' will be the comment for the child group in the main/meta PBXGroup.
364+
//'basename' is set by pbxFile to path's basename. Because we search for 'src' folder in plugins for source code, without this check
365+
// all root groups will have the same 'src' comment in the .pbxproject.x
366+
if (file.basename !== name) {
367+
file.basename = name;
368+
}
369+
mainGroup.children.push(pbxGroupChild(file));
370+
}
371+
337372
return {uuid: pbxGroupUuid, pbxGroup: pbxGroup};
338373
}
339374

0 commit comments

Comments
 (0)