diff --git a/src/execution.ts b/src/execution.ts index 1920763..a215595 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -1,8 +1,10 @@ +import * as core from '@actions/core' import * as exec from '@actions/exec' import fs from 'fs' import path from 'path' +import * as gradlew from './gradlew' -export async function execute(executable: string, root: string, args: string[]): Promise { +export async function executeGradleBuild(executable: string | undefined, root: string, args: string[]): Promise { let buildScanUrl: string | undefined const buildScanFile = path.resolve(root, 'gradle-build-scan.txt') @@ -10,7 +12,9 @@ export async function execute(executable: string, root: string, args: string[]): fs.unlinkSync(buildScanFile) } - const status: number = await exec.exec(executable, args, { + // Use the provided executable, or look for a Gradle wrapper script to run + const toExecute = executable ?? gradlew.locateGradleWrapperScript(root) + const status: number = await exec.exec(toExecute, args, { cwd: root, ignoreReturnCode: true }) @@ -19,14 +23,11 @@ export async function execute(executable: string, root: string, args: string[]): buildScanUrl = fs.readFileSync(buildScanFile, 'utf-8') } - return new BuildResultImpl(status, buildScanUrl) -} - -export interface BuildResult { - readonly status: number - readonly buildScanUrl?: string -} - -class BuildResultImpl implements BuildResult { - constructor(readonly status: number, readonly buildScanUrl?: string) {} + if (status !== 0) { + if (buildScanUrl) { + core.setFailed(`Gradle build failed: ${buildScanUrl}`) + } else { + core.setFailed(`Gradle build failed: process exited with status ${status}`) + } + } } diff --git a/src/main.ts b/src/main.ts index f6edcc8..f82d3f2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,6 @@ import {parseArgsStringToArgv} from 'string-argv' import * as caches from './caches' import * as execution from './execution' -import * as gradlew from './gradlew' import * as provision from './provision' /** @@ -19,20 +18,16 @@ export async function run(): Promise { await caches.restore(gradleUserHome) + const executable = await provisionGradle(workspaceDirectory) + // executable will be undefined if using Gradle wrapper + if (executable !== undefined) { + core.addPath(path.dirname(executable)) + } + + // Only execute if arguments have been provided const args: string[] = parseCommandLineArguments() - - const result = await execution.execute( - await resolveGradleExecutable(workspaceDirectory, buildRootDirectory), - buildRootDirectory, - args - ) - - if (result.status !== 0) { - if (result.buildScanUrl) { - core.setFailed(`Gradle build failed: ${result.buildScanUrl}`) - } else { - core.setFailed(`Gradle build failed: process exited with status ${result.status}`) - } + if (args.length > 0) { + await execution.executeGradleBuild(executable, buildRootDirectory, args) } } catch (error) { core.setFailed(String(error)) @@ -44,7 +39,7 @@ export async function run(): Promise { run() -async function resolveGradleExecutable(workspaceDirectory: string, buildRootDirectory: string): Promise { +async function provisionGradle(workspaceDirectory: string): Promise { const gradleVersion = core.getInput('gradle-version') if (gradleVersion !== '' && gradleVersion !== 'wrapper') { return path.resolve(await provision.gradleVersion(gradleVersion)) @@ -55,7 +50,7 @@ async function resolveGradleExecutable(workspaceDirectory: string, buildRootDire return path.resolve(workspaceDirectory, gradleExecutable) } - return gradlew.locateGradleWrapperScript(buildRootDirectory) + return undefined } function resolveBuildRootDirectory(baseDirectory: string): string {