UNPKG

65.1 kBJavaScriptView Raw
1var util = require('util'),
2 f = util.format,
3 EventEmitter = require('events').EventEmitter,
4 path = require('path'),
5 uuid = require('node-uuid'),
6 fork = require('child_process').fork,
7 pbxWriter = require('./pbxWriter'),
8 pbxFile = require('./pbxFile'),
9 fs = require('fs'),
10 parser = require('./parser/pbxproj'),
11 plist = require('simple-plist'),
12 COMMENT_KEY = /_comment$/
13
14function pbxProject(filename) {
15 if (!(this instanceof pbxProject))
16 return new pbxProject(filename);
17
18 this.filepath = path.resolve(filename)
19}
20
21util.inherits(pbxProject, EventEmitter)
22
23pbxProject.prototype.parse = function(cb) {
24 var worker = fork(__dirname + '/parseJob.js', [this.filepath])
25
26 worker.on('message', function(msg) {
27 if (msg.name == 'SyntaxError' || msg.code) {
28 this.emit('error', msg);
29 } else {
30 this.hash = msg;
31 this.emit('end', null, msg)
32 }
33 }.bind(this));
34
35 if (cb) {
36 this.on('error', cb);
37 this.on('end', cb);
38 }
39
40 return this;
41}
42
43pbxProject.prototype.parseSync = function() {
44 var file_contents = fs.readFileSync(this.filepath, 'utf-8');
45
46 this.hash = parser.parse(file_contents);
47 return this;
48}
49
50pbxProject.prototype.writeSync = function() {
51 this.writer = new pbxWriter(this.hash);
52 return this.writer.writeSync();
53}
54
55pbxProject.prototype.allUuids = function() {
56 var sections = this.hash.project.objects,
57 uuids = [],
58 section;
59
60 for (key in sections) {
61 section = sections[key]
62 uuids = uuids.concat(Object.keys(section))
63 }
64
65 uuids = uuids.filter(function(str) {
66 return !COMMENT_KEY.test(str) && str.length == 24;
67 });
68
69 return uuids;
70}
71
72pbxProject.prototype.generateUuid = function() {
73 var id = uuid.v4()
74 .replace(/-/g, '')
75 .substr(0, 24)
76 .toUpperCase()
77
78 if (this.allUuids().indexOf(id) >= 0) {
79 return this.generateUuid();
80 } else {
81 return id;
82 }
83}
84
85pbxProject.prototype.addPluginFile = function(path, opt) {
86 var file = new pbxFile(path, opt);
87
88 file.plugin = true; // durr
89 correctForPluginsPath(file, this);
90
91 // null is better for early errors
92 if (this.hasFile(file.path)) return null;
93
94 file.fileRef = this.generateUuid();
95
96 this.addToPbxFileReferenceSection(file); // PBXFileReference
97 this.addToPluginsPbxGroup(file); // PBXGroup
98
99 return file;
100}
101
102pbxProject.prototype.removePluginFile = function(path, opt) {
103 var file = new pbxFile(path, opt);
104 correctForPluginsPath(file, this);
105
106 this.removeFromPbxFileReferenceSection(file); // PBXFileReference
107 this.removeFromPluginsPbxGroup(file); // PBXGroup
108
109 return file;
110}
111
112pbxProject.prototype.addProductFile = function(targetPath, opt) {
113 var file = new pbxFile(targetPath, opt);
114
115 file.includeInIndex = 0;
116 file.fileRef = this.generateUuid();
117 file.target = opt ? opt.target : undefined;
118 file.group = opt ? opt.group : undefined;
119 file.uuid = this.generateUuid();
120 file.path = file.basename;
121
122 this.addToPbxFileReferenceSection(file);
123 this.addToProductsPbxGroup(file); // PBXGroup
124
125 return file;
126}
127
128pbxProject.prototype.removeProductFile = function(path, opt) {
129 var file = new pbxFile(path, opt);
130
131 this.removeFromProductsPbxGroup(file); // PBXGroup
132
133 return file;
134}
135
136/**
137 *
138 * @param path {String}
139 * @param opt {Object} see pbxFile for avail options
140 * @param group {String} group key
141 * @returns {Object} file; see pbxFile
142 */
143pbxProject.prototype.addSourceFile = function (path, opt, group) {
144 var file;
145 if (group) {
146 file = this.addFile(path, group, opt);
147 }
148 else {
149 file = this.addPluginFile(path, opt);
150 }
151
152 if (!file) return false;
153
154 file.target = opt ? opt.target : undefined;
155 file.uuid = this.generateUuid();
156
157 this.addToPbxBuildFileSection(file); // PBXBuildFile
158 this.addToPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase
159
160 return file;
161}
162
163/**
164 *
165 * @param path {String}
166 * @param opt {Object} see pbxFile for avail options
167 * @param group {String} group key
168 * @returns {Object} file; see pbxFile
169 */
170pbxProject.prototype.removeSourceFile = function (path, opt, group) {
171 var file;
172 if (group) {
173 file = this.removeFile(path, group, opt);
174 }
175 else {
176 file = this.removePluginFile(path, opt);
177 }
178 file.target = opt ? opt.target : undefined;
179 this.removeFromPbxBuildFileSection(file); // PBXBuildFile
180 this.removeFromPbxSourcesBuildPhase(file); // PBXSourcesBuildPhase
181
182 return file;
183}
184
185/**
186 *
187 * @param path {String}
188 * @param opt {Object} see pbxFile for avail options
189 * @param group {String} group key
190 * @returns {Object} file; see pbxFile
191 */
192pbxProject.prototype.addHeaderFile = function (path, opt, group) {
193 if (group) {
194 return this.addFile(path, group, opt);
195 }
196 else {
197 return this.addPluginFile(path, opt);
198 }
199}
200
201/**
202 *
203 * @param path {String}
204 * @param opt {Object} see pbxFile for avail options
205 * @param group {String} group key
206 * @returns {Object} file; see pbxFile
207 */
208pbxProject.prototype.removeHeaderFile = function (path, opt, group) {
209 if (group) {
210 return this.removeFile(path, group, opt);
211 }
212 else {
213 return this.removePluginFile(path, opt);
214 }
215}
216
217/**
218 *
219 * @param path {String}
220 * @param opt {Object} see pbxFile for avail options
221 * @param group {String} group key
222 * @returns {Object} file; see pbxFile
223 */
224pbxProject.prototype.addResourceFile = function(path, opt, group) {
225 opt = opt || {};
226
227 var file;
228
229 if (opt.plugin) {
230 file = this.addPluginFile(path, opt);
231 if (!file) return false;
232 } else {
233 file = new pbxFile(path, opt);
234 if (this.hasFile(file.path)) return false;
235 }
236
237 file.uuid = this.generateUuid();
238 file.target = opt ? opt.target : undefined;
239
240 if (!opt.plugin) {
241 correctForResourcesPath(file, this);
242 file.fileRef = this.generateUuid();
243 }
244
245 if (!opt.variantGroup) {
246 this.addToPbxBuildFileSection(file); // PBXBuildFile
247 this.addToPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase
248 }
249
250 if (!opt.plugin) {
251 this.addToPbxFileReferenceSection(file); // PBXFileReference
252 if (group) {
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 }
259 }
260 else {
261 this.addToResourcesPbxGroup(file); // PBXGroup
262 }
263
264 }
265
266 return file;
267}
268
269/**
270 *
271 * @param path {String}
272 * @param opt {Object} see pbxFile for avail options
273 * @param group {String} group key
274 * @returns {Object} file; see pbxFile
275 */
276pbxProject.prototype.removeResourceFile = function(path, opt, group) {
277 var file = new pbxFile(path, opt);
278 file.target = opt ? opt.target : undefined;
279
280 correctForResourcesPath(file, this);
281
282 this.removeFromPbxBuildFileSection(file); // PBXBuildFile
283 this.removeFromPbxFileReferenceSection(file); // PBXFileReference
284 if (group) {
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 }
291 }
292 else {
293 this.removeFromResourcesPbxGroup(file); // PBXGroup
294 }
295 this.removeFromPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase
296
297 return file;
298}
299
300pbxProject.prototype.addFramework = function(fpath, opt) {
301 var customFramework = opt && opt.customFramework == true;
302 var link = !opt || (opt.link == undefined || opt.link); //defaults to true if not specified
303 var embed = opt && opt.embed; //defaults to false if not specified
304
305 if (opt) {
306 delete opt.embed;
307 }
308
309 var file = new pbxFile(fpath, opt);
310
311 file.uuid = this.generateUuid();
312 file.fileRef = this.generateUuid();
313 file.target = opt ? opt.target : undefined;
314
315 if (this.hasFile(file.path)) return false;
316
317 this.addToPbxBuildFileSection(file); // PBXBuildFile
318 this.addToPbxFileReferenceSection(file); // PBXFileReference
319 this.addToFrameworksPbxGroup(file); // PBXGroup
320
321 if (link) {
322 this.addToPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase
323 }
324
325 if (customFramework) {
326 this.addToFrameworkSearchPaths(file);
327
328 if (embed) {
329 opt.embed = embed;
330 var embeddedFile = new pbxFile(fpath, opt);
331
332 embeddedFile.uuid = this.generateUuid();
333 embeddedFile.fileRef = file.fileRef;
334
335 //keeping a separate PBXBuildFile entry for Embed Frameworks
336 this.addToPbxBuildFileSection(embeddedFile); // PBXBuildFile
337
338 this.addToPbxEmbedFrameworksBuildPhase(embeddedFile); // PBXCopyFilesBuildPhase
339
340 return embeddedFile;
341 }
342 }
343
344 return file;
345}
346
347pbxProject.prototype.removeFramework = function(fpath, opt) {
348 var embed = opt && opt.embed;
349
350 if (opt) {
351 delete opt.embed;
352 }
353
354 var file = new pbxFile(fpath, opt);
355 file.target = opt ? opt.target : undefined;
356
357 this.removeFromPbxBuildFileSection(file); // PBXBuildFile
358 this.removeFromPbxFileReferenceSection(file); // PBXFileReference
359 this.removeFromFrameworksPbxGroup(file); // PBXGroup
360 this.removeFromPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase
361
362 if (opt && opt.customFramework) {
363 this.removeFromFrameworkSearchPaths(file);
364 }
365
366 opt = opt || {};
367 opt.embed = true;
368 var embeddedFile = new pbxFile(fpath, opt);
369
370 embeddedFile.fileRef = file.fileRef;
371
372 this.removeFromPbxBuildFileSection(embeddedFile); // PBXBuildFile
373 this.removeFromPbxEmbedFrameworksBuildPhase(embeddedFile); // PBXCopyFilesBuildPhase
374
375 return file;
376}
377
378
379pbxProject.prototype.addCopyfile = function(fpath, opt) {
380 var file = new pbxFile(fpath, opt);
381
382 // catch duplicates
383 if (this.hasFile(file.path)) {
384 file = this.hasFile(file.path);
385 }
386
387 file.fileRef = file.uuid = this.generateUuid();
388 file.target = opt ? opt.target : undefined;
389
390 this.addToPbxBuildFileSection(file); // PBXBuildFile
391 this.addToPbxFileReferenceSection(file); // PBXFileReference
392 this.addToPbxCopyfilesBuildPhase(file); // PBXCopyFilesBuildPhase
393
394 return file;
395}
396
397pbxProject.prototype.pbxCopyfilesBuildPhaseObj = function(target) {
398 return this.buildPhaseObject('PBXCopyFilesBuildPhase', 'Copy Files', target);
399}
400
401pbxProject.prototype.addToPbxCopyfilesBuildPhase = function(file) {
402 var sources = this.buildPhaseObject('PBXCopyFilesBuildPhase', 'Copy Files', file.target);
403 sources.files.push(pbxBuildPhaseObj(file));
404}
405
406pbxProject.prototype.removeCopyfile = function(fpath, opt) {
407 var file = new pbxFile(fpath, opt);
408 file.target = opt ? opt.target : undefined;
409
410 this.removeFromPbxBuildFileSection(file); // PBXBuildFile
411 this.removeFromPbxFileReferenceSection(file); // PBXFileReference
412 this.removeFromPbxCopyfilesBuildPhase(file); // PBXFrameworksBuildPhase
413
414 return file;
415}
416
417pbxProject.prototype.removeFromPbxCopyfilesBuildPhase = function(file) {
418 var sources = this.pbxCopyfilesBuildPhaseObj(file.target);
419 for (i in sources.files) {
420 if (sources.files[i].comment == longComment(file)) {
421 sources.files.splice(i, 1);
422 break;
423 }
424 }
425}
426
427pbxProject.prototype.addStaticLibrary = function(path, opt) {
428 opt = opt || {};
429
430 var file;
431
432 if (opt.plugin) {
433 file = this.addPluginFile(path, opt);
434 if (!file) return false;
435 } else {
436 file = new pbxFile(path, opt);
437 if (this.hasFile(file.path)) return false;
438 }
439
440 file.uuid = this.generateUuid();
441 file.target = opt ? opt.target : undefined;
442
443 if (!opt.plugin) {
444 file.fileRef = this.generateUuid();
445 this.addToPbxFileReferenceSection(file); // PBXFileReference
446 }
447
448 this.addToPbxBuildFileSection(file); // PBXBuildFile
449 this.addToPbxFrameworksBuildPhase(file); // PBXFrameworksBuildPhase
450 this.addToLibrarySearchPaths(file); // make sure it gets built!
451
452 return file;
453}
454
455// helper addition functions
456pbxProject.prototype.addToPbxBuildFileSection = function(file) {
457 var commentKey = f("%s_comment", file.uuid);
458
459 this.pbxBuildFileSection()[file.uuid] = pbxBuildFileObj(file);
460 this.pbxBuildFileSection()[commentKey] = pbxBuildFileComment(file);
461}
462
463pbxProject.prototype.removeFromPbxBuildFileSection = function(file) {
464 var uuid;
465
466 for (uuid in this.pbxBuildFileSection()) {
467 if (this.pbxBuildFileSection()[uuid].fileRef_comment == file.basename) {
468 file.uuid = uuid;
469 delete this.pbxBuildFileSection()[uuid];
470 }
471 }
472 var commentKey = f("%s_comment", file.uuid);
473 delete this.pbxBuildFileSection()[commentKey];
474}
475
476pbxProject.prototype.addPbxGroup = function(filePathsArray, name, path, sourceTree) {
477 var groups = this.hash.project.objects['PBXGroup'],
478 pbxGroupUuid = this.generateUuid(),
479 commentKey = f("%s_comment", pbxGroupUuid),
480 pbxGroup = {
481 isa: 'PBXGroup',
482 children: [],
483 name: name,
484 path: path,
485 sourceTree: sourceTree ? sourceTree : '"<group>"'
486 },
487 fileReferenceSection = this.pbxFileReferenceSection(),
488 filePathToReference = {};
489
490 for (var key in fileReferenceSection) {
491 // only look for comments
492 if (!COMMENT_KEY.test(key)) continue;
493
494 var fileReferenceKey = key.split(COMMENT_KEY)[0],
495 fileReference = fileReferenceSection[fileReferenceKey];
496
497 filePathToReference[fileReference.path] = { fileRef: fileReferenceKey, basename: fileReferenceSection[key] };
498 }
499
500 for (var index = 0; index < filePathsArray.length; index++) {
501 var filePath = filePathsArray[index],
502 filePathQuoted = "\"" + filePath + "\"";
503 if (filePathToReference[filePath]) {
504 pbxGroup.children.push(pbxGroupChild(filePathToReference[filePath]));
505 continue;
506 } else if (filePathToReference[filePathQuoted]) {
507 pbxGroup.children.push(pbxGroupChild(filePathToReference[filePathQuoted]));
508 continue;
509 }
510
511 var file = new pbxFile(filePath);
512 file.uuid = this.generateUuid();
513 file.fileRef = this.generateUuid();
514 this.addToPbxFileReferenceSection(file); // PBXFileReference
515 this.addToPbxBuildFileSection(file); // PBXBuildFile
516 pbxGroup.children.push(pbxGroupChild(file));
517 }
518
519 if (groups) {
520 groups[pbxGroupUuid] = pbxGroup;
521 groups[commentKey] = name;
522 }
523
524 return { uuid: pbxGroupUuid, pbxGroup: pbxGroup };
525}
526
527pbxProject.prototype.removePbxGroup = function (groupName) {
528 var section = this.hash.project.objects['PBXGroup'],
529 key, itemKey;
530
531 for (key in section) {
532 // only look for comments
533 if (!COMMENT_KEY.test(key)) continue;
534
535 if (section[key] == groupName) {
536 itemKey = key.split(COMMENT_KEY)[0];
537 delete section[itemKey];
538 }
539 }
540}
541
542pbxProject.prototype.addToPbxProjectSection = function(target) {
543
544 var newTarget = {
545 value: target.uuid,
546 comment: pbxNativeTargetComment(target.pbxNativeTarget)
547 };
548
549 this.pbxProjectSection()[this.getFirstProject()['uuid']]['targets'].push(newTarget);
550}
551
552pbxProject.prototype.addToPbxNativeTargetSection = function(target) {
553 var commentKey = f("%s_comment", target.uuid);
554
555 this.pbxNativeTargetSection()[target.uuid] = target.pbxNativeTarget;
556 this.pbxNativeTargetSection()[commentKey] = target.pbxNativeTarget.name;
557}
558
559pbxProject.prototype.addToPbxFileReferenceSection = function(file) {
560 var commentKey = f("%s_comment", file.fileRef);
561
562 this.pbxFileReferenceSection()[file.fileRef] = pbxFileReferenceObj(file);
563 this.pbxFileReferenceSection()[commentKey] = pbxFileReferenceComment(file);
564}
565
566pbxProject.prototype.removeFromPbxFileReferenceSection = function(file) {
567
568 var i;
569 var refObj = pbxFileReferenceObj(file);
570 for (i in this.pbxFileReferenceSection()) {
571 if (this.pbxFileReferenceSection()[i].name == refObj.name ||
572 ('"' + this.pbxFileReferenceSection()[i].name + '"') == refObj.name ||
573 this.pbxFileReferenceSection()[i].path == refObj.path ||
574 ('"' + this.pbxFileReferenceSection()[i].path + '"') == refObj.path) {
575 file.fileRef = file.uuid = i;
576 delete this.pbxFileReferenceSection()[i];
577 break;
578 }
579 }
580 var commentKey = f("%s_comment", file.fileRef);
581 if (this.pbxFileReferenceSection()[commentKey] != undefined) {
582 delete this.pbxFileReferenceSection()[commentKey];
583 }
584
585 return file;
586}
587
588pbxProject.prototype.addToXcVersionGroupSection = function(file) {
589 if (!file.models || !file.currentModel) {
590 throw new Error("Cannot create a XCVersionGroup section from not a data model document file");
591 }
592
593 var commentKey = f("%s_comment", file.fileRef);
594
595 if (!this.xcVersionGroupSection()[file.fileRef]) {
596 this.xcVersionGroupSection()[file.fileRef] = {
597 isa: 'XCVersionGroup',
598 children: file.models.map(function (el) { return el.fileRef; }),
599 currentVersion: file.currentModel.fileRef,
600 name: path.basename(file.path),
601 path: file.path,
602 sourceTree: '"<group>"',
603 versionGroupType: 'wrapper.xcdatamodel'
604 };
605 this.xcVersionGroupSection()[commentKey] = path.basename(file.path);
606 }
607}
608
609pbxProject.prototype.addToPluginsPbxGroup = function(file) {
610 var pluginsGroup = this.pbxGroupByName('Plugins');
611 pluginsGroup.children.push(pbxGroupChild(file));
612}
613
614pbxProject.prototype.removeFromPluginsPbxGroup = function(file) {
615 var pluginsGroupChildren = this.pbxGroupByName('Plugins').children, i;
616 for (i in pluginsGroupChildren) {
617 if (pbxGroupChild(file).value == pluginsGroupChildren[i].value &&
618 pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) {
619 pluginsGroupChildren.splice(i, 1);
620 break;
621 }
622 }
623}
624
625pbxProject.prototype.addToResourcesPbxGroup = function(file) {
626 var pluginsGroup = this.pbxGroupByName('Resources');
627 pluginsGroup.children.push(pbxGroupChild(file));
628}
629
630pbxProject.prototype.removeFromResourcesPbxGroup = function(file) {
631 var pluginsGroupChildren = this.pbxGroupByName('Resources').children, i;
632 for (i in pluginsGroupChildren) {
633 if (pbxGroupChild(file).value == pluginsGroupChildren[i].value &&
634 pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) {
635 pluginsGroupChildren.splice(i, 1);
636 break;
637 }
638 }
639}
640
641pbxProject.prototype.addToFrameworksPbxGroup = function(file) {
642 var pluginsGroup = this.pbxGroupByName('Frameworks');
643 pluginsGroup.children.push(pbxGroupChild(file));
644}
645
646pbxProject.prototype.removeFromFrameworksPbxGroup = function(file) {
647 var pluginsGroupChildren = this.pbxGroupByName('Frameworks').children;
648
649 for (i in pluginsGroupChildren) {
650 if (pbxGroupChild(file).value == pluginsGroupChildren[i].value &&
651 pbxGroupChild(file).comment == pluginsGroupChildren[i].comment) {
652 pluginsGroupChildren.splice(i, 1);
653 break;
654 }
655 }
656}
657
658pbxProject.prototype.addToPbxEmbedFrameworksBuildPhase = function (file) {
659 var sources = this.pbxEmbedFrameworksBuildPhaseObj(file.target);
660 if (sources) {
661 sources.files.push(pbxBuildPhaseObj(file));
662 }
663}
664
665pbxProject.prototype.removeFromPbxEmbedFrameworksBuildPhase = function (file) {
666 var sources = this.pbxEmbedFrameworksBuildPhaseObj(file.target);
667 if (sources) {
668 var files = [];
669 for (i in sources.files) {
670 if (sources.files[i].comment != longComment(file)) {
671 files.push(sources.files[i]);
672 }
673 }
674 sources.files = files;
675 }
676}
677
678pbxProject.prototype.addToProductsPbxGroup = function(file) {
679 var productsGroup = this.pbxGroupByName('Products');
680 productsGroup.children.push(pbxGroupChild(file));
681}
682
683pbxProject.prototype.removeFromProductsPbxGroup = function(file) {
684 var productsGroupChildren = this.pbxGroupByName('Products').children, i;
685 for (i in productsGroupChildren) {
686 if (pbxGroupChild(file).value == productsGroupChildren[i].value &&
687 pbxGroupChild(file).comment == productsGroupChildren[i].comment) {
688 productsGroupChildren.splice(i, 1);
689 break;
690 }
691 }
692}
693
694pbxProject.prototype.addToPbxSourcesBuildPhase = function(file) {
695 var sources = this.pbxSourcesBuildPhaseObj(file.target);
696 sources.files.push(pbxBuildPhaseObj(file));
697}
698
699pbxProject.prototype.removeFromPbxSourcesBuildPhase = function(file) {
700
701 var sources = this.pbxSourcesBuildPhaseObj(file.target), i;
702 for (i in sources.files) {
703 if (sources.files[i].comment == longComment(file)) {
704 sources.files.splice(i, 1);
705 break;
706 }
707 }
708}
709
710pbxProject.prototype.addToPbxResourcesBuildPhase = function(file) {
711 var sources = this.pbxResourcesBuildPhaseObj(file.target);
712 sources.files.push(pbxBuildPhaseObj(file));
713}
714
715pbxProject.prototype.removeFromPbxResourcesBuildPhase = function(file) {
716 var sources = this.pbxResourcesBuildPhaseObj(file.target), i;
717
718 for (i in sources.files) {
719 if (sources.files[i].comment == longComment(file)) {
720 sources.files.splice(i, 1);
721 break;
722 }
723 }
724}
725
726pbxProject.prototype.addToPbxFrameworksBuildPhase = function(file) {
727 var sources = this.pbxFrameworksBuildPhaseObj(file.target);
728 sources.files.push(pbxBuildPhaseObj(file));
729}
730
731pbxProject.prototype.removeFromPbxFrameworksBuildPhase = function(file) {
732 var sources = this.pbxFrameworksBuildPhaseObj(file.target);
733 for (i in sources.files) {
734 if (sources.files[i].comment == longComment(file)) {
735 sources.files.splice(i, 1);
736 break;
737 }
738 }
739}
740
741pbxProject.prototype.addXCConfigurationList = function(configurationObjectsArray, defaultConfigurationName, comment) {
742 var pbxBuildConfigurationSection = this.pbxXCBuildConfigurationSection(),
743 pbxXCConfigurationListSection = this.pbxXCConfigurationList(),
744 xcConfigurationListUuid = this.generateUuid(),
745 commentKey = f("%s_comment", xcConfigurationListUuid),
746 xcConfigurationList = {
747 isa: 'XCConfigurationList',
748 buildConfigurations: [],
749 defaultConfigurationIsVisible: 0,
750 defaultConfigurationName: defaultConfigurationName
751 };
752
753 for (var index = 0; index < configurationObjectsArray.length; index++) {
754 var configuration = configurationObjectsArray[index],
755 configurationUuid = this.generateUuid(),
756 configurationCommentKey = f("%s_comment", configurationUuid);
757
758 pbxBuildConfigurationSection[configurationUuid] = configuration;
759 pbxBuildConfigurationSection[configurationCommentKey] = configuration.name;
760 xcConfigurationList.buildConfigurations.push({ value: configurationUuid, comment: configuration.name });
761 }
762
763 if (pbxXCConfigurationListSection) {
764 pbxXCConfigurationListSection[xcConfigurationListUuid] = xcConfigurationList;
765 pbxXCConfigurationListSection[commentKey] = comment;
766 }
767
768 return { uuid: xcConfigurationListUuid, xcConfigurationList: xcConfigurationList };
769}
770
771pbxProject.prototype.addTargetDependency = function(target, dependencyTargets) {
772 if (!target)
773 return undefined;
774
775 var nativeTargets = this.pbxNativeTargetSection();
776
777 if (typeof nativeTargets[target] == "undefined")
778 throw new Error("Invalid target: " + target);
779
780 for (var index = 0; index < dependencyTargets.length; index++) {
781 var dependencyTarget = dependencyTargets[index];
782 if (typeof nativeTargets[dependencyTarget] == "undefined")
783 throw new Error("Invalid target: " + dependencyTarget);
784 }
785
786 var pbxTargetDependency = 'PBXTargetDependency',
787 pbxContainerItemProxy = 'PBXContainerItemProxy',
788 pbxTargetDependencySection = this.hash.project.objects[pbxTargetDependency],
789 pbxContainerItemProxySection = this.hash.project.objects[pbxContainerItemProxy];
790
791 for (var index = 0; index < dependencyTargets.length; index++) {
792 var dependencyTargetUuid = dependencyTargets[index],
793 dependencyTargetCommentKey = f("%s_comment", dependencyTargetUuid),
794 targetDependencyUuid = this.generateUuid(),
795 targetDependencyCommentKey = f("%s_comment", targetDependencyUuid),
796 itemProxyUuid = this.generateUuid(),
797 itemProxyCommentKey = f("%s_comment", itemProxyUuid),
798 itemProxy = {
799 isa: pbxContainerItemProxy,
800 containerPortal: this.hash.project['rootObject'],
801 containerPortal_comment: this.hash.project['rootObject_comment'],
802 proxyType: 1,
803 remoteGlobalIDString: dependencyTargetUuid,
804 remoteInfo: nativeTargets[dependencyTargetUuid].name
805 },
806 targetDependency = {
807 isa: pbxTargetDependency,
808 target: dependencyTargetUuid,
809 target_comment: nativeTargets[dependencyTargetCommentKey],
810 targetProxy: itemProxyUuid,
811 targetProxy_comment: pbxContainerItemProxy
812 };
813
814 if (pbxContainerItemProxySection && pbxTargetDependencySection) {
815 pbxContainerItemProxySection[itemProxyUuid] = itemProxy;
816 pbxContainerItemProxySection[itemProxyCommentKey] = pbxContainerItemProxy;
817 pbxTargetDependencySection[targetDependencyUuid] = targetDependency;
818 pbxTargetDependencySection[targetDependencyCommentKey] = pbxTargetDependency;
819 nativeTargets[target].dependencies.push({ value: targetDependencyUuid, comment: pbxTargetDependency })
820 }
821 }
822
823 return { uuid: target, target: nativeTargets[target] };
824}
825
826pbxProject.prototype.addBuildPhase = function(filePathsArray, buildPhaseType, comment, target, optionsOrFolderType, subfolderPath) {
827 var buildPhaseSection,
828 fileReferenceSection = this.pbxFileReferenceSection(),
829 buildFileSection = this.pbxBuildFileSection(),
830 buildPhaseUuid = this.generateUuid(),
831 buildPhaseTargetUuid = target || this.getFirstTarget().uuid,
832 commentKey = f("%s_comment", buildPhaseUuid),
833 buildPhase = {
834 isa: buildPhaseType,
835 buildActionMask: 2147483647,
836 files: [],
837 runOnlyForDeploymentPostprocessing: 0
838 },
839 filePathToBuildFile = {};
840
841 if (buildPhaseType === 'PBXCopyFilesBuildPhase') {
842 buildPhase = pbxCopyFilesBuildPhaseObj(buildPhase, optionsOrFolderType, subfolderPath, comment);
843 } else if (buildPhaseType === 'PBXShellScriptBuildPhase') {
844 buildPhase = pbxShellScriptBuildPhaseObj(buildPhase, optionsOrFolderType, comment)
845 }
846
847 if (!this.hash.project.objects[buildPhaseType]) {
848 this.hash.project.objects[buildPhaseType] = new Object();
849 }
850
851 if (!this.hash.project.objects[buildPhaseType][buildPhaseUuid]) {
852 this.hash.project.objects[buildPhaseType][buildPhaseUuid] = buildPhase;
853 this.hash.project.objects[buildPhaseType][commentKey] = comment;
854 }
855
856 if (this.hash.project.objects['PBXNativeTarget'][buildPhaseTargetUuid]['buildPhases']) {
857 this.hash.project.objects['PBXNativeTarget'][buildPhaseTargetUuid]['buildPhases'].push({
858 value: buildPhaseUuid,
859 comment: comment
860 })
861
862 }
863
864
865 for (var key in buildFileSection) {
866 // only look for comments
867 if (!COMMENT_KEY.test(key)) continue;
868
869 var buildFileKey = key.split(COMMENT_KEY)[0],
870 buildFile = buildFileSection[buildFileKey];
871 fileReference = fileReferenceSection[buildFile.fileRef];
872
873 if (!fileReference) continue;
874
875 var pbxFileObj = new pbxFile(fileReference.path);
876
877 filePathToBuildFile[fileReference.path] = { uuid: buildFileKey, basename: pbxFileObj.basename, group: pbxFileObj.group };
878 }
879
880 for (var index = 0; index < filePathsArray.length; index++) {
881 var filePath = filePathsArray[index],
882 filePathQuoted = "\"" + filePath + "\"",
883 file = new pbxFile(filePath);
884
885 if (filePathToBuildFile[filePath]) {
886 buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePath]));
887 continue;
888 } else if (filePathToBuildFile[filePathQuoted]) {
889 buildPhase.files.push(pbxBuildPhaseObj(filePathToBuildFile[filePathQuoted]));
890 continue;
891 }
892
893 file.uuid = this.generateUuid();
894 file.fileRef = this.generateUuid();
895 this.addToPbxFileReferenceSection(file); // PBXFileReference
896 this.addToPbxBuildFileSection(file); // PBXBuildFile
897 buildPhase.files.push(pbxBuildPhaseObj(file));
898 }
899
900 if (buildPhaseSection) {
901 buildPhaseSection[buildPhaseUuid] = buildPhase;
902 buildPhaseSection[commentKey] = comment;
903 }
904
905 return { uuid: buildPhaseUuid, buildPhase: buildPhase };
906}
907
908// helper access functions
909pbxProject.prototype.pbxProjectSection = function() {
910 return this.hash.project.objects['PBXProject'];
911}
912pbxProject.prototype.pbxBuildFileSection = function() {
913 return this.hash.project.objects['PBXBuildFile'];
914}
915
916pbxProject.prototype.pbxXCBuildConfigurationSection = function() {
917 return this.hash.project.objects['XCBuildConfiguration'];
918}
919
920pbxProject.prototype.pbxFileReferenceSection = function() {
921 return this.hash.project.objects['PBXFileReference'];
922}
923
924pbxProject.prototype.pbxNativeTargetSection = function() {
925 return this.hash.project.objects['PBXNativeTarget'];
926}
927
928pbxProject.prototype.xcVersionGroupSection = function () {
929 if (typeof this.hash.project.objects['XCVersionGroup'] !== 'object') {
930 this.hash.project.objects['XCVersionGroup'] = {};
931 }
932
933 return this.hash.project.objects['XCVersionGroup'];
934}
935
936pbxProject.prototype.pbxXCConfigurationList = function() {
937 return this.hash.project.objects['XCConfigurationList'];
938}
939
940pbxProject.prototype.pbxGroupByName = function(name) {
941 var groups = this.hash.project.objects['PBXGroup'],
942 key, groupKey;
943
944 for (key in groups) {
945 // only look for comments
946 if (!COMMENT_KEY.test(key)) continue;
947
948 if (groups[key] == name) {
949 groupKey = key.split(COMMENT_KEY)[0];
950 return groups[groupKey];
951 }
952 }
953
954 return null;
955}
956
957pbxProject.prototype.pbxTargetByName = function(name) {
958 return this.pbxItemByComment(name, 'PBXNativeTarget');
959}
960
961pbxProject.prototype.findTargetKey = function(name) {
962 var targets = this.hash.project.objects['PBXNativeTarget'];
963
964 for (var key in targets) {
965 // only look for comments
966 if (COMMENT_KEY.test(key)) continue;
967
968 var target = targets[key];
969 if (target.name === name) {
970 return key;
971 }
972 }
973
974 return null;
975}
976
977pbxProject.prototype.pbxItemByComment = function(name, pbxSectionName) {
978 var section = this.hash.project.objects[pbxSectionName],
979 key, itemKey;
980
981 for (key in section) {
982 // only look for comments
983 if (!COMMENT_KEY.test(key)) continue;
984
985 if (section[key] == name) {
986 itemKey = key.split(COMMENT_KEY)[0];
987 return section[itemKey];
988 }
989 }
990
991 return null;
992}
993
994pbxProject.prototype.pbxSourcesBuildPhaseObj = function(target) {
995 return this.buildPhaseObject('PBXSourcesBuildPhase', 'Sources', target);
996}
997
998pbxProject.prototype.pbxResourcesBuildPhaseObj = function(target) {
999 return this.buildPhaseObject('PBXResourcesBuildPhase', 'Resources', target);
1000}
1001
1002pbxProject.prototype.pbxFrameworksBuildPhaseObj = function(target) {
1003 return this.buildPhaseObject('PBXFrameworksBuildPhase', 'Frameworks', target);
1004}
1005
1006pbxProject.prototype.pbxEmbedFrameworksBuildPhaseObj = function (target) {
1007 return this.buildPhaseObject('PBXCopyFilesBuildPhase', 'Embed Frameworks', target);
1008};
1009
1010// Find Build Phase from group/target
1011pbxProject.prototype.buildPhase = function(group, target) {
1012
1013 if (!target)
1014 return undefined;
1015
1016 var nativeTargets = this.pbxNativeTargetSection();
1017 if (typeof nativeTargets[target] == "undefined")
1018 throw new Error("Invalid target: " + target);
1019
1020 var nativeTarget = nativeTargets[target];
1021 var buildPhases = nativeTarget.buildPhases;
1022 for(var i in buildPhases)
1023 {
1024 var buildPhase = buildPhases[i];
1025 if (buildPhase.comment==group)
1026 return buildPhase.value + "_comment";
1027 }
1028 }
1029
1030pbxProject.prototype.buildPhaseObject = function(name, group, target) {
1031 var section = this.hash.project.objects[name],
1032 obj, sectionKey, key;
1033 var buildPhase = this.buildPhase(group, target);
1034
1035 for (key in section) {
1036
1037 // only look for comments
1038 if (!COMMENT_KEY.test(key)) continue;
1039
1040 // select the proper buildPhase
1041 if (buildPhase && buildPhase!=key)
1042 continue;
1043 if (section[key] == group) {
1044 sectionKey = key.split(COMMENT_KEY)[0];
1045 return section[sectionKey];
1046 }
1047 }
1048 return null;
1049}
1050
1051pbxProject.prototype.addBuildProperty = function(prop, value, build_name) {
1052 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1053 key, configuration;
1054
1055 for (key in configurations){
1056 configuration = configurations[key];
1057 if (!build_name || configuration.name === build_name){
1058 configuration.buildSettings[prop] = value;
1059 }
1060 }
1061}
1062
1063pbxProject.prototype.removeBuildProperty = function(prop, build_name) {
1064 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1065 key, configuration;
1066
1067 for (key in configurations){
1068 configuration = configurations[key];
1069 if (configuration.buildSettings[prop] &&
1070 !build_name || configuration.name === build_name){
1071 delete configuration.buildSettings[prop];
1072 }
1073 }
1074}
1075
1076/**
1077 *
1078 * @param prop {String}
1079 * @param value {String|Array|Object|Number|Boolean}
1080 * @param build {String} Release or Debug
1081 */
1082pbxProject.prototype.updateBuildProperty = function(prop, value, build) {
1083 var configs = this.pbxXCBuildConfigurationSection();
1084 for (var configName in configs) {
1085 if (!COMMENT_KEY.test(configName)) {
1086 var config = configs[configName];
1087 if ( (build && config.name === build) || (!build) ) {
1088 config.buildSettings[prop] = value;
1089 }
1090 }
1091 }
1092}
1093
1094pbxProject.prototype.updateProductName = function(name) {
1095 this.updateBuildProperty('PRODUCT_NAME', '"' + name + '"');
1096}
1097
1098pbxProject.prototype.removeFromFrameworkSearchPaths = function(file) {
1099 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1100 INHERITED = '"$(inherited)"',
1101 SEARCH_PATHS = 'FRAMEWORK_SEARCH_PATHS',
1102 config, buildSettings, searchPaths;
1103 var new_path = searchPathForFile(file, this);
1104
1105 for (config in configurations) {
1106 buildSettings = configurations[config].buildSettings;
1107
1108 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
1109 continue;
1110
1111 searchPaths = buildSettings[SEARCH_PATHS];
1112
1113 if (searchPaths) {
1114 var matches = searchPaths.filter(function(p) {
1115 return p.indexOf(new_path) > -1;
1116 });
1117 matches.forEach(function(m) {
1118 var idx = searchPaths.indexOf(m);
1119 searchPaths.splice(idx, 1);
1120 });
1121 }
1122 }
1123}
1124
1125pbxProject.prototype.addToFrameworkSearchPaths = function(file) {
1126 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1127 INHERITED = '"$(inherited)"',
1128 config, buildSettings, searchPaths;
1129
1130 for (config in configurations) {
1131 buildSettings = configurations[config].buildSettings;
1132
1133 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
1134 continue;
1135
1136 if (!buildSettings['FRAMEWORK_SEARCH_PATHS']
1137 || buildSettings['FRAMEWORK_SEARCH_PATHS'] === INHERITED) {
1138 buildSettings['FRAMEWORK_SEARCH_PATHS'] = [INHERITED];
1139 }
1140
1141 buildSettings['FRAMEWORK_SEARCH_PATHS'].push(searchPathForFile(file, this));
1142 }
1143}
1144
1145pbxProject.prototype.removeFromLibrarySearchPaths = function(file) {
1146 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1147 INHERITED = '"$(inherited)"',
1148 SEARCH_PATHS = 'LIBRARY_SEARCH_PATHS',
1149 config, buildSettings, searchPaths;
1150 var new_path = searchPathForFile(file, this);
1151
1152 for (config in configurations) {
1153 buildSettings = configurations[config].buildSettings;
1154
1155 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
1156 continue;
1157
1158 searchPaths = buildSettings[SEARCH_PATHS];
1159
1160 if (searchPaths) {
1161 var matches = searchPaths.filter(function(p) {
1162 return p.indexOf(new_path) > -1;
1163 });
1164 matches.forEach(function(m) {
1165 var idx = searchPaths.indexOf(m);
1166 searchPaths.splice(idx, 1);
1167 });
1168 }
1169
1170 }
1171}
1172
1173pbxProject.prototype.addToLibrarySearchPaths = function(file) {
1174 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1175 INHERITED = '"$(inherited)"',
1176 config, buildSettings, searchPaths;
1177
1178 for (config in configurations) {
1179 buildSettings = configurations[config].buildSettings;
1180
1181 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
1182 continue;
1183
1184 if (!buildSettings['LIBRARY_SEARCH_PATHS']
1185 || buildSettings['LIBRARY_SEARCH_PATHS'] === INHERITED) {
1186 buildSettings['LIBRARY_SEARCH_PATHS'] = [INHERITED];
1187 }
1188
1189 if (typeof file === 'string') {
1190 buildSettings['LIBRARY_SEARCH_PATHS'].push(file);
1191 } else {
1192 buildSettings['LIBRARY_SEARCH_PATHS'].push(searchPathForFile(file, this));
1193 }
1194 }
1195}
1196
1197pbxProject.prototype.removeFromHeaderSearchPaths = function(file) {
1198 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1199 INHERITED = '"$(inherited)"',
1200 SEARCH_PATHS = 'HEADER_SEARCH_PATHS',
1201 config, buildSettings, searchPaths;
1202 var new_path = searchPathForFile(file, this);
1203
1204 for (config in configurations) {
1205 buildSettings = configurations[config].buildSettings;
1206
1207 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
1208 continue;
1209
1210 if (buildSettings[SEARCH_PATHS]) {
1211 var matches = buildSettings[SEARCH_PATHS].filter(function(p) {
1212 return p.indexOf(new_path) > -1;
1213 });
1214 matches.forEach(function(m) {
1215 var idx = buildSettings[SEARCH_PATHS].indexOf(m);
1216 buildSettings[SEARCH_PATHS].splice(idx, 1);
1217 });
1218 }
1219
1220 }
1221}
1222pbxProject.prototype.addToHeaderSearchPaths = function(file) {
1223 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1224 INHERITED = '"$(inherited)"',
1225 config, buildSettings, searchPaths;
1226
1227 for (config in configurations) {
1228 buildSettings = configurations[config].buildSettings;
1229
1230 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
1231 continue;
1232
1233 if (!buildSettings['HEADER_SEARCH_PATHS']) {
1234 buildSettings['HEADER_SEARCH_PATHS'] = [INHERITED];
1235 }
1236
1237 if (typeof file === 'string') {
1238 buildSettings['HEADER_SEARCH_PATHS'].push(file);
1239 } else {
1240 buildSettings['HEADER_SEARCH_PATHS'].push(searchPathForFile(file, this));
1241 }
1242 }
1243}
1244
1245pbxProject.prototype.addToOtherLinkerFlags = function (flag) {
1246 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1247 INHERITED = '"$(inherited)"',
1248 OTHER_LDFLAGS = 'OTHER_LDFLAGS',
1249 config, buildSettings;
1250
1251 for (config in configurations) {
1252 buildSettings = configurations[config].buildSettings;
1253
1254 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName)
1255 continue;
1256
1257 if (!buildSettings[OTHER_LDFLAGS]
1258 || buildSettings[OTHER_LDFLAGS] === INHERITED) {
1259 buildSettings[OTHER_LDFLAGS] = [INHERITED];
1260 }
1261
1262 buildSettings[OTHER_LDFLAGS].push(flag);
1263 }
1264}
1265
1266pbxProject.prototype.removeFromOtherLinkerFlags = function (flag) {
1267 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1268 OTHER_LDFLAGS = 'OTHER_LDFLAGS',
1269 config, buildSettings;
1270
1271 for (config in configurations) {
1272 buildSettings = configurations[config].buildSettings;
1273
1274 if (unquote(buildSettings['PRODUCT_NAME']) != this.productName) {
1275 continue;
1276 }
1277
1278 if (buildSettings[OTHER_LDFLAGS]) {
1279 var matches = buildSettings[OTHER_LDFLAGS].filter(function (p) {
1280 return p.indexOf(flag) > -1;
1281 });
1282 matches.forEach(function (m) {
1283 var idx = buildSettings[OTHER_LDFLAGS].indexOf(m);
1284 buildSettings[OTHER_LDFLAGS].splice(idx, 1);
1285 });
1286 }
1287 }
1288}
1289
1290pbxProject.prototype.addToBuildSettings = function (buildSetting, value) {
1291 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1292 config, buildSettings;
1293
1294 for (config in configurations) {
1295 buildSettings = configurations[config].buildSettings;
1296
1297 buildSettings[buildSetting] = value;
1298 }
1299}
1300
1301pbxProject.prototype.removeFromBuildSettings = function (buildSetting) {
1302 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1303 config, buildSettings;
1304
1305 for (config in configurations) {
1306 buildSettings = configurations[config].buildSettings;
1307
1308 if (buildSettings[buildSetting]) {
1309 delete buildSettings[buildSetting];
1310 }
1311 }
1312}
1313
1314// a JS getter. hmmm
1315pbxProject.prototype.__defineGetter__("productName", function() {
1316 var configurations = nonComments(this.pbxXCBuildConfigurationSection()),
1317 config, productName;
1318
1319 for (config in configurations) {
1320 productName = configurations[config].buildSettings['PRODUCT_NAME'];
1321
1322 if (productName) {
1323 return unquote(productName);
1324 }
1325 }
1326});
1327
1328// check if file is present
1329pbxProject.prototype.hasFile = function(filePath) {
1330 var files = nonComments(this.pbxFileReferenceSection()),
1331 file, id;
1332 for (id in files) {
1333 file = files[id];
1334 if (file.path == filePath || file.path == ('"' + filePath + '"')) {
1335 return file;
1336 }
1337 }
1338
1339 return false;
1340}
1341
1342pbxProject.prototype.addTarget = function(name, type, subfolder) {
1343
1344 // Setup uuid and name of new target
1345 var targetUuid = this.generateUuid(),
1346 targetType = type,
1347 targetSubfolder = subfolder || name,
1348 targetName = name.trim();
1349
1350 // Check type against list of allowed target types
1351 if (!targetName) {
1352 throw new Error("Target name missing.");
1353 }
1354
1355 // Check type against list of allowed target types
1356 if (!targetType) {
1357 throw new Error("Target type missing.");
1358 }
1359
1360 // Check type against list of allowed target types
1361 if (!producttypeForTargettype(targetType)) {
1362 throw new Error("Target type invalid: " + targetType);
1363 }
1364
1365 // Build Configuration: Create
1366 var buildConfigurationsList = [
1367 {
1368 name: 'Debug',
1369 isa: 'XCBuildConfiguration',
1370 buildSettings: {
1371 GCC_PREPROCESSOR_DEFINITIONS: ['"DEBUG=1"', '"$(inherited)"'],
1372 INFOPLIST_FILE: '"' + path.join(targetSubfolder, targetSubfolder + '-Info.plist' + '"'),
1373 LD_RUNPATH_SEARCH_PATHS: '"$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"',
1374 PRODUCT_NAME: '"' + targetName + '"',
1375 SKIP_INSTALL: 'YES'
1376 }
1377 },
1378 {
1379 name: 'Release',
1380 isa: 'XCBuildConfiguration',
1381 buildSettings: {
1382 INFOPLIST_FILE: '"' + path.join(targetSubfolder, targetSubfolder + '-Info.plist' + '"'),
1383 LD_RUNPATH_SEARCH_PATHS: '"$(inherited) @executable_path/Frameworks @executable_path/../../Frameworks"',
1384 PRODUCT_NAME: '"' + targetName + '"',
1385 SKIP_INSTALL: 'YES'
1386 }
1387 }
1388 ];
1389
1390 // Build Configuration: Add
1391 var buildConfigurations = this.addXCConfigurationList(buildConfigurationsList, 'Release', 'Build configuration list for PBXNativeTarget "' + targetName +'"');
1392
1393 // Product: Create
1394 var productName = targetName,
1395 productType = producttypeForTargettype(targetType),
1396 productFileType = filetypeForProducttype(productType),
1397 productFile = this.addProductFile(productName, { group: 'Copy Files', 'target': targetUuid, 'explicitFileType': productFileType}),
1398 productFileName = productFile.basename;
1399
1400
1401 // Product: Add to build file list
1402 this.addToPbxBuildFileSection(productFile);
1403
1404 // Target: Create
1405 var target = {
1406 uuid: targetUuid,
1407 pbxNativeTarget: {
1408 isa: 'PBXNativeTarget',
1409 name: '"' + targetName + '"',
1410 productName: '"' + targetName + '"',
1411 productReference: productFile.fileRef,
1412 productType: '"' + producttypeForTargettype(targetType) + '"',
1413 buildConfigurationList: buildConfigurations.uuid,
1414 buildPhases: [],
1415 buildRules: [],
1416 dependencies: []
1417 }
1418 };
1419
1420 // Target: Add to PBXNativeTarget section
1421 this.addToPbxNativeTargetSection(target)
1422
1423 // Product: Embed (only for "extension"-type targets)
1424 if (targetType === 'app_extension') {
1425
1426 // Create CopyFiles phase in first target
1427 this.addBuildPhase([], 'PBXCopyFilesBuildPhase', 'Copy Files', this.getFirstTarget().uuid, targetType)
1428
1429 // Add product to CopyFiles phase
1430 this.addToPbxCopyfilesBuildPhase(productFile)
1431
1432 // this.addBuildPhaseToTarget(newPhase.buildPhase, this.getFirstTarget().uuid)
1433
1434 };
1435
1436 // Target: Add uuid to root project
1437 this.addToPbxProjectSection(target);
1438
1439 // Target: Add dependency for this target to first (main) target
1440 this.addTargetDependency(this.getFirstTarget().uuid, [target.uuid]);
1441
1442
1443 // Return target on success
1444 return target;
1445
1446};
1447
1448// helper recursive prop search+replace
1449function propReplace(obj, prop, value) {
1450 var o = {};
1451 for (var p in obj) {
1452 if (o.hasOwnProperty.call(obj, p)) {
1453 if (typeof obj[p] == 'object' && !Array.isArray(obj[p])) {
1454 propReplace(obj[p], prop, value);
1455 } else if (p == prop) {
1456 obj[p] = value;
1457 }
1458 }
1459 }
1460}
1461
1462// helper object creation functions
1463function pbxBuildFileObj(file) {
1464 var obj = Object.create(null);
1465
1466 obj.isa = 'PBXBuildFile';
1467 obj.fileRef = file.fileRef;
1468 obj.fileRef_comment = file.basename;
1469 if (file.settings) obj.settings = file.settings;
1470
1471 return obj;
1472}
1473
1474function pbxFileReferenceObj(file) {
1475 var fileObject = {
1476 isa: "PBXFileReference",
1477 name: "\"" + file.basename + "\"",
1478 path: "\"" + file.path.replace(/\\/g, '/') + "\"",
1479 sourceTree: file.sourceTree,
1480 fileEncoding: file.fileEncoding,
1481 lastKnownFileType: file.lastKnownFileType,
1482 explicitFileType: file.explicitFileType,
1483 includeInIndex: file.includeInIndex
1484 };
1485
1486 return fileObject;
1487}
1488
1489function pbxGroupChild(file) {
1490 var obj = Object.create(null);
1491
1492 obj.value = file.fileRef;
1493 obj.comment = file.basename;
1494
1495 return obj;
1496}
1497
1498function pbxBuildPhaseObj(file) {
1499 var obj = Object.create(null);
1500
1501 obj.value = file.uuid;
1502 obj.comment = longComment(file);
1503
1504 return obj;
1505}
1506
1507function pbxCopyFilesBuildPhaseObj(obj, folderType, subfolderPath, phaseName) {
1508
1509 // Add additional properties for 'CopyFiles' build phase
1510 var DESTINATION_BY_TARGETTYPE = {
1511 application: 'wrapper',
1512 app_extension: 'plugins',
1513 bundle: 'wrapper',
1514 command_line_tool: 'wrapper',
1515 dynamic_library: 'products_directory',
1516 framework: 'shared_frameworks',
1517 frameworks: 'frameworks',
1518 static_library: 'products_directory',
1519 unit_test_bundle: 'wrapper',
1520 watch_app: 'wrapper',
1521 watch_extension: 'plugins'
1522 }
1523 var SUBFOLDERSPEC_BY_DESTINATION = {
1524 absolute_path: 0,
1525 executables: 6,
1526 frameworks: 10,
1527 java_resources: 15,
1528 plugins: 13,
1529 products_directory: 16,
1530 resources: 7,
1531 shared_frameworks: 11,
1532 shared_support: 12,
1533 wrapper: 1,
1534 xpc_services: 0
1535 }
1536
1537 obj.name = '"' + phaseName + '"';
1538 obj.dstPath = subfolderPath || '""';
1539 obj.dstSubfolderSpec = SUBFOLDERSPEC_BY_DESTINATION[DESTINATION_BY_TARGETTYPE[folderType]];
1540
1541 return obj;
1542}
1543
1544function pbxShellScriptBuildPhaseObj(obj, options, phaseName) {
1545 obj.name = '"' + phaseName + '"';
1546 obj.inputPaths = options.inputPaths || [];
1547 obj.outputPaths = options.outputPaths || [];
1548 obj.shellPath = options.shellPath;
1549 obj.shellScript = '"' + options.shellScript.replace(/"/g, '\\"') + '"';
1550
1551 return obj;
1552}
1553
1554function pbxBuildFileComment(file) {
1555 return longComment(file);
1556}
1557
1558function pbxFileReferenceComment(file) {
1559 return file.basename || path.basename(file.path);
1560}
1561
1562function pbxNativeTargetComment(target) {
1563 return target.name;
1564}
1565
1566function longComment(file) {
1567 return f("%s in %s", file.basename, file.group);
1568}
1569
1570// respect <group> path
1571function correctForPluginsPath(file, project) {
1572 return correctForPath(file, project, 'Plugins');
1573}
1574
1575function correctForResourcesPath(file, project) {
1576 return correctForPath(file, project, 'Resources');
1577}
1578
1579function correctForFrameworksPath(file, project) {
1580 return correctForPath(file, project, 'Frameworks');
1581}
1582
1583function correctForPath(file, project, group) {
1584 var r_group_dir = new RegExp('^' + group + '[\\\\/]');
1585
1586 if (project.pbxGroupByName(group).path)
1587 file.path = file.path.replace(r_group_dir, '');
1588
1589 return file;
1590}
1591
1592function searchPathForFile(file, proj) {
1593 var plugins = proj.pbxGroupByName('Plugins'),
1594 pluginsPath = plugins ? plugins.path : null,
1595 fileDir = path.dirname(file.path);
1596
1597 if (fileDir == '.') {
1598 fileDir = '';
1599 } else {
1600 fileDir = '/' + fileDir;
1601 }
1602
1603 if (file.plugin && pluginsPath) {
1604 return '"\\"$(SRCROOT)/' + unquote(pluginsPath) + '\\""';
1605 } else if (file.customFramework && file.dirname) {
1606 return '"\\"' + file.dirname + '\\""';
1607 } else {
1608 return '"\\"$(SRCROOT)/' + proj.productName + fileDir + '\\""';
1609 }
1610}
1611
1612function nonComments(obj) {
1613 var keys = Object.keys(obj),
1614 newObj = {}, i = 0;
1615
1616 for (i; i < keys.length; i++) {
1617 if (!COMMENT_KEY.test(keys[i])) {
1618 newObj[keys[i]] = obj[keys[i]];
1619 }
1620 }
1621
1622 return newObj;
1623}
1624
1625function unquote(str) {
1626 if (str) return str.replace(/^"(.*)"$/, "$1");
1627}
1628
1629
1630function buildPhaseNameForIsa (isa) {
1631
1632 BUILDPHASENAME_BY_ISA = {
1633 PBXCopyFilesBuildPhase: 'Copy Files',
1634 PBXResourcesBuildPhase: 'Resources',
1635 PBXSourcesBuildPhase: 'Sources',
1636 PBXFrameworksBuildPhase: 'Frameworks'
1637 }
1638
1639 return BUILDPHASENAME_BY_ISA[isa]
1640}
1641
1642function producttypeForTargettype (targetType) {
1643
1644 PRODUCTTYPE_BY_TARGETTYPE = {
1645 application: 'com.apple.product-type.application',
1646 app_extension: 'com.apple.product-type.app-extension',
1647 bundle: 'com.apple.product-type.bundle',
1648 command_line_tool: 'com.apple.product-type.tool',
1649 dynamic_library: 'com.apple.product-type.library.dynamic',
1650 framework: 'com.apple.product-type.framework',
1651 static_library: 'com.apple.product-type.library.static',
1652 unit_test_bundle: 'com.apple.product-type.bundle.unit-test',
1653 watch_app: 'com.apple.product-type.application.watchapp',
1654 watch_extension: 'com.apple.product-type.watchkit-extension'
1655 };
1656
1657 return PRODUCTTYPE_BY_TARGETTYPE[targetType]
1658}
1659
1660function filetypeForProducttype (productType) {
1661
1662 FILETYPE_BY_PRODUCTTYPE = {
1663 'com.apple.product-type.application': '"wrapper.application"',
1664 'com.apple.product-type.app-extension': '"wrapper.app-extension"',
1665 'com.apple.product-type.bundle': '"wrapper.plug-in"',
1666 'com.apple.product-type.tool': '"compiled.mach-o.dylib"',
1667 'com.apple.product-type.library.dynamic': '"compiled.mach-o.dylib"',
1668 'com.apple.product-type.framework': '"wrapper.framework"',
1669 'com.apple.product-type.library.static': '"archive.ar"',
1670 'com.apple.product-type.bundle.unit-test': '"wrapper.cfbundle"',
1671 'com.apple.product-type.application.watchapp': '"wrapper.application"',
1672 'com.apple.product-type.watchkit-extension': '"wrapper.app-extension"'
1673 };
1674
1675 return FILETYPE_BY_PRODUCTTYPE[productType]
1676}
1677
1678pbxProject.prototype.getFirstProject = function() {
1679
1680 // Get pbxProject container
1681 var pbxProjectContainer = this.pbxProjectSection();
1682
1683 // Get first pbxProject UUID
1684 var firstProjectUuid = Object.keys(pbxProjectContainer)[0];
1685
1686 // Get first pbxProject
1687 var firstProject = pbxProjectContainer[firstProjectUuid];
1688
1689 return {
1690 uuid: firstProjectUuid,
1691 firstProject: firstProject
1692 }
1693}
1694
1695pbxProject.prototype.getFirstTarget = function() {
1696
1697 // Get first targets UUID
1698 var firstTargetUuid = this.getFirstProject()['firstProject']['targets'][0].value;
1699
1700 // Get first pbxNativeTarget
1701 var firstTarget = this.pbxNativeTargetSection()[firstTargetUuid];
1702
1703 return {
1704 uuid: firstTargetUuid,
1705 firstTarget: firstTarget
1706 }
1707}
1708
1709/*** NEW ***/
1710
1711pbxProject.prototype.addToPbxGroupType = function (file, groupKey, groupType) {
1712 var group = this.getPBXGroupByKeyAndType(groupKey, groupType);
1713 if (group && group.children !== undefined) {
1714 if (typeof file === 'string') {
1715 //Group Key
1716 var childGroup = {
1717 value:file,
1718 };
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 }
1725
1726 group.children.push(childGroup);
1727 }
1728 else {
1729 //File Object
1730 group.children.push(pbxGroupChild(file));
1731 }
1732 }
1733}
1734
1735pbxProject.prototype.addToPbxVariantGroup = function (file, groupKey) {
1736 this.addToPbxGroupType(file, groupKey, 'PBXVariantGroup');
1737}
1738
1739pbxProject.prototype.addToPbxGroup = function (file, groupKey) {
1740 this.addToPbxGroupType(file, groupKey, 'PBXGroup');
1741}
1742
1743
1744
1745pbxProject.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
1770pbxProject.prototype.pbxCreateVariantGroup = function(name) {
1771 return this.pbxCreateGroupWithType(name, undefined, 'PBXVariantGroup')
1772}
1773
1774pbxProject.prototype.pbxCreateGroup = function(name, pathName) {
1775 return this.pbxCreateGroupWithType(name, pathName, 'PBXGroup');
1776}
1777
1778
1779
1780pbxProject.prototype.removeFromPbxGroupAndType = function (file, groupKey, groupType) {
1781 var group = this.getPBXGroupByKeyAndType(groupKey, groupType);
1782 if (group) {
1783 var groupChildren = group.children, i;
1784 for(i in groupChildren) {
1785 if(pbxGroupChild(file).value == groupChildren[i].value &&
1786 pbxGroupChild(file).comment == groupChildren[i].comment) {
1787 groupChildren.splice(i, 1);
1788 break;
1789 }
1790 }
1791 }
1792}
1793
1794pbxProject.prototype.removeFromPbxGroup = function (file, groupKey) {
1795 this.removeFromPbxGroupAndType(file, groupKey, 'PBXGroup');
1796}
1797
1798pbxProject.prototype.removeFromPbxVariantGroup = function (file, groupKey) {
1799 this.removeFromPbxGroupAndType(file, groupKey, 'PBXVariantGroup');
1800}
1801
1802
1803
1804pbxProject.prototype.getPBXGroupByKeyAndType = function(key, groupType) {
1805 return this.hash.project.objects[groupType][key];
1806};
1807
1808pbxProject.prototype.getPBXGroupByKey = function(key) {
1809 return this.hash.project.objects['PBXGroup'][key];
1810};
1811
1812pbxProject.prototype.getPBXVariantGroupByKey = function(key) {
1813 return this.hash.project.objects['PBXVariantGroup'][key];
1814};
1815
1816
1817
1818pbxProject.prototype.findPBXGroupKeyAndType = function(criteria, groupType) {
1819 var groups = this.hash.project.objects[groupType];
1820 var target;
1821
1822 for (var key in groups) {
1823 // only look for comments
1824 if (COMMENT_KEY.test(key)) continue;
1825
1826 var group = groups[key];
1827 if (criteria && criteria.path && criteria.name) {
1828 if (criteria.path === group.path && criteria.name === group.name) {
1829 target = key;
1830 break
1831 }
1832 }
1833 else if (criteria && criteria.path) {
1834 if (criteria.path === group.path) {
1835 target = key;
1836 break
1837 }
1838 }
1839 else if (criteria && criteria.name) {
1840 if (criteria.name === group.name) {
1841 target = key;
1842 break
1843 }
1844 }
1845 }
1846
1847 return target;
1848}
1849
1850pbxProject.prototype.findPBXGroupKey = function(criteria) {
1851 return this.findPBXGroupKeyAndType(criteria, 'PBXGroup');
1852}
1853
1854pbxProject.prototype.findPBXVariantGroupKey = function(criteria) {
1855 return this.findPBXGroupKeyAndType(criteria, 'PBXVariantGroup');
1856}
1857
1858pbxProject.prototype.addLocalizationVariantGroup = function(name) {
1859 var groupKey = this.pbxCreateVariantGroup(name);
1860
1861 var resourceGroupKey = this.findPBXGroupKey({name: 'Resources'});
1862 this.addToPbxGroup(groupKey, resourceGroupKey);
1863
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};
1874
1875pbxProject.prototype.addKnownRegion = function (name) {
1876 if (!this.pbxProjectSection()[this.getFirstProject()['uuid']]['knownRegions']) {
1877 this.pbxProjectSection()[this.getFirstProject()['uuid']]['knownRegions'] = [];
1878 }
1879 if (!this.hasKnownRegion(name)) {
1880 this.pbxProjectSection()[this.getFirstProject()['uuid']]['knownRegions'].push(name);
1881 }
1882}
1883
1884pbxProject.prototype.removeKnownRegion = function (name) {
1885 var regions = this.pbxProjectSection()[this.getFirstProject()['uuid']]['knownRegions'];
1886 if (regions) {
1887 for (var i = 0; i < regions.length; i++) {
1888 if (regions[i] === name) {
1889 regions.splice(i, 1);
1890 break;
1891 }
1892 }
1893 this.pbxProjectSection()[this.getFirstProject()['uuid']]['knownRegions'] = regions;
1894 }
1895}
1896
1897pbxProject.prototype.hasKnownRegion = function (name) {
1898 var regions = this.pbxProjectSection()[this.getFirstProject()['uuid']]['knownRegions'];
1899 if (regions) {
1900 for (var i in regions) {
1901 if (regions[i] === name) {
1902 return true;
1903 }
1904 }
1905 }
1906 return false;
1907}
1908
1909pbxProject.prototype.getPBXObject = function(name) {
1910 return this.hash.project.objects[name];
1911}
1912
1913
1914
1915pbxProject.prototype.addFile = function (path, group, opt) {
1916 var file = new pbxFile(path, opt);
1917
1918 // null is better for early errors
1919 if (this.hasFile(file.path)) return null;
1920
1921 file.fileRef = this.generateUuid();
1922
1923 this.addToPbxFileReferenceSection(file); // PBXFileReference
1924
1925 if (this.getPBXGroupByKey(group)) {
1926 this.addToPbxGroup(file, group); // PBXGroup
1927 }
1928 else if (this.getPBXVariantGroupByKey(group)) {
1929 this.addToPbxVariantGroup(file, group); // PBXVariantGroup
1930 }
1931
1932 return file;
1933}
1934
1935pbxProject.prototype.removeFile = function (path, group, opt) {
1936 var file = new pbxFile(path, opt);
1937
1938 this.removeFromPbxFileReferenceSection(file); // PBXFileReference
1939
1940 if (this.getPBXGroupByKey(group)) {
1941 this.removeFromPbxGroup(file, group); // PBXGroup
1942 }
1943 else if (this.getPBXVariantGroupByKey(group)) {
1944 this.removeFromPbxVariantGroup(file, group); // PBXVariantGroup
1945 }
1946
1947 return file;
1948}
1949
1950
1951
1952pbxProject.prototype.getBuildProperty = function(prop, build) {
1953 var target;
1954 var configs = this.pbxXCBuildConfigurationSection();
1955 for (var configName in configs) {
1956 if (!COMMENT_KEY.test(configName)) {
1957 var config = configs[configName];
1958 if ( (build && config.name === build) || (build === undefined) ) {
1959 if (config.buildSettings[prop] !== undefined) {
1960 target = config.buildSettings[prop];
1961 }
1962 }
1963 }
1964 }
1965 return target;
1966}
1967
1968pbxProject.prototype.getBuildConfigByName = function(name) {
1969 var target = {};
1970 var configs = this.pbxXCBuildConfigurationSection();
1971 for (var configName in configs) {
1972 if (!COMMENT_KEY.test(configName)) {
1973 var config = configs[configName];
1974 if (config.name === name) {
1975 target[configName] = config;
1976 }
1977 }
1978 }
1979 return target;
1980}
1981
1982pbxProject.prototype.addDataModelDocument = function(filePath, group, opt) {
1983 if (!group) {
1984 group = 'Resources';
1985 }
1986 if (!this.getPBXGroupByKey(group)) {
1987 group = this.findPBXGroupKey({ name: group });
1988 }
1989
1990 var file = new pbxFile(filePath, opt);
1991
1992 if (!file || this.hasFile(file.path)) return null;
1993
1994 file.fileRef = this.generateUuid();
1995 this.addToPbxGroup(file, group);
1996
1997 if (!file) return false;
1998
1999 file.target = opt ? opt.target : undefined;
2000 file.uuid = this.generateUuid();
2001
2002 this.addToPbxBuildFileSection(file);
2003 this.addToPbxSourcesBuildPhase(file);
2004
2005 file.models = [];
2006 var currentVersionName;
2007 var modelFiles = fs.readdirSync(file.path);
2008 for (var index in modelFiles) {
2009 var modelFileName = modelFiles[index];
2010 var modelFilePath = path.join(filePath, modelFileName);
2011
2012 if (modelFileName == '.xccurrentversion') {
2013 currentVersionName = plist.readFileSync(modelFilePath)._XCCurrentVersionName;
2014 continue;
2015 }
2016
2017 var modelFile = new pbxFile(modelFilePath);
2018 modelFile.fileRef = this.generateUuid();
2019
2020 this.addToPbxFileReferenceSection(modelFile);
2021
2022 file.models.push(modelFile);
2023
2024 if (currentVersionName && currentVersionName === modelFileName) {
2025 file.currentModel = modelFile;
2026 }
2027 }
2028
2029 if (!file.currentModel) {
2030 file.currentModel = file.models[0];
2031 }
2032
2033 this.addToXcVersionGroupSection(file);
2034
2035 return file;
2036}
2037
2038pbxProject.prototype.addTargetAttribute = function(prop, value, target) {
2039 var attributes = this.getFirstProject()['firstProject']['attributes'];
2040 if (attributes['TargetAttributes'] === undefined) {
2041 attributes['TargetAttributes'] = {};
2042 }
2043 target = target || this.getFirstTarget();
2044 if (attributes['TargetAttributes'][target.uuid] === undefined) {
2045 attributes['TargetAttributes'][target.uuid] = {};
2046 }
2047 attributes['TargetAttributes'][target.uuid][prop] = value;
2048}
2049
2050pbxProject.prototype.removeTargetAttribute = function(prop, target) {
2051 var attributes = this.getFirstProject()['firstProject']['attributes'];
2052 target = target || this.getFirstTarget();
2053 if (attributes['TargetAttributes'] &&
2054 attributes['TargetAttributes'][target.uuid]) {
2055 delete attributes['TargetAttributes'][target.uuid][prop];
2056 }
2057}
2058
2059module.exports = pbxProject;