Skip to content

Fix getting info for Android tools #2610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 83 additions & 110 deletions lib/android-tools-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from "path";
import * as semver from "semver";
import { EOL } from "os";
import { cache } from "./common/decorators";

export class AndroidToolsInfo implements IAndroidToolsInfo {
private static ANDROID_TARGET_PREFIX = "android";
Expand All @@ -13,19 +14,8 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
private showWarningsAsErrors: boolean;
private toolsInfo: IAndroidToolsInfoData;
private selectedCompileSdk: number;
private installedTargetsCache: string[] = null;
private androidHome = process.env["ANDROID_HOME"];
private pathToAndroidExecutable: string;
private _androidExecutableName: string;
private get androidExecutableName(): string {
if (!this._androidExecutableName) {
this._androidExecutableName = "android";
if (this.$hostInfo.isWindows) {
this._androidExecutableName += ".bat";
}
}

return this._androidExecutableName;
private get androidHome(): string {
return process.env["ANDROID_HOME"];
}

constructor(private $childProcess: IChildProcess,
Expand All @@ -34,54 +24,17 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
private $hostInfo: IHostInfo,
private $logger: ILogger,
private $options: IOptions,
private $adb: Mobile.IAndroidDebugBridge,
protected $staticConfig: Config.IStaticConfig) { }

public async getPathToAndroidExecutable(options?: { showWarningsAsErrors: boolean }): Promise<string> {
if (options) {
this.showWarningsAsErrors = options.showWarningsAsErrors;
}
if (!this.pathToAndroidExecutable) {
if (this.validateAndroidHomeEnvVariable(this.androidHome)) {
let androidPath = path.join(this.androidHome, "tools", this.androidExecutableName);
if (!await this.trySetAndroidPath(androidPath) && !await this.trySetAndroidPath(this.androidExecutableName)) {
this.printMessage(`Unable to find "${this.androidExecutableName}" executable file. Make sure you have set ANDROID_HOME environment variable correctly.`);
}
} else {
this.$logger.trace("ANDROID_HOME environment variable is not set correctly.");
}
}

return this.pathToAndroidExecutable;
}

private async trySetAndroidPath(androidPath: string): Promise<boolean> {
let isAndroidPathCorrect = true;
try {
let result = await this.$adb.executeCommand(["--help"], { returnChildProcess: true });
if (result && result.stdout) {
this.$logger.trace(result.stdout);
this.pathToAndroidExecutable = androidPath;
} else {
this.$logger.trace(`Unable to find android executable from '${androidPath}'.`);
isAndroidPathCorrect = false;
}
} catch (err) {
this.$logger.trace(`Error occurred while checking androidExecutable from '${androidPath}'. ${err.message}`);
isAndroidPathCorrect = false;
}

return isAndroidPathCorrect;
}

public async getToolsInfo(): Promise<IAndroidToolsInfoData> {
@cache()
public getToolsInfo(): IAndroidToolsInfoData {
if (!this.toolsInfo) {
let infoData: IAndroidToolsInfoData = Object.create(null);
infoData.androidHomeEnvVar = this.androidHome;
infoData.compileSdkVersion = await this.getCompileSdk();
infoData.buildToolsVersion = await this.getBuildToolsVersion();
infoData.targetSdkVersion = await this.getTargetSdk();
infoData.supportRepositoryVersion = await this.getAndroidSupportRepositoryVersion();
infoData.compileSdkVersion = this.getCompileSdkVersion();
infoData.buildToolsVersion = this.getBuildToolsVersion();
infoData.targetSdkVersion = this.getTargetSdk();
infoData.supportRepositoryVersion = this.getAndroidSupportRepositoryVersion();
infoData.generateTypings = this.shouldGenerateTypings();

this.toolsInfo = infoData;
Expand All @@ -90,14 +43,14 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
return this.toolsInfo;
}

public async validateInfo(options?: { showWarningsAsErrors: boolean, validateTargetSdk: boolean }): Promise<boolean> {
public validateInfo(options?: { showWarningsAsErrors: boolean, validateTargetSdk: boolean }): boolean {
let detectedErrors = false;
this.showWarningsAsErrors = options && options.showWarningsAsErrors;
let toolsInfoData = await this.getToolsInfo();
let isAndroidHomeValid = this.validateAndroidHomeEnvVariable(toolsInfoData.androidHomeEnvVar);
let toolsInfoData = this.getToolsInfo();
let 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 `$ android` to manage your Android SDK versions.");
`Run \`\$ ${this.getPathToSdkManagementTool()}\` to manage your Android SDK versions.`);
detectedErrors = true;
}

Expand All @@ -111,7 +64,7 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
message = `You have to install version ${versionRangeMatches[1]}.`;
}

let invalidBuildToolsAdditionalMsg = 'Run `android` from your command-line to install required `Android Build Tools`.';
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.';
}
Expand All @@ -121,7 +74,7 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
}

if (!toolsInfoData.supportRepositoryVersion) {
let invalidSupportLibAdditionalMsg = 'Run `$ android` to manage the Android Support Repository.';
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.';
}
Expand Down Expand Up @@ -153,6 +106,7 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
if (options) {
this.showWarningsAsErrors = options.showWarningsAsErrors;
}

let 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;
Expand Down Expand Up @@ -186,6 +140,51 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
return null;
}

