-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http-client-java, validate JDK and Maven (#5374)
fix #5365 run on no JDK/Maven ``` error http-client-java: Java Development Kit (JDK) is not found in PATH. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download error http-client-java: Apache Maven is not found in PATH. Apache Maven can be downloaded from https://maven.apache.org/download.cgi Found 2 errors. ``` fix #5366 run on JDK version too old ``` error http-client-java: Java Development Kit (JDK) in PATH is version 8. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download Found 1 error. ``` Currently validation only happen on `onEmit`. I didn't add it to postinstall script, as this could be flagged as security warning.
- Loading branch information
1 parent
3bd6a87
commit 821b7f9
Showing
7 changed files
with
199 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,7 @@ words: | |
- itor | ||
- ivar | ||
- Jacoco | ||
- javac | ||
- jdwp | ||
- jobject | ||
- Johan | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Program } from "@typespec/compiler"; | ||
import { logError, spawnAsync } from "./utils.js"; | ||
|
||
export const JDK_NOT_FOUND_MESSAGE = | ||
"Java Development Kit (JDK) is not found in PATH. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download"; | ||
|
||
export async function validateDependencies( | ||
program: Program | undefined, | ||
logDiagnostic: boolean = false, | ||
) { | ||
// Check JDK and version | ||
try { | ||
const result = await spawnAsync("javac", ["-version"], { stdio: "pipe" }); | ||
const javaVersion = findJavaVersion(result.stdout) ?? findJavaVersion(result.stderr); | ||
if (javaVersion) { | ||
if (javaVersion < 11) { | ||
// the message is JDK 17, because clientcore depends on JDK 17 | ||
// emitter only require JDK 11 | ||
const message = `Java Development Kit (JDK) in PATH is version ${javaVersion}. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download`; | ||
// // eslint-disable-next-line no-console | ||
// console.log("[ERROR] " + message); | ||
if (program && logDiagnostic) { | ||
logError(program, message); | ||
} | ||
} | ||
} | ||
} catch (error: any) { | ||
let message = error.message; | ||
if (error && "code" in error && error["code"] === "ENOENT") { | ||
message = JDK_NOT_FOUND_MESSAGE; | ||
} | ||
// // eslint-disable-next-line no-console | ||
// console.log("[ERROR] " + message); | ||
if (program && logDiagnostic) { | ||
logError(program, message); | ||
} | ||
} | ||
|
||
// Check Maven | ||
// nodejs does not allow spawn of .cmd on win32 | ||
const shell = process.platform === "win32"; | ||
try { | ||
await spawnAsync("mvn", ["-v"], { stdio: "pipe", shell: shell }); | ||
} catch (error: any) { | ||
let message = error.message; | ||
if (shell || (error && "code" in error && error["code"] === "ENOENT")) { | ||
message = | ||
"Apache Maven is not found in PATH. Apache Maven can be downloaded from https://maven.apache.org/download.cgi"; | ||
} | ||
// // eslint-disable-next-line no-console | ||
// console.log("[ERROR] " + message); | ||
if (program && logDiagnostic) { | ||
logError(program, message); | ||
} | ||
} | ||
} | ||
|
||
function findJavaVersion(output: string): number | undefined { | ||
const regex = /javac (\d+)\.(\d+)\..*/; | ||
const matches = output.match(regex); | ||
if (matches && matches.length > 2) { | ||
if (matches[1] === "1") { | ||
// "javac 1.8.0_422" -> 8 | ||
return +matches[2]; | ||
} else { | ||
// "javac 21.0.3" -> 21 | ||
return +matches[1]; | ||
} | ||
} | ||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters