forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform-controller.ts
176 lines (157 loc) · 5.25 KB
/
platform-controller.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
import { NativePlatformStatus, SupportedPlatform } from "../constants";
import * as path from "path";
import {
IProjectDataService,
IProjectData,
INativePrepare,
} from "../definitions/project";
import {
IPlatformController,
IAddPlatformService,
IPlatformsDataService,
IAddPlatformData,
IPlatformData,
} from "../definitions/platform";
import { IPackageInstallationManager } from "../declarations";
import { IFileSystem, IErrors } from "../common/declarations";
import { injector } from "../common/yok";
export class PlatformController implements IPlatformController {
constructor(
private $addPlatformService: IAddPlatformService,
private $errors: IErrors,
private $fs: IFileSystem,
private $logger: ILogger,
private $packageInstallationManager: IPackageInstallationManager,
private $projectDataService: IProjectDataService,
private $platformsDataService: IPlatformsDataService,
private $projectChangesService: IProjectChangesService
) {}
public async addPlatform(addPlatformData: IAddPlatformData): Promise<void> {
const [platform, version] = addPlatformData.platform
.toLowerCase()
.split("@");
const projectData = this.$projectDataService.getProjectData(
addPlatformData.projectDir
);
const platformData = this.$platformsDataService.getPlatformData(
platform,
projectData
);
this.$logger.trace(
`Creating NativeScript project for the ${platform} platform`
);
this.$logger.trace(`Path: ${platformData.projectRoot}`);
this.$logger.trace(`Package: ${projectData.projectIdentifiers[platform]}`);
this.$logger.trace(`Name: ${projectData.projectName}`);
this.$logger.info("Copying template files...");
const packageToInstall = await this.getPackageToInstall(
platformData,
projectData,
addPlatformData.frameworkPath,
version
);
this.$logger.trace("Determined package to install is", packageToInstall);
const installedPlatformVersion = await this.$addPlatformService.addPlatformSafe(
projectData,
platformData,
packageToInstall,
addPlatformData
);
this.$fs.ensureDirectoryExists(
path.join(projectData.platformsDir, platform)
);
this.$logger.info(
`Platform ${platform} successfully added. v${installedPlatformVersion}`
);
}
public async addPlatformIfNeeded(
addPlatformData: IAddPlatformData
): Promise<void> {
const [platform] = addPlatformData.platform.toLowerCase().split("@");
const projectData = this.$projectDataService.getProjectData(
addPlatformData.projectDir
);
const platformData = this.$platformsDataService.getPlatformData(
platform,
projectData
);
const shouldAddPlatform = this.shouldAddPlatform(
platformData,
projectData,
addPlatformData.nativePrepare
);
if (shouldAddPlatform) {
await this.addPlatform(addPlatformData);
}
}
private async getPackageToInstall(
platformData: IPlatformData,
projectData: IProjectData,
frameworkPath?: string,
version?: string
): Promise<string> {
let result = null;
if (frameworkPath) {
if (!this.$fs.exists(frameworkPath)) {
this.$errors.fail(
`Invalid frameworkPath: ${frameworkPath}. Please ensure the specified frameworkPath exists.`
);
}
result = "file:" + path.resolve(frameworkPath);
} else {
const desiredRuntimePackage = this.$projectDataService.getRuntimePackage(
projectData.projectDir,
platformData.platformNameLowerCase as SupportedPlatform
);
if (version) {
desiredRuntimePackage.version = version;
}
if (!desiredRuntimePackage.version) {
// if no version is explicitly added, then we use the latest
desiredRuntimePackage.version = await this.$packageInstallationManager.getLatestCompatibleVersion(
desiredRuntimePackage.name
);
}
// const currentPlatformData = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
// version = (currentPlatformData && currentPlatformData.version) ||
// await this.$packageInstallationManager.getLatestCompatibleVersion(platformData.frameworkPackageName);
result = `${desiredRuntimePackage.name}@${desiredRuntimePackage.version}`;
}
return result;
}
private shouldAddPlatform(
platformData: IPlatformData,
projectData: IProjectData,
nativePrepare: INativePrepare
): boolean {
const platformName = platformData.platformNameLowerCase;
const hasPlatformDirectory = this.$fs.exists(
path.join(projectData.platformsDir, platformName)
);
if (hasPlatformDirectory) {
const platformDirectoryItemCount = this.$fs.readDirectory(
path.join(projectData.platformsDir, platformName)
).length;
if (platformDirectoryItemCount <= 5) {
this.$logger.warn(
`The platforms/${platformName} folder appears to be invalid. If the build fails, run 'ns clean' and rebuild the app.`,
{ wrapMessageWithBorders: true }
);
}
}
const shouldAddNativePlatform =
!nativePrepare || !nativePrepare.skipNativePrepare;
const prepareInfo = this.$projectChangesService.getPrepareInfo(
platformData
);
const requiresNativePlatformAdd =
prepareInfo &&
prepareInfo.nativePlatformStatus ===
NativePlatformStatus.requiresPlatformAdd;
const result =
!hasPlatformDirectory ||
(shouldAddNativePlatform && requiresNativePlatformAdd);
return !!result;
}
}
injector.register("platformController", PlatformController);