gradle-build-action/src/provision.ts

194 lines
5.9 KiB
TypeScript
Raw Normal View History

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'
import * as httpm from '@actions/http-client'
2020-06-13 11:44:30 +00:00
import * as core from '@actions/core'
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'
import {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':
core.warning(
`Specifying gradle-version 'rc' has been deprecated. Use 'release-candidate' instead.`
)
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> {
2020-06-13 12:11:33 +00:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 11:44:30 +00:00
`${gradleVersionsBaseUrl}/current`
)
return provisionGradle(versionInfo)
2019-09-21 14:01:53 +00:00
}
async function gradleReleaseCandidate(): Promise<string> {
2020-06-13 12:11:33 +00:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 11:44:30 +00:00
`${gradleVersionsBaseUrl}/release-candidate`
)
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
return provisionGradle(versionInfo)
2019-09-21 14:01:53 +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> {
2020-06-13 12:11:33 +00:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 11:44:30 +00:00
`${gradleVersionsBaseUrl}/nightly`
)
return provisionGradle(versionInfo)
2019-09-21 14:01:53 +00:00
}
async function gradleReleaseNightly(): Promise<string> {
2020-06-13 12:11:33 +00:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 11:44:30 +00:00
`${gradleVersionsBaseUrl}/release-nightly`
)
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
}
return provisionGradle(versionInfo)
2019-09-21 14:01:53 +00:00
}
2020-06-13 12:11:33 +00:00
async function gradleVersionDeclaration(
url: string
): Promise<GradleVersionInfo> {
return await httpGetGradleVersion(url)
2019-09-21 14:01:53 +00:00
}
2020-06-13 11:44:30 +00:00
async function findGradleVersionDeclaration(
version: string
2020-06-13 12:11:33 +00:00
): Promise<GradleVersionInfo | undefined> {
const gradleVersions = await httpGetGradleVersions(
`${gradleVersionsBaseUrl}/all`
)
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
}
async function provisionGradle(
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 14:01:53 +00:00
}
const downloadPath = await downloadAndCacheGradleDistribution(versionInfo)
await toolCache.extractZip(downloadPath, installsDir)
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
}
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}`
const restoreKey = await cache.restoreCache([downloadPath], cacheKey)
if (restoreKey) {
core.info(
`Restored Gradle distribution ${cacheKey} from cache to ${downloadPath}`
)
return downloadPath
}
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) {
// Fail on validation errors or non-errors (the latter to keep Typescript happy)
if (
error instanceof cache.ValidationError ||
!(error instanceof Error)
) {
throw error
}
core.warning(error.message)
}
}
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 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))
}
async function httpGetGradleVersions(
url: string
): Promise<GradleVersionInfo[]> {
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 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
}