@@ -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 httpm = __importStar ( require ( "typed-rest-client/HttpClient" ) ) ;
2526const IS_WINDOWS = process . platform === 'win32' ;
2627if ( ! tempDirectory ) {
2728 let baseLocation ;
@@ -46,15 +47,20 @@ function getJava(version, arch, jdkFile) {
4647 core . debug ( `Tool found in cache ${ toolPath } ` ) ;
4748 }
4849 else {
50+ let compressedFileExtension = '' ;
4951 if ( ! jdkFile ) {
50- throw new Error ( `Failed to find Java ${ version } in the cache. Please specify a valid jdk file to install from instead.` ) ;
52+ core . debug ( 'Downloading Jdk from Azul' ) ;
53+ jdkFile = yield downloadJava ( version ) ;
54+ compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz' ;
5155 }
52- core . debug ( 'Retrieving Jdk from local path' ) ;
53- const compressedFileExtension = getFileEnding ( jdkFile ) ;
56+ else {
57+ core . debug ( 'Retrieving Jdk from local path' ) ;
58+ }
59+ compressedFileExtension = compressedFileExtension || getFileEnding ( jdkFile ) ;
5460 let tempDir = path . join ( tempDirectory , 'temp_' + Math . floor ( Math . random ( ) * 2000000000 ) ) ;
5561 const jdkDir = yield unzipJavaDownload ( jdkFile , compressedFileExtension , tempDir ) ;
5662 core . debug ( `jdk extracted to ${ jdkDir } ` ) ;
57- toolPath = yield tc . cacheDir ( jdkDir , 'Java' , ` ${ version } .0.0` , arch ) ;
63+ toolPath = yield tc . cacheDir ( jdkDir , 'Java' , normalizeVersion ( version ) , arch ) ;
5864 }
5965 let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch ;
6066 core . exportVariable ( 'JAVA_HOME' , toolPath ) ;
@@ -63,6 +69,13 @@ function getJava(version, arch, jdkFile) {
6369 } ) ;
6470}
6571exports . getJava = getJava ;
72+ function normalizeVersion ( version ) {
73+ const versionArray = version . split ( '.' ) ;
74+ const major = versionArray [ 0 ] ;
75+ const minor = versionArray . length > 1 ? versionArray [ 1 ] : '0' ;
76+ const patch = versionArray . length > 2 ? versionArray [ 2 ] : '0' ;
77+ return `${ major } .${ minor } .${ patch } ` ;
78+ }
6679function getFileEnding ( file ) {
6780 let fileEnding = '' ;
6881 if ( file . endsWith ( '.tar' ) ) {
@@ -126,7 +139,7 @@ function unpackJars(fsPath, javaBinPath) {
126139 }
127140 } ) ;
128141}
129- function unzipJavaDownload ( repoRoot , fileEnding , destinationFolder ) {
142+ function unzipJavaDownload ( repoRoot , fileEnding , destinationFolder , extension ) {
130143 return __awaiter ( this , void 0 , void 0 , function * ( ) {
131144 // Create the destination folder if it doesn't exist
132145 yield io . mkdirP ( destinationFolder ) ;
@@ -143,3 +156,32 @@ function unzipJavaDownload(repoRoot, fileEnding, destinationFolder) {
143156 }
144157 } ) ;
145158}
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` ;
164+ }
165+ 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+ }
172+ }
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` ) ;
183+ }
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 } ` ) ;
186+ } ) ;
187+ }
0 commit comments