|
| 1 | +import { ChildProcess } from "./wrappers/child-process"; |
| 2 | +import * as Promise from "bluebird"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +export class SysInfo { |
| 6 | + // Different java has different format for `java -version` command. |
| 7 | + private static JAVA_VERSION_REGEXP = /(?:openjdk|java) version \"((?:\d+\.)+(?:\d+))/i; |
| 8 | + |
| 9 | + // For other versions of java javac version output is not on first line. |
| 10 | + // Thus can't use ^ for starts with in regex. |
| 11 | + private static JAVA_COMPILER_VERSION_REGEXP = /javac (.*)/i; |
| 12 | + |
| 13 | + private javaVerCache: string; |
| 14 | + private javaCompilerVerCache: string; |
| 15 | + |
| 16 | + constructor(private childProcess: ChildProcess) { } |
| 17 | + |
| 18 | + public getJavaVersion(): Promise<string> { |
| 19 | + if (!this.javaVerCache) { |
| 20 | + return new Promise<string>((resolve, reject) => { |
| 21 | + this.childProcess.spawnFromEvent("java", ["-version"], "exit") |
| 22 | + .then((spawnResult) => { |
| 23 | + this.javaVerCache = SysInfo.JAVA_VERSION_REGEXP.exec(spawnResult.stderr)[1]; |
| 24 | + resolve(this.javaVerCache); |
| 25 | + }) |
| 26 | + .catch((err) => { |
| 27 | + this.javaVerCache = null; |
| 28 | + resolve(this.javaVerCache); |
| 29 | + }); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + return Promise.resolve(this.javaVerCache); |
| 34 | + } |
| 35 | + |
| 36 | + public getJavaCompilerVersion(): Promise<string> { |
| 37 | + if (!this.javaCompilerVerCache) { |
| 38 | + return new Promise<string>((resolve, reject) => { |
| 39 | + let javaCompileExecutableName = "javac"; |
| 40 | + let javaHome = process.env.JAVA_HOME; |
| 41 | + let pathToJavaCompilerExecutable = javaHome ? path.join(javaHome, "bin", javaCompileExecutableName) : javaCompileExecutableName; |
| 42 | + this.childProcess.exec(`"${pathToJavaCompilerExecutable}" -version`) |
| 43 | + .then((output) => { |
| 44 | + this.javaCompilerVerCache = output ? SysInfo.JAVA_COMPILER_VERSION_REGEXP.exec(output.stderr)[1] : null; |
| 45 | + resolve(this.javaCompilerVerCache); |
| 46 | + }) |
| 47 | + .catch((err) => { |
| 48 | + this.javaCompilerVerCache = null; |
| 49 | + resolve(this.javaCompilerVerCache); |
| 50 | + }); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + return Promise.resolve(this.javaCompilerVerCache); |
| 55 | + } |
| 56 | +} |
0 commit comments