1- import * as httpm from '@actions/http-client' ;
2- import * as core from '@actions/core' ;
31import fs from 'fs' ;
4- import os from 'os' ;
2+ import os , { EOL } from 'os' ;
53import * as path from 'path' ;
6- import * as semver from 'semver ' ;
4+ import { IJavaInfo } from './distributors/base-installer ' ;
75
86export const IS_WINDOWS = process . platform === 'win32' ;
97export 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