@@ -35,7 +35,8 @@ var util = require('util'),
3535 isEntitlementFileType = constants . isEntitlementFileType ,
3636 isAssetFileType = constants . isAssetFileType ,
3737 isPlistFileType = constants . isPlistFileType ,
38- isModuleMapFileType = constants . isModuleMapFileType ;
38+ isModuleMapFileType = constants . isModuleMapFileType ,
39+ guidMapper = require ( './guidMapper' ) ;
3940
4041function pbxProject ( filename ) {
4142 if ( ! ( this instanceof pbxProject ) )
@@ -74,6 +75,10 @@ pbxProject.prototype.parseSync = function() {
7475}
7576
7677pbxProject . prototype . writeSync = function ( options ) {
78+ if ( this . pbxGroupTracker ) {
79+ this . pbxGroupTracker . writeFileSync ( ) ;
80+ }
81+
7782 this . writer = new pbxWriter ( this . hash , options ) ;
7883 return this . writer . writeSync ( ) ;
7984}
@@ -538,10 +543,27 @@ pbxProject.prototype.findMainPbxGroup = function () {
538543
539544 return null ;
540545}
546+ pbxProject . prototype . getPbxGroupTracker = function ( path ) {
547+
548+ if ( ! this . pbxGroupTracker ) {
549+ this . pbxGroupTracker = new guidMapper ( $path . join ( path , '.ns-build-pbxgroup-data.json' ) ) ;
550+ }
551+
552+ return this . pbxGroupTracker ;
553+ }
541554
542555pbxProject . prototype . addPbxGroup = function ( filePathsArray , name , path , sourceTree , opt ) {
543556 opt = opt || { } ;
544557 var srcRootPath = $path . dirname ( $path . dirname ( this . filepath ) ) ;
558+
559+ var existingGroupId = this . getPbxGroupTracker ( srcRootPath ) . findEntryGuid ( name , path ) ;
560+ if ( existingGroupId ) {
561+ if ( this . getPBXGroupByKey ( existingGroupId ) ) {
562+ this . removePbxGroupByKey ( existingGroupId , path ) ;
563+ }
564+ this . pbxGroupTracker . removeEntry ( existingGroupId ) ;
565+ }
566+
545567 var groups = this . hash . project . objects [ 'PBXGroup' ] ,
546568 pbxGroupUuid = opt . uuid || this . generateUuid ( ) ,
547569 commentKey = f ( "%s_comment" , pbxGroupUuid ) ,
@@ -560,6 +582,9 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT
560582 pbxGroup . path = path ;
561583 }
562584
585+ // save to group to the tracker
586+ this . pbxGroupTracker . addEntry ( pbxGroupUuid , path , name ) ;
587+
563588 for ( var key in fileReferenceSection ) {
564589 // only look for comments
565590 if ( ! COMMENT_KEY . test ( key ) ) continue ;
0 commit comments