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
@@ -222,10 +229,16 @@ export class CocoaPodsService implements ICocoaPodsService {
222229 return `${ CocoaPodsService . PODFILE_POST_INSTALL_SECTION_NAME } do |${ CocoaPodsService . INSTALLER_BLOCK_PARAMETER_NAME } |${ EOL } ` ;
223230 }
224231
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 } {
226233 const pluginPodfileContent = this . $fs . readText ( pluginPodFilePath ) ;
227234 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+ }
229242
230243 return {
231244 podfileContent : `${ this . getPluginPodfileHeader ( pluginPodFilePath ) } ${ EOL } ${ replacedContent } ${ EOL } ${ this . getPluginPodfileEnd ( ) } ` ,
@@ -234,6 +247,43 @@ export class CocoaPodsService implements ICocoaPodsService {
234247 } ;
235248 }
236249
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+ }
237287}
238288
239289$injector . register ( "cocoapodsService" , CocoaPodsService ) ;
0 commit comments