gradle-build-action/src/provision.ts

175 lines
5.3 KiB
TypeScript
Raw Normal View History

2020-06-13 11:44:30 +00:00
import * as fs from 'fs'
import * as path from 'path'
import * as httpm from 'typed-rest-client/HttpClient'
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 14:01:53 +00:00
2020-06-13 11:44:30 +00:00
import * as gradlew from './gradlew'
2019-09-23 10:12:03 +00:00
2020-06-13 11:44:30 +00:00
const httpc = new httpm.HttpClient('eskatos/gradle-command-action')
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':
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`
)
2020-06-13 12:11:33 +00:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
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`
)
2020-06-13 12:11:33 +00:00
if (versionInfo) {
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
2019-09-21 14:01:53 +00:00
}
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`
)
2020-06-13 12:11:33 +00:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
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`
)
2020-06-13 12:11:33 +00:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
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
}
2020-06-13 12:11:33 +00:00
return provisionGradle(versionInfo.version, versionInfo.downloadUrl)
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(version: string, url: string): Promise<string> {
2020-06-13 11:44:30 +00:00
const cachedInstall: string = toolCache.find('gradle', version)
2019-09-23 10:12:03 +00:00
if (cachedInstall.length > 0) {
2020-06-13 11:44:30 +00:00
const cachedExecutable = executableFrom(cachedInstall)
core.info(`Provisioned Gradle executable ${cachedExecutable}`)
return cachedExecutable
2019-09-21 14:01:53 +00:00
}
2020-06-13 11:44:30 +00:00
const home = process.env['HOME'] || ''
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 14:01:53 +00:00
2020-06-13 11:44:30 +00:00
core.info(`Downloading ${url}`)
2019-09-21 14:01:53 +00:00
2020-06-13 11:44:30 +00: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 14:01:53 +00:00
2020-06-13 11:44:30 +00:00
await extractZip(downloadPath, installsDir)
const installDir = path.join(installsDir, `gradle-${version}`)
core.info(`Extracted in ${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
toolCache.cacheDir(installDir, 'gradle', version)
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
}
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> {
2020-06-13 11:44:30 +00:00
const response = await httpc.get(url)
2020-06-13 12:11:33 +00:00
return response.readBody()
2019-09-23 10:12:03 +00:00
}
2020-06-13 11:54:27 +00:00
async function httpDownload(url: string, localPath: string): Promise<void> {
2020-06-13 12:37:12 +00: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 14:01:53 +00:00
}
async function extractZip(zip: string, destination: string): Promise<void> {
return new Promise<void>(function (resolve, reject) {
fs.createReadStream(zip)
2020-06-13 11:44:30 +00:00
.pipe(unzip.Extract({path: destination}))
.on('close', () => {
resolve()
2019-09-21 14:01:53 +00:00
})
2020-06-13 11:44:30 +00:00
.on('error', err => {
2019-09-21 14:01:53 +00:00
reject(err)
2020-06-13 11:44:30 +00:00
})
})
2019-09-21 14:01:53 +00:00
}
2020-06-13 12:11:33 +00:00
interface GradleVersionInfo {
version: string
downloadUrl: string
}