Skip to content

Commit

Permalink
Download Java 11 if only Java 8 is installed
Browse files Browse the repository at this point in the history
See #39.
  • Loading branch information
valentjn committed Jul 19, 2020
1 parent 7329f1b commit f912d4c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

## 6.1.0 (upcoming)

- Download Java 11 if only Java 8 is installed (see [#39](https://github.com/valentjn/vscode-ltex/issues/39) and [documentation](https://valentjn.github.io/vscode-ltex/docs/deprecation-of-java-8.html))
- Upgrade bundled AdoptOpenJDK JRE to 11.0.8+10
- Update some NPM dependencies

Expand Down
1 change: 1 addition & 0 deletions i18n/messages.nls.de.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"copyReportAndCreateIssue": "Bericht kopieren und Problem erstellen",
"couldNotDownloadExtractRunJava": "Herunterladen/Entpacken/Ausführung von Java fehlgeschlagen.",
"couldNotDownloadOrExtractJava11": "Herunterladen oder Entpacken von Java 11 fehlgeschlagen, jedoch wurde eine funktionierende Installation von Java {0} (Version {1}) auf Ihrem System gefunden. Die Verwendung von LTeX mit Java-Versionen älter als 11 ist deprecated. [In Zukunft wird LTeX Java 11 oder neuer voraussetzen.](https://valentjn.github.io/vscode-ltex/docs/deprecation-of-java-8.html) Bitte aktualisieren Sie Java zu Java 11 oder neuer so bald wie möglich.",
"couldNotInstallLtexLs": "Installation von ltex-ls fehlgeschlagen.",
"couldNotStartLanguageClient": "Start des Language Clients fehlgeschlagen!",
"downloading": "Lade {0} herunter ...",
Expand Down
5 changes: 4 additions & 1 deletion i18n/messages.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"copyReportAndCreateIssue": "Copy report and create issue",
"couldNotDeleteLeavingTemporaryDirectoryOnDisk": "Could not delete '{0}', leaving temporary directory on disk.",
"couldNotDeleteLeavingTemporaryFileOnDisk": "Could not delete '{0}', leaving temporary file on disk.",
"couldNotDownloadOrExtractLtexLs": "Could not download or extract ltex-ls.",
"couldNotDownloadExtractRunJava": "Could not download/extract/run Java.",
"couldNotDownloadOrExtractJava11": "Could not download or extract Java 11, but a working installation of Java {0} (version {1}) has been found on your system. Running LTeX with Java versions prior to 11 is deprecated. [In the future, LTeX will require Java 11 or newer.](https://valentjn.github.io/vscode-ltex/docs/deprecation-of-java-8.html) Please upgrade to Java 11 or newer as soon as possible.",
"couldNotDownloadOrExtractLtexLs": "Could not download or extract ltex-ls.",
"couldNotFindCompatibleVersionOfLtexLsIn": "Could not find a compatible version of ltex-ls in '{0}'.",
"couldNotFindCompatibleVersionOfLtexLsOnGitHub": "Could not find a compatible version of ltex-ls on GitHub. LTeX version is '{0}', available ltex-ls versions are '{1}'.",
"couldNotFindDirectoryAfterExtractingArchive": "Could not find a directory after extracting the archive.",
Expand Down Expand Up @@ -71,7 +72,9 @@
"stdoutOfLtexLs": "stdout of ltex-ls:",
"testingLtexLs": "Testing ltex-ls...",
"testFailed": "Test failed.",
"testFailedDueToTooOldJava": "Test failed due to too old Java. You have Java {0} (version {1}). Please upgrade to Java 11 or newer.",
"testSuccessful": "Test successful!",
"testSuccessfulButJavaIsTooOld": "Test successful, but your Java version is old. You have Java {0} (version {1}). Running LTeX with Java < 11 is possible, but deprecated. In the future, LTeX will require Java 11 or newer. Upgrade your Java installation, or LTeX will stop working.",
"thanksForUpgradingFromLtex4x": "Thanks for upgrading from LTeX 4.x to 5.x or later!",
"thanksForHelpingToImproveLtex": "Thanks for helping to improve LTeX. Be sure to [read the instructions on how to report bugs](https://valentjn.github.io/vscode-ltex/docs/contributing-code-issues.html#how-to-report-bugs) first. When you create the issue on GitHub, paste the bug report from the clipboard into the description of the issue.",
"tryAgain": "Try again",
Expand Down
66 changes: 47 additions & 19 deletions src/Dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ export default class Dependencies {
}

try {
// try 0: only use ltex.ltexLs.path (don't use lib/, don't download)
// try 1: use ltex.ltexLs.path or lib/ (don't download)
// try 2: use ltex.ltexLs.path or lib/ or download
// try 0: use ltex.ltexLs.path
// try 1: use lib/ (don't download)
// try 2: download and use lib/
Logger.log('');
this._ltexLsPath = workspaceConfig.get('ltex-ls.path', '');

Expand Down Expand Up @@ -426,16 +426,19 @@ export default class Dependencies {
}

try {
// try 0: only use ltex.java.path (don't use lib/, don't download)
// try 1: use ltex.java.path or lib/ (don't download)
// try 2: use ltex.java.path or lib/ or download
for (let i: number = 0; i < 3; i++) {
// try 0: use ltex.java.path, Java 11+ only
// try 1: use lib/ (don't download), Java 11+ only
// try 2: download and use lib/, Java 11+ only
// try 3: use ltex.java.path
// try 4: use lib/ (don't download)
// try 5: download and use lib/
for (let i: number = 0; i < 6; i++) {
Logger.log('');
this._javaPath = Dependencies.getRenamedSetting(workspaceConfig, 'java.path', 'javaHome');

if (Dependencies.isValidPath(this._javaPath)) {
Logger.log(i18n('ltexJavaPathSetTo', this._javaPath));
} else if (i == 0) {
} else if (i % 3 == 0) {
Logger.log(i18n('ltexJavaPathNotSet'));
} else {
Logger.log(i18n('searchingForJavaIn', libDirPath));
Expand All @@ -446,7 +449,7 @@ export default class Dependencies {
} else {
Logger.log(i18n('couldNotFindJavaIn', libDirPath));

if (i <= 1) {
if (i % 3 <= 1) {
continue;
} else {
await this.installJava();
Expand All @@ -469,7 +472,9 @@ export default class Dependencies {
Logger.log(i18n('usingJavaFromPathOrJavaHome'));
}

if (await this.test()) {
const requireJava11: boolean = (i < 3);

if (await this.test(requireJava11)) {
Logger.log('');
return true;
}
Expand Down Expand Up @@ -501,7 +506,7 @@ export default class Dependencies {
});
}

private async test(): Promise<boolean> {
private async test(requireJava11: boolean): Promise<boolean> {
const executable: CodeLanguageClient.Executable = await this.getLtexLsExecutable();
if (executable.args == null) executable.args = [];
executable.args.push('--version');
Expand All @@ -520,37 +525,60 @@ export default class Dependencies {
const childProcess: ChildProcess.SpawnSyncReturns<string> = ChildProcess.spawnSync(
executable.command, executable.args, executableOptions);
let success: boolean = false;
let ltexLsVersion: string = '';
let javaVersion: string = '';
let javaMajorVersion: number = -1;

if ((childProcess.status == 0) && childProcess.stdout.includes('ltex-ls')) {
try {
const versionInfo: any = JSON.parse(childProcess.stdout);

if (Object.prototype.hasOwnProperty.call(versionInfo, 'ltex-ls')) {
this._ltexLsVersion = versionInfo['ltex-ls'];
ltexLsVersion = versionInfo['ltex-ls'];
}

if (Object.prototype.hasOwnProperty.call(versionInfo, 'java')) {
this._javaVersion = versionInfo['java'];
const match: RegExpMatchArray | null = versionInfo['java'].match(/(\d+)\.(\d+)/);

if ((match != null) && (match.length >= 3)) {
javaVersion = versionInfo['java'];
javaMajorVersion = parseInt(match[1]);
if (javaMajorVersion == 1) javaMajorVersion = parseInt(match[2]);
}
}

success = true;
if ((ltexLsVersion.length > 0) && (javaVersion.length > 0)) {
success = true;
}
} catch (e) {
// don't throw error as debug info is printed below
}
}

if (success) {
Logger.log(i18n('testSuccessful'));
} else {
if (!success) {
Logger.log(i18n('testFailed'));
Logger.log(i18n('exitCodeOfLtexLs', childProcess.status));
Logger.log(i18n('stdoutOfLtexLs'));
Logger.log(childProcess.stdout);
Logger.log(i18n('stderrOfLtexLs'));
Logger.log(childProcess.stderr);
return false;
} else if (javaMajorVersion >= 11) {
Logger.log(i18n('testSuccessful'));
this._ltexLsVersion = ltexLsVersion;
this._javaVersion = javaVersion;
return true;
} else if (requireJava11) {
Logger.log(i18n('testFailedDueToTooOldJava', javaMajorVersion, javaVersion));
return false;
} else {
Logger.log(i18n('testSuccessfulButJavaIsTooOld', javaMajorVersion, javaVersion));
Code.window.showWarningMessage(i18n('couldNotDownloadOrExtractJava11', javaMajorVersion,
javaVersion));
this._ltexLsVersion = ltexLsVersion;
this._javaVersion = javaVersion;
return true;
}

return success;
}

public async getLtexLsExecutable(): Promise<CodeLanguageClient.Executable> {
Expand Down

0 comments on commit f912d4c

Please sign in to comment.