@cache()
public validateAndroidHomeEnvVariable(options?: { showWarningsAsErrors: boolean }): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need tests for this functionality? Will the cache work as expected here, don't we need to somehow include the "androidHome" to the cacheKey?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache decorator has tests here
The androidHome will not be changed between executions of the method (I've also change androidHome to be only getter).

Regarding tests for the whole androidToolsInfo - we'll move it to nativescript-doctor package very soon and we'll add tests there.

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;
}
Expand All @@ -211,19 +210,19 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
}
}

private async getCompileSdk(): Promise<number> {
private getCompileSdkVersion(): number {
if (!this.selectedCompileSdk) {
let userSpecifiedCompileSdk = this.$options.compileSdk;
if (userSpecifiedCompileSdk) {
let installedTargets = await this.getInstalledTargets();
let installedTargets = this.getInstalledTargets();
let 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 {
let latestValidAndroidTarget = await this.getLatestValidAndroidTarget();
let latestValidAndroidTarget = this.getLatestValidAndroidTarget();
if (latestValidAndroidTarget) {
let integerVersion = this.parseAndroidSdkString(latestValidAndroidTarget);

Expand All @@ -237,8 +236,8 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
return this.selectedCompileSdk;
}

private async getTargetSdk(): Promise<number> {
let targetSdk = this.$options.sdk ? parseInt(this.$options.sdk) : await this.getCompileSdk();
private getTargetSdk(): number {
let targetSdk = this.$options.sdk ? parseInt(this.$options.sdk) : this.getCompileSdkVersion();
this.$logger.trace(`Selected targetSdk is: ${targetSdk}`);
return targetSdk;
}
Expand Down Expand Up @@ -273,7 +272,7 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
return `${AndroidToolsInfo.REQUIRED_BUILD_TOOLS_RANGE_PREFIX} <=${this.getMaxSupportedVersion()}`;
}

private async getBuildToolsVersion(): Promise<string> {
private getBuildToolsVersion(): string {
let buildToolsVersion: string;
if (this.androidHome) {
let pathToBuildTools = path.join(this.androidHome, "build-tools");
Expand All @@ -284,8 +283,8 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
return buildToolsVersion;
}

private async getAppCompatRange(): Promise<string> {
let compileSdkVersion = await this.getCompileSdk();
private getAppCompatRange(): string {
let compileSdkVersion = this.getCompileSdkVersion();
let requiredAppCompatRange: string;
if (compileSdkVersion) {
requiredAppCompatRange = `>=${compileSdkVersion} <${compileSdkVersion + 1}`;
Expand All @@ -294,9 +293,9 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
return requiredAppCompatRange;
}

private async getAndroidSupportRepositoryVersion(): Promise<string> {
private getAndroidSupportRepositoryVersion(): string {
let selectedAppCompatVersion: string;
let requiredAppCompatRange = await this.getAppCompatRange();
let requiredAppCompatRange = this.getAppCompatRange();
if (this.androidHome && requiredAppCompatRange) {
let pathToAppCompat = path.join(this.androidHome, "extras", "android", "m2repository", "com", "android", "support", "appcompat-v7");
selectedAppCompatVersion = this.getMatchingDir(pathToAppCompat, requiredAppCompatRange);
Expand All @@ -306,56 +305,30 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
return selectedAppCompatVersion;
}

private async getLatestValidAndroidTarget(): Promise<string> {
let installedTargets = await this.getInstalledTargets();
private getLatestValidAndroidTarget(): string {
let 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}-`, ""));
}

private async getInstalledTargets(): Promise<string[]> {
if (!this.installedTargetsCache) {
try {
let pathToAndroidExecutable = await this.getPathToAndroidExecutable();
if (pathToAndroidExecutable) {
let result = await this.$childProcess.spawnFromEvent(pathToAndroidExecutable, ["list", "targets"], "close", {}, { throwError: false });
if (result && result.stdout) {
this.$logger.trace(result.stdout);
this.installedTargetsCache = [];
result.stdout.replace(/id: \d+ or "(.+)"/g, (m: string, p1: string) => (this.installedTargetsCache.push(p1), m));
}
}
} catch (err) {
this.$logger.trace("Unable to get Android targets. Error is: " + err);
}
@cache()
private getInstalledTargets(): string[] {
let installedTargets: string[] = [];
const pathToInstalledTargets = path.join(this.androidHome, "platforms");
if (this.$fs.exists(pathToInstalledTargets)) {
installedTargets = this.$fs.readDirectory(pathToInstalledTargets);
}
return this.installedTargetsCache;

this.$logger.trace("Installed Android Targets are: ", installedTargets);

return installedTargets;
}

private getMaxSupportedVersion(): number {
return this.parseAndroidSdkString(_.last(AndroidToolsInfo.SUPPORTED_TARGETS.sort()));
}

private _cachedAndroidHomeValidationResult: boolean = null;
private validateAndroidHomeEnvVariable(androidHomeEnvVar: string): boolean {
if (this._cachedAndroidHomeValidationResult === null) {
this._cachedAndroidHomeValidationResult = true;
let expectedDirectoriesInAndroidHome = ["build-tools", "tools", "platform-tools", "extras"];
if (!androidHomeEnvVar || !this.$fs.exists(androidHomeEnvVar)) {
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.");
this._cachedAndroidHomeValidationResult = false;
} else if (!_.some(expectedDirectoriesInAndroidHome.map(dir => this.$fs.exists(path.join(androidHomeEnvVar, 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.");
this._cachedAndroidHomeValidationResult = false;
}
}

return this._cachedAndroidHomeValidationResult;
}
}
$injector.register("androidToolsInfo", AndroidToolsInfo);
11 changes: 5 additions & 6 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ interface IAndroidToolsInfo {
* and ANDROID_HOME environement variable.
* @return {IAndroidToolsInfoData} Information about installed Android Tools and SDKs.
*/
getToolsInfo(): Promise<IAndroidToolsInfoData>;
getToolsInfo(): IAndroidToolsInfoData;

/**
* Validates the information about required Android tools and SDK versions.
* @param {any} options Defines if the warning messages should treated as error and if the targetSdk value should be validated as well.
* @return {boolean} True if there are detected issues, false otherwise.
*/
validateInfo(options?: { showWarningsAsErrors: boolean, validateTargetSdk: boolean }): Promise<boolean>;
validateInfo(options?: { showWarningsAsErrors: boolean, validateTargetSdk: boolean }): boolean;

/**
* Validates the information about required JAVA version.
Expand All @@ -238,12 +238,11 @@ interface IAndroidToolsInfo {
validateJavacVersion(installedJavaVersion: string, options?: { showWarningsAsErrors: boolean }): Promise<boolean>;

/**
* Returns the path to `android` executable. It should be `$ANDROID_HOME/tools/android`.
* In case ANDROID_HOME is not defined, check if `android` is part of $PATH.
* Validates if ANDROID_HOME environment variable is set correctly.
* @param {any} options Defines if the warning messages should treated as error.
* @return {string} Path to the `android` executable.
* @returns {boolean} true in case ANDROID_HOME is correctly set, false otherwise.
*/
getPathToAndroidExecutable(options?: { showWarningsAsErrors: boolean }): Promise<string>;
validateAndroidHomeEnvVariable(options?: { showWarningsAsErrors: boolean }): boolean;

/**
* Gets the path to `adb` executable from ANDROID_HOME. It should be `$ANDROID_HOME/platform-tools/adb` in case it exists.
Expand Down
2 changes: 1 addition & 1 deletion lib/definitions/emulator-platform-service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ interface IEmulatorPlatformService {
listAvailableEmulators(platform: string): Promise<void>;
getEmulatorInfo(platform: string, nameOfId: string): Promise<IEmulatorInfo>;
getiOSEmulators(): Promise<IEmulatorInfo[]>;
getAndroidEmulators(): Promise<IEmulatorInfo[]>;
getAndroidEmulators(): IEmulatorInfo[];
startEmulator(info: IEmulatorInfo, projectData: IProjectData): Promise<void>;
}
2 changes: 1 addition & 1 deletion lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ interface IPlatformProjectService extends NodeJS.EventEmitter {
validate(projectData: IProjectData): Promise<void>;
createProject(frameworkDir: string, frameworkVersion: string, projectData: IProjectData, pathToTemplate?: string): Promise<void>;
interpolateData(projectData: IProjectData, platformSpecificData: IPlatformSpecificData): Promise<void>;
interpolateConfigurationFile(projectData: IProjectData, platformSpecificData: IPlatformSpecificData): Promise<void>;
interpolateConfigurationFile(projectData: IProjectData, platformSpecificData: IPlatformSpecificData): void;

/**
* Executes additional actions after native project is created.
Expand Down
Loading