forked from play-co/native-ios
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
378 lines (328 loc) · 11.1 KB
/
build.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
var chalk = require('chalk');
var path = require('path');
var fs = require('fs-extra');
var updatePlist = require('./lib/updatePlist');
var xcodeUtil = require('./lib/xcodeUtil');
var copyResources = require('./lib/copyResources');
var Rsync = require('rsync');
var mkdirs = Promise.promisify(fs.mkdirs);
var DEFAULT_FRAMEWORKS = [
'StoreKit',
'AddressBook',
'libresolv.dylib',
'CoreTelephony',
'Security',
'MobileCoreServices',
'SystemConfiguration',
'MessageUI',
'CFNetwork',
'AudioToolbox',
'OpenAL',
'AVFoundation',
'AdSupport',
'CoreText'
];
var iosVersion = require('./package.json').version;
var logger;
var getModuleConfig = function(modulePath) {
var config = null;
var configPath = path.join(modulePath, 'config.json');
if (fs.existsSync(configPath)) {
try {
var rawConfig = fs.readFileSync(configPath, 'utf-8');
config = JSON.parse(rawConfig);
} catch (e) {
throw new Error('Error parsing config ' + configPath);
}
} else {
throw new Error('No ios config found for', modulePath);
}
return config;
};
var installModule = function (app, config, modulePath, xcodeProject, infoPlist, entitlements) {
var moduleConfig = getModuleConfig(modulePath);
return xcodeProject
.installModule(modulePath, moduleConfig)
.then(function () {
if (moduleConfig.plist) {
infoPlist.add(app.manifest, moduleConfig.plist);
}
if (moduleConfig.entitlements) {
entitlements.add(app.manifest, moduleConfig.entitlements);
}
});
};
function executeOnCreate(api, app, config) {
var modules = app.modules;
var hookName = 'onCreateProject';
return Promise.resolve(Object.keys(modules))
.map(function (moduleName) {
var module = modules[moduleName];
var buildExtension = module.extensions && module.extensions.build;
buildExtension = buildExtension ? require(buildExtension) : null;
if (!buildExtension || !buildExtension[hookName]) {
return;
}
return new Promise(function (resolve, reject) {
var retVal = buildExtension[hookName](api, app, config, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
if (retVal) { resolve(retVal); }
})
});
}
function updateConfig(config) {
if (!config.xcodeProjectPath) {
config.xcodeProjectPath = path.join(config.outputPath, 'xcodeproject');
}
}
exports.createXcodeProject = function (config) {
updateConfig(config);
var srcPath;
if (config.srcXcodeProjectPath) {
srcPath = config.srcXcodeProjectPath;
} else {
srcPath = __dirname + '/tealeaf/';
}
var destPath = config.xcodeProjectPath;
return mkdirs(destPath)
.then(function () {
var rsync = new Rsync()
.flags('a')
.set('exclude', config.xcodeResourcesPath)
.set('delete-before')
.source(srcPath)
.destination(destPath);
return Promise.fromNode(rsync.execute.bind(rsync));
});
};
function removeKeysForObjects(parentObject, objects, keys) {
for (var ii = 0; ii < objects.length; ++ii) {
var objectName = objects[ii];
var obj = parentObject[objectName];
for (var jj = 0; jj < keys.length; ++jj) {
var key = keys[jj];
var index = obj.indexOf(key);
if (index !== -1) {
obj.splice(index, 1);
}
}
}
}
function updateInfoPlist(app, config, plist) {
var manifest = app.manifest;
var raw = plist.getRaw();
// Remove unsupported modes
var orient = manifest.supportedOrientations;
if (orient.indexOf("landscape") == -1) {
logger.log("Orientations: Removing landscape support");
removeKeysForObjects(raw, ["UISupportedInterfaceOrientations", "UISupportedInterfaceOrientations~ipad"],
["UIInterfaceOrientationLandscapeRight", "UIInterfaceOrientationLandscapeLeft"]);
}
if (orient.indexOf("portrait") == -1) {
logger.log("Orientations: Removing portrait support");
removeKeysForObjects(raw, ["UISupportedInterfaceOrientations", "UISupportedInterfaceOrientations~ipad"],
["UIInterfaceOrientationPortrait", "UIInterfaceOrientationPortraitUpsideDown"]);
}
// Update the version numbers
var version = (manifest.ios && manifest.ios.version) ||
manifest.version ||
'0.0.0';
var buildNumber = (manifest.ios && manifest.ios.buildNumber) || version;
raw.CFBundleShortVersionString = version;
// Convert to string
raw.CFBundleVersion = buildNumber + "";
// If RenderGloss enabled,
if (manifest.ios && manifest.ios.icons && manifest.ios.icons.renderGloss) {
// Note: Default is for Xcode to render it for you
logger.log("RenderGloss: Removing pre-rendered icon flag");
delete raw.UIPrerenderedIcon;
//delete raw.CFBundleIcons.CFBundlePrimaryIcon.UIPrerenderedIcon;
}
raw.CFBundleDisplayName = app.manifest.title || "";
raw.CFBundleIdentifier = config.bundleID;
raw.CFBundleName = config.bundleID;
if(app.manifest.ios.requireFullScreen) {
raw.UIRequiresFullScreen = app.manifest.ios.requireFullScreen;
}
// Add languages
var key = "CFBundleLocalizations";
var languages = app.manifest.ios.languages;
var resLanguages = raw[key] || [];
if (languages && languages.length > 0) {
languages.forEach(function (curr_lang) {
resLanguages.push(curr_lang);
});
raw[key] = resLanguages;
}
// For each URLTypes array entry,
var found = 0;
for (var ii = 0; ii < raw.CFBundleURLTypes.length; ++ii) {
var obj = raw.CFBundleURLTypes[ii];
// If it's the URLName one,
if (obj.CFBundleURLName) {
obj.CFBundleURLName = config.bundleID;
++found;
}
// If it's the URLSchemes one,
if (obj.CFBundleURLSchemes) {
// Note this blows away all the array entries
obj.CFBundleURLSchemes = [config.bundleID];
++found;
}
}
if (found != 2) {
throw new Error("Unable to update URLTypes");
}
}
function updateConfigPlist(app, config, plist) {
var gameVersion = require(path.join(app.paths.root, 'manifest.json')).version;
plist.add(app.manifest, {
remote_loading: 'false',
tcp_port: 4747,
code_port: 9201,
screen_width: 480,
screen_height: 800,
code_host: 'localhost',
entry_point: 'devkit.native.launchClient',
app_id: app.manifest.appID || "example.appid",
tcp_host: 'localhost',
source_dir: '/',
game_hash: gameVersion,
sdk_hash: config.sdkVersion,
native_hash: iosVersion,
code_path: 'native.js',
studio_name: (app.manifest.studio && app.manifest.studio.name) || "example.studio",
debug_build: config.debug,
apple_id: app.manifest.ios && app.manifest.ios.appleID || "example.appleid",
bundle_id: config.bundleID,
version: config.version
});
}
function buildXcodeProject(api, app, config) {
return xcodeUtil
.getXcodeProject(config.xcodeProjectPath)
.then(function (xcodeProject) {
var infoPlist = updatePlist.getInfoPlist(config.xcodeProjectPath);
var configPlist = updatePlist.get(path.join(config.xcodeProjectPath, 'resources', 'config.plist'));
var entitlements = updatePlist.get(path.join(config.xcodeProjectPath, 'TeaLeafIOS.entitlements'));
updateInfoPlist(app, config, infoPlist);
updateConfigPlist(app, config, configPlist);
return Promise
.resolve(Object.keys(app.modules))
.map(function (moduleName) {
return app.modules[moduleName].extensions.ios;
})
.filter(function (iosExtension) {
return !!iosExtension;
})
.each(function(iosExtension) {
return installModule(app, config, iosExtension, xcodeProject, infoPlist, entitlements);
})
.then(function () {
var appLinkKey = "com.apple.developer.associated-domains";
var otherAppsKey = "LSApplicationQueriesSchemes";
var appLinks = app.manifest.ios && app.manifest.ios.appLinks;
var otherApps = app.manifest.ios && app.manifest.ios.otherApps;
var rawEntitlements = entitlements.getRaw();
var rawPlist = infoPlist.getRaw();
var resLinks = rawEntitlements[appLinkKey] || [];
var resOtherApps = rawPlist[otherAppsKey] || [];
// Add App Links
if (appLinks && appLinks.length > 0) {
appLinks.forEach(function (curr) {
resLinks.push("applinks:" + curr);
});
rawEntitlements[appLinkKey] = resLinks;
}
if (otherApps && otherApps.length > 0) {
otherApps.forEach(function (curr) {
resOtherApps.push(curr);
});
rawPlist[otherAppsKey] = resOtherApps;
}
})
.then(function () {
return [
copyResources.copyIcons(api, app, config),
copyResources.copySplash(api, app, config),
xcodeProject.installModule(null, {frameworks: DEFAULT_FRAMEWORKS}),
xcodeProject.addResourceFiles('resources.bundle')
];
})
.all()
.then(function () {
return [
xcodeProject.write(),
infoPlist.write(),
configPlist.write(),
entitlements.write(),
executeOnCreate(api, app, config)
];
})
.all();
});
}
exports.build = function(api, app, config) {
logger = api.logging.get('ios-build');
updateConfig(config);
return Promise
.resolve()
.then(function () {
if (!config.repack) {
return buildXcodeProject(api, app, config);
}
})
.then(function () {
if (config.ipaPath) {
// TODO
return require('./lib/ipa').buildIPA(api, app, config);
}
})
.then(function () {
var exec = require('child_process').exec;
var projectDir = xcodeUtil.getXcodeProjectDir(config.xcodeProjectPath);
logger.log("built", chalk.yellow(config.bundleID));
if (config.ipaPath) {
logger.log("saved to " + chalk.blue(config.ipaPath));
} else {
logger.log("xcode project file is at", chalk.blue(projectDir));
logger.log("build output at", chalk.blue(config.xcodeProjectPath));
}
if (config.open) {
// open the xcode project
logger.log('opening xcode project...');
exec('open "' + projectDir + '"');
}
if (config.reveal) {
// show the ipaFile or the project in Finder
if (config.ipaPath) {
logger.log('revealing ipa file...');
exec('open --reveal "' + config.ipaPath + '"');
} else {
logger.log('revealing xcode project...');
exec('open --reveal "' + projectDir + '"');
}
}
});
};
//All of these (and the plist copying), should go into "externalProject"
//copy native resources
//update bundle id in plist
//update orientation in plist
//update version in plist
//update title
//copy urltypes from manifest.json
//add required frameworks to externalProject, add libweebyUnity.a, add weeby.h?
//rename installModule
//ios 5.0
//linker flag -ObjC
//use clang/libc++
//build the library project and copy it over
//put the deps in a file that we read?
//it would be nice to make all projects use this library project instead of
//copying the whole thing