11import { EOL } from "os" ;
22import * as path from "path" ;
33import { PluginNativeDirNames , PODFILE_NAME , NS_BASE_PODFILE } from "../constants" ;
4- import { regExpEscape } from "../common/helpers" ;
4+ import { regExpEscape , getHash } from "../common/helpers" ;
55
66export class CocoaPodsService implements ICocoaPodsService {
77 private static PODFILE_POST_INSTALL_SECTION_NAME = "post_install" ;
88 private static INSTALLER_BLOCK_PARAMETER_NAME = "installer" ;
9-
9+ private getCocoaPodsFromPodfile : Function ;
1010 constructor (
1111 private $cocoaPodsPlatformManager : ICocoaPodsPlatformManager ,
1212 private $fs : IFileSystem ,
1313 private $childProcess : IChildProcess ,
1414 private $errors : IErrors ,
1515 private $logger : ILogger ,
1616 private $config : IConfiguration ,
17- private $xcconfigService : IXcconfigService ) { }
17+ private $xcconfigService : IXcconfigService ) {
18+ this . getCocoaPodsFromPodfile = _ . memoize ( this . _getCocoaPodsFromPodfile , getHash ) ;
19+ }
1820
1921 public getPodfileHeader ( targetName : string ) : string {
2022 return `use_frameworks!${ EOL } ${ EOL } target "${ targetName } " do${ EOL } ` ;
@@ -35,7 +37,11 @@ export class CocoaPodsService implements ICocoaPodsService {
3537 const podInstallResult = await this . $childProcess . spawnFromEvent ( podTool , [ "install" ] , "close" , { cwd : projectRoot , stdio : [ 'pipe' , process . stdout , process . stdout ] } , { throwError : false } ) ;
3638
3739 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 } `) ;
3945 }
4046
4147 return podInstallResult ;
@@ -59,17 +65,18 @@ export class CocoaPodsService implements ICocoaPodsService {
5965 const mainPodfilePath = path . join ( projectData . appResourcesDirectoryPath , normalizedPlatformName , PODFILE_NAME ) ;
6066 const projectPodfilePath = this . getProjectPodfilePath ( projectRoot ) ;
6167 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 ) ;
6369 }
6470 }
6571
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 ;
6774 if ( ! this . $fs . exists ( podfilePath ) ) {
6875 this . removePodfileFromProject ( moduleName , podfilePath , projectData , nativeProjectPath ) ;
6976 return ;
7077 }
7178
72- const { podfileContent, replacedFunctions, podfilePlatformData } = this . buildPodfileContent ( podfilePath , moduleName ) ;
79+ const { podfileContent, replacedFunctions, podfilePlatformData } = this . buildPodfileContent ( podfilePath , moduleName , projectData , platformData ) ;
7380 const pathToProjectPodfile = this . getProjectPodfilePath ( nativeProjectPath ) ;
7481 const projectPodfileContent = this . $fs . exists ( pathToProjectPodfile ) ? this . $fs . readText ( pathToProjectPodfile ) . trim ( ) : "" ;
7582
@@ -86,6 +93,10 @@ export class CocoaPodsService implements ICocoaPodsService {
8693 finalPodfileContent = this . $cocoaPodsPlatformManager . addPlatformSection ( projectData , podfilePlatformData , finalPodfileContent ) ;
8794 }
8895
96+ if ( this . isMainPodFile ( podfilePath , projectData , platformData ) && projectData . nsConfig && projectData . nsConfig . overridePods ) {
97+ finalPodfileContent = this . overridePodsFromFile ( finalPodfileContent , projectData , platformData ) ;
98+ }
99+
89100 finalPodfileContent = `${ finalPodfileContent . trim ( ) } ${ EOL } ${ EOL } ${ podfileContent . trim ( ) } ${ EOL } ` ;
90101 this . saveProjectPodfile ( projectData , finalPodfileContent , nativeProjectPath ) ;
91102 }
@@ -222,10 +233,17 @@ export class CocoaPodsService implements ICocoaPodsService {
222233 return `${ CocoaPodsService . PODFILE_POST_INSTALL_SECTION_NAME } do |${ CocoaPodsService . INSTALLER_BLOCK_PARAMETER_NAME } |${ EOL } ` ;
223234 }
224235
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 ) ;
226238 const pluginPodfileContent = this . $fs . readText ( pluginPodFilePath ) ;
227239 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 && projectData . nsConfig . overridePods && mainPodfilePath !== pluginPodFilePath ) {
245+ replacedContent = this . overridePodsFromFile ( replacedContent , projectData , platformData ) ;
246+ }
229247
230248 return {
231249 podfileContent : `${ this . getPluginPodfileHeader ( pluginPodFilePath ) } ${ EOL } ${ replacedContent } ${ EOL } ${ this . getPluginPodfileEnd ( ) } ` ,
@@ -234,6 +252,43 @@ export class CocoaPodsService implements ICocoaPodsService {
234252 } ;
235253 }
236254
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+ }
237292}
238293
239294$injector . register ( "cocoapodsService" , CocoaPodsService ) ;
0 commit comments