gradle-build-action/src/provision.ts

177 lines
5.4 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 unzip from 'unzipper'
import * as core from '@actions/core'
import * as io from '@actions/io'
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'
2019-09-23 06:12:03 -04:00
2020-06-13 07:44:30 -04:00
const httpc = new httpm.HttpClient('eskatos/gradle-command-action')
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':
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> {
2020-06-13 08:11:33 -04:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 07:44:30 -04:00
`${gradleVersionsBaseUrl}/current`
)
2020-06-13 08:11:33 -04:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
2019-09-21 10:01:53 -04:00
}
async function gradleReleaseCandidate(): Promise<string> {
2020-06-13 08:11:33 -04:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 07:44:30 -04:00
`${gradleVersionsBaseUrl}/release-candidate`
)
if (versionInfo && versionInfo.version && versionInfo.downloadUrl) {
2020-06-13 08:11:33 -04:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
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> {
2020-06-13 08:11:33 -04:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 07:44:30 -04:00
`${gradleVersionsBaseUrl}/nightly`
)
2020-06-13 08:11:33 -04:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
2019-09-21 10:01:53 -04:00
}
async function gradleReleaseNightly(): Promise<string> {
2020-06-13 08:11:33 -04:00
const versionInfo = await gradleVersionDeclaration(
2020-06-13 07:44:30 -04:00
`${gradleVersionsBaseUrl}/release-nightly`
)
2020-06-13 08:11:33 -04:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
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
}
2020-06-13 08:11:33 -04:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
2019-09-21 10:01:53 -04:00
}
2020-06-13 08:11:33 -04:00
async function gradleVersionDeclaration(
url: string
): Promise<GradleVersionInfo> {
return await httpGetGradleVersion(url)
2019-09-21 10:01:53 -04:00
}
2020-06-13 07:44:30 -04:00
async function findGradleVersionDeclaration(
version: string
2020-06-13 08:11:33 -04:00
): Promise<GradleVersionInfo | undefined> {
const gradleVersions = await httpGetGradleVersions(
`${gradleVersionsBaseUrl}/all`
)
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(version: string, url: string): Promise<string> {
2020-06-13 07:44:30 -04:00
const cachedInstall: string = toolCache.find('gradle', version)
2019-09-23 06:12:03 -04:00
if (cachedInstall.length > 0) {
2020-06-13 07:44:30 -04:00
const cachedExecutable = executableFrom(cachedInstall)
core.info(`Provisioned Gradle executable ${cachedExecutable}`)
return cachedExecutable
2019-09-21 10:01:53 -04:00
}
2020-06-14 06:28:17 -04:00
const home = os.homedir()
2020-06-13 07:44:30 -04:00
const tmpdir = path.join(home, 'gradle-provision-tmpdir')
const downloadsDir = path.join(tmpdir, 'downloads')
const installsDir = path.join(tmpdir, 'installs')
await io.mkdirP(downloadsDir)
await io.mkdirP(installsDir)
2019-09-21 10:01:53 -04:00
2020-06-13 07:44:30 -04:00
core.info(`Downloading ${url}`)
2019-09-21 10:01:53 -04:00
2020-06-13 07:44:30 -04:00
const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`)
await httpDownload(url, downloadPath)
core.info(
`Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}`
)
2019-09-21 10:01:53 -04:00
2020-06-13 07:44:30 -04:00
await extractZip(downloadPath, installsDir)
const installDir = path.join(installsDir, `gradle-${version}`)
core.info(`Extracted in ${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
toolCache.cacheDir(installDir, 'gradle', version)
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
}
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[]> {
return JSON.parse(await httpGetString(url))
}
async function httpGetString(url: string): Promise<string> {
2020-06-13 07:44:30 -04:00
const response = await httpc.get(url)
2020-06-13 08:11:33 -04:00
return response.readBody()
2019-09-23 06:12:03 -04:00
}
2020-06-13 07:54:27 -04:00
async function httpDownload(url: string, localPath: string): Promise<void> {
2020-06-13 08:37:12 -04:00
const response = await httpc.get(url)
return new Promise<void>(function (resolve, reject) {
const writeStream = fs.createWriteStream(localPath)
response.message
.pipe(writeStream)
.on('close', () => {
resolve()
})
.on('error', err => {
reject(err)
})
})
2019-09-21 10:01:53 -04:00
}
async function extractZip(zip: string, destination: string): Promise<void> {
return new Promise<void>(function (resolve, reject) {
fs.createReadStream(zip)
2020-06-13 07:44:30 -04:00
.pipe(unzip.Extract({path: destination}))
.on('close', () => {
resolve()
2019-09-21 10:01:53 -04:00
})
2020-06-13 07:44:30 -04:00
.on('error', err => {
2019-09-21 10:01:53 -04:00
reject(err)
2020-06-13 07:44:30 -04:00
})
})
2019-09-21 10:01:53 -04:00
}
2020-06-13 08:11:33 -04:00
interface GradleVersionInfo {
version: string
downloadUrl: string
}