Skip to content

Commit 22b6079

Browse files
author
Maxim Lobanov
authored
refactor release file (#1)
1 parent 5ed261e commit 22b6079

File tree

2 files changed

+46
-88
lines changed

2 files changed

+46
-88
lines changed

src/distributors/base-installer.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as core from '@actions/core';
33
import semver from 'semver';
44
import path from 'path';
55
import * as httpm from '@actions/http-client';
6-
import { getJavaPreInstalledPath, IS_LINUX, IS_WINDOWS } from '../util';
6+
import { getJavaPreInstalledPath, getVersionFromToolcachePath, IS_LINUX, IS_WINDOWS, parseLocalVersions } from '../util';
77

88
export interface JavaInitOptions {
99
version: string;
@@ -39,7 +39,7 @@ export abstract class JavaBase {
3939
let foundJava = this.findInToolcache(range);
4040
if (!foundJava) {
4141
// try to find Java in default system locations outside tool-cache
42-
foundJava = this.findInKnownLocations(range.raw)
42+
foundJava = this.findInKnownLocations(range)
4343
}
4444

4545
if(!foundJava) {
@@ -59,17 +59,25 @@ export abstract class JavaBase {
5959
}
6060

6161
return {
62-
javaVersion: this.getVersionFromPath(toolPath),
62+
javaVersion: getVersionFromToolcachePath(toolPath),
6363
javaPath: toolPath
6464
};
6565
}
6666

67-
protected findInKnownLocations(version: string): IJavaInfo | null {
67+
protected findInKnownLocations(version: semver.Range): IJavaInfo | null {
6868
if(this.javaPackage !== 'jdk') {
6969
return null;
7070
}
7171

72-
return getJavaPreInstalledPath(version, this.distributor, this.getJavaVersionsPath());
72+
let knownLocation = null;
73+
switch(process.platform) {
74+
case "win32": knownLocation = path.normalize('C:/Program Files/Java');
75+
case "darwin": knownLocation = '/Library/Java/JavaVirtualMachines';
76+
default: knownLocation = '/usr/lib/jvm'
77+
}
78+
79+
const localVersions = parseLocalVersions(knownLocation, this.distributor);
80+
return localVersions.find(localVersion => semver.satisfies(localVersion.javaVersion, version)) ?? null;
7381
}
7482

7583
protected setJavaDefault(toolPath: string) {
@@ -124,12 +132,4 @@ export abstract class JavaBase {
124132

125133
return version;
126134
}
127-
128-
private getVersionFromPath(toolPath: string) {
129-
if(toolPath) {
130-
return path.basename(path.dirname(toolPath));
131-
}
132-
133-
return toolPath;
134-
}
135135
}

src/util.ts

Lines changed: 33 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import * as httpm from '@actions/http-client';
2-
import * as core from '@actions/core';
31
import fs from 'fs';
4-
import os from 'os';
2+
import os, { EOL } from 'os';
53
import * as path from 'path';
6-
import * as semver from 'semver';
4+
import { IJavaInfo } from './distributors/base-installer';
75

86
export const IS_WINDOWS = process.platform === 'win32';
97
export const IS_LINUX = process.platform === 'linux';
@@ -17,88 +15,48 @@ export function getTempDir() {
1715
return tempDirectory;
1816
}
1917

20-
export function createHttpClient() {
21-
const http = new httpm.HttpClient('setup-java', undefined, {
22-
allowRetries: true,
23-
maxRetries: 3
24-
});
18+
export function getVersionFromToolcachePath(toolPath: string) {
19+
if(toolPath) {
20+
return path.basename(path.dirname(toolPath));
21+
}
2522

26-
return http;
23+
return toolPath;
2724
}
2825

29-
export function getJavaPreInstalledPath(
30-
version: string,
31-
distributor: string,
32-
versionsPath: string
33-
) {
34-
const versionsDir = fs.readdirSync(versionsPath);
35-
const javaInformations = versionsDir.map(versionDir => {
36-
let javaPath = path.join(versionsPath, versionDir);
26+
export function parseLocalVersions(rootLocation: string, distributor: string): IJavaInfo[] {
27+
const potentialVersions = fs.readdirSync(rootLocation);
28+
const foundVersions: IJavaInfo[] = [];
29+
30+
potentialVersions.forEach(potentialVersion => {
31+
let javaPath = path.join(rootLocation, potentialVersion);
3732
if (IS_MACOS) {
3833
javaPath = path.join(javaPath, macOSJavaContentDir);
3934
}
40-
41-
const content: string | null = getJavaReleaseFileContent(javaPath);
42-
if (!content) {
43-
return null;
35+
const javaReleaseFile = path.join(javaPath, 'release');
36+
if (!fs.existsSync(javaReleaseFile)) {
37+
return;
4438
}
4539

46-
const implemetation = parseFile('IMPLEMENTOR', content);
47-
48-
const re = new RegExp(/^[7,8]\./);
49-
if (!re.test(version) && implemetation !== distributor) {
50-
return null;
40+
const dict = parseReleaseFile(javaReleaseFile);
41+
if (dict["IMPLEMENTOR"] && dict["IMPLEMENTOR"].includes(distributor)) {
42+
foundVersions.push({
43+
javaVersion: dict["JAVA_VERSION"],
44+
javaPath: javaPath
45+
});
5146
}
52-
53-
const javaVersion = parseFile('JAVA_VERSION', content);
54-
55-
if (!javaVersion) {
56-
return null;
57-
}
58-
59-
core.info(`found java ${javaVersion} version for ${implemetation}`);
60-
61-
return {
62-
javaVersion: semver.coerce(javaVersion.split('_')[0])!.version,
63-
javaPath: javaPath
64-
};
6547
});
6648

67-
const javaInfo =
68-
javaInformations.find(item => {
69-
return (
70-
item && semver.satisfies(item.javaVersion, new semver.Range(version))
71-
);
72-
}) || null;
73-
74-
return javaInfo;
49+
return foundVersions;
7550
}
7651

77-
export function getJavaReleaseFileContent(javaDirectory: string) {
78-
let javaReleaseFile = path.join(javaDirectory, 'release');
79-
80-
if (
81-
!(fs.existsSync(javaReleaseFile) && fs.lstatSync(javaReleaseFile).isFile())
82-
) {
83-
core.info('Release file for java was not found');
84-
return null;
85-
}
86-
87-
const content: string = fs.readFileSync(javaReleaseFile).toString();
88-
89-
return content;
90-
}
91-
92-
export function parseFile(keyWord: string, content: string) {
93-
const re = new RegExp(`${keyWord}="(.*)"$`, 'gm');
94-
const regexExecArr = re.exec(content);
95-
if (!regexExecArr) {
96-
return null;
97-
}
98-
99-
let version = regexExecArr[1].startsWith('1.')
100-
? regexExecArr[1].replace('1.', '')
101-
: regexExecArr[1];
52+
function parseReleaseFile(releaseFilePath: string): {[key: string]: string} {
53+
const content: string = fs.readFileSync(releaseFilePath).toString();
54+
const lines = content.split(EOL);
55+
const dict: {[key: string]: string} = {}
56+
lines.forEach(line => {
57+
const [key, value] = line.split('=', 2);
58+
dict[key] = value;
59+
});
10260

103-
return version;
104-
}
61+
return dict;
62+
}

0 commit comments

Comments
 (0)