forked from JaneaSystems/nodejs-mobile-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-postlink.js
342 lines (306 loc) · 14.6 KB
/
module-postlink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
var path = require('path');
var fs = require('fs');
var ncp = require('ncp');
var xcode = require('xcode');
const android = require('./../../../node_modules/react-native/local-cli/core/android');
const ios = require('./../../../node_modules/react-native/local-cli/core/ios');
function hostPackageDir(file) {
var pathComponents = file.split(path.sep);
var modulesDirIndex = pathComponents.lastIndexOf('node_modules');
if (modulesDirIndex < 1) return undefined;
return pathComponents.slice(0, modulesDirIndex).join(path.sep);
}
const getRNPMConfig = (folder) =>
// $FlowFixMe non-literal require
require(path.join(folder, './package.json')).rnpm || {};
var scriptPath = __filename;
const folder = hostPackageDir(scriptPath);
const rnpm = getRNPMConfig(folder);
function getProjectConfig() {
var scriptPath = __filename;
console.error("ScriptPath: "+scriptPath);
const folder = hostPackageDir(scriptPath);
console.error("folder: "+folder);
var returnObj={};
returnObj.ios = ios.projectConfig(folder, rnpm.ios || {});
returnObj.android = android.projectConfig(folder, rnpm.android || {});
return returnObj;
}
// Adds a custom function to remove script build phases, which is not supported in the xcode module.
xcode.project.prototype.myRemovePbxScriptBuildPhase = function (buildPhaseName, target) {
var buildPhaseTargetUuid = target || this.getFirstTarget().uuid;
var buildPhaseUuid_comment = this.buildPhase(buildPhaseName, buildPhaseTargetUuid);
if (!buildPhaseUuid_comment)
{
throw new Error("Couldn't find the build script phase to remove: " + buildPhaseName );
}
// Remove the '_comment' suffix to get the actual uuid.
var buildPhaseUuid=buildPhaseUuid_comment.split('_')[0];
// Remove from the pbxBuildPhaseObjects
var pbxBuildPhaseObjects = this.getPBXObject('PBXShellScriptBuildPhase');
if (pbxBuildPhaseObjects) {
delete pbxBuildPhaseObjects[buildPhaseUuid];
delete pbxBuildPhaseObjects[buildPhaseUuid_comment];
}
// Remove from the target's buildPhases
var nativeTargets = this.pbxNativeTargetSection();
var nativeTarget = nativeTargets[buildPhaseTargetUuid];
var buildPhases = nativeTarget.buildPhases;
for(var i in buildPhases)
{
var buildPhase = buildPhases[i];
if (buildPhase.value == buildPhaseUuid) {
buildPhases.splice(i, 1);
break;
}
}
};
var detectedConfigs=getProjectConfig();
if ( detectedConfigs && detectedConfigs.ios && detectedConfigs.ios.pbxprojPath)
{
var pbxProjectPath = detectedConfigs.ios.pbxprojPath;
var xcodeProject = xcode.project(pbxProjectPath);
var mainProjectName = detectedConfigs.ios.projectName;
xcodeProject.parse(function (error) {
if (error) {
console.error("Couldn't parse .pbx project : " + JSON.stringify(error));
return 1;
}
//Create a Frameworks group and add it to the project's main Group
var mainGroupUUID = xcodeProject.pbxProjectSection()[xcodeProject.getFirstProject().uuid].mainGroup;
var frameworksGroup = xcodeProject.pbxGroupByName('Frameworks');
if (!frameworksGroup)
{
var frameworksUUID = xcodeProject.pbxCreateGroup("Frameworks");
xcodeProject.addToPbxGroup(frameworksUUID,mainGroupUUID);
}
//Add an EmbedFrameworks build phase to the app's target
var firstTargetUUID = xcodeProject.getFirstTarget().uuid;
if(!xcodeProject.pbxEmbedFrameworksBuildPhaseObj(firstTargetUUID))
{
var buildPhaseResult = xcodeProject.addBuildPhase(
[],
'PBXCopyFilesBuildPhase',
'Embed Frameworks',
firstTargetUUID,
'framework');
buildPhaseResult.buildPhase.dstSubfolderSpec=10;
}
//Override addToFrameworkSearchPaths to add the framework path to all targets.
//The one provided in the xcode module adds the wrong path and not to the right target.
let myaddToFrameworkSearchPaths = function(fileName)
{
var configurations = xcodeProject.pbxXCBuildConfigurationSection(),
INHERITED = '"$(inherited)"',
config, buildSettings, searchPaths;
var fileDir = path.dirname(fileName);
var filePos = '"\\"' + fileDir + '\\""';
for (config in configurations) {
buildSettings = configurations[config].buildSettings;
if(!buildSettings || !buildSettings['PRODUCT_NAME'])
continue;
if (!buildSettings['FRAMEWORK_SEARCH_PATHS']
|| buildSettings['FRAMEWORK_SEARCH_PATHS'] === INHERITED) {
buildSettings['FRAMEWORK_SEARCH_PATHS'] = [INHERITED];
}
buildSettings['FRAMEWORK_SEARCH_PATHS'].push(filePos);
}
};
//Removes the old libnode.framework from the firstTarget, if it exists.
var oldFrameworkPath="../node_modules/nodejs-mobile-react-native/ios/libnode.framework";
if (xcodeProject.hasFile(oldFrameworkPath)) {
var deletedFrameworkFileRef=xcodeProject.removeFramework(
oldFrameworkPath,
{customFramework:true, embed:true, link: true, sign: true, target: firstTargetUUID}
);
}
//Adds the NodeMobile framework to the firstTarget (it's the main app target)
var frameworkPath="../node_modules/nodejs-mobile-react-native/ios/NodeMobile.framework";
var frameworkFileRef=xcodeProject.addFramework(
frameworkPath,
{customFramework:true, embed:true, link: true, sign: true, target: firstTargetUUID}
);
if(frameworkFileRef)
{
myaddToFrameworkSearchPaths(frameworkPath);
}
//Create the resources group.
var resourcesGroup = xcodeProject.pbxGroupByName('Resources');
if (!resourcesGroup)
{
xcodeProject.pbxCreateGroup("Resources");
}
//Adds the default node project as a resource to the first project, in the main group.
xcodeProject.addResourceFile(
"../nodejs-assets/nodejs-project",
{target: firstTargetUUID},
mainGroupUUID
);
//Adds the built-in module inside the plugin as a resource to the first project, in the main group.
xcodeProject.addResourceFile(
"../node_modules/nodejs-mobile-react-native/install/resources/nodejs-modules/builtin_modules",
{target: firstTargetUUID},
mainGroupUUID
);
//Disable bitcode, as it's not present in the libnode binary.
xcodeProject.addBuildProperty(
'ENABLE_BITCODE','NO','Debug'
);
xcodeProject.addBuildProperty(
'ENABLE_BITCODE','NO','Release'
);
//Adds a build phase to rebuild native modules
var rebuildNativeModulesBuildPhaseName = 'Build NodeJS Mobile Native Modules';
var rebuildNativeModulesBuildPhaseScript = `
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, look for it in the project's
#nodejs-assets/BUILD_NATIVE_MODULES.txt file.
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
PREFERENCE_FILE_PATH="$NODEJS_ASSETS_DIR/BUILD_NATIVE_MODULES.txt"
if [ -f "$PREFERENCE_FILE_PATH" ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)"
fi
fi
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, try to find .gyp files
#to turn it on.
gypfiles=($(find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -type f -name "*.gyp"))
if [ \${#gypfiles[@]} -gt 0 ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES=1
else
NODEJS_MOBILE_BUILD_NATIVE_MODULES=0
fi
fi
if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi
# Delete object files that may already come from within the npm package.
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.o" -type f -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.a" -type f -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.node" -type f -delete
# Delete bundle contents that may be there from previous builds.
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.node/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.node" -type d -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.framework/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d -delete
# Apply patches to the modules package.json
if [ -d "$CODESIGNING_FOLDER_PATH"/nodejs-project/node_modules/ ]; then
PATCH_SCRIPT_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/scripts/ && pwd )"
NODEJS_PROJECT_MODULES_DIR="$( cd "$CODESIGNING_FOLDER_PATH" && cd nodejs-project/node_modules/ && pwd )"
node "$PATCH_SCRIPT_DIR"/patch-package.js $NODEJS_PROJECT_MODULES_DIR
fi
# Get the nodejs-mobile-gyp location
if [ -d "$PROJECT_DIR/../node_modules/nodejs-mobile-gyp/" ]; then
NODEJS_MOBILE_GYP_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-gyp/ && pwd )"
else
NODEJS_MOBILE_GYP_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/node_modules/nodejs-mobile-gyp/ && pwd )"
fi
NODEJS_MOBILE_GYP_BIN_FILE="$NODEJS_MOBILE_GYP_DIR"/bin/node-gyp.js
# Rebuild modules with right environment
NODEJS_HEADERS_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/ios/libnode/ && pwd )"
pushd $CODESIGNING_FOLDER_PATH/nodejs-project/
if [ "$PLATFORM_NAME" == "iphoneos" ]
then
GYP_DEFINES="OS=ios" npm_config_nodedir="$NODEJS_HEADERS_DIR" npm_config_node_gyp="$NODEJS_MOBILE_GYP_BIN_FILE" npm_config_platform="ios" npm_config_format="make-ios" npm_config_node_engine="chakracore" npm_config_arch="arm64" npm --verbose rebuild --build-from-source
else
GYP_DEFINES="OS=ios" npm_config_nodedir="$NODEJS_HEADERS_DIR" npm_config_node_gyp="$NODEJS_MOBILE_GYP_BIN_FILE" npm_config_platform="ios" npm_config_format="make-ios" npm_config_node_engine="chakracore" npm_config_arch="x64" npm --verbose rebuild --build-from-source
fi
popd
`
var rebuildNativeModulesBuildPhase = xcodeProject.buildPhaseObject('PBXShellScriptBuildPhase', rebuildNativeModulesBuildPhaseName, firstTargetUUID);
if (rebuildNativeModulesBuildPhase) {
xcodeProject.myRemovePbxScriptBuildPhase(rebuildNativeModulesBuildPhaseName, firstTargetUUID);
}
xcodeProject.addBuildPhase(
[],
'PBXShellScriptBuildPhase',
rebuildNativeModulesBuildPhaseName,
firstTargetUUID,
{ shellPath: '/bin/sh', shellScript: rebuildNativeModulesBuildPhaseScript }
);
//Adds a build phase to sign native modules
var signNativeModulesBuildPhaseName = 'Sign NodeJS Mobile Native Modules';
var signNativeModulesBuildPhaseScript = `
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, look for it in the project's
#nodejs-assets/BUILD_NATIVE_MODULES.txt file.
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
PREFERENCE_FILE_PATH="$NODEJS_ASSETS_DIR/BUILD_NATIVE_MODULES.txt"
if [ -f "$PREFERENCE_FILE_PATH" ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)"
fi
fi
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, try to find .gyp files
#to turn it on.
gypfiles=($(find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -type f -name "*.gyp"))
if [ \${#gypfiles[@]} -gt 0 ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES=1
else
NODEJS_MOBILE_BUILD_NATIVE_MODULES=0
fi
fi
if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi
# Delete object files
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.o" -type f -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.a" -type f -delete
# Create Info.plist for each framework built and loader override.
PATCH_SCRIPT_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/scripts/ && pwd )"
NODEJS_PROJECT_DIR="$( cd "$CODESIGNING_FOLDER_PATH" && cd nodejs-project/ && pwd )"
node "$PATCH_SCRIPT_DIR"/ios-create-plists-and-dlopen-override.js $NODEJS_PROJECT_DIR
# Embed every resulting .framework in the application and delete them afterwards.
embed_framework()
{
FRAMEWORK_NAME="$(basename "$1")"
cp -r "$1" "$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/"
/usr/bin/codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY --preserve-metadata=identifier,entitlements,flags --timestamp=none "$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/$FRAMEWORK_NAME"
}
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d | while read frmwrk_path; do embed_framework "$frmwrk_path"; done
#Delete gyp temporary .deps dependency folders from the project structure.
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/.deps/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name ".deps" -type d -delete
#Delete frameworks from their build paths
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.framework/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d -delete
`
var signNativeModulesBuildPhase = xcodeProject.buildPhaseObject('PBXShellScriptBuildPhase', signNativeModulesBuildPhaseName, firstTargetUUID);
if (signNativeModulesBuildPhase) {
xcodeProject.myRemovePbxScriptBuildPhase(signNativeModulesBuildPhaseName, firstTargetUUID);
}
xcodeProject.addBuildPhase(
[],
'PBXShellScriptBuildPhase',
signNativeModulesBuildPhaseName,
firstTargetUUID,
{ shellPath: '/bin/sh', shellScript: signNativeModulesBuildPhaseScript }
);
//Adds a build phase to remove the x64 strips from the NodeMobile framework. Needed for correct App Store submission.
var removeSimulatorArchsBuildPhaseName = 'Remove NodeJS Mobile Framework Simulator Strips';
var removeSimulatorArchsBuildPhaseScript = `
set -e
FRAMEWORK_BINARY_PATH="$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/NodeMobile.framework/NodeMobile"
FRAMEWORK_STRIPPED_PATH="$FRAMEWORK_BINARY_PATH-strip"
if [ "$PLATFORM_NAME" != "iphonesimulator" ]; then
if $(lipo "$FRAMEWORK_BINARY_PATH" -verify_arch "x86_64") ; then
lipo -output "$FRAMEWORK_STRIPPED_PATH" -remove "x86_64" "$FRAMEWORK_BINARY_PATH"
rm "$FRAMEWORK_BINARY_PATH"
mv "$FRAMEWORK_STRIPPED_PATH" "$FRAMEWORK_BINARY_PATH"
echo "Removed simulator strip from NodeMobile.framework"
fi
fi
`
var removeSimulatorArchsBuildPhase = xcodeProject.buildPhaseObject('PBXShellScriptBuildPhase', removeSimulatorArchsBuildPhaseName, firstTargetUUID);
if (removeSimulatorArchsBuildPhase) {
xcodeProject.myRemovePbxScriptBuildPhase(removeSimulatorArchsBuildPhaseName, firstTargetUUID);
}
xcodeProject.addBuildPhase(
[],
'PBXShellScriptBuildPhase',
removeSimulatorArchsBuildPhaseName,
firstTargetUUID,
{ shellPath: '/bin/sh', shellScript: removeSimulatorArchsBuildPhaseScript }
);
//Writes the updated .pbx file
fs.writeFileSync(pbxProjectPath,xcodeProject.writeSync(),'utf-8');
});
}