@@ -517,6 +517,7 @@ pbxProject.prototype.findMainPbxGroup = function () {
517517
518518pbxProject . prototype . addPbxGroup = function ( filePathsArray , name , path , sourceTree , opt ) {
519519 opt = opt || { } ;
520+ var srcRootPath = $path . dirname ( $path . dirname ( this . filepath ) ) ;
520521 var oldGroup = this . pbxGroupByName ( name ) ;
521522 if ( oldGroup ) {
522523 this . removePbxGroup ( name , path ) ;
@@ -560,7 +561,6 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT
560561 continue ;
561562 }
562563
563- var srcRootPath = $path . dirname ( $path . dirname ( this . filepath ) ) ;
564564 var relativePath = $path . relative ( srcRootPath , filePath ) ;
565565 var file = new pbxFile ( opt . filesRelativeToProject ? relativePath : filePath ) ;
566566 file . uuid = this . generateUuid ( ) ;
@@ -569,22 +569,21 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT
569569 file . target = opt . target ;
570570 }
571571 if ( fs . existsSync ( filePath ) && fs . lstatSync ( filePath ) . isDirectory ( ) && ! isAssetFileType ( file . lastKnownFileType ) ) {
572- file . fileRef = file . uuid ;
573- var files = fs . readdirSync ( filePath ) . map ( p => $path . join ( filePath , p ) ) ;
574572 if ( $path . extname ( filePath ) === ".lproj" ) {
575- addLocalizationGroup . call ( this , pbxGroup , file , files , srcRootPath , opt ) ;
576- } else {
577- this . addToPbxFileReferenceSection ( file ) ; // PBXFileReference
578- this . addToPbxBuildFileSection ( file ) ;
579- pbxGroup . children . push ( pbxGroupChild ( file ) ) ;
580- this . addPbxGroup ( files , $path . basename ( filePath ) , filePath , null , { uuid : file . uuid , filesRelativeToProject : opt . filesRelativeToProject , target : opt . target } ) ;
573+ continue ;
581574 }
575+ file . fileRef = file . uuid ;
576+ var files = fs . readdirSync ( filePath ) . map ( p => $path . join ( filePath , p ) ) ;
577+ this . addToPbxFileReferenceSection ( file ) ; // PBXFileReference
578+ this . addToPbxBuildFileSection ( file ) ;
579+ pbxGroup . children . push ( pbxGroupChild ( file ) ) ;
580+ this . addPbxGroup ( files , $path . basename ( filePath ) , filePath , null , { uuid : file . uuid , filesRelativeToProject : opt . filesRelativeToProject , target : opt . target } ) ;
582581 } else {
583582 this . addToPbxFileReferenceSection ( file ) ; // PBXFileReference
584583 pbxGroup . children . push ( pbxGroupChild ( file ) ) ;
585584 if ( isHeaderFileType ( file . lastKnownFileType ) || isPlist ( file . lastKnownFileType ) ) {
586585 continue ;
587- }
586+ }
588587
589588 if ( isEntitlement ( file . lastKnownFileType ) ) {
590589 this . addToBuildSettings ( 'CODE_SIGN_ENTITLEMENTS' , file . path , opt . target ) ;
@@ -601,6 +600,8 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT
601600 }
602601 }
603602
603+ handleLocalization . call ( this , filePathsArray , pbxGroup , srcRootPath , opt ) ;
604+
604605 if ( groups ) {
605606 groups [ pbxGroupUuid ] = pbxGroup ;
606607 groups [ commentKey ] = name ;
@@ -618,26 +619,55 @@ pbxProject.prototype.addPbxGroup = function (filePathsArray, name, path, sourceT
618619 return { uuid : pbxGroupUuid , pbxGroup : pbxGroup } ;
619620}
620621
621- function addLocalizationGroup ( pbxGroup , variantFile , files , srcRootPath , opt ) {
622- for ( var i = 0 ; i < files . length ; i ++ ) {
623- var variantGroupName = $path . parse ( $path . basename ( files [ i ] ) ) . name + ".storyboard" ;
624- var variantGroup = this . findPBXVariantGroupKey ( { name : variantGroupName } ) ;
625- if ( ! variantGroup ) {
626- variantGroup = this . addLocalizationVariantGroup ( variantGroupName , { target : opt . target , skipAddToResourcesGroup : true } ) ;
627- pbxGroup . children . push ( pbxGroupChild ( variantGroup ) ) ;
622+ function handleLocalization ( files , pbxGroup , srcRootPath , opt ) {
623+ var storyboardNames = { } ;
624+ var allNames = { } ;
625+ var regions = { } ;
626+
627+ for ( let i = 0 ; i < files . length ; i ++ ) {
628+ const filePath = files [ i ] ;
629+ const parsedPath = $path . parse ( filePath ) ;
630+ if ( $path . extname ( filePath ) === ".lproj" ) {
631+ var regionName = parsedPath . name ;
632+ var region = regions [ regionName ] = { } ;
633+ var regionFiles = fs . readdirSync ( filePath ) ;
634+ this . addKnownRegion ( regionName ) ;
635+ for ( let j = 0 ; j < regionFiles . length ; j ++ ) {
636+ var regionFilePath = regionFiles [ j ] ;
637+ var parsedRegionFilePath = $path . parse ( regionFilePath ) ;
638+ var regionFileName = parsedRegionFilePath . name ;
639+ if ( parsedRegionFilePath . ext === ".storyboard" ) {
640+ storyboardNames [ parsedRegionFilePath . name ] = true ;
641+ }
642+ fileRegions = allNames [ parsedRegionFilePath . name ] = allNames [ parsedRegionFilePath . name ] || [ ] ;
643+ fileRegions . push ( regionName ) ;
644+ region [ regionFileName ] = $path . join ( filePath , regionFilePath ) ;
645+ }
628646 }
629- var refFile = new pbxFile ( $path . relative ( srcRootPath , files [ i ] ) , { basename : $path . parse ( variantFile . basename ) . name } ) ;
630- refFile . fileRef = this . generateUuid ( ) ;
631- this . addToPbxFileReferenceSection ( refFile ) ;
632- this . addToPbxVariantGroup ( refFile , variantGroup . fileRef ) ;
633647 }
634- }
648+
649+ for ( var name in allNames ) {
650+ fileRegions = allNames [ name ]
651+ var variantGroupName = storyboardNames [ name ] ? name + ".storyboard" : name + ".strings" ;
652+
653+ var variantGroup = this . addLocalizationVariantGroup ( variantGroupName , { target : opt . target , skipAddToResourcesGroup : true } ) ;
654+ pbxGroup . children . push ( pbxGroupChild ( variantGroup ) ) ;
655+ for ( let k = 0 ; k < fileRegions . length ; k ++ ) {
656+ var file = regions [ fileRegions [ k ] ] [ name ] ;
657+ var refFile = new pbxFile ( $path . relative ( srcRootPath , file ) , { basename : fileRegions [ k ] } ) ;
658+ refFile . fileRef = this . generateUuid ( ) ;
659+ this . addToPbxFileReferenceSection ( refFile ) ;
660+ this . addToPbxVariantGroup ( refFile , variantGroup . fileRef ) ;
661+ }
662+ }
663+ } ;
635664
636665pbxProject . prototype . removePbxGroup = function ( groupName , path ) {
637- var group = this . pbxGroupByName ( groupName ) ;
638- if ( ! group ) {
666+ var groupKey = this . findPBXGroupKey ( { name : groupName } ) || this . findPBXVariantGroupKey ( { name : groupName } ) ;
667+ if ( ! groupKey ) {
639668 return ;
640669 }
670+ var group = this . getPBXGroupByKey ( groupKey ) || this . getPBXVariantGroupByKey ( groupKey )
641671
642672 path = path || "" ;
643673
@@ -663,8 +693,12 @@ pbxProject.prototype.removePbxGroup = function(groupName, path) {
663693 }
664694 }
665695
666- var section = this . hash . project . objects [ 'PBXGroup' ] ,
667- key , itemKey ;
696+ var section , key , itemKey ;
697+ if ( unquote ( group . isa ) === "PBXVariantGroup" ) {
698+ section = this . hash . project . objects [ 'PBXVariantGroup' ] ;
699+ } else {
700+ section = this . hash . project . objects [ 'PBXGroup' ] ;
701+ }
668702
669703 for ( key in section ) {
670704 // only look for comments
0 commit comments