@@ -22,6 +22,7 @@ const exec = __importStar(require("@actions/exec"));
2222const tc = __importStar ( require ( "@actions/tool-cache" ) ) ;
2323const fs = __importStar ( require ( "fs" ) ) ;
2424const path = __importStar ( require ( "path" ) ) ;
25+ const semver = __importStar ( require ( "semver" ) ) ;
2526const httpm = __importStar ( require ( "typed-rest-client/HttpClient" ) ) ;
2627const IS_WINDOWS = process . platform === 'win32' ;
2728if ( ! tempDirectory ) {
@@ -50,7 +51,12 @@ function getJava(version, arch, jdkFile) {
5051 let compressedFileExtension = '' ;
5152 if ( ! jdkFile ) {
5253 core . debug ( 'Downloading Jdk from Azul' ) ;
53- jdkFile = yield downloadJava ( version ) ;
54+ let http = new httpm . HttpClient ( 'setup-java' ) ;
55+ let contents = yield ( yield http . get ( 'https://static.azul.com/zulu/bin/' ) ) . readBody ( ) ;
56+ let refs = contents . match ( / < a h r e f .* \" > / gi) || [ ] ;
57+ const downloadInfo = getDownloadInfo ( refs , version ) ;
58+ jdkFile = yield tc . downloadTool ( downloadInfo . url ) ;
59+ version = downloadInfo . version ;
5460 compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz' ;
5561 }
5662 else {
@@ -60,7 +66,7 @@ function getJava(version, arch, jdkFile) {
6066 let tempDir = path . join ( tempDirectory , 'temp_' + Math . floor ( Math . random ( ) * 2000000000 ) ) ;
6167 const jdkDir = yield unzipJavaDownload ( jdkFile , compressedFileExtension , tempDir ) ;
6268 core . debug ( `jdk extracted to ${ jdkDir } ` ) ;
63- toolPath = yield tc . cacheDir ( jdkDir , 'Java' , normalizeVersion ( version ) , arch ) ;
69+ toolPath = yield tc . cacheDir ( jdkDir , 'Java' , getCacheVersionString ( version ) , arch ) ;
6470 }
6571 let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch ;
6672 core . exportVariable ( 'JAVA_HOME' , toolPath ) ;
@@ -69,7 +75,7 @@ function getJava(version, arch, jdkFile) {
6975 } ) ;
7076}
7177exports . getJava = getJava ;
72- function normalizeVersion ( version ) {
78+ function getCacheVersionString ( version ) {
7379 const versionArray = version . split ( '.' ) ;
7480 const major = versionArray [ 0 ] ;
7581 const minor = versionArray . length > 1 ? versionArray [ 1 ] : '0' ;
@@ -156,32 +162,70 @@ function unzipJavaDownload(repoRoot, fileEnding, destinationFolder, extension) {
156162 }
157163 } ) ;
158164}
159- function downloadJava ( version ) {
160- return __awaiter ( this , void 0 , void 0 , function * ( ) {
161- let filterString = '' ;
162- if ( IS_WINDOWS ) {
163- filterString = `jdk${ version } -win_x64.zip` ;
165+ function getDownloadInfo ( refs , version ) {
166+ version = normalizeVersion ( version ) ;
167+ let extension = '' ;
168+ if ( IS_WINDOWS ) {
169+ extension = `-win_x64.zip` ;
170+ }
171+ else {
172+ if ( process . platform === 'darwin' ) {
173+ extension = `-macosx_x64.tar.gz` ;
164174 }
165175 else {
166- if ( process . platform === 'darwin' ) {
167- filterString = `jdk${ version } -macosx_x64.tar.gz` ;
168- }
169- else {
170- filterString = `jdk${ version } -linux_x64.tar.gz` ;
171- }
176+ extension = `-linux_x64.tar.gz` ;
172177 }
173- let http = new httpm . HttpClient ( 'setup-java' ) ;
174- let contents = yield ( yield http . get ( 'https://static.azul.com/zulu/bin/' ) ) . readBody ( ) ;
175- let refs = contents . match ( / < a h r e f .* \" > / gi) || [ ] ;
176- refs = refs . filter ( val => {
177- if ( val . indexOf ( filterString ) > - 1 ) {
178- return true ;
179- }
180- } ) ;
181- if ( refs . length == 0 ) {
182- throw new Error ( `No valid download found for version ${ version } . Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument` ) ;
178+ }
179+ // Maps version to url
180+ let versionMap = new Map ( ) ;
181+ // Filter by platform
182+ refs . forEach ( ref => {
183+ if ( ref . indexOf ( extension ) < 0 ) {
184+ return ;
185+ }
186+ // If we haven't returned, means we're looking at the correct platform
187+ let versions = ref . match ( / j d k .* - / gi) || [ ] ;
188+ if ( versions . length > 1 ) {
189+ throw new Error ( `Invalid ref received from https://static.azul.com/zulu/bin/: ${ ref } ` ) ;
190+ }
191+ if ( versions . length == 0 ) {
192+ return ;
193+ }
194+ const refVersion = versions [ 0 ] . slice ( 'jdk' . length , versions [ 0 ] . length - 1 ) ;
195+ if ( semver . satisfies ( refVersion , version ) ) {
196+ versionMap . set ( refVersion , 'https://static.azul.com/zulu/bin/' +
197+ ref . slice ( '<a href="' . length , ref . length - '">' . length ) ) ;
183198 }
184- const fileName = refs [ 0 ] . slice ( '<a href="' . length , refs [ 0 ] . length - '">' . length ) ;
185- return yield tc . downloadTool ( `https://static.azul.com/zulu/bin/${ fileName } ` ) ;
186199 } ) ;
200+ // Choose the most recent satisfying version
201+ let curVersion = '0.0.0' ;
202+ let curUrl = '' ;
203+ for ( const entry of versionMap . entries ( ) ) {
204+ const entryVersion = entry [ 0 ] ;
205+ const entryUrl = entry [ 1 ] ;
206+ if ( semver . gt ( entryVersion , curVersion ) ) {
207+ curUrl = entryUrl ;
208+ curVersion = entryVersion ;
209+ }
210+ }
211+ if ( curUrl == '' ) {
212+ throw new Error ( `No valid download found for version ${ version } . Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument` ) ;
213+ }
214+ return { version : curVersion , url : curUrl } ;
215+ }
216+ function normalizeVersion ( version ) {
217+ if ( version . slice ( 0 , 2 ) === '1.' ) {
218+ // Trim leading 1. for versions like 1.8
219+ version = version . slice ( 2 ) ;
220+ if ( ! version ) {
221+ throw new Error ( '1. is not a valid version' ) ;
222+ }
223+ }
224+ // Add trailing .x if it is missing
225+ if ( version . split ( '.' ) . length != 3 ) {
226+ if ( version [ version . length - 1 ] != 'x' ) {
227+ version = version + '.x' ;
228+ }
229+ }
230+ return version ;
187231}
0 commit comments