diff --git a/src/distributors/base-installer.ts b/src/distributors/base-installer.ts index d5864e1f9..4cdb26487 100644 --- a/src/distributors/base-installer.ts +++ b/src/distributors/base-installer.ts @@ -3,7 +3,7 @@ import * as core from '@actions/core'; import semver from 'semver'; import path from 'path'; import * as httpm from '@actions/http-client'; -import { getJavaPreInstalledPath, IS_LINUX, IS_WINDOWS } from '../util'; +import { getJavaPreInstalledPath, getVersionFromToolcachePath, IS_LINUX, IS_WINDOWS, parseLocalVersions } from '../util'; export interface JavaInitOptions { version: string; @@ -39,7 +39,7 @@ export abstract class JavaBase { let foundJava = this.findInToolcache(range); if (!foundJava) { // try to find Java in default system locations outside tool-cache - foundJava = this.findInKnownLocations(range.raw) + foundJava = this.findInKnownLocations(range) } if(!foundJava) { @@ -59,17 +59,25 @@ export abstract class JavaBase { } return { - javaVersion: this.getVersionFromPath(toolPath), + javaVersion: getVersionFromToolcachePath(toolPath), javaPath: toolPath }; } - protected findInKnownLocations(version: string): IJavaInfo | null { + protected findInKnownLocations(version: semver.Range): IJavaInfo | null { if(this.javaPackage !== 'jdk') { return null; } - return getJavaPreInstalledPath(version, this.distributor, this.getJavaVersionsPath()); + let knownLocation = null; + switch(process.platform) { + case "win32": knownLocation = path.normalize('C:/Program Files/Java'); + case "darwin": knownLocation = '/Library/Java/JavaVirtualMachines'; + default: knownLocation = '/usr/lib/jvm' + } + + const localVersions = parseLocalVersions(knownLocation, this.distributor); + return localVersions.find(localVersion => semver.satisfies(localVersion.javaVersion, version)) ?? null; } protected setJavaDefault(toolPath: string) { @@ -124,12 +132,4 @@ export abstract class JavaBase { return version; } - - private getVersionFromPath(toolPath: string) { - if(toolPath) { - return path.basename(path.dirname(toolPath)); - } - - return toolPath; - } } \ No newline at end of file diff --git a/src/util.ts b/src/util.ts index 02f911f1f..7aa78c252 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,9 +1,7 @@ -import * as httpm from '@actions/http-client'; -import * as core from '@actions/core'; import fs from 'fs'; -import os from 'os'; +import os, { EOL } from 'os'; import * as path from 'path'; -import * as semver from 'semver'; +import { IJavaInfo } from './distributors/base-installer'; export const IS_WINDOWS = process.platform === 'win32'; export const IS_LINUX = process.platform === 'linux'; @@ -17,88 +15,48 @@ export function getTempDir() { return tempDirectory; } -export function createHttpClient() { - const http = new httpm.HttpClient('setup-java', undefined, { - allowRetries: true, - maxRetries: 3 - }); +export function getVersionFromToolcachePath(toolPath: string) { + if(toolPath) { + return path.basename(path.dirname(toolPath)); + } - return http; + return toolPath; } -export function getJavaPreInstalledPath( - version: string, - distributor: string, - versionsPath: string -) { - const versionsDir = fs.readdirSync(versionsPath); - const javaInformations = versionsDir.map(versionDir => { - let javaPath = path.join(versionsPath, versionDir); +export function parseLocalVersions(rootLocation: string, distributor: string): IJavaInfo[] { + const potentialVersions = fs.readdirSync(rootLocation); + const foundVersions: IJavaInfo[] = []; + + potentialVersions.forEach(potentialVersion => { + let javaPath = path.join(rootLocation, potentialVersion); if (IS_MACOS) { javaPath = path.join(javaPath, macOSJavaContentDir); } - - const content: string | null = getJavaReleaseFileContent(javaPath); - if (!content) { - return null; + const javaReleaseFile = path.join(javaPath, 'release'); + if (!fs.existsSync(javaReleaseFile)) { + return; } - const implemetation = parseFile('IMPLEMENTOR', content); - - const re = new RegExp(/^[7,8]\./); - if (!re.test(version) && implemetation !== distributor) { - return null; + const dict = parseReleaseFile(javaReleaseFile); + if (dict["IMPLEMENTOR"] && dict["IMPLEMENTOR"].includes(distributor)) { + foundVersions.push({ + javaVersion: dict["JAVA_VERSION"], + javaPath: javaPath + }); } - - const javaVersion = parseFile('JAVA_VERSION', content); - - if (!javaVersion) { - return null; - } - - core.info(`found java ${javaVersion} version for ${implemetation}`); - - return { - javaVersion: semver.coerce(javaVersion.split('_')[0])!.version, - javaPath: javaPath - }; }); - const javaInfo = - javaInformations.find(item => { - return ( - item && semver.satisfies(item.javaVersion, new semver.Range(version)) - ); - }) || null; - - return javaInfo; + return foundVersions; } -export function getJavaReleaseFileContent(javaDirectory: string) { - let javaReleaseFile = path.join(javaDirectory, 'release'); - - if ( - !(fs.existsSync(javaReleaseFile) && fs.lstatSync(javaReleaseFile).isFile()) - ) { - core.info('Release file for java was not found'); - return null; - } - - const content: string = fs.readFileSync(javaReleaseFile).toString(); - - return content; -} - -export function parseFile(keyWord: string, content: string) { - const re = new RegExp(`${keyWord}="(.*)"$`, 'gm'); - const regexExecArr = re.exec(content); - if (!regexExecArr) { - return null; - } - - let version = regexExecArr[1].startsWith('1.') - ? regexExecArr[1].replace('1.', '') - : regexExecArr[1]; +function parseReleaseFile(releaseFilePath: string): {[key: string]: string} { + const content: string = fs.readFileSync(releaseFilePath).toString(); + const lines = content.split(EOL); + const dict: {[key: string]: string} = {} + lines.forEach(line => { + const [key, value] = line.split('=', 2); + dict[key] = value; + }); - return version; -} + return dict; +} \ No newline at end of file