-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathplatform-service.ts
374 lines (301 loc) · 12.4 KB
/
platform-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
///<reference path="../.d.ts"/>
import path = require("path");
import shell = require("shelljs");
import util = require("util");
import constants = require("./../constants");
import helpers = require("./../common/helpers");
import options = require("./../options");
class PlatformsData implements IPlatformsData {
private platformsData : { [index: string]: any } = {};
constructor($androidProjectService: IPlatformProjectService,
$iOSProjectService: IPlatformProjectService) {
this.platformsData = {
ios: $iOSProjectService.platformData,
android: $androidProjectService.platformData
}
}
public get platformsNames() {
return Object.keys(this.platformsData);
}
public getPlatformData(platform: string): IPlatformData {
return this.platformsData[platform];
}
}
$injector.register("platformsData", PlatformsData);
export class PlatformService implements IPlatformService {
constructor(private $errors: IErrors,
private $fs: IFileSystem,
private $logger: ILogger,
private $npm: INodePackageManager,
private $projectData: IProjectData,
private $platformsData: IPlatformsData,
private $devicesServices: Mobile.IDevicesServices) { }
public addPlatforms(platforms: string[]): IFuture<void> {
return (() => {
if(!platforms || platforms.length === 0) {
this.$errors.fail("No platform specified. Please specify a platform to add");
}
var platformsDir = this.$projectData.platformsDir;
this.$fs.ensureDirectoryExists(platformsDir).wait();
_.each(platforms, platform => {
this.addPlatform(platform.toLowerCase()).wait();
});
}).future<void>()();
}
private addPlatform(platform: string): IFuture<void> {
return(() => {
var parts = platform.split("@");
platform = parts[0];
var version = parts[1];
this.validatePlatform(platform);
var platformPath = path.join(this.$projectData.platformsDir, platform);
if (this.$fs.exists(platformPath).wait()) {
this.$errors.fail("Platform %s already added", platform);
}
var platformData = this.$platformsData.getPlatformData(platform);
// Copy platform specific files in platforms dir
var platformProjectService = platformData.platformProjectService;
platformProjectService.validate().wait();
// Log the values for project
this.$logger.trace("Creating NativeScript project for the %s platform", platform);
this.$logger.trace("Path: %s", platformData.projectRoot);
this.$logger.trace("Package: %s", this.$projectData.projectId);
this.$logger.trace("Name: %s", this.$projectData.projectName);
this.$logger.out("Copying template files...");
// get path to downloaded framework package
var frameworkDir = this.$npm.install(platformData.frameworkPackageName,
path.join(this.$projectData.platformsDir, platform), version).wait();
frameworkDir = path.join(frameworkDir, constants.PROJECT_FRAMEWORK_FOLDER_NAME);
try {
this.addPlatformCore(platformData, frameworkDir).wait();
} catch(err) {
this.$fs.deleteDirectory(platformPath).wait();
throw err;
}
this.$logger.out("Project successfully created.");
}).future<void>()();
}
private addPlatformCore(platformData: IPlatformData, frameworkDir: string): IFuture<void> {
return (() => {
platformData.platformProjectService.createProject(platformData.projectRoot, frameworkDir).wait();
// Need to remove unneeded node_modules folder
// One level up is the runtime module and one above is the node_modules folder.
this.$fs.deleteDirectory(path.join("../", frameworkDir)).wait();
platformData.platformProjectService.interpolateData(platformData.projectRoot).wait();
platformData.platformProjectService.afterCreateProject(platformData.projectRoot).wait();
}).future<void>()();
}
public getInstalledPlatforms(): IFuture<string[]> {
return(() => {
if(!this.$fs.exists(this.$projectData.platformsDir).wait()) {
return [];
}
var subDirs = this.$fs.readDirectory(this.$projectData.platformsDir).wait();
return _.filter(subDirs, p => this.$platformsData.platformsNames.indexOf(p) > -1);
}).future<string[]>()();
}
public getAvailablePlatforms(): IFuture<string[]> {
return (() => {
var installedPlatforms = this.getInstalledPlatforms().wait();
return _.filter(this.$platformsData.platformsNames, p => {
return installedPlatforms.indexOf(p) < 0 && this.isPlatformSupportedForOS(p); // Only those not already installed
});
}).future<string[]>()();
}
public preparePlatform(platform: string): IFuture<void> {
return (() => {
this.validatePlatformInstalled(platform);
platform = platform.toLowerCase();
var platformData = this.$platformsData.getPlatformData(platform);
var platformProjectService = platformData.platformProjectService;
var appFilesLocation = platformProjectService.prepareProject(platformData).wait();
this.processPlatformSpecificFiles(platform, helpers.enumerateFilesInDirectorySync(path.join(appFilesLocation, constants.APP_FOLDER_NAME))).wait();
this.processPlatformSpecificFiles(platform, helpers.enumerateFilesInDirectorySync(path.join(appFilesLocation, constants.TNS_MODULES_FOLDER_NAME))).wait();
}).future<void>()();
}
public buildPlatform(platform: string): IFuture<void> {
return (() => {
this.validatePlatformInstalled(platform);
platform = platform.toLowerCase();
var platformData = this.$platformsData.getPlatformData(platform);
platformData.platformProjectService.buildProject(platformData.projectRoot).wait();
this.$logger.out("Project successfully built");
}).future<void>()();
}
public runPlatform(platform: string): IFuture<void> {
return (() => {
this.validatePlatformInstalled(platform);
platform = platform.toLowerCase();
this.preparePlatform(platform).wait();
// We need to set device option here
var cachedDeviceOption = options.device;
options.device = true;
this.buildPlatform(platform).wait();
options.device = cachedDeviceOption;
this.deploy(platform).wait();
}).future<void>()();
}
public removePlatforms(platforms: string[]): IFuture<void> {
return (() => {
if(!platforms || platforms.length === 0) {
this.$errors.fail("No platform specified. Please specify a platform to remove");
}
_.each(platforms, platform => {
this.validatePlatformInstalled(platform);
var platformDir = path.join(this.$projectData.platformsDir, platform);
this.$fs.deleteDirectory(platformDir).wait();
});
}).future<void>()();
}
public deploy(platform: string): IFuture<void> {
return (() => {
platform = platform.toLowerCase();
this.validatePlatformInstalled(platform);
var platformData = this.$platformsData.getPlatformData(platform);
// Get latest package that is produced from build
var candidates = this.$fs.readDirectory(platformData.buildOutputPath).wait();
var packages = _.filter(candidates, candidate => {
return _.contains(platformData.validPackageNames, candidate);
}).map(currentPackage => {
currentPackage = path.join(platformData.buildOutputPath, currentPackage);
return {
pkg: currentPackage,
time: this.$fs.getFsStats(currentPackage).wait().mtime
};
});
packages = _.sortBy(packages, pkg => pkg.time ).reverse(); // We need to reverse because sortBy always sorts in ascending order
if(packages.length === 0) {
var packageExtName = path.extname(platformData.validPackageNames[0]);
this.$errors.fail("No %s found in %s directory", packageExtName, platformData.buildOutputPath)
}
var packageFile = packages[0].pkg;
this.$logger.out("Using ", packageFile);
this.$devicesServices.initialize(platform, options.device).wait();
var action = (device: Mobile.IDevice): IFuture<void> => { return device.deploy(packageFile, this.$projectData.projectId); };
this.$devicesServices.execute(action).wait();
}).future<void>()();
}
public updatePlatforms(platforms: string[]): IFuture<void> {
return (() => {
if(!platforms || platforms.length === 0) {
this.$errors.fail("No platforms specified. Please specify a platform to update");
}
_.each(platforms, platform => {
if (!this.isPlatformInstalled(platform).wait())
{
this.addPlatform(platform.toLowerCase()).wait();
}
else
{
this.updatePlatform(platform.toLowerCase()).wait();
}
});
}).future<void>()();
}
private updatePlatform(platform: string): IFuture<void> {
return(() => {
this.validatePlatform(platform);
var platformPath = path.join(this.$projectData.platformsDir, platform);
if (!this.$fs.exists(platformPath).wait()) {
this.addPlatform(platform).wait();
}
// var parts = platform.split("@");
// platform = parts[0];
// var version = parts[1];
//
// this.validatePlatform(platform);
//
// var platformPath = path.join(this.$projectData.platformsDir, platform);
// if (this.$fs.exists(platformPath).wait()) {
// this.$errors.fail("Platform %s already added", platform);
// }
//
// var platformData = this.$platformsData.getPlatformData(platform);
//
// // Copy platform specific files in platforms dir
// var platformProjectService = platformData.platformProjectService;
// platformProjectService.validate().wait();
//
// // Log the values for project
// this.$logger.trace("Creating NativeScript project for the %s platform", platform);
// this.$logger.trace("Path: %s", platformData.projectRoot);
// this.$logger.trace("Package: %s", this.$projectData.projectId);
// this.$logger.trace("Name: %s", this.$projectData.projectName);
//
// this.$logger.out("Copying template files...");
//
// // get path to downloaded framework package
// var frameworkDir = this.$npm.install(platformData.frameworkPackageName,
// path.join(this.$projectData.platformsDir, platform), version).wait();
// frameworkDir = path.join(frameworkDir, constants.PROJECT_FRAMEWORK_FOLDER_NAME);
//
// try {
// this.addPlatformCore(platformData, frameworkDir).wait();
// } catch(err) {
// this.$fs.deleteDirectory(platformPath).wait();
// throw err;
// }
//
// this.$logger.out("Project successfully created.");
}).future<void>()();
}
private validatePlatform(platform: string): void {
if(!platform) {
this.$errors.fail("No platform specified.")
}
platform = platform.toLowerCase();
if (!this.isValidPlatform(platform)) {
this.$errors.fail("Invalid platform %s. Valid platforms are %s.", platform, helpers.formatListOfNames(this.$platformsData.platformsNames));
}
if (!this.isPlatformSupportedForOS(platform)) {
this.$errors.fail("Applications for platform %s can not be built on this OS - %s", platform, process.platform);
}
}
private validatePlatformInstalled(platform: string): void {
this.validatePlatform(platform);
if (!this.isPlatformInstalled(platform).wait()) {
this.$errors.fail("The platform %s is not added to this project. Please use 'tns platform add <platform>'", platform);
}
}
private isValidPlatform(platform: string) {
return this.$platformsData.getPlatformData(platform);
}
private isPlatformSupportedForOS(platform: string): boolean {
var targetedOS = this.$platformsData.getPlatformData(platform).targetedOS;
if(!targetedOS || targetedOS.indexOf("*") >= 0 || targetedOS.indexOf(process.platform) >= 0) {
return true;
}
return false;
}
private isPlatformInstalled(platform: string): IFuture<boolean> {
return (() => {
return this.$fs.exists(path.join(this.$projectData.platformsDir, platform)).wait();
}).future<boolean>()();
}
private static parsePlatformSpecificFileName(fileName: string, platforms: string[]): any {
var regex = util.format("^(.+?)\.(%s)(\..+?)$", platforms.join("|"));
var parsed = fileName.toLowerCase().match(new RegExp(regex, "i"));
if (parsed) {
return {
platform: parsed[2],
onDeviceName: parsed[1] + parsed[3]
};
}
return undefined;
}
private processPlatformSpecificFiles( platform: string, files: string[]): IFuture<void> {
// Renames the files that have `platform` as substring and removes the files from other platform
return (() => {
_.each(files, fileName => {
var platformInfo = PlatformService.parsePlatformSpecificFileName(path.basename(fileName), this.$platformsData.platformsNames);
var shouldExcludeFile = platformInfo && platformInfo.platform !== platform;
if (shouldExcludeFile) {
this.$fs.deleteFile(fileName).wait();
} else if (platformInfo && platformInfo.onDeviceName) {
this.$fs.rename(fileName, path.join(path.dirname(fileName), platformInfo.onDeviceName)).wait();
}
});
}).future<void>()();
}
}
$injector.register("platformService", PlatformService);