gradle-build-action/src/provision.ts

162 lines
5.8 KiB
TypeScript
Raw Normal View History

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