-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-tools-info.ts
193 lines (166 loc) · 5.43 KB
/
android-tools-info.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
import * as path from "path";
import * as _ from "lodash";
import { cache } from "./common/decorators";
import { androidToolsInfo } from "@nativescript/doctor";
import { injector } from "./common/yok";
import {
IAndroidToolsInfo,
IOptions,
IAndroidToolsInfoData,
IAndroidToolsInfoValidateInput,
IAndroidToolsInfoOptions,
} from "./declarations";
import { IErrors, IProjectDir } from "./common/declarations";
export class AndroidToolsInfo implements IAndroidToolsInfo {
constructor(
private $errors: IErrors,
private $logger: ILogger,
private $options: IOptions,
protected $staticConfig: Config.IStaticConfig
) {}
@cache()
public getToolsInfo(config: IProjectDir): IAndroidToolsInfoData {
const infoData: IAndroidToolsInfoData = <IAndroidToolsInfoData>(
androidToolsInfo.getToolsInfo({ projectDir: config.projectDir })
);
infoData.androidHomeEnvVar = androidToolsInfo.androidHome;
infoData.compileSdkVersion = this.getCompileSdkVersion(
infoData.installedTargets,
infoData.compileSdkVersion
);
infoData.targetSdkVersion = this.getTargetSdk(infoData.compileSdkVersion);
infoData.generateTypings = this.shouldGenerateTypings();
this.$logger.trace(
"Installed Android Targets are: ",
infoData.installedTargets
);
this.$logger.trace(
"Selected buildToolsVersion is:",
infoData.buildToolsVersion
);
return infoData;
}
public validateInfo(options?: IAndroidToolsInfoValidateInput): boolean {
let detectedErrors = false;
const showWarningsAsErrors = options && options.showWarningsAsErrors;
const isAndroidHomeValid = this.validateAndroidHomeEnvVariable(options);
detectedErrors =
androidToolsInfo
.validateInfo({ projectDir: options.projectDir })
.map((warning) =>
this.printMessage(warning.warning, showWarningsAsErrors)
).length > 0;
if (options && options.validateTargetSdk) {
detectedErrors = this.validateTargetSdk(options);
}
return detectedErrors || !isAndroidHomeValid;
}
public validateTargetSdk(options: IAndroidToolsInfoOptions): boolean {
let detectedErrors = false;
const toolsInfoData = this.getToolsInfo({ projectDir: options.projectDir });
const targetSdk = toolsInfoData.targetSdkVersion;
detectedErrors =
androidToolsInfo
.validateMinSupportedTargetSdk({
targetSdk,
projectDir: options.projectDir,
})
.map((warning) =>
this.printMessage(warning.warning, options.showWarningsAsErrors)
).length > 0;
if (!detectedErrors) {
androidToolsInfo
.validataMaxSupportedTargetSdk({
targetSdk,
projectDir: options.projectDir,
})
.map((warning) => this.$logger.warn(warning.warning));
}
return detectedErrors;
}
public validateJavacVersion(
installedJavacVersion: string,
options?: IAndroidToolsInfoOptions
): boolean {
const showWarningsAsErrors = options && options.showWarningsAsErrors;
return (
androidToolsInfo
.validateJavacVersion(installedJavacVersion)
.map((warning) =>
this.printMessage(warning.warning, showWarningsAsErrors)
).length > 0
);
}
public async getPathToAdbFromAndroidHome(): Promise<string> {
try {
return androidToolsInfo.getPathToAdbFromAndroidHome();
} 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 '${path.join(
androidToolsInfo.androidHome,
"platform-tools",
"adb"
)} help'. Error is: ${err.message}`
);
}
return null;
}
@cache()
public validateAndroidHomeEnvVariable(
options?: IAndroidToolsInfoOptions
): boolean {
const showWarningsAsErrors = options && options.showWarningsAsErrors;
return (
androidToolsInfo
.validateAndroidHomeEnvVariable()
.map((warning) =>
this.printMessage(warning.warning, showWarningsAsErrors)
).length > 0
);
}
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.
* @return {void}
*/
private printMessage(msg: string, showWarningsAsErrors: boolean): void {
if (showWarningsAsErrors) {
this.$errors.fail(msg);
} else {
this.$logger.warn(msg);
}
}
private getCompileSdkVersion(
installedTargets: string[],
latestCompileSdk: number
): number {
const userSpecifiedCompileSdk = this.$options.compileSdk;
if (userSpecifiedCompileSdk) {
const androidCompileSdk = `${androidToolsInfo.ANDROID_TARGET_PREFIX}-${userSpecifiedCompileSdk}`;
if (!_.includes(installedTargets, androidCompileSdk)) {
this.$errors.fail(
`You have specified '${userSpecifiedCompileSdk}' for compile sdk, but it is not installed on your system.`
);
}
return userSpecifiedCompileSdk;
}
return latestCompileSdk;
}
// TODO check if still needed
private getTargetSdk(compileSdk: number): number {
const targetSdk = this.$options.sdk
? parseInt(this.$options.sdk)
: compileSdk;
this.$logger.trace(`Selected targetSdk is: ${targetSdk}`);
return targetSdk;
}
}
injector.register("androidToolsInfo", AndroidToolsInfo);