-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathplatform-service.ts
176 lines (138 loc) · 5.18 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
///<reference path="../.d.ts"/>
import path = require("path");
import util = require("util");
import helpers = require("./../common/helpers");
class PlatformsData implements IPlatformsData {
private platformsData: { [key: string]: IPlatformData } = {
ios: {
frameworkPackageName: "tns-ios",
platformProjectService: $injector.resolve("iOSProjectService"),
projectRoot: "",
targetedOS: ['darwin']
},
android: {
frameworkPackageName: "tns-android",
platformProjectService: $injector.resolve("androidProjectService"),
projectRoot: ""
}
};
constructor($projectData: IProjectData) {
this.platformsData["ios"].projectRoot = "";
this.platformsData["android"].projectRoot = path.join($projectData.platformsDir, "android");
}
public get platformsNames() {
return Object.keys(this.platformsData);
}
public getPlatformData(platform): IPlatformData {
return this.platformsData[platform];
}
}
$injector.register("platformsData", PlatformsData);
export class PlatformService implements IPlatformService {
constructor(private $errors: IErrors,
private $fs: IFileSystem,
private $projectService: IProjectService,
private $platformProjectService: IPlatformProjectService,
private $projectData: IProjectData,
private $platformsData: IPlatformsData) {
}
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(() => {
platform = platform.split("@")[0];
this.validatePlatform(platform, true);
var platformPath = path.join(this.$projectData.platformsDir, platform);
// TODO: Check for version compatability if the platform is in format platform@version. This should be done in PR for semanting versioning
if (this.$fs.exists(platformPath).wait()) {
this.$errors.fail("Platform %s already added", platform);
}
// Copy platform specific files in platforms dir
this.$platformProjectService.createProject(platform).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 => { return 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 runPlatform(platform: string): IFuture<void> {
return (() => {
}).future<void>()();
}
public preparePlatform(platform: string): IFuture<void> {
return (() => {
platform = platform.toLowerCase();
this.validatePlatform(platform);
var normalizedPlatformName = this.normalizePlatformName(platform);
this.$platformProjectService.prepareProject(normalizedPlatformName, this.$platformsData.platformsNames).wait();
}).future<void>()();
}
public buildPlatform(platform: string): IFuture<void> {
return (() => {
platform = platform.toLowerCase();
this.validatePlatform(platform);
this.$platformProjectService.buildProject(platform).wait();
}).future<void>()();
}
private validatePlatform(platform: string, skipIsPlatformInstalledCheck?: boolean): void {
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);
}
if(!skipIsPlatformInstalledCheck) {
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 normalizePlatformName(platform: string): string {
switch(platform) {
case "android":
return "Android";
case "ios":
return "iOS";
}
return undefined;
}
}
$injector.register("platformService", PlatformService);