1
1
import { EOL } from "os" ;
2
2
import * as path from "path" ;
3
3
import { PluginNativeDirNames , PODFILE_NAME , NS_BASE_PODFILE } from "../constants" ;
4
- import { regExpEscape } from "../common/helpers" ;
4
+ import { regExpEscape , getHash } from "../common/helpers" ;
5
5
6
6
export class CocoaPodsService implements ICocoaPodsService {
7
7
private static PODFILE_POST_INSTALL_SECTION_NAME = "post_install" ;
8
8
private static INSTALLER_BLOCK_PARAMETER_NAME = "installer" ;
9
-
9
+ private getCocoaPodsFromPodfile : Function ;
10
10
constructor (
11
11
private $cocoaPodsPlatformManager : ICocoaPodsPlatformManager ,
12
12
private $fs : IFileSystem ,
13
13
private $childProcess : IChildProcess ,
14
14
private $errors : IErrors ,
15
15
private $logger : ILogger ,
16
16
private $config : IConfiguration ,
17
- private $xcconfigService : IXcconfigService ) { }
17
+ private $xcconfigService : IXcconfigService ) {
18
+ this . getCocoaPodsFromPodfile = _ . memoize ( this . _getCocoaPodsFromPodfile , getHash ) ;
19
+ }
18
20
19
21
public getPodfileHeader ( targetName : string ) : string {
20
22
return `use_frameworks!${ EOL } ${ EOL } target "${ targetName } " do${ EOL } ` ;
@@ -35,7 +37,11 @@ export class CocoaPodsService implements ICocoaPodsService {
35
37
const podInstallResult = await this . $childProcess . spawnFromEvent ( podTool , [ "install" ] , "close" , { cwd : projectRoot , stdio : [ 'pipe' , process . stdout , process . stdout ] } , { throwError : false } ) ;
36
38
37
39
if ( podInstallResult . exitCode !== 0 ) {
38
- this . $errors . fail ( `'${ podTool } install' command failed.${ podInstallResult . stderr ? " Error is: " + podInstallResult . stderr : "" } ` ) ;
40
+ // https://github.com/CocoaPods/CocoaPods/blob/92aaf0f1120d32f3487960b485fb69fcaf61486c/lib/cocoapods/resolver.rb#L498
41
+ // TODO add article
42
+ const versionResolutionHint = podInstallResult . exitCode === 31 ? `For more information on resolving CocoaPod issues in NativeScript read.` : "" ;
43
+ this . $errors . fail ( `'${ podTool } install' command failed.${ podInstallResult . stderr ? " Error is: " + podInstallResult . stderr : "" }
44
+ ${ versionResolutionHint } `) ;
39
45
}
40
46
41
47
return podInstallResult ;
@@ -59,17 +65,18 @@ export class CocoaPodsService implements ICocoaPodsService {
59
65
const mainPodfilePath = path . join ( projectData . appResourcesDirectoryPath , normalizedPlatformName , PODFILE_NAME ) ;
60
66
const projectPodfilePath = this . getProjectPodfilePath ( projectRoot ) ;
61
67
if ( this . $fs . exists ( projectPodfilePath ) || this . $fs . exists ( mainPodfilePath ) ) {
62
- await this . applyPodfileToProject ( NS_BASE_PODFILE , mainPodfilePath , projectData , projectRoot ) ;
68
+ await this . applyPodfileToProject ( NS_BASE_PODFILE , mainPodfilePath , projectData , platformData ) ;
63
69
}
64
70
}
65
71
66
- public async applyPodfileToProject ( moduleName : string , podfilePath : string , projectData : IProjectData , nativeProjectPath : string ) : Promise < void > {
72
+ public async applyPodfileToProject ( moduleName : string , podfilePath : string , projectData : IProjectData , platformData : IPlatformData ) : Promise < void > {
73
+ const nativeProjectPath = platformData . projectRoot ;
67
74
if ( ! this . $fs . exists ( podfilePath ) ) {
68
75
this . removePodfileFromProject ( moduleName , podfilePath , projectData , nativeProjectPath ) ;
69
76
return ;
70
77
}
71
78
72
- const { podfileContent, replacedFunctions, podfilePlatformData } = this . buildPodfileContent ( podfilePath , moduleName ) ;
79
+ const { podfileContent, replacedFunctions, podfilePlatformData } = this . buildPodfileContent ( podfilePath , moduleName , projectData , platformData ) ;
73
80
const pathToProjectPodfile = this . getProjectPodfilePath ( nativeProjectPath ) ;
74
81
const projectPodfileContent = this . $fs . exists ( pathToProjectPodfile ) ? this . $fs . readText ( pathToProjectPodfile ) . trim ( ) : "" ;
75
82
@@ -86,6 +93,10 @@ export class CocoaPodsService implements ICocoaPodsService {
86
93
finalPodfileContent = this . $cocoaPodsPlatformManager . addPlatformSection ( projectData , podfilePlatformData , finalPodfileContent ) ;
87
94
}
88
95
96
+ if ( this . isMainPodFile ( podfilePath , projectData , platformData ) && projectData . nsConfig . overridePods ) {
97
+ finalPodfileContent = this . overridePodsFromFile ( finalPodfileContent , projectData , platformData ) ;
98
+ }
99
+
89
100
finalPodfileContent = `${ finalPodfileContent . trim ( ) } ${ EOL } ${ EOL } ${ podfileContent . trim ( ) } ${ EOL } ` ;
90
101
this . saveProjectPodfile ( projectData , finalPodfileContent , nativeProjectPath ) ;
91
102
}
@@ -222,10 +233,17 @@ export class CocoaPodsService implements ICocoaPodsService {
222
233
return `${ CocoaPodsService . PODFILE_POST_INSTALL_SECTION_NAME } do |${ CocoaPodsService . INSTALLER_BLOCK_PARAMETER_NAME } |${ EOL } ` ;
223
234
}
224
235
225
- private buildPodfileContent ( pluginPodFilePath : string , pluginName : string ) : { podfileContent : string , replacedFunctions : IRubyFunction [ ] , podfilePlatformData : IPodfilePlatformData } {
236
+ private buildPodfileContent ( pluginPodFilePath : string , pluginName : string , projectData : IProjectData , platformData : IPlatformData ) : { podfileContent : string , replacedFunctions : IRubyFunction [ ] , podfilePlatformData : IPodfilePlatformData } {
237
+ const mainPodfilePath = this . getMainPodFilePath ( projectData , platformData ) ;
226
238
const pluginPodfileContent = this . $fs . readText ( pluginPodFilePath ) ;
227
239
const data = this . replaceHookContent ( CocoaPodsService . PODFILE_POST_INSTALL_SECTION_NAME , pluginPodfileContent , pluginName ) ;
228
- const { replacedContent, podfilePlatformData } = this . $cocoaPodsPlatformManager . replacePlatformRow ( data . replacedContent , pluginPodFilePath ) ;
240
+ const cocoapodsData = this . $cocoaPodsPlatformManager . replacePlatformRow ( data . replacedContent , pluginPodFilePath ) ;
241
+ const podfilePlatformData = cocoapodsData . podfilePlatformData ;
242
+ let replacedContent = cocoapodsData . replacedContent ;
243
+
244
+ if ( projectData . nsConfig . overridePods && mainPodfilePath !== pluginPodFilePath ) {
245
+ replacedContent = this . overridePodsFromFile ( replacedContent , projectData , platformData ) ;
246
+ }
229
247
230
248
return {
231
249
podfileContent : `${ this . getPluginPodfileHeader ( pluginPodFilePath ) } ${ EOL } ${ replacedContent } ${ EOL } ${ this . getPluginPodfileEnd ( ) } ` ,
@@ -234,6 +252,43 @@ export class CocoaPodsService implements ICocoaPodsService {
234
252
} ;
235
253
}
236
254
255
+ private getMainPodFilePath ( projectData : IProjectData , platformData : IPlatformData ) : string {
256
+ return path . join ( projectData . appResourcesDirectoryPath , platformData . normalizedPlatformName , PODFILE_NAME ) ;
257
+ }
258
+
259
+ private isMainPodFile ( podFilePath : string , projectData : IProjectData , platformData : IPlatformData ) : boolean {
260
+ const mainPodfilePath = this . getMainPodFilePath ( projectData , platformData ) ;
261
+
262
+ return podFilePath === mainPodfilePath ;
263
+ }
264
+
265
+ private overridePodsFromFile ( podfileContent : string , projectData : IProjectData , platformData : IPlatformData ) : string {
266
+ const mainPodfilePath = this . getMainPodFilePath ( projectData , platformData ) ;
267
+ const mainPodfileContent = this . $fs . readText ( mainPodfilePath ) ;
268
+ const pods = this . getCocoaPodsFromPodfile ( mainPodfileContent ) ;
269
+ _ . forEach ( pods , pod => {
270
+ podfileContent = podfileContent . replace ( new RegExp ( `^[ ]*pod\\s*["']${ pod } ['"].*$` , "gm" ) , '#$&' ) ;
271
+ } ) ;
272
+
273
+ return podfileContent ;
274
+ }
275
+
276
+ private _getCocoaPodsFromPodfile ( podfileContent : string ) : Array < string > {
277
+ const pods = [ ] ;
278
+ const podsRegex = / ^ \s * p o d \s * [ " ' ] ( .* ?) [ ' " ] .* $ / gm;
279
+
280
+ let match = podsRegex . exec ( podfileContent ) ;
281
+ while ( match != null ) {
282
+ const podName : string = match [ 1 ] ;
283
+ if ( podName ) {
284
+ pods . push ( podName ) ;
285
+ }
286
+
287
+ match = podsRegex . exec ( podfileContent ) ;
288
+ }
289
+
290
+ return pods ;
291
+ }
237
292
}
238
293
239
294
$injector . register ( "cocoapodsService" , CocoaPodsService ) ;
0 commit comments