2020-06-13 11:44:30 +00:00
|
|
|
import * as fs from 'fs'
|
2020-06-14 10:28:17 +00:00
|
|
|
import * as os from 'os'
|
2020-06-13 11:44:30 +00:00
|
|
|
import * as path from 'path'
|
2020-09-13 12:17:37 +00:00
|
|
|
import * as httpm from '@actions/http-client'
|
2020-06-13 11:44:30 +00:00
|
|
|
import * as core from '@actions/core'
|
2021-07-08 18:22:48 +00:00
|
|
|
import * as cache from '@actions/cache'
|
2020-06-13 11:44:30 +00:00
|
|
|
import * as toolCache from '@actions/tool-cache'
|
2019-09-21 14:01:53 +00:00
|
|
|
|
2020-06-13 11:44:30 +00:00
|
|
|
import * as gradlew from './gradlew'
|
2021-11-05 12:54:31 +00:00
|
|
|
import {handleCacheFailure, isCacheDisabled, isCacheReadOnly} from './cache-utils'
|
2019-09-23 10:12:03 +00:00
|
|
|
|
2020-06-13 11:44:30 +00:00
|
|
|
const gradleVersionsBaseUrl = 'https://services.gradle.org/versions'
|
2019-09-23 10:12:03 +00:00
|
|
|
|
2019-09-21 14:01:53 +00:00
|
|
|
/**
|
2019-09-23 10:12:03 +00:00
|
|
|
* @return Gradle executable path
|
2019-09-21 14:01:53 +00:00
|
|
|
*/
|
2020-06-13 11:54:27 +00:00
|
|
|
export async function gradleVersion(version: string): Promise<string> {
|
|
|
|
switch (version) {
|
2020-06-13 11:44:30 +00:00
|
|
|
case 'current':
|
|
|
|
return gradleCurrent()
|
|
|
|
case 'rc':
|
2021-10-29 13:34:44 +00:00
|
|
|
core.warning(`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`)
|
2021-06-24 22:59:03 +00:00
|
|
|
return gradleReleaseCandidate()
|
|
|
|
case 'release-candidate':
|
2020-06-13 11:44:30 +00:00
|
|
|
return gradleReleaseCandidate()
|
|
|
|
case 'nightly':
|
|
|
|
return gradleNightly()
|
|
|
|
case 'release-nightly':
|
|
|
|
return gradleReleaseNightly()
|
2019-09-21 14:01:53 +00:00
|
|
|
default:
|
2020-06-13 11:54:27 +00:00
|
|
|
return gradle(version)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleCurrent(): Promise<string> {
|
2021-10-29 13:34:44 +00:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`)
|
2021-07-08 18:22:48 +00:00
|
|
|
return provisionGradle(versionInfo)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleReleaseCandidate(): Promise<string> {
|
2021-10-29 13:34:44 +00:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`)
|
2020-09-13 11:30:54 +00:00
|
|
|
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
|
2021-07-08 18:22:48 +00:00
|
|
|
return provisionGradle(versionInfo)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
2020-09-13 11:30:54 +00:00
|
|
|
core.info('No current release-candidate found, will fallback to current')
|
2020-06-13 11:44:30 +00:00
|
|
|
return gradleCurrent()
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleNightly(): Promise<string> {
|
2021-10-29 13:34:44 +00:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`)
|
2021-07-08 18:22:48 +00:00
|
|
|
return provisionGradle(versionInfo)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradleReleaseNightly(): Promise<string> {
|
2021-10-29 13:34:44 +00:00
|
|
|
const versionInfo = await gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`)
|
2021-07-08 18:22:48 +00:00
|
|
|
return provisionGradle(versionInfo)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function gradle(version: string): Promise<string> {
|
2020-06-13 12:11:33 +00:00
|
|
|
const versionInfo = await findGradleVersionDeclaration(version)
|
|
|
|
if (!versionInfo) {
|
2020-06-13 11:44:30 +00:00
|
|
|
throw new Error(`Gradle version ${version} does not exists`)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
2021-07-08 18:22:48 +00:00
|
|
|
return provisionGradle(versionInfo)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
async function gradleVersionDeclaration(url: string): Promise<GradleVersionInfo> {
|
2020-06-13 12:11:33 +00:00
|
|
|
return await httpGetGradleVersion(url)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
async function findGradleVersionDeclaration(version: string): Promise<GradleVersionInfo | undefined> {
|
|
|
|
const gradleVersions = await httpGetGradleVersions(`${gradleVersionsBaseUrl}/all`)
|
2020-06-13 12:11:33 +00:00
|
|
|
return gradleVersions.find((entry: GradleVersionInfo) => {
|
2020-06-13 11:44:30 +00:00
|
|
|
return entry.version === version
|
|
|
|
})
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
async function provisionGradle(versionInfo: GradleVersionInfo): Promise<string> {
|
2021-09-13 17:47:32 +00:00
|
|
|
return core.group(`Provision Gradle ${versionInfo.version}`, async () => {
|
|
|
|
return locateGradleAndDownloadIfRequired(versionInfo)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo): Promise<string> {
|
2021-07-08 18:22:48 +00:00
|
|
|
const installsDir = path.join(os.homedir(), 'gradle-installations/installs')
|
|
|
|
const installDir = path.join(installsDir, `gradle-${versionInfo.version}`)
|
|
|
|
if (fs.existsSync(installDir)) {
|
|
|
|
core.info(`Gradle installation already exists at ${installDir}`)
|
|
|
|
return executableFrom(installDir)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 18:22:48 +00:00
|
|
|
const downloadPath = await downloadAndCacheGradleDistribution(versionInfo)
|
2021-07-05 19:17:31 +00:00
|
|
|
await toolCache.extractZip(downloadPath, installsDir)
|
2021-07-08 18:22:48 +00:00
|
|
|
core.info(`Extracted Gradle ${versionInfo.version} to ${installDir}`)
|
2019-09-21 14:01:53 +00:00
|
|
|
|
2020-06-13 11:44:30 +00:00
|
|
|
const executable = executableFrom(installDir)
|
|
|
|
fs.chmodSync(executable, '755')
|
|
|
|
core.info(`Provisioned Gradle executable ${executable}`)
|
2019-09-21 14:01:53 +00:00
|
|
|
|
2020-06-13 11:44:30 +00:00
|
|
|
return executable
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo): Promise<string> {
|
|
|
|
const downloadPath = path.join(os.homedir(), `gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`)
|
2021-07-08 18:22:48 +00:00
|
|
|
|
2021-09-12 20:26:38 +00:00
|
|
|
if (isCacheDisabled()) {
|
2021-07-08 18:22:48 +00:00
|
|
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
|
|
|
return downloadPath
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = `gradle-${versionInfo.version}`
|
2021-11-05 12:54:31 +00:00
|
|
|
try {
|
|
|
|
const restoreKey = await cache.restoreCache([downloadPath], cacheKey)
|
|
|
|
if (restoreKey) {
|
|
|
|
core.info(`Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}`)
|
|
|
|
return downloadPath
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
handleCacheFailure(error, `Restore Gradle distribution ${versionInfo.version} failed`)
|
2021-08-24 18:57:17 +00:00
|
|
|
}
|
2021-11-05 12:54:31 +00:00
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
core.info(`Gradle distribution ${versionInfo.version} not found in cache. Will download.`)
|
2021-08-24 18:57:17 +00:00
|
|
|
await downloadGradleDistribution(versionInfo, downloadPath)
|
2021-07-08 18:22:48 +00:00
|
|
|
|
2021-09-12 20:26:38 +00:00
|
|
|
if (!isCacheReadOnly()) {
|
2021-07-08 18:22:48 +00:00
|
|
|
try {
|
|
|
|
await cache.saveCache([downloadPath], cacheKey)
|
|
|
|
} catch (error) {
|
2021-11-05 12:54:31 +00:00
|
|
|
handleCacheFailure(error, `Save Gradle distribution ${versionInfo.version} failed`)
|
2021-07-08 18:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return downloadPath
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
async function downloadGradleDistribution(versionInfo: GradleVersionInfo, downloadPath: string): Promise<void> {
|
2021-07-08 18:22:48 +00:00
|
|
|
await toolCache.downloadTool(versionInfo.downloadUrl, downloadPath)
|
2021-10-29 13:34:44 +00:00
|
|
|
core.info(`Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${fs.statSync(downloadPath).size})`)
|
2021-07-08 18:22:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 14:01:53 +00:00
|
|
|
function executableFrom(installDir: string): string {
|
2020-06-13 11:44:30 +00:00
|
|
|
return path.join(installDir, 'bin', `${gradlew.installScriptFilename()}`)
|
2019-09-21 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 12:11:33 +00:00
|
|
|
async function httpGetGradleVersion(url: string): Promise<GradleVersionInfo> {
|
|
|
|
return JSON.parse(await httpGetString(url))
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:34:44 +00:00
|
|
|
async function httpGetGradleVersions(url: string): Promise<GradleVersionInfo[]> {
|
2020-06-13 12:11:33 +00:00
|
|
|
return JSON.parse(await httpGetString(url))
|
|
|
|
}
|
|
|
|
|
|
|
|
async function httpGetString(url: string): Promise<string> {
|
2021-08-07 22:17:27 +00:00
|
|
|
const httpClient = new httpm.HttpClient('gradle/gradle-build-action')
|
2021-07-05 19:17:31 +00:00
|
|
|
const response = await httpClient.get(url)
|
2020-06-13 12:11:33 +00:00
|
|
|
return response.readBody()
|
2019-09-23 10:12:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 12:11:33 +00:00
|
|
|
interface GradleVersionInfo {
|
|
|
|
version: string
|
|
|
|
downloadUrl: string
|
|
|
|
}
|