1+
2+ var linkTools = require ( '../../tools/linkToolsIos' ) ;
13var fs = require ( "fs" ) ;
2- var glob = require ( "glob" ) ;
34var inquirer = require ( 'inquirer' ) ;
4- var path = require ( "path" ) ;
55var plist = require ( "plist" ) ;
6- var xcode = require ( "xcode" ) ;
76var semver = require ( 'semver' ) ;
87
98var package = require ( '../../../../../package.json' ) ;
@@ -12,15 +11,7 @@ module.exports = () => {
1211
1312 console . log ( "Running ios postlink script" ) ;
1413
15- var ignoreNodeModules = { ignore : "node_modules/**" } ;
16- var ignoreNodeModulesAndPods = { ignore : [ "node_modules/**" , "ios/Pods/**" ] } ;
17- var appDelegatePaths = glob . sync ( "**/AppDelegate.+(mm|m)" , ignoreNodeModules ) ;
18-
19- // Fix for https://github.com/Microsoft/react-native-code-push/issues/477
20- // Typical location of AppDelegate.m for newer RN versions: $PROJECT_ROOT/ios/<project_name>/AppDelegate.m
21- // Let's try to find that path by filtering the whole array for any path containing <project_name>
22- // If we can't find it there, play dumb and pray it is the first path we find.
23- var appDelegatePath = findFileByAppName ( appDelegatePaths , package ? package . name : null ) || appDelegatePaths [ 0 ] ;
14+ var appDelegatePath = linkTools . getAppDeletePath ( ) ;
2415
2516 if ( ! appDelegatePath ) {
2617 return Promise . reject ( `Couldn't find AppDelegate. You might need to update it manually \
@@ -31,13 +22,12 @@ module.exports = () => {
3122 var appDelegateContents = fs . readFileSync ( appDelegatePath , "utf8" ) ;
3223
3324 // 1. Add the header import statement
34- var codePushHeaderImportStatement = `#import <CodePush/CodePush.h>` ;
35- if ( ~ appDelegateContents . indexOf ( codePushHeaderImportStatement ) ) {
25+ if ( ~ appDelegateContents . indexOf ( linkTools . codePushHeaderImportStatement ) ) {
3626 console . log ( `"CodePush.h" header already imported.` ) ;
3727 } else {
3828 var appDelegateHeaderImportStatement = `#import "AppDelegate.h"` ;
3929 appDelegateContents = appDelegateContents . replace ( appDelegateHeaderImportStatement ,
40- `${ appDelegateHeaderImportStatement } \n ${ codePushHeaderImportStatement } ` ) ;
30+ `${ appDelegateHeaderImportStatement } ${ linkTools . codePushHeaderImportStatementFormatted } ` ) ;
4131 }
4232
4333 // 2. Modify jsCodeLocation value assignment
@@ -46,8 +36,8 @@ module.exports = () => {
4636 if ( ! reactNativeVersion ) {
4737 console . log ( `Can't take react-native version from package.json` ) ;
4838 } else if ( semver . gte ( semver . coerce ( reactNativeVersion ) , "0.59.0" ) ) {
49- var oldBundleUrl = "[[NSBundle mainBundle] URLForResource:@\"main\" withExtension:@\"jsbundle\"]" ;
50- var codePushBundleUrl = "[CodePush bundleURL]" ;
39+ var oldBundleUrl = linkTools . oldBundleUrl ;
40+ var codePushBundleUrl = linkTools . codePushBundleUrl ;
5141
5242 if ( ~ appDelegateContents . indexOf ( codePushBundleUrl ) ) {
5343 console . log ( `"BundleUrl" already pointing to "[CodePush bundleURL]".` ) ;
@@ -65,21 +55,16 @@ module.exports = () => {
6555 console . log ( 'Couldn\'t find jsCodeLocation setting in AppDelegate.' ) ;
6656 }
6757
68- var newJsCodeLocationAssignmentStatement = "jsCodeLocation = [CodePush bundleURL];" ;
58+ var newJsCodeLocationAssignmentStatement = linkTools . codePushGradleLink ;
6959 if ( ~ appDelegateContents . indexOf ( newJsCodeLocationAssignmentStatement ) ) {
7060 console . log ( `"jsCodeLocation" already pointing to "[CodePush bundleURL]".` ) ;
7161 } else {
7262 if ( jsCodeLocations . length === 1 ) {
73- // If there is one `jsCodeLocation` it means that react-native app version is lower than 0.57.8
63+ // If there is one `jsCodeLocation` it means that react-native app version is not the 0.57.8 or 0.57.0 and lower than 0.59
7464 // and we should replace this line with DEBUG ifdef statement and add CodePush call for Release case
7565
7666 var oldJsCodeLocationAssignmentStatement = jsCodeLocations [ 0 ] ;
77- var jsCodeLocationPatch = `
78- #ifdef DEBUG
79- ${ oldJsCodeLocationAssignmentStatement }
80- #else
81- ${ newJsCodeLocationAssignmentStatement }
82- #endif` ;
67+ var jsCodeLocationPatch = linkTools . getJsCodeLocationPatch ( oldJsCodeLocationAssignmentStatement ) ;
8368 appDelegateContents = appDelegateContents . replace ( oldJsCodeLocationAssignmentStatement ,
8469 jsCodeLocationPatch ) ;
8570 } else if ( jsCodeLocations . length === 2 ) {
@@ -94,7 +79,7 @@ module.exports = () => {
9479 }
9580 }
9681
97- var plistPath = getPlistPath ( ) ;
82+ var plistPath = linkTools . getPlistPath ( ) ;
9883
9984 if ( ! plistPath ) {
10085 return Promise . reject ( `Couldn't find .plist file. You might need to update it manually \
@@ -128,103 +113,4 @@ module.exports = () => {
128113 fs . writeFileSync ( appDelegatePath , appDelegateContents ) ;
129114 fs . writeFileSync ( plistPath , plistContents ) ;
130115 }
131-
132- // Helper that filters an array with AppDelegate.m paths for a path with the app name inside it
133- // Should cover nearly all cases
134- function findFileByAppName ( array , appName ) {
135- if ( array . length === 0 || ! appName ) return null ;
136-
137- for ( var i = 0 ; i < array . length ; i ++ ) {
138- var path = array [ i ] ;
139- if ( path && path . indexOf ( appName ) !== - 1 ) {
140- return path ;
141- }
142- }
143-
144- return null ;
145- }
146-
147- function getDefaultPlistPath ( ) {
148- //this is old logic in case we are unable to find PLIST from xcode/pbxproj - at least we can fallback to default solution
149- return glob . sync ( `**/${ package . name } /*Info.plist` , ignoreNodeModules ) [ 0 ] ;
150- }
151-
152- // This is enhanced version of standard implementation of xcode 'getBuildProperty' function
153- // but allows us to narrow results by PRODUCT_NAME property also.
154- // So we suppose that proj name should be the same as package name, otherwise fallback to default plist path searching logic
155- function getBuildSettingsPropertyMatchingTargetProductName ( parsedXCodeProj , prop , targetProductName , build ) {
156- var target ;
157- var COMMENT_KEY = / _ c o m m e n t $ / ;
158- var PRODUCT_NAME_PROJECT_KEY = 'PRODUCT_NAME' ;
159- var TV_OS_DEPLOYMENT_TARGET_PROPERTY_NAME = 'TVOS_DEPLOYMENT_TARGET' ;
160- var TEST_HOST_PROPERTY_NAME = 'TEST_HOST' ;
161-
162- var configs = parsedXCodeProj . pbxXCBuildConfigurationSection ( ) ;
163- for ( var configName in configs ) {
164- if ( ! COMMENT_KEY . test ( configName ) ) {
165- var config = configs [ configName ] ;
166- if ( ( build && config . name === build ) || ( build === undefined ) ) {
167- if ( targetProductName ) {
168- if ( config . buildSettings [ prop ] !== undefined && config . buildSettings [ PRODUCT_NAME_PROJECT_KEY ] == targetProductName ) {
169- target = config . buildSettings [ prop ] ;
170- }
171- } else {
172- if ( config . buildSettings [ prop ] !== undefined &&
173- //exclude tvOS projects
174- config . buildSettings [ TV_OS_DEPLOYMENT_TARGET_PROPERTY_NAME ] == undefined &&
175- //exclude test app
176- config . buildSettings [ TEST_HOST_PROPERTY_NAME ] == undefined ) {
177- target = config . buildSettings [ prop ] ;
178- }
179- }
180- }
181- }
182- }
183- return target ;
184- }
185-
186- function getPlistPath ( ) {
187- var xcodeProjectPaths = glob . sync ( `**/*.xcodeproj/project.pbxproj` , ignoreNodeModulesAndPods ) ;
188- if ( ! xcodeProjectPaths ) {
189- return getDefaultPlistPath ( ) ;
190- }
191-
192- if ( xcodeProjectPaths . length !== 1 ) {
193- console . log ( 'Could not determine correct xcode proj path to retrieve related plist file, there are multiple xcodeproj under the solution.' ) ;
194- return getDefaultPlistPath ( ) ;
195- }
196-
197- var xcodeProjectPath = xcodeProjectPaths [ 0 ] ;
198- var parsedXCodeProj ;
199-
200- try {
201- var proj = xcode . project ( xcodeProjectPath ) ;
202- //use sync version because there are some problems with async version of xcode lib as of current version
203- parsedXCodeProj = proj . parseSync ( ) ;
204- }
205- catch ( e ) {
206- console . log ( 'Couldn\'t read info.plist path from xcode project - error: ' + e . message ) ;
207- return getDefaultPlistPath ( ) ;
208- }
209-
210- var INFO_PLIST_PROJECT_KEY = 'INFOPLIST_FILE' ;
211- var RELEASE_BUILD_PROPERTY_NAME = "Release" ;
212- var targetProductName = package ? package . name : null ;
213-
214- //Try to get 'Release' build of ProductName matching the package name first and if it doesn't exist then try to get any other if existing
215- var plistPathValue = getBuildSettingsPropertyMatchingTargetProductName ( parsedXCodeProj , INFO_PLIST_PROJECT_KEY , targetProductName , RELEASE_BUILD_PROPERTY_NAME ) ||
216- getBuildSettingsPropertyMatchingTargetProductName ( parsedXCodeProj , INFO_PLIST_PROJECT_KEY , targetProductName ) ||
217- getBuildSettingsPropertyMatchingTargetProductName ( parsedXCodeProj , INFO_PLIST_PROJECT_KEY , null , RELEASE_BUILD_PROPERTY_NAME ) ||
218- getBuildSettingsPropertyMatchingTargetProductName ( parsedXCodeProj , INFO_PLIST_PROJECT_KEY ) ||
219- parsedXCodeProj . getBuildProperty ( INFO_PLIST_PROJECT_KEY , RELEASE_BUILD_PROPERTY_NAME ) ||
220- parsedXCodeProj . getBuildProperty ( INFO_PLIST_PROJECT_KEY ) ;
221-
222- if ( ! plistPathValue ) {
223- return getDefaultPlistPath ( ) ;
224- }
225-
226- //also remove surrounding quotes from plistPathValue to get correct path resolved
227- //(see https://github.com/Microsoft/react-native-code-push/issues/534#issuecomment-302069326 for details)
228- return path . resolve ( path . dirname ( xcodeProjectPath ) , '..' , plistPathValue . replace ( / ^ " ( .* ) " $ / , '$1' ) ) ;
229- }
230- }
116+ }
0 commit comments