gradle-build-action/src/main.ts

76 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-06-13 19:44:30 +08:00
import * as core from '@actions/core'
import * as path from 'path'
import * as os from 'os'
2020-06-13 19:44:30 +08:00
import {parseArgsStringToArgv} from 'string-argv'
2019-09-21 22:01:53 +08:00
import * as caches from './caches'
2020-06-13 19:44:30 +08:00
import * as execution from './execution'
import * as provision from './provision'
2019-09-21 22:01:53 +08:00
/**
* The main entry point for the action, called by Github Actions for the step.
*/
2020-06-13 19:54:27 +08:00
export async function run(): Promise<void> {
try {
const workspaceDirectory = process.env[`GITHUB_WORKSPACE`] || ''
const buildRootDirectory = resolveBuildRootDirectory(workspaceDirectory)
const gradleUserHome = determineGradleUserHome(buildRootDirectory)
await caches.restore(gradleUserHome)
2019-09-21 22:01:53 +08:00
const executable = await provisionGradle(workspaceDirectory)
// executable will be undefined if using Gradle wrapper
if (executable !== undefined) {
core.addPath(path.dirname(executable))
}
2019-09-21 22:01:53 +08:00
// Only execute if arguments have been provided
const args: string[] = parseCommandLineArguments()
if (args.length > 0) {
await execution.executeGradleBuild(executable, buildRootDirectory, args)
2019-09-23 18:11:18 +08:00
}
2019-09-21 22:01:53 +08:00
} catch (error) {
core.setFailed(String(error))
if (error instanceof Error && error.stack) {
core.info(error.stack)
}
2019-09-21 22:01:53 +08:00
}
2019-09-21 05:06:59 +08:00
}
2020-06-13 19:44:30 +08:00
run()
2019-09-21 22:01:53 +08:00
async function provisionGradle(workspaceDirectory: string): Promise<string | undefined> {
const gradleVersion = core.getInput('gradle-version')
if (gradleVersion !== '' && gradleVersion !== 'wrapper') {
2019-10-28 20:30:27 +08:00
return path.resolve(await provision.gradleVersion(gradleVersion))
2019-09-21 22:01:53 +08:00
}
const gradleExecutable = core.getInput('gradle-executable')
if (gradleExecutable !== '') {
return path.resolve(workspaceDirectory, gradleExecutable)
2019-09-21 22:01:53 +08:00
}
return undefined
2019-09-21 22:01:53 +08:00
}
function resolveBuildRootDirectory(baseDirectory: string): string {
const buildRootDirectory = core.getInput('build-root-directory')
2020-06-13 22:02:48 +08:00
const resolvedBuildRootDirectory =
buildRootDirectory === '' ? path.resolve(baseDirectory) : path.resolve(baseDirectory, buildRootDirectory)
2020-06-13 22:02:48 +08:00
return resolvedBuildRootDirectory
2019-09-21 22:01:53 +08:00
}
function determineGradleUserHome(rootDir: string): string {
const customGradleUserHome = process.env['GRADLE_USER_HOME']
if (customGradleUserHome) {
return path.resolve(rootDir, customGradleUserHome)
}
return path.resolve(os.homedir(), '.gradle')
}
2019-09-21 22:01:53 +08:00
function parseCommandLineArguments(): string[] {
const input = core.getInput('arguments')
return parseArgsStringToArgv(input)
2019-09-21 22:01:53 +08:00
}