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
@@ -222,10 +229,16 @@ export class CocoaPodsService implements ICocoaPodsService {
222
229
return `${ CocoaPodsService . PODFILE_POST_INSTALL_SECTION_NAME } do |${ CocoaPodsService . INSTALLER_BLOCK_PARAMETER_NAME } |${ EOL } ` ;
223
230
}
224
231
225
- private buildPodfileContent ( pluginPodFilePath : string , pluginName : string ) : { podfileContent : string , replacedFunctions : IRubyFunction [ ] , podfilePlatformData : IPodfilePlatformData } {
232
+ private buildPodfileContent ( pluginPodFilePath : string , pluginName : string , projectData : IProjectData , platformData : IPlatformData ) : { podfileContent : string , replacedFunctions : IRubyFunction [ ] , podfilePlatformData : IPodfilePlatformData } {
226
233
const pluginPodfileContent = this . $fs . readText ( pluginPodFilePath ) ;
227
234
const data = this . replaceHookContent ( CocoaPodsService . PODFILE_POST_INSTALL_SECTION_NAME , pluginPodfileContent , pluginName ) ;
228
- const { replacedContent, podfilePlatformData } = this . $cocoaPodsPlatformManager . replacePlatformRow ( data . replacedContent , pluginPodFilePath ) ;
235
+ const cocoapodsData = this . $cocoaPodsPlatformManager . replacePlatformRow ( data . replacedContent , pluginPodFilePath ) ;
236
+ const podfilePlatformData = cocoapodsData . podfilePlatformData ;
237
+ let replacedContent = cocoapodsData . replacedContent ;
238
+
239
+ if ( projectData . nsConfig && projectData . nsConfig . overridePods && ! this . isMainPodFile ( pluginPodFilePath , projectData , platformData ) ) {
240
+ replacedContent = this . overridePodsFromFile ( replacedContent , projectData , platformData ) ;
241
+ }
229
242
230
243
return {
231
244
podfileContent : `${ this . getPluginPodfileHeader ( pluginPodFilePath ) } ${ EOL } ${ replacedContent } ${ EOL } ${ this . getPluginPodfileEnd ( ) } ` ,
@@ -234,6 +247,43 @@ export class CocoaPodsService implements ICocoaPodsService {
234
247
} ;
235
248
}
236
249
250
+ private getMainPodFilePath ( projectData : IProjectData , platformData : IPlatformData ) : string {
251
+ return path . join ( projectData . appResourcesDirectoryPath , platformData . normalizedPlatformName , PODFILE_NAME ) ;
252
+ }
253
+
254
+ private isMainPodFile ( podFilePath : string , projectData : IProjectData , platformData : IPlatformData ) : boolean {
255
+ const mainPodfilePath = this . getMainPodFilePath ( projectData , platformData ) ;
256
+
257
+ return podFilePath === mainPodfilePath ;
258
+ }
259
+
260
+ private overridePodsFromFile ( podfileContent : string , projectData : IProjectData , platformData : IPlatformData ) : string {
261
+ const mainPodfilePath = this . getMainPodFilePath ( projectData , platformData ) ;
262
+ const mainPodfileContent = this . $fs . readText ( mainPodfilePath ) ;
263
+ const pods = this . getCocoaPodsFromPodfile ( mainPodfileContent ) ;
264
+ _ . forEach ( pods , pod => {
265
+ podfileContent = podfileContent . replace ( new RegExp ( `^[ ]*pod\\s*["']${ pod } ['"].*$` , "gm" ) , '#$&' ) ;
266
+ } ) ;
267
+
268
+ return podfileContent ;
269
+ }
270
+
271
+ private _getCocoaPodsFromPodfile ( podfileContent : string ) : Array < string > {
272
+ const pods = [ ] ;
273
+ const podsRegex = / ^ \s * p o d \s * [ " ' ] ( .* ?) [ ' " ] .* $ / gm;
274
+
275
+ let match = podsRegex . exec ( podfileContent ) ;
276
+ while ( match != null ) {
277
+ const podName : string = match [ 1 ] ;
278
+ if ( podName ) {
279
+ pods . push ( podName ) ;
280
+ }
281
+
282
+ match = podsRegex . exec ( podfileContent ) ;
283
+ }
284
+
285
+ return pods ;
286
+ }
237
287
}
238
288
239
289
$injector . register ( "cocoapodsService" , CocoaPodsService ) ;
0 commit comments