11import * as path from "path" ;
2-
3- export class IOSExtensionsService implements IIOSExtensionsService {
4- constructor ( private $fs : IFileSystem ,
5- private $pbxprojDomXcode : IPbxprojDomXcode ,
6- private $xcode : IXcode ) {
2+ import { NativeTargetServiceBase } from "./ios-native-target-service-base" ;
3+ import { IOSNativeTargetProductTypes , IOSNativeTargetTypes } from "../constants" ;
4+
5+ export class IOSExtensionsService extends NativeTargetServiceBase implements IIOSExtensionsService {
6+ constructor ( protected $fs : IFileSystem ,
7+ protected $pbxprojDomXcode : IPbxprojDomXcode ,
8+ protected $xcode : IXcode ) {
9+ super ( $fs , $pbxprojDomXcode , $xcode ) ;
710 }
811
912 public async addExtensionsFromPath ( { extensionsFolderPath, projectData, platformData, pbxProjPath} : IAddExtensionsFromPathOptions ) : Promise < boolean > {
@@ -14,81 +17,35 @@ export class IOSExtensionsService implements IIOSExtensionsService {
1417 }
1518 const project = new this . $xcode . project ( pbxProjPath ) ;
1619 project . parseSync ( ) ;
17- this . $fs . readDirectory ( extensionsFolderPath )
18- . filter ( fileName => {
19- const filePath = path . join ( extensionsFolderPath , fileName ) ;
20- const stats = this . $fs . getFsStats ( filePath ) ;
21-
22- return stats . isDirectory ( ) && ! fileName . startsWith ( "." ) ;
23- } )
20+ this . getTargetDirectories ( extensionsFolderPath )
2421 . forEach ( extensionFolder => {
25- const targetUuid = this . addExtensionToProject ( extensionsFolderPath , extensionFolder , project , projectData , platformData ) ;
26- targetUuids . push ( targetUuid ) ;
22+ const target = this . addTargetToProject ( extensionsFolderPath , extensionFolder , IOSNativeTargetTypes . appExtension , project , platformData ) ;
23+ this . configureTarget ( extensionFolder , path . join ( extensionsFolderPath , extensionFolder ) , target , project , projectData ) ;
24+ targetUuids . push ( target . uuid ) ;
2725 addedExtensions = true ;
2826 } ) ;
2927
3028 this . $fs . writeFile ( pbxProjPath , project . writeSync ( { omitEmptyValues : true } ) ) ;
31- this . prepareExtensionSigning ( targetUuids , projectData , pbxProjPath ) ;
29+ this . prepareSigning ( targetUuids , projectData , pbxProjPath ) ;
3230
3331 return addedExtensions ;
3432 }
3533
36- private addExtensionToProject ( extensionsFolderPath : string , extensionFolder : string , project : IXcode . project , projectData : IProjectData , platformData : IPlatformData ) : string {
37- const extensionPath = path . join ( extensionsFolderPath , extensionFolder ) ;
38- const extensionRelativePath = path . relative ( platformData . projectRoot , extensionPath ) ;
39- const files = this . $fs . readDirectory ( extensionPath )
40- . filter ( filePath => ! filePath . startsWith ( "." ) )
41- . map ( filePath => path . join ( extensionPath , filePath ) ) ;
42- const target = project . addTarget ( extensionFolder , 'app_extension' , extensionRelativePath ) ;
43- project . addBuildPhase ( [ ] , 'PBXSourcesBuildPhase' , 'Sources' , target . uuid ) ;
44- project . addBuildPhase ( [ ] , 'PBXResourcesBuildPhase' , 'Resources' , target . uuid ) ;
45- project . addBuildPhase ( [ ] , 'PBXFrameworksBuildPhase' , 'Frameworks' , target . uuid ) ;
46-
47- const extJsonPath = path . join ( extensionsFolderPath , extensionFolder , "extension.json" ) ;
48- if ( this . $fs . exists ( extJsonPath ) ) {
49- const extensionJson = this . $fs . readJson ( extJsonPath ) ;
50- _ . forEach ( extensionJson . frameworks , framework => {
51- project . addFramework (
52- framework ,
53- { target : target . uuid }
54- ) ;
55- } ) ;
56- if ( extensionJson . assetcatalogCompilerAppiconName ) {
57- project . addToBuildSettings ( "ASSETCATALOG_COMPILER_APPICON_NAME" , extensionJson . assetcatalogCompilerAppiconName , target . uuid ) ;
58- }
59- }
34+ private configureTarget ( extensionName : string , extensionPath : string , target : IXcode . target , project : IXcode . project , projectData : IProjectData ) {
35+ const extJsonPath = path . join ( extensionPath , "extension.json" ) ;
6036
61- project . addPbxGroup ( files , extensionFolder , extensionPath , null , { isMain : true , target : target . uuid , filesRelativeToProject : true } ) ;
62- project . addBuildProperty ( "PRODUCT_BUNDLE_IDENTIFIER" , `${ projectData . projectIdentifiers . ios } .${ extensionFolder } ` , "Debug" , extensionFolder ) ;
63- project . addBuildProperty ( "PRODUCT_BUNDLE_IDENTIFIER" , ` ${ projectData . projectIdentifiers . ios } . ${ extensionFolder } ` , "Release" , extensionFolder ) ;
64- project . addToHeaderSearchPaths ( extensionPath , target . pbxNativeTarget . productName ) ;
37+ this . setXcodeTargetBuildConfigurationProperties (
38+ [ { name : "PRODUCT_BUNDLE_IDENTIFIER" , value : `${ projectData . projectIdentifiers . ios } .${ extensionName } ` } ] ,
39+ extensionName ,
40+ project ) ;
6541
66- return target . uuid ;
67- }
68-
69- private prepareExtensionSigning ( targetUuids : string [ ] , projectData :IProjectData , projectPath : string ) {
70- const xcode = this . $pbxprojDomXcode . Xcode . open ( projectPath ) ;
71- const signing = xcode . getSigning ( projectData . projectName ) ;
72- if ( signing !== undefined ) {
73- _ . forEach ( targetUuids , targetUuid => {
74- if ( signing . style === "Automatic" ) {
75- xcode . setAutomaticSigningStyleByTargetKey ( targetUuid , signing . team ) ;
76- } else {
77- for ( const config in signing . configurations ) {
78- const signingConfiguration = signing . configurations [ config ] ;
79- xcode . setManualSigningStyleByTargetKey ( targetUuid , signingConfiguration ) ;
80- break ;
81- }
82- }
83- } ) ;
84- }
85- xcode . save ( ) ;
42+ this . setConfigurationsFromJsonFile ( extJsonPath , target . uuid , extensionName , project ) ;
8643 }
8744
8845 public removeExtensions ( { pbxProjPath} : IRemoveExtensionsOptions ) : void {
8946 const project = new this . $xcode . project ( pbxProjPath ) ;
9047 project . parseSync ( ) ;
91- project . removeTargetsByProductType ( "com.apple.product-type.app-extension" ) ;
48+ project . removeTargetsByProductType ( IOSNativeTargetProductTypes . appExtension ) ;
9249 this . $fs . writeFile ( pbxProjPath , project . writeSync ( { omitEmptyValues : true } ) ) ;
9350 }
9451}
0 commit comments