1
+
2
+ var linkTools = require ( '../../tools/linkToolsIos' ) ;
1
3
var fs = require ( "fs" ) ;
2
- var glob = require ( "glob" ) ;
3
4
var inquirer = require ( 'inquirer' ) ;
4
- var path = require ( "path" ) ;
5
5
var plist = require ( "plist" ) ;
6
- var xcode = require ( "xcode" ) ;
7
6
var semver = require ( 'semver' ) ;
8
7
9
8
var package = require ( '../../../../../package.json' ) ;
@@ -12,15 +11,7 @@ module.exports = () => {
12
11
13
12
console . log ( "Running ios postlink script" ) ;
14
13
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 ( ) ;
24
15
25
16
if ( ! appDelegatePath ) {
26
17
return Promise . reject ( `Couldn't find AppDelegate. You might need to update it manually \
@@ -31,13 +22,12 @@ module.exports = () => {
31
22
var appDelegateContents = fs . readFileSync ( appDelegatePath , "utf8" ) ;
32
23
33
24
// 1. Add the header import statement
34
- var codePushHeaderImportStatement = `#import <CodePush/CodePush.h>` ;
35
- if ( ~ appDelegateContents . indexOf ( codePushHeaderImportStatement ) ) {
25
+ if ( ~ appDelegateContents . indexOf ( linkTools . codePushHeaderImportStatement ) ) {
36
26
console . log ( `"CodePush.h" header already imported.` ) ;
37
27
} else {
38
28
var appDelegateHeaderImportStatement = `#import "AppDelegate.h"` ;
39
29
appDelegateContents = appDelegateContents . replace ( appDelegateHeaderImportStatement ,
40
- `${ appDelegateHeaderImportStatement } \n ${ codePushHeaderImportStatement } ` ) ;
30
+ `${ appDelegateHeaderImportStatement } ${ linkTools . codePushHeaderImportStatementFormatted } ` ) ;
41
31
}
42
32
43
33
// 2. Modify jsCodeLocation value assignment
@@ -46,8 +36,8 @@ module.exports = () => {
46
36
if ( ! reactNativeVersion ) {
47
37
console . log ( `Can't take react-native version from package.json` ) ;
48
38
} 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 ;
51
41
52
42
if ( ~ appDelegateContents . indexOf ( codePushBundleUrl ) ) {
53
43
console . log ( `"BundleUrl" already pointing to "[CodePush bundleURL]".` ) ;
@@ -65,21 +55,16 @@ module.exports = () => {
65
55
console . log ( 'Couldn\'t find jsCodeLocation setting in AppDelegate.' ) ;
66
56
}
67
57
68
- var newJsCodeLocationAssignmentStatement = "jsCodeLocation = [CodePush bundleURL];" ;
58
+ var newJsCodeLocationAssignmentStatement = linkTools . codePushGradleLink ;
69
59
if ( ~ appDelegateContents . indexOf ( newJsCodeLocationAssignmentStatement ) ) {
70
60
console . log ( `"jsCodeLocation" already pointing to "[CodePush bundleURL]".` ) ;
71
61
} else {
72
62
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
74
64
// and we should replace this line with DEBUG ifdef statement and add CodePush call for Release case
75
65
76
66
var oldJsCodeLocationAssignmentStatement = jsCodeLocations [ 0 ] ;
77
- var jsCodeLocationPatch = `
78
- #ifdef DEBUG
79
- ${ oldJsCodeLocationAssignmentStatement }
80
- #else
81
- ${ newJsCodeLocationAssignmentStatement }
82
- #endif` ;
67
+ var jsCodeLocationPatch = linkTools . getJsCodeLocationPatch ( oldJsCodeLocationAssignmentStatement ) ;
83
68
appDelegateContents = appDelegateContents . replace ( oldJsCodeLocationAssignmentStatement ,
84
69
jsCodeLocationPatch ) ;
85
70
} else if ( jsCodeLocations . length === 2 ) {
@@ -94,7 +79,7 @@ module.exports = () => {
94
79
}
95
80
}
96
81
97
- var plistPath = getPlistPath ( ) ;
82
+ var plistPath = linkTools . getPlistPath ( ) ;
98
83
99
84
if ( ! plistPath ) {
100
85
return Promise . reject ( `Couldn't find .plist file. You might need to update it manually \
@@ -128,103 +113,4 @@ module.exports = () => {
128
113
fs . writeFileSync ( appDelegatePath , appDelegateContents ) ;
129
114
fs . writeFileSync ( plistPath , plistContents ) ;
130
115
}
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