| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  | import * as core from '@actions/core'; | 
					
						
							|  |  |  | import * as tc from '@actions/tool-cache'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import fs from 'fs'; | 
					
						
							|  |  |  | import path from 'path'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  | import {JavaBase} from '../base-installer'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   JavaDownloadRelease, | 
					
						
							|  |  |  |   JavaInstallerOptions, | 
					
						
							|  |  |  |   JavaInstallerResults | 
					
						
							|  |  |  | } from '../base-models'; | 
					
						
							| 
									
										
										
										
											2024-10-11 03:02:25 +05:30
										 |  |  | import { | 
					
						
							|  |  |  |   extractJdkFile, | 
					
						
							|  |  |  |   getDownloadArchiveExtension, | 
					
						
							|  |  |  |   renameWinArchive | 
					
						
							|  |  |  | } from '../../util'; | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  | import {HttpCodes} from '@actions/http-client'; | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const ORACLE_DL_BASE = 'https://download.oracle.com/java'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class OracleDistribution extends JavaBase { | 
					
						
							|  |  |  |   constructor(installerOptions: JavaInstallerOptions) { | 
					
						
							|  |  |  |     super('Oracle', installerOptions); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  |   protected async downloadTool( | 
					
						
							|  |  |  |     javaRelease: JavaDownloadRelease | 
					
						
							|  |  |  |   ): Promise<JavaInstallerResults> { | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  |     core.info( | 
					
						
							|  |  |  |       `Downloading Java ${javaRelease.version} (${this.distribution}) from ${javaRelease.url} ...` | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2024-10-11 03:02:25 +05:30
										 |  |  |     let javaArchivePath = await tc.downloadTool(javaRelease.url); | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     core.info(`Extracting Java archive...`); | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  |     const extension = getDownloadArchiveExtension(); | 
					
						
							| 
									
										
										
										
											2024-10-11 03:02:25 +05:30
										 |  |  |     if (process.platform === 'win32') { | 
					
						
							|  |  |  |       javaArchivePath = renameWinArchive(javaArchivePath); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  |     const extractedJavaPath = await extractJdkFile(javaArchivePath, extension); | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const archiveName = fs.readdirSync(extractedJavaPath)[0]; | 
					
						
							|  |  |  |     const archivePath = path.join(extractedJavaPath, archiveName); | 
					
						
							|  |  |  |     const version = this.getToolcacheVersionName(javaRelease.version); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  |     const javaPath = await tc.cacheDir( | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  |       archivePath, | 
					
						
							|  |  |  |       this.toolcacheFolderName, | 
					
						
							|  |  |  |       version, | 
					
						
							|  |  |  |       this.architecture | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  |     return {version: javaRelease.version, path: javaPath}; | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 14:49:35 +02:00
										 |  |  |   protected async findPackageForDownload( | 
					
						
							|  |  |  |     range: string | 
					
						
							|  |  |  |   ): Promise<JavaDownloadRelease> { | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  |     const arch = this.distributionArchitecture(); | 
					
						
							|  |  |  |     if (arch !== 'x64' && arch !== 'aarch64') { | 
					
						
							|  |  |  |       throw new Error(`Unsupported architecture: ${this.architecture}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!this.stable) { | 
					
						
							|  |  |  |       throw new Error('Early access versions are not supported'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (this.packageType !== 'jdk') { | 
					
						
							|  |  |  |       throw new Error('Oracle JDK provides only the `jdk` package type'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const platform = this.getPlatform(); | 
					
						
							|  |  |  |     const extension = getDownloadArchiveExtension(); | 
					
						
							| 
									
										
										
										
											2023-07-18 09:28:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const isOnlyMajorProvided = !range.includes('.'); | 
					
						
							|  |  |  |     const major = isOnlyMajorProvided ? range : range.split('.')[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const possibleUrls: string[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * NOTE | 
					
						
							|  |  |  |      * If only major version was provided we will check it under /latest first | 
					
						
							|  |  |  |      * in order to retrieve the latest possible version if possible, | 
					
						
							|  |  |  |      * otherwise we will fall back to /archive where we are guaranteed to | 
					
						
							|  |  |  |      * find any version if it exists | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     if (isOnlyMajorProvided) { | 
					
						
							|  |  |  |       possibleUrls.push( | 
					
						
							|  |  |  |         `${ORACLE_DL_BASE}/${major}/latest/jdk-${major}_${platform}-${arch}_bin.${extension}` | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     possibleUrls.push( | 
					
						
							|  |  |  |       `${ORACLE_DL_BASE}/${major}/archive/jdk-${range}_${platform}-${arch}_bin.${extension}` | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (parseInt(major) < 17) { | 
					
						
							|  |  |  |       throw new Error('Oracle JDK is only supported for JDK 17 and later'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-18 09:28:12 +02:00
										 |  |  |     for (const url of possibleUrls) { | 
					
						
							|  |  |  |       const response = await this.http.head(url); | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-18 09:28:12 +02:00
										 |  |  |       if (response.message.statusCode === HttpCodes.OK) { | 
					
						
							|  |  |  |         return {url, version: range}; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-18 09:28:12 +02:00
										 |  |  |       if (response.message.statusCode !== HttpCodes.NotFound) { | 
					
						
							|  |  |  |         throw new Error( | 
					
						
							|  |  |  |           `Http request for Oracle JDK failed with status code: ${response.message.statusCode}` | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-18 09:28:12 +02:00
										 |  |  |     throw new Error(`Could not find Oracle JDK for SemVer ${range}`); | 
					
						
							| 
									
										
										
										
											2023-02-07 16:29:21 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public getPlatform(platform: NodeJS.Platform = process.platform): OsVersions { | 
					
						
							|  |  |  |     switch (platform) { | 
					
						
							|  |  |  |       case 'darwin': | 
					
						
							|  |  |  |         return 'macos'; | 
					
						
							|  |  |  |       case 'win32': | 
					
						
							|  |  |  |         return 'windows'; | 
					
						
							|  |  |  |       case 'linux': | 
					
						
							|  |  |  |         return 'linux'; | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         throw new Error( | 
					
						
							|  |  |  |           `Platform '${platform}' is not supported. Supported platforms: 'linux', 'macos', 'windows'` | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |