Skip to content

Commit 8998a98

Browse files
Merge pull request #50 from NativeScript/vladimirov/fix-java-prompts
fix: prompts to install javac should not be shown on macOS
2 parents dc410fd + 644c381 commit 8998a98

File tree

6 files changed

+207
-33
lines changed

6 files changed

+207
-33
lines changed

lib/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ export class Constants {
1515
public static ANDROID_RUNTIME = "tns-android";
1616
public static VERSION_PROPERTY_NAME = "version";
1717
public static XCODE_MIN_REQUIRED_VERSION = 9;
18+
public static JAVAC_EXECUTABLE_NAME = "javac";
19+
public static JAVA_EXECUTABLE_NAME = "java";
1820
}

lib/sys-info.ts

+68-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Constants } from "./constants";
1313

1414
export class SysInfo implements NativeScriptDoctor.ISysInfo {
1515
private static JAVA_COMPILER_VERSION_REGEXP = /^javac (.*)/im;
16+
private static JAVA_VERSION_REGEXP = /^(?:(?:java)|(?:openjdk)).*?\"(.*)\"/im;
1617
private static XCODE_VERSION_REGEXP = /Xcode (.*)/;
1718
private static VERSION_REGEXP = /(\d{1,})\.(\d{1,})\.*([\w-]{0,})/m;
1819
private static CLI_OUTPUT_VERSION_REGEXP = /^(?:\d+\.){2}\d+.*?$/m;
@@ -22,6 +23,9 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
2223
private monoVerRegExp = /version (\d+[.]\d+[.]\d+) /gm;
2324

2425
private javaCompilerVerCache: string;
26+
private javaVerCache: string;
27+
private javaVerJavaHomeCache: string;
28+
private javaVerPathCache: string;
2529
private xCodeVerCache: string;
2630
private npmVerCache: string;
2731
private nodeVerCache: string;
@@ -57,15 +61,29 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
5761

5862
public getJavaCompilerVersion(): Promise<string> {
5963
return this.getValueForProperty(() => this.javaCompilerVerCache, async (): Promise<string> => {
60-
const javaCompileExecutableName = "javac";
61-
const javaHome = process.env["JAVA_HOME"];
62-
const pathToJavaCompilerExecutable = javaHome ? path.join(javaHome, "bin", javaCompileExecutableName) : javaCompileExecutableName;
63-
try {
64-
const output = await this.childProcess.exec(`"${pathToJavaCompilerExecutable}" -version`);
65-
return SysInfo.JAVA_COMPILER_VERSION_REGEXP.exec(`${output.stderr}${EOL}${output.stdout}`)[1];
66-
} catch (err) {
67-
return null;
68-
}
64+
const javacVersion = (await this.getVersionOfJavaExecutableFromJavaHome(Constants.JAVAC_EXECUTABLE_NAME, SysInfo.JAVA_COMPILER_VERSION_REGEXP)) ||
65+
(await this.getVersionOfJavaExecutableFromPath(Constants.JAVAC_EXECUTABLE_NAME, SysInfo.JAVA_COMPILER_VERSION_REGEXP));
66+
67+
return javacVersion;
68+
});
69+
}
70+
71+
public getJavaVersion(): Promise<string> {
72+
return this.getValueForProperty(() => this.javaVerCache, async (): Promise<string> => {
73+
const javaVersion = (await this.getJavaVersionFromJavaHome()) || (await this.getJavaVersionFromPath());
74+
return javaVersion;
75+
});
76+
}
77+
78+
public getJavaVersionFromPath(): Promise<string> {
79+
return this.getValueForProperty(() => this.javaVerPathCache, (): Promise<string> => {
80+
return this.getVersionOfJavaExecutableFromPath(Constants.JAVA_EXECUTABLE_NAME, SysInfo.JAVA_VERSION_REGEXP);
81+
});
82+
}
83+
84+
public getJavaVersionFromJavaHome(): Promise<string> {
85+
return this.getValueForProperty(() => this.javaVerJavaHomeCache, (): Promise<string> => {
86+
return this.getVersionOfJavaExecutableFromJavaHome(Constants.JAVA_EXECUTABLE_NAME, SysInfo.JAVA_VERSION_REGEXP);
6987
});
7088
}
7189

@@ -490,6 +508,7 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
490508

491509
result.dotNetVer = await this.hostInfo.dotNetVersion();
492510
result.javacVersion = await this.getJavaCompilerVersion();
511+
result.javaVersion = await this.getJavaVersion();
493512
result.adbVer = await this.getAdbVersion(config && config.androidToolsInfo && config.androidToolsInfo.pathToAdb);
494513
result.androidInstalled = await this.isAndroidInstalled();
495514
result.monoVer = await this.getMonoVersion();
@@ -499,4 +518,44 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
499518
return result;
500519
});
501520
}
521+
522+
private async getVersionOfJavaExecutableFromJavaHome(javaExecutableName: string, regExp: RegExp): Promise<string> {
523+
let javaExecutableVersion: string = null;
524+
525+
try {
526+
const javaHome = process.env["JAVA_HOME"];
527+
const javaExecutableFile = this.hostInfo.isWindows ? `${javaExecutableName}.exe` : javaExecutableName;
528+
529+
if (javaHome) {
530+
const pathToJavaExecutable = path.join(javaHome, "bin", javaExecutableFile);
531+
if (this.fileSystem.exists(pathToJavaExecutable)) {
532+
javaExecutableVersion = await this.getVersionOfJavaExecutable(pathToJavaExecutable, regExp);
533+
}
534+
}
535+
} catch (err) { /* intentionally left blank */ }
536+
537+
return javaExecutableVersion;
538+
}
539+
540+
private async getVersionOfJavaExecutableFromPath(javaExecutableName: string, regExp: RegExp): Promise<string> {
541+
let javaExecutableVersion: string = null;
542+
543+
try {
544+
const detectionCommand = this.hostInfo.isWindows ? "where" : "which";
545+
// if this command succeeds, we have javac in the PATH. In case it is not there, it will throw an error.
546+
await this.childProcess.exec(`${detectionCommand} ${javaExecutableName}`);
547+
javaExecutableVersion = await this.getVersionOfJavaExecutable(javaExecutableName, regExp);
548+
} catch (err) { /* intentionally left blank */ }
549+
550+
return javaExecutableVersion;
551+
}
552+
553+
private async getVersionOfJavaExecutable(executable: string, regExp: RegExp): Promise<string> {
554+
try {
555+
const output = await this.childProcess.exec(`"${executable}" -version`);
556+
return regExp.exec(`${output.stderr}${EOL}${output.stdout}`)[1];
557+
} catch (err) {
558+
return null;
559+
}
560+
}
502561
}

package-lock.json

+22-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "nativescript-doctor",
3-
"version": "1.8.1",
3+
"version": "1.9.0",
44
"description": "Library that helps identifying if the environment can be used for development of {N} apps.",
55
"main": "lib/index.js",
66
"types": "./typings/nativescript-doctor.d.ts",
77
"scripts": {
8-
"test": "node_modules/.bin/istanbul cover node_modules/.bin/mocha -- --recursive"
8+
"test": "node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha -- --recursive"
99
},
1010
"repository": {
1111
"type": "git",

0 commit comments

Comments
 (0)