-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathandroid-tools-info.ts
More file actions
346 lines (292 loc) · 14.9 KB
/
android-tools-info.ts
File metadata and controls
346 lines (292 loc) · 14.9 KB
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
import * as path from "path";
import * as semver from "semver";
import { EOL } from "os";
import { cache } from "./common/decorators";
import { appendZeroesToVersion } from './common/helpers';
export class AndroidToolsInfo implements IAndroidToolsInfo {
private static ANDROID_TARGET_PREFIX = "android";
private static SUPPORTED_TARGETS = ["android-17", "android-18", "android-19", "android-21", "android-22", "android-23", "android-24", "android-25", "android-26", "android-27"];
private static MIN_REQUIRED_COMPILE_TARGET = 22;
private static REQUIRED_BUILD_TOOLS_RANGE_PREFIX = ">=23";
private static VERSION_REGEX = /((\d+\.){2}\d+)/;
private static MIN_JAVA_VERSION = "1.8.0";
private static MAX_JAVA_VERSION = "1.9.0";
private showWarningsAsErrors: boolean;
private toolsInfo: IAndroidToolsInfoData;
private selectedCompileSdk: number;
private get androidHome(): string {
return process.env["ANDROID_HOME"];
}
constructor(private $childProcess: IChildProcess,
private $errors: IErrors,
private $fs: IFileSystem,
private $hostInfo: IHostInfo,
private $logger: ILogger,
private $options: IOptions,
protected $staticConfig: Config.IStaticConfig) { }
@cache()
public getToolsInfo(): IAndroidToolsInfoData {
if (!this.toolsInfo) {
const infoData: IAndroidToolsInfoData = Object.create(null);
infoData.androidHomeEnvVar = this.androidHome;
infoData.compileSdkVersion = this.getCompileSdkVersion();
infoData.buildToolsVersion = this.getBuildToolsVersion();
infoData.targetSdkVersion = this.getTargetSdk();
infoData.supportRepositoryVersion = this.getAndroidSupportRepositoryVersion();
infoData.generateTypings = this.shouldGenerateTypings();
this.toolsInfo = infoData;
}
return this.toolsInfo;
}
public validateInfo(options?: { showWarningsAsErrors: boolean, validateTargetSdk: boolean }): boolean {
let detectedErrors = false;
this.showWarningsAsErrors = options && options.showWarningsAsErrors;
const toolsInfoData = this.getToolsInfo();
const isAndroidHomeValid = this.validateAndroidHomeEnvVariable();
if (!toolsInfoData.compileSdkVersion) {
this.printMessage(`Cannot find a compatible Android SDK for compilation. To be able to build for Android, install Android SDK ${AndroidToolsInfo.MIN_REQUIRED_COMPILE_TARGET} or later.`,
`Run \`\$ ${this.getPathToSdkManagementTool()}\` to manage your Android SDK versions.`);
detectedErrors = true;
}
if (!toolsInfoData.buildToolsVersion) {
const buildToolsRange = this.getBuildToolsRange();
const versionRangeMatches = buildToolsRange.match(/^.*?([\d\.]+)\s+.*?([\d\.]+)$/);
let message = `You can install any version in the following range: '${buildToolsRange}'.`;
// Improve message in case buildToolsRange is something like: ">=22.0.0 <=22.0.0" - same numbers on both sides
if (versionRangeMatches && versionRangeMatches[1] && versionRangeMatches[2] && versionRangeMatches[1] === versionRangeMatches[2]) {
message = `You have to install version ${versionRangeMatches[1]}.`;
}
let invalidBuildToolsAdditionalMsg = `Run \`\$ ${this.getPathToSdkManagementTool()}\` from your command-line to install required \`Android Build Tools\`.`;
if (!isAndroidHomeValid) {
invalidBuildToolsAdditionalMsg += ' In case you already have them installed, make sure `ANDROID_HOME` environment variable is set correctly.';
}
this.printMessage("You need to have the Android SDK Build-tools installed on your system. " + message, invalidBuildToolsAdditionalMsg);
detectedErrors = true;
}
if (!toolsInfoData.supportRepositoryVersion) {
let invalidSupportLibAdditionalMsg = `Run \`\$ ${this.getPathToSdkManagementTool()}\` to manage the Android Support Repository.`;
if (!isAndroidHomeValid) {
invalidSupportLibAdditionalMsg += ' In case you already have it installed, make sure `ANDROID_HOME` environment variable is set correctly.';
}
this.printMessage(`You need to have Android SDK ${AndroidToolsInfo.MIN_REQUIRED_COMPILE_TARGET} or later and the latest Android Support Repository installed on your system.`, invalidSupportLibAdditionalMsg);
detectedErrors = true;
}
if (options && options.validateTargetSdk) {
const targetSdk = toolsInfoData.targetSdkVersion;
const newTarget = `${AndroidToolsInfo.ANDROID_TARGET_PREFIX}-${targetSdk}`;
if (!_.includes(AndroidToolsInfo.SUPPORTED_TARGETS, newTarget)) {
const supportedVersions = AndroidToolsInfo.SUPPORTED_TARGETS.sort();
const minSupportedVersion = this.parseAndroidSdkString(_.first(supportedVersions));
if (targetSdk && (targetSdk < minSupportedVersion)) {
this.printMessage(`The selected Android target SDK ${newTarget} is not supported. You must target ${minSupportedVersion} or later.`);
detectedErrors = true;
} else if (!targetSdk || targetSdk > this.getMaxSupportedVersion()) {
this.$logger.warn(`Support for the selected Android target SDK ${newTarget} is not verified. Your Android app might not work as expected.`);
}
}
}
return detectedErrors || !isAndroidHomeValid;
}
public validateJavacVersion(installedJavacVersion: string, options?: { showWarningsAsErrors: boolean }): boolean {
let hasProblemWithJavaVersion = false;
if (options) {
this.showWarningsAsErrors = options.showWarningsAsErrors;
}
const additionalMessage = "You will not be able to build your projects for Android." + EOL
+ "To be able to build for Android, verify that you have installed The Java Development Kit (JDK) and configured it according to system requirements as" + EOL +
" described in " + this.$staticConfig.SYS_REQUIREMENTS_LINK;
const matchingVersion = appendZeroesToVersion(installedJavacVersion || "", 3).match(AndroidToolsInfo.VERSION_REGEX);
const installedJavaCompilerVersion = matchingVersion && matchingVersion[1];
if (installedJavaCompilerVersion) {
if (semver.lt(installedJavaCompilerVersion, AndroidToolsInfo.MIN_JAVA_VERSION)) {
hasProblemWithJavaVersion = true;
this.printMessage(`Javac version ${installedJavacVersion} is not supported. You have to install at least ${AndroidToolsInfo.MIN_JAVA_VERSION}.`, additionalMessage);
} else if (semver.gte(installedJavaCompilerVersion, AndroidToolsInfo.MAX_JAVA_VERSION)) {
hasProblemWithJavaVersion = true;
this.printMessage(`Javac version ${installedJavacVersion} is not supported. You have to install version ${AndroidToolsInfo.MIN_JAVA_VERSION}.`, additionalMessage);
}
} else {
hasProblemWithJavaVersion = true;
this.printMessage("Error executing command 'javac'. Make sure you have installed The Java Development Kit (JDK) and set JAVA_HOME environment variable.", additionalMessage);
}
return hasProblemWithJavaVersion;
}
public async getPathToAdbFromAndroidHome(): Promise<string> {
if (this.androidHome) {
const pathToAdb = path.join(this.androidHome, "platform-tools", "adb");
try {
await this.$childProcess.execFile(pathToAdb, ["help"]);
return pathToAdb;
} catch (err) {
// adb does not exist, so ANDROID_HOME is not set correctly
// try getting default adb path (included in CLI package)
this.$logger.trace(`Error while executing '${pathToAdb} help'. Error is: ${err.message}`);
}
}
return null;
}
@cache()
public validateAndroidHomeEnvVariable(options?: { showWarningsAsErrors: boolean }): boolean {
if (options) {
this.showWarningsAsErrors = options.showWarningsAsErrors;
}
const expectedDirectoriesInAndroidHome = ["build-tools", "tools", "platform-tools", "extras"];
let androidHomeValidationResult = true;
if (!this.androidHome || !this.$fs.exists(this.androidHome)) {
this.printMessage("The ANDROID_HOME environment variable is not set or it points to a non-existent directory. You will not be able to perform any build-related operations for Android.",
"To be able to perform Android build-related operations, set the `ANDROID_HOME` variable to point to the root of your Android SDK installation directory.");
androidHomeValidationResult = false;
} else if (!_.some(expectedDirectoriesInAndroidHome.map(dir => this.$fs.exists(path.join(this.androidHome, dir))))) {
this.printMessage("The ANDROID_HOME environment variable points to incorrect directory. You will not be able to perform any build-related operations for Android.",
"To be able to perform Android build-related operations, set the `ANDROID_HOME` variable to point to the root of your Android SDK installation directory, " +
"where you will find `tools` and `platform-tools` directories.");
androidHomeValidationResult = false;
}
return androidHomeValidationResult;
}
@cache()
private getPathToSdkManagementTool(): string {
const sdkManagerName = "sdkmanager";
let sdkManagementToolPath = sdkManagerName;
const isAndroidHomeValid = this.validateAndroidHomeEnvVariable();
if (isAndroidHomeValid) {
// In case ANDROID_HOME is correct, check if sdkmanager exists and if not it means the SDK has not been updated.
// In this case user shoud use `android` from the command-line instead of sdkmanager.
const pathToSdkManager = path.join(this.androidHome, "tools", "bin", sdkManagerName);
const pathToAndroidExecutable = path.join(this.androidHome, "tools", "android");
const pathToExecutable = this.$fs.exists(pathToSdkManager) ? pathToSdkManager : pathToAndroidExecutable;
this.$logger.trace(`Path to Android SDK Management tool is: ${pathToExecutable}`);
sdkManagementToolPath = pathToExecutable.replace(this.androidHome, this.$hostInfo.isWindows ? "%ANDROID_HOME%" : "$ANDROID_HOME");
}
return sdkManagementToolPath;
}
private shouldGenerateTypings(): boolean {
return this.$options.androidTypings;
}
/**
* Prints messages on the screen. In case the showWarningsAsErrors flag is set to true, warnings are shown, else - errors.
* Uses logger.warn for warnings and errors.failWithoutHelp when erros must be shown.
* In case additional details must be shown as info message, use the second parameter.
* NOTE: The additional information will not be printed when showWarningsAsErrors flag is set.
* @param {string} msg The message that will be shown as warning or error.
* @param {string} additionalMsg The additional message that will be shown as info message.
* @return {void}
*/
private printMessage(msg: string, additionalMsg?: string): void {
if (this.showWarningsAsErrors) {
this.$errors.failWithoutHelp(msg);
} else {
this.$logger.warn(msg);
}
if (additionalMsg) {
this.$logger.printMarkdown(additionalMsg);
}
}
private getCompileSdkVersion(): number {
if (!this.selectedCompileSdk) {
const userSpecifiedCompileSdk = this.$options.compileSdk;
if (userSpecifiedCompileSdk) {
const installedTargets = this.getInstalledTargets();
const androidCompileSdk = `${AndroidToolsInfo.ANDROID_TARGET_PREFIX}-${userSpecifiedCompileSdk}`;
if (!_.includes(installedTargets, androidCompileSdk)) {
this.$errors.failWithoutHelp(`You have specified '${userSpecifiedCompileSdk}' for compile sdk, but it is not installed on your system.`);
}
this.selectedCompileSdk = userSpecifiedCompileSdk;
} else {
const latestValidAndroidTarget = this.getLatestValidAndroidTarget();
if (latestValidAndroidTarget) {
const integerVersion = this.parseAndroidSdkString(latestValidAndroidTarget);
if (integerVersion && integerVersion >= AndroidToolsInfo.MIN_REQUIRED_COMPILE_TARGET) {
this.selectedCompileSdk = integerVersion;
}
}
}
}
return this.selectedCompileSdk;
}
private getTargetSdk(): number {
const targetSdk = this.$options.sdk ? parseInt(this.$options.sdk) : this.getCompileSdkVersion();
this.$logger.trace(`Selected targetSdk is: ${targetSdk}`);
return targetSdk;
}
private getMatchingDir(pathToDir: string, versionRange: string): string {
let selectedVersion: string;
if (this.$fs.exists(pathToDir)) {
const subDirs = this.$fs.readDirectory(pathToDir);
this.$logger.trace(`Directories found in ${pathToDir} are ${subDirs.join(", ")}`);
const subDirsVersions = subDirs
.map(dirName => {
const dirNameGroups = dirName.match(AndroidToolsInfo.VERSION_REGEX);
if (dirNameGroups) {
return dirNameGroups[1];
}
return null;
})
.filter(dirName => !!dirName);
this.$logger.trace(`Versions found in ${pathToDir} are ${subDirsVersions.join(", ")}`);
const version = semver.maxSatisfying(subDirsVersions, versionRange);
if (version) {
selectedVersion = _.find(subDirs, dir => dir.indexOf(version) !== -1);
}
}
this.$logger.trace("Selected version is: ", selectedVersion);
return selectedVersion;
}
private getBuildToolsRange(): string {
return `${AndroidToolsInfo.REQUIRED_BUILD_TOOLS_RANGE_PREFIX} <=${this.getMaxSupportedVersion()}`;
}
private getBuildToolsVersion(): string {
let buildToolsVersion: string;
if (this.androidHome) {
const pathToBuildTools = path.join(this.androidHome, "build-tools");
const buildToolsRange = this.getBuildToolsRange();
buildToolsVersion = this.getMatchingDir(pathToBuildTools, buildToolsRange);
}
return buildToolsVersion;
}
private getAppCompatRange(): string {
const compileSdkVersion = this.getCompileSdkVersion();
let requiredAppCompatRange: string;
if (compileSdkVersion) {
requiredAppCompatRange = `>=${compileSdkVersion} <${compileSdkVersion + 1}`;
}
return requiredAppCompatRange;
}
private getAndroidSupportRepositoryVersion(): string {
let selectedAppCompatVersion: string;
const requiredAppCompatRange = this.getAppCompatRange();
if (this.androidHome && requiredAppCompatRange) {
const pathToAppCompat = path.join(this.androidHome, "extras", "android", "m2repository", "com", "android", "support", "appcompat-v7");
selectedAppCompatVersion = this.getMatchingDir(pathToAppCompat, requiredAppCompatRange);
if (!selectedAppCompatVersion) {
// get latest matching version, as there's no available appcompat versions for latest SDK versions.
selectedAppCompatVersion = this.getMatchingDir(pathToAppCompat, "*");
}
}
this.$logger.trace(`Selected AppCompat version is: ${selectedAppCompatVersion}`);
return selectedAppCompatVersion;
}
private getLatestValidAndroidTarget(): string {
const installedTargets = this.getInstalledTargets();
return _.findLast(AndroidToolsInfo.SUPPORTED_TARGETS.sort(), supportedTarget => _.includes(installedTargets, supportedTarget));
}
private parseAndroidSdkString(androidSdkString: string): number {
return parseInt(androidSdkString.replace(`${AndroidToolsInfo.ANDROID_TARGET_PREFIX}-`, ""));
}
@cache()
private getInstalledTargets(): string[] {
let installedTargets: string[] = [];
if (this.androidHome) {
const pathToInstalledTargets = path.join(this.androidHome, "platforms");
if (this.$fs.exists(pathToInstalledTargets)) {
installedTargets = this.$fs.readDirectory(pathToInstalledTargets);
}
}
this.$logger.trace("Installed Android Targets are: ", installedTargets);
return installedTargets;
}
private getMaxSupportedVersion(): number {
return this.parseAndroidSdkString(_.last(AndroidToolsInfo.SUPPORTED_TARGETS.sort()));
}
}
$injector.register("androidToolsInfo", AndroidToolsInfo);