-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-project-service.ts
524 lines (440 loc) · 20.5 KB
/
android-project-service.ts
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
///<reference path="../.d.ts"/>
"use strict";
import path = require("path");
import shell = require("shelljs");
import util = require("util");
import Future = require("fibers/future");
import constants = require("../constants");
import helpers = require("../common/helpers");
import fs = require("fs");
import os = require("os");
import androidProjectPropertiesManagerLib = require("./android-project-properties-manager");
import projectServiceBaseLib = require("./platform-project-service-base");
class AndroidProjectService extends projectServiceBaseLib.PlatformProjectServiceBase implements IPlatformProjectService {
private static MIN_SUPPORTED_VERSION = 17;
private SUPPORTED_TARGETS = ["android-17", "android-18", "android-19", "android-21", "android-22"]; // forbidden for now: "android-MNC"
private static ANDROID_TARGET_PREFIX = "android";
private static RES_DIRNAME = "res";
private static VALUES_DIRNAME = "values";
private static VALUES_VERSION_DIRNAME_PREFIX = AndroidProjectService.VALUES_DIRNAME + "-v";
private static ANDROID_PLATFORM_NAME = "android";
private static LIBS_FOLDER_NAME = "libs";
private targetApi: string;
private _androidProjectPropertiesManagers: IDictionary<IAndroidProjectPropertiesManager>;
constructor(private $androidEmulatorServices: Mobile.IEmulatorPlatformServices,
private $childProcess: IChildProcess,
private $errors: IErrors,
private $hostInfo: IHostInfo,
private $injector: IInjector,
private $logger: ILogger,
private $options: IOptions,
private $projectData: IProjectData,
private $propertiesParser: IPropertiesParser,
$fs: IFileSystem) {
super($fs);
this._androidProjectPropertiesManagers = Object.create(null);
}
private _platformData: IPlatformData = null;
public get platformData(): IPlatformData {
if (!this._platformData) {
let projectRoot = path.join(this.$projectData.platformsDir, "android");
this._platformData = {
frameworkPackageName: "tns-android",
normalizedPlatformName: "Android",
appDestinationDirectoryPath: path.join(projectRoot, "assets"),
appResourcesDestinationDirectoryPath: path.join(projectRoot, "res"),
platformProjectService: this,
emulatorServices: this.$androidEmulatorServices,
projectRoot: projectRoot,
deviceBuildOutputPath: path.join(this.$projectData.platformsDir, "android", "bin"),
validPackageNamesForDevice: [
util.format("%s-%s.%s", this.$projectData.projectName, "debug", "apk"),
util.format("%s-%s.%s", this.$projectData.projectName, "release", "apk")
],
frameworkFilesExtensions: [".jar", ".dat", ".so"],
configurationFileName: "AndroidManifest.xml",
configurationFilePath: path.join(this.$projectData.platformsDir, "android", "AndroidManifest.xml"),
mergeXmlConfig: [{ "nodename": "manifest", "attrname": "*" }, {"nodename": "application", "attrname": "*"}]
};
}
return this._platformData;
}
public validate(): IFuture<void> {
return (() => {
this.validatePackageName(this.$projectData.projectId);
this.validateProjectName(this.$projectData.projectName);
this.checkAnt().wait() && this.checkAndroid().wait();
}).future<void>()();
}
public createProject(projectRoot: string, frameworkDir: string): IFuture<void> {
return (() => {
this.$fs.ensureDirectoryExists(projectRoot).wait();
let newTarget = this.getAndroidTarget(frameworkDir).wait();
this.$logger.trace(`Using Android SDK '${newTarget}'.`);
let versionNumber = _.last(newTarget.split("-"));
if(this.$options.symlink) {
this.copyResValues(projectRoot, frameworkDir, versionNumber).wait();
this.copy(projectRoot, frameworkDir, ".project AndroidManifest.xml project.properties custom_rules.xml", "-f").wait();
this.symlinkDirectory("assets", projectRoot, frameworkDir).wait();
this.symlinkDirectory("libs", projectRoot, frameworkDir).wait();
} else {
this.copyResValues(projectRoot, frameworkDir, versionNumber).wait();
this.copy(projectRoot, frameworkDir, "assets libs", "-R").wait();
this.copy(projectRoot, frameworkDir, ".project AndroidManifest.xml project.properties custom_rules.xml", "-f").wait();
}
if(newTarget) {
this.updateTarget(projectRoot, newTarget).wait();
}
// Create src folder
let packageName = this.$projectData.projectId;
let packageAsPath = packageName.replace(/\./g, path.sep);
let activityDir = path.join(projectRoot, 'src', packageAsPath);
this.$fs.createDirectory(activityDir).wait();
}).future<any>()();
}
private copyResValues(projectRoot: string, frameworkDir: string, versionNumber: string): IFuture<void> {
return (() => {
let resSourceDir = path.join(frameworkDir, AndroidProjectService.RES_DIRNAME);
let resDestinationDir = path.join(projectRoot, AndroidProjectService.RES_DIRNAME);
this.$fs.createDirectory(resDestinationDir).wait();
let versionDirName = AndroidProjectService.VALUES_VERSION_DIRNAME_PREFIX + versionNumber;
let directoriesToCopy = [AndroidProjectService.VALUES_DIRNAME];
let directoriesInResFolder = this.$fs.readDirectory(resSourceDir).wait();
let integerFrameworkVersion = parseInt(versionNumber);
let versionDir = _.find(directoriesInResFolder, dir => dir === versionDirName) ||
_(directoriesInResFolder)
.map(dir => {
return {
dirName: dir,
sdkNum: parseInt(dir.substr(AndroidProjectService.VALUES_VERSION_DIRNAME_PREFIX.length))
}
})
.filter(dir => dir.dirName.match(AndroidProjectService.VALUES_VERSION_DIRNAME_PREFIX) && dir.sdkNum && (!integerFrameworkVersion || (integerFrameworkVersion >= dir.sdkNum)))
.max(dir => dir.sdkNum)
.dirName;
if(versionDir) {
directoriesToCopy.push(versionDir);
}
this.copy(resDestinationDir, resSourceDir, directoriesToCopy.join(" "), "-R").wait();
}).future<void>()();
}
public interpolateData(projectRoot: string): IFuture<void> {
return (() => {
// Interpolate the activity name and package
let manifestPath = path.join(projectRoot, "AndroidManifest.xml");
let safeActivityName = this.$projectData.projectName.replace(/\W/g, '');
shell.sed('-i', /__PACKAGE__/, this.$projectData.projectId, manifestPath);
shell.sed('-i', /__APILEVEL__/, this.getTarget(projectRoot).wait().split('-')[1], manifestPath);
let stringsFilePath = path.join(projectRoot, 'res', 'values', 'strings.xml');
shell.sed('-i', /__NAME__/, this.$projectData.projectName, stringsFilePath);
shell.sed('-i', /__TITLE_ACTIVITY__/, this.$projectData.projectName, stringsFilePath);
shell.sed('-i', /__NAME__/, this.$projectData.projectName, path.join(projectRoot, '.project'));
}).future<void>()();
}
public afterCreateProject(projectRoot: string): IFuture<void> {
return (() => {
let targetApi = this.getTarget(projectRoot).wait();
this.$logger.trace("Android target: %s", targetApi);
this.runAndroidUpdate(projectRoot, targetApi).wait();
this.adjustMinSdk(projectRoot);
}).future<void>()();
}
private adjustMinSdk(projectRoot: string): void {
let manifestPath = path.join(projectRoot, "AndroidManifest.xml");
let apiLevel = this.getTarget(projectRoot).wait().split('-')[1];
if (apiLevel === "MNC") { // MNC SDK requires that minSdkVersion is set to "MNC"
shell.sed('-i', /android:minSdkVersion=".*?"/, `android:minSdkVersion="${apiLevel}"`, manifestPath);
}
}
public getDebugOnDeviceSetup(): Mobile.IDebugOnDeviceSetup {
return { };
}
public canUpdatePlatform(currentVersion: string, newVersion: string): IFuture<boolean> {
return Future.fromResult<boolean>(true);
}
public updatePlatform(currentVersion: string, newVersion: string): IFuture<void> {
return (() => { }).future<void>()();
}
public buildProject(projectRoot: string): IFuture<void> {
let buildConfiguration = this.$options.release ? "release" : "debug";
let args = this.getAntArgs(buildConfiguration, projectRoot);
return this.spawn('ant', args);
}
public isPlatformPrepared(projectRoot: string): IFuture<boolean> {
return this.$fs.exists(path.join(projectRoot, "assets", constants.APP_FOLDER_NAME));
}
private getProjectPropertiesManager(filePath: string): IAndroidProjectPropertiesManager {
if(!this._androidProjectPropertiesManagers[filePath]) {
this._androidProjectPropertiesManagers[filePath] = this.$injector.resolve(androidProjectPropertiesManagerLib.AndroidProjectPropertiesManager, { directoryPath: filePath });
}
return this._androidProjectPropertiesManagers[filePath];
}
private parseProjectProperties(projDir: string, destDir: string): IFuture<void> { // projDir is libraryPath, targetPath is the path to lib folder
return (() => {
let projProp = path.join(projDir, "project.properties");
if (!this.$fs.exists(projProp).wait()) {
this.$logger.warn("Warning: File %s does not exist", projProp);
return;
}
let projectPropertiesManager = this.getProjectPropertiesManager(projDir);
let references = projectPropertiesManager.getProjectReferences().wait();
_.each(references, reference => {
let adjustedPath = this.$fs.isRelativePath(reference.path) ? path.join(projDir, reference.path) : reference.path;
this.parseProjectProperties(adjustedPath, destDir).wait();
});
this.$logger.info("Copying %s", projDir);
shell.cp("-Rf", projDir, destDir);
let targetDir = path.join(destDir, path.basename(projDir));
let targetSdk = `android-${this.$options.sdk || AndroidProjectService.MIN_SUPPORTED_VERSION}`;
this.$logger.info("Generate build.xml for %s", targetDir);
this.runAndroidUpdate(targetDir, targetSdk).wait();
}).future<void>()();
}
public addLibrary(libraryPath: string): IFuture<void> {
return (() => {
let name = path.basename(libraryPath);
let targetLibPath = this.getLibraryPath(name);
let targetPath = path.dirname(targetLibPath);
this.$fs.ensureDirectoryExists(targetPath).wait();
if(this.$fs.exists(path.join(libraryPath, "project.properties")).wait()) {
this.parseProjectProperties(libraryPath, targetPath).wait();
}
shell.cp("-f", path.join(libraryPath, "*.jar"), targetPath);
let projectLibsDir = path.join(this.platformData.projectRoot, "libs");
this.$fs.ensureDirectoryExists(projectLibsDir).wait();
shell.cp("-f", path.join(libraryPath, "*.jar"), projectLibsDir);
let libProjProp = path.join(libraryPath, "project.properties");
if (this.$fs.exists(libProjProp).wait()) {
this.updateProjectReferences(this.platformData.projectRoot, targetLibPath).wait();
}
}).future<void>()();
}
public getFrameworkFilesExtensions(): string[] {
return [".jar", ".dat"];
}
public prepareProject(): IFuture<void> {
return (() => { }).future<void>()();
}
public prepareAppResources(appResourcesDirectoryPath: string): IFuture<void> {
return (() => {
let resourcesDirPath = path.join(appResourcesDirectoryPath, this.platformData.normalizedPlatformName);
let valuesDirRegExp = /^values/;
let resourcesDirs = this.$fs.readDirectory(resourcesDirPath).wait().filter(resDir => !resDir.match(valuesDirRegExp));
_.each(resourcesDirs, resourceDir => {
this.$fs.deleteDirectory(path.join(this.platformData.appResourcesDestinationDirectoryPath, resourceDir)).wait();
});
}).future<void>()();
}
public preparePluginNativeCode(pluginData: IPluginData): IFuture<void> {
return (() => {
let pluginPlatformsFolderPath = this.getPluginPlatformsFolderPath(pluginData, AndroidProjectService.ANDROID_PLATFORM_NAME);
// Handle *.jars inside libs folder
let libsFolderPath = path.join(pluginPlatformsFolderPath, AndroidProjectService.LIBS_FOLDER_NAME);
if(this.$fs.exists(libsFolderPath).wait()) {
this.addLibrary(libsFolderPath).wait();
}
// Handle android libraries
let librarries = this.getAllLibrariesForPlugin(pluginData).wait();
_.each(librarries, libraryName => this.addLibrary(path.join(pluginPlatformsFolderPath, libraryName)).wait());
}).future<void>()();
}
public removePluginNativeCode(pluginData: IPluginData): IFuture<void> {
return (() => {
let projectPropertiesManager = this.getProjectPropertiesManager(this.platformData.projectRoot);
let libraries = this.getAllLibrariesForPlugin(pluginData).wait();
_.each(libraries, libraryName => {
let libraryPath = this.getLibraryPath(libraryName);
let libraryRelativePath = this.getLibraryRelativePath(this.platformData.projectRoot, libraryPath);
projectPropertiesManager.removeProjectReference(libraryRelativePath).wait();
this.$fs.deleteDirectory(libraryPath).wait();
});
}).future<void>()();
}
private getLibraryRelativePath(basePath: string, libraryPath: string): string {
return path.relative(basePath, libraryPath).split("\\").join("/");
}
private getLibraryPath(libraryName: string): string {
return path.join(this.$projectData.projectDir, "lib", this.platformData.normalizedPlatformName, libraryName);
}
private updateProjectReferences(projDir: string, libraryPath: string): IFuture<void> {
let relLibDir = this.getLibraryRelativePath(projDir, libraryPath);
let projectPropertiesManager = this.getProjectPropertiesManager(projDir);
return projectPropertiesManager.addProjectReference(relLibDir);
}
private getAllLibrariesForPlugin(pluginData: IPluginData): IFuture<string[]> {
return (() => {
let filterCallback = (fileName: string, pluginPlatformsFolderPath: string) => fileName !== AndroidProjectService.LIBS_FOLDER_NAME && this.$fs.exists(path.join(pluginPlatformsFolderPath, fileName, "project.properties")).wait();
return this.getAllNativeLibrariesForPlugin(pluginData, AndroidProjectService.ANDROID_PLATFORM_NAME, filterCallback).wait();
}).future<string[]>()();
}
private copy(projectRoot: string, frameworkDir: string, files: string, cpArg: string): IFuture<void> {
return (() => {
let paths = files.split(' ').map(p => path.join(frameworkDir, p));
shell.cp(cpArg, paths, projectRoot);
}).future<void>()();
}
private spawn(command: string, args: string[]): IFuture<void> {
if (this.$hostInfo.isWindows) {
args.unshift('/s', '/c', command);
command = process.env.COMSPEC || 'cmd.exe';
}
return this.$childProcess.spawnFromEvent(command, args, "close", {stdio: "inherit"});
}
private getAntArgs(configuration: string, projectRoot: string): string[] {
let args = [configuration, "-f", path.join(projectRoot, "build.xml")];
if(configuration === "release") {
if(this.$options.keyStorePath) {
args = args.concat(["-Dkey.store", this.$options.keyStorePath]);
}
if(this.$options.keyStorePassword) {
args = args.concat(["-Dkey.store.password", this.$options.keyStorePassword]);
}
if(this.$options.keyStoreAlias) {
args = args.concat(["-Dkey.alias", this.$options.keyStoreAlias]);
}
if(this.$options.keyStoreAliasPassword) {
args = args.concat(["-Dkey.alias.password", this.$options.keyStoreAliasPassword])
}
}
// metadata generation support
args = args.concat(["-Dns.resources", path.join(__dirname, "../../resources/tools")]);
return args;
}
private runAndroidUpdate(projectPath: string, targetApi: string): IFuture<void> {
let args = [
"--path", projectPath,
"--target", targetApi,
"--name", this.$projectData.projectName
];
return this.spawn("android", ['update', 'project'].concat(args));
}
private validatePackageName(packageName: string): void {
//Make the package conform to Java package types
//Enforce underscore limitation
if (!/^[a-zA-Z]+(\.[a-zA-Z0-9][a-zA-Z0-9_]*)+$/.test(packageName)) {
this.$errors.fail("Package name must look like: com.company.Name");
}
//Class is a reserved word
if(/\b[Cc]lass\b/.test(packageName)) {
this.$errors.fail("class is a reserved word");
}
}
private validateProjectName(projectName: string): void {
if (projectName === '') {
this.$errors.fail("Project name cannot be empty");
}
//Classes in Java don't begin with numbers
if (/^[0-9]/.test(projectName)) {
this.$errors.fail("Project name must not begin with a number");
}
}
private getAndroidTarget(frameworkDir: string): IFuture<string> {
return ((): string => {
let newTarget = this.$options.sdk ? `${AndroidProjectService.ANDROID_TARGET_PREFIX}-${this.$options.sdk}` : this.getLatestValidAndroidTarget(frameworkDir).wait();
if(!_.contains(this.SUPPORTED_TARGETS, newTarget)) {
let versionNumber = parseInt(_.last(newTarget.split("-")));
if(versionNumber && (versionNumber < AndroidProjectService.MIN_SUPPORTED_VERSION)) {
this.$errors.failWithoutHelp(`The selected target SDK ${newTarget} is not supported. You should target at least ${AndroidProjectService.MIN_SUPPORTED_VERSION}.`);
}
if(!_.contains(this.getInstalledTargets().wait(), newTarget)) {
this.$errors.failWithoutHelp(`You have selected to use ${newTarget}, but it is not currently installed.`+
' Run \"android\" from your command-line to install/update any missing SDKs or tools.');
}
this.$logger.warn(`The selected Android target '${newTarget}' is not verified as supported. Some functionality may not work as expected.`);
}
return newTarget;
}).future<string>()();
}
private getLatestValidAndroidTarget(frameworkDir: string): IFuture<string> {
return (() => {
let validTarget = this.getTarget(frameworkDir).wait();
let installedTargets = this.getInstalledTargets().wait();
// adjust to the latest available version
var newTarget = _(this.SUPPORTED_TARGETS).sort().findLast(supportedTarget => _.contains(installedTargets, supportedTarget));
if (!newTarget) {
this.$errors.failWithoutHelp(`Could not find supported Android target. Please install one of the following: ${this.SUPPORTED_TARGETS.join(", ")}.` +
" Make sure you have the latest Android tools installed as well." +
' Run "android" from your command-line to install/update any missing SDKs or tools.')
}
return newTarget;
}).future<string>()();
}
private updateTarget(projectRoot: string, newTarget: string): IFuture<void> {
return (() => {
let file = path.join(projectRoot, "project.properties");
let editor = this.$propertiesParser.createEditor(file).wait();
editor.set("target", newTarget);
let future = new Future<void>();
editor.save((err:any) => {
if (err) {
future.throw(err);
} else {
this.targetApi = null; // so that later we can repopulate the cache
future.return();
}
});
future.wait();
}).future<void>()();
}
private installedTargetsCache: string[] = null;
private getInstalledTargets(): IFuture<string[]> {
return (() => {
if (!this.installedTargetsCache) {
this.installedTargetsCache = [];
let output = this.$childProcess.exec('android list targets').wait();
output.replace(/id: \d+ or "(.+)"/g, (m:string, p1:string) => (this.installedTargetsCache.push(p1), m));
}
return this.installedTargetsCache;
}).future<string[]>()();
}
private getTarget(projectRoot: string): IFuture<string> {
return (() => {
if(!this.targetApi) {
let projectPropertiesFilePath = path.join(projectRoot, "project.properties");
if (this.$fs.exists(projectPropertiesFilePath).wait()) {
let properties = this.$propertiesParser.createEditor(projectPropertiesFilePath).wait();
this.targetApi = properties.get("target");
}
}
return this.targetApi;
}).future<string>()();
}
private checkAnt(): IFuture<void> {
return (() => {
try {
this.$childProcess.exec("ant -version").wait();
} catch(error) {
this.$errors.fail("Error executing commands 'ant', make sure you have ant installed and added to your PATH.");
}
}).future<void>()();
}
private checkAndroid(): IFuture<void> {
return (() => {
try {
this.$childProcess.exec('android list targets').wait();
} catch(error) {
if (error.match(/command\snot\sfound/)) {
this.$errors.fail("The command \"android\" failed. Make sure you have the latest Android SDK installed, and the \"android\" command (inside the tools/ folder) is added to your path.");
} else {
this.$errors.fail("An error occurred while listing Android targets");
}
}
}).future<void>()();
}
private symlinkDirectory(directoryName: string, projectRoot: string, frameworkDir: string): IFuture<void> {
return (() => {
this.$fs.createDirectory(path.join(projectRoot, directoryName)).wait();
let directoryContent = this.$fs.readDirectory(path.join(frameworkDir, directoryName)).wait();
_.each(directoryContent, (file: string) => {
let sourceFilePath = path.join(frameworkDir, directoryName, file);
let destinationFilePath = path.join(projectRoot, directoryName, file);
if(this.$fs.getFsStats(sourceFilePath).wait().isFile()) {
this.$fs.symlink(sourceFilePath, destinationFilePath).wait();
} else {
this.$fs.symlink(sourceFilePath, destinationFilePath, "dir").wait();
}
});
}).future<void>()();
}
}
$injector.register("androidProjectService", AndroidProjectService);