Skip to content

Commit

Permalink
Add validation for local java runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Flanker32 committed Sep 8, 2022
1 parent ec8fe90 commit f09587a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/commands/createNewProject/javaSteps/JavaVersionStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ import { previewDescription } from "../../../constants";
import { hasMinFuncCliVersion } from "../../../funcCoreTools/hasMinFuncCliVersion";
import { localize } from "../../../localize";
import { IJavaProjectWizardContext } from "./IJavaProjectWizardContext";
import { getJavaVersion } from "./JavaVersions";

export const java8: string = '8';
export const java11: string = '11';
export const java17: string = '17';

const versionInfo: [string, string, string, string?][] = [
[java8, 'Java 8', '1.0.0'],
[java11, 'Java 11', '3.0.2630'],
[java17, 'Java 17', '4.0.0', previewDescription]
type javaVersionInfo = {
label: string,
data: string,
description?: string,
miniFunc: string
}

const versionInfo: javaVersionInfo[] = [
{ label: 'Java 8', data: java8, miniFunc: '1.0.0' },
{ label: 'Java 11', data: java11, miniFunc: '3.0.2630' },
{ label: 'Java 17', data: java17, miniFunc: '4.0.0', description: previewDescription }
];

export class JavaVersionStep extends AzureWizardPromptStep<IJavaProjectWizardContext> {
Expand All @@ -33,12 +41,12 @@ export class JavaVersionStep extends AzureWizardPromptStep<IJavaProjectWizardCon
context.javaVersion = (await context.ui.showQuickPick(picks, { placeHolder })).data;
}

// todo: get runtime from Get Function App Stacks API and validate local java version
async getPicks(context: IJavaProjectWizardContext): Promise<IAzureQuickPickItem<string>[]> {
const javaVersion: number = await getJavaVersion();
const result: IAzureQuickPickItem<string>[] = [];
for (const [javaVersion, displayName, miniFunc, description] of versionInfo) {
if (await hasMinFuncCliVersion(context, miniFunc, context.version)) {
result.push({ label: displayName, data: javaVersion, description: description });
for (const version of versionInfo) {
if (await hasMinFuncCliVersion(context, version.miniFunc, context.version) && javaVersion >= Number(version.data)) {
result.push(version);
}
}
return result;
Expand Down
69 changes: 69 additions & 0 deletions src/commands/createNewProject/javaSteps/JavaVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fse from 'fs-extra';
import { localize } from '../../../localize';
import { cpUtils } from "../../../utils/cpUtils";

import path = require("path");

export async function getJavaVersion(): Promise<number> {
const javaHome: string | undefined = process.env['JAVA_HOME'];
let javaVersion = javaHome ? await checkVersionInReleaseFile(javaHome) : undefined;
if (!javaVersion) {
javaVersion = await checkVersionByCLI(javaHome ? path.join(javaHome, 'bin', 'java') : 'java');
}
if (!javaVersion) {
const message: string = localize('javaNotFound', 'Failed to get java version, please ensure that java is installed and JAVA_HOME is set correctly.');
throw Error(message);
}
return javaVersion;
}

async function checkVersionInReleaseFile(javaHome: string): Promise<number | undefined> {
if (!javaHome) {
return undefined;
}
const releaseFile = path.join(javaHome, "release");
if (!fse.existsSync(releaseFile)) {
return undefined;
}

try {
const content = fse.readFileSync(releaseFile);
const regexp = /^JAVA_VERSION="(.*)"/gm;
const match = regexp.exec(content.toString());
return match ? flattenMajorVersion(match[1]) : undefined;
} catch (error) {
// ignore
return undefined;
}
}

async function checkVersionByCLI(javaExec: string): Promise<number | undefined> {
if (!javaExec) {
return undefined;
}
const result: cpUtils.ICommandResult = await cpUtils.tryExecuteCommand(undefined, undefined, javaExec, '-version');
const output: string = result.cmdOutputIncludingStderr;
const regexp = /version "(.*)"/g;
const match = regexp.exec(output);
return match ? flattenMajorVersion(match[1]) : undefined;
}

function flattenMajorVersion(version: string): number {
// Ignore '1.' prefix for legacy Java versions
if (version.startsWith("1.")) {
version = version.substring(2);
}

const regexp = /\d+/g;
const match = regexp.exec(version);
let javaVersion = 0;
if (match) {
javaVersion = parseInt(match[0], 10);
}

return javaVersion;
}

0 comments on commit f09587a

Please sign in to comment.