-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·247 lines (207 loc) · 7.74 KB
/
index.ts
File metadata and controls
executable file
·247 lines (207 loc) · 7.74 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
import * as Promise from "bluebird"
import * as utils from "./utils"
import * as WinReg from "winreg"
import * as which from "which"
import {each as asyncEach} from "async"
import {join, basename} from "path"
import {unique} from "underscore"
import {execFile} from "child_process"
import {inspect} from "util"
export class JavaVersion {
major: number
minor: number
patch: number
update: number
constructor(version: string) {
const matches = version.match(/(\d+?)\.(\d+?)\.(\d+?)(?:_(\d+))?/)
this.major = parseInt(matches[1])
this.minor = parseInt(matches[2])
this.patch = parseInt(matches[3])
this.update = parseInt(matches[4] || "0")
}
}
export class JavaInstall {
private _path: string
private _arch: string
private _version: JavaVersion
private _gotInfo: boolean
private _invalid: boolean
constructor(path: string) {
this._path = path
this._gotInfo = false
this._invalid = false
}
get path() {
return this._path
}
get arch() {
return this._arch
}
get version() {
return this._version
}
get invalid() {
return this._invalid
}
/**
* @internal
*/
ensureInfo(): Promise<void> {
if (!this._gotInfo && !this._invalid) {
return new Promise<void>((resolve, reject) => {
var jarFile = __dirname + "/java/PrintJavaVersion.jar"
debug(`jarFile: ${jarFile}`)
execFile(this._path, ["-jar", jarFile], {timeout: 1000}, (err, stdoutBuf, stderrBuf) => {
if (err) {
debug(`[${this._path}] Err: ${err}`)
this._invalid = true
resolve()
return
}
const stdout = stdoutBuf.toString().trim()
debug(`[${this._path}] stdout: ${inspect(stdout)}`)
const lines = stdout.split("\n")
const arch = lines[1]
switch (arch) {
case "32":
this._arch = "x86"
break
case "64":
this._arch = "x64"
break
default:
this._arch = "unknown"
break
}
const version = lines[0]
this._version = new JavaVersion(version)
debug(`[${this._path}] ${inspect(this)}`)
this._gotInfo = true
resolve()
})
})
} else {
return Promise.resolve()
}
}
}
let debug = (debug: string) => {}
export function setDebug(debugFn: (debug: string) => void) {
debug = debugFn
}
export const getJavas = utils.PromiseCache((): Promise<Array<JavaInstall>> => {
debug(`getJavas start (${process.platform})`)
let javas: Promise<Array<JavaInstall>>
switch (process.platform) {
case "win32":
javas = findJavasWindows()
break
case "darwin":
javas = findJavasMac()
break
case "linux":
javas = findJavasLinux()
break
default:
javas = findJavaOnPath()
break
}
return javas
.tap(v => {debug(`Versions Raw: ${inspect(v.map(v => v.path))}`)})
.filter<JavaInstall>(version => utils.canExecute(version.path))
.tap(v => {debug(`Versions Existing: ${inspect(v.map(v => v.path))}`)})
.then(versions => unique(versions, v => v.path))
.each<JavaInstall, void>(version => version.ensureInfo())
.filter<JavaInstall>(version => !version.invalid)
.tap(v => {debug(`Versions Final: ${inspect(v)}`)})
})
//region Linux
const defaultJavasLinux = [
new JavaInstall("/opt/java/bin/java"),
new JavaInstall("/usr/bin/java")
]
function findJavasLinux(): Promise<Array<JavaInstall>> {
return Promise.all([defaultJavasLinux, findJavaOnPath()]).then(utils.flatten)
}
//endregion
//region Mac
const defaultJavasMac = [
new JavaInstall("/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/MacOS/itms/java/bin/java"),
new JavaInstall("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"),
new JavaInstall("/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java")
]
function findJavasMac(): Promise<Array<JavaInstall>> {
let javaVersionPromises: Array<Promise<Array<JavaInstall>>> = []
javaVersionPromises.push(findJavaOnPath())
javaVersionPromises.push(Promise.resolve(defaultJavasMac))
javaVersionPromises.push(
utils.allDirectories("/Library/Java/JavaVirtualMachines/").catch(err => [])
.map(dir => [
new JavaInstall(join(dir, "Contents/Home/bin/java")),
new JavaInstall(join(dir, "Contents/Home/jre/bin/java"))
]).then(utils.flatten)
)
javaVersionPromises.push(
utils.allDirectories("/System/Library/Java/JavaVirtualMachines/").catch(err => [])
.map(dir => [
new JavaInstall(join(dir, "Contents/Home/bin/java")),
new JavaInstall(join(dir, "Contents/Commands/java"))
]).then(utils.flatten)
)
return Promise.all(javaVersionPromises).then(utils.flatten)
}
//endregion
//region Windows
const javaRegKeys = [
"SOFTWARE\\JavaSoft\\Java Runtime Environment",
"SOFTWARE\\JavaSoft\\Java Development Kit"
]
const defaultJavasWindows = [
new JavaInstall("C:/Program Files/Java/jre8/bin/javaw.exe"),
new JavaInstall("C:/Program Files/Java/jre7/bin/javaw.exe"),
new JavaInstall("C:/Program Files/Java/jre6/bin/javaw.exe"),
new JavaInstall("C:/Program Files (x86)/Java/jre8/bin/javaw.exe"),
new JavaInstall("C:/Program Files (x86)/Java/jre7/bin/javaw.exe"),
new JavaInstall("C:/Program Files (x86)/Java/jre6/bin/javaw.exe")
]
function findJavasWindows(): Promise<Array<JavaInstall>> {
let javaVersionPromises: Array<Promise<Array<JavaInstall>>> = []
javaVersionPromises.push(Promise.resolve(defaultJavasWindows))
javaVersionPromises.push(findJavaOnPath())
javaRegKeys.forEach(key => {
javaVersionPromises.push(findJavasFromRegistryKey(key, "x64").catch(err => []))
javaVersionPromises.push(findJavasFromRegistryKey(key, "x86").catch(err => []))
})
return Promise.all(javaVersionPromises).then(utils.flatten)
}
function findJavasFromRegistryKey(keyName: string, arch: string): Promise<Array<JavaInstall>> {
return new Promise<Array<JavaInstall>>((resolve, reject) => {
let key = new WinReg({ key: keyName, arch: arch })
// For each subkey of the given key, each of which should be
key.keys((err: Error, javaKeys: Array<WinReg>) => {
if (err) {
resolve([])
return
}
let javaVersions: Array<JavaInstall> = []
asyncEach<WinReg>(javaKeys, (javaKey, cb) => {
javaKey.get("JavaHome", (err, javaHome) => {
if (err) return
let path = join(javaHome.value, "bin", "javaw.exe")
javaVersions.push(new JavaInstall(path))
cb()
})
}, (err) => {
debug(`Reg key ${keyName} arch ${arch}, got ${javaVersions}`)
if (err) reject(err)
else resolve(javaVersions)
})
})
})
}
//endregion
const whichP = Promise.promisify(which)
function findJavaOnPath(): Promise<Array<JavaInstall>> {
return whichP("java")
.then(path => [new JavaInstall(path)], err => [])
}