From 3335c16182c0c4318cdf9f840322c5442afa9163 Mon Sep 17 00:00:00 2001 From: daz Date: Mon, 8 Apr 2024 14:04:29 -0600 Subject: [PATCH] Move execution-related code into subpackage --- sources/src/dependency-graph.ts | 15 ++++---- sources/src/dependency-submission/main.ts | 11 ++---- sources/src/execution.ts | 17 --------- sources/src/execution/gradle.ts | 42 +++++++++++++++++++++++ sources/src/{ => execution}/gradlew.ts | 0 sources/src/{ => execution}/provision.ts | 6 ++-- sources/src/input-params.ts | 4 +++ sources/src/repository-layout.ts | 16 --------- sources/src/setup-gradle.ts | 5 ++- sources/src/setup-gradle/main.ts | 13 ++----- 10 files changed, 62 insertions(+), 67 deletions(-) delete mode 100644 sources/src/execution.ts create mode 100644 sources/src/execution/gradle.ts rename sources/src/{ => execution}/gradlew.ts (100%) rename sources/src/{ => execution}/provision.ts (97%) delete mode 100644 sources/src/repository-layout.ts diff --git a/sources/src/dependency-graph.ts b/sources/src/dependency-graph.ts index bfa0e4a..c7bc4e6 100644 --- a/sources/src/dependency-graph.ts +++ b/sources/src/dependency-graph.ts @@ -9,9 +9,8 @@ import type {PullRequestEvent} from '@octokit/webhooks-types' import * as path from 'path' import fs from 'fs' -import * as layout from './repository-layout' import {PostActionJobFailure} from './errors' -import {DependencyGraphConfig, DependencyGraphOption, getGithubToken} from './input-params' +import {DependencyGraphConfig, DependencyGraphOption, getGithubToken, getWorkspaceDirectory} from './input-params' const DEPENDENCY_GRAPH_PREFIX = 'dependency-graph_' @@ -34,10 +33,10 @@ export async function setup(config: DependencyGraphConfig): Promise { maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_JOB_ID', github.context.runId) maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_REF', github.context.ref) maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_SHA', getShaFromContext()) - maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_WORKSPACE', layout.workspaceDirectory()) + maybeExportVariable('GITHUB_DEPENDENCY_GRAPH_WORKSPACE', getWorkspaceDirectory()) maybeExportVariable( 'DEPENDENCY_GRAPH_REPORT_DIR', - path.resolve(layout.workspaceDirectory(), 'dependency-graph-reports') + path.resolve(getWorkspaceDirectory(), 'dependency-graph-reports') ) // To clear the dependency graph, we generate an empty graph by excluding all projects and configurations @@ -74,7 +73,7 @@ export async function complete(config: DependencyGraphConfig): Promise { } async function findGeneratedDependencyGraphFiles(): Promise { - const workspaceDirectory = layout.workspaceDirectory() + const workspaceDirectory = getWorkspaceDirectory() return await findDependencyGraphFiles(workspaceDirectory) } @@ -85,7 +84,7 @@ async function uploadDependencyGraphs(dependencyGraphFiles: string[], config: De return } - const workspaceDirectory = layout.workspaceDirectory() + const workspaceDirectory = getWorkspaceDirectory() const artifactClient = new DefaultArtifactClient() for (const dependencyGraphFile of dependencyGraphFiles) { @@ -157,7 +156,7 @@ async function submitDependencyGraphFile(jsonFile: string): Promise { } async function downloadDependencyGraphs(): Promise { - const workspaceDirectory = layout.workspaceDirectory() + const workspaceDirectory = getWorkspaceDirectory() const findBy = github.context.payload.workflow_run ? { @@ -220,7 +219,7 @@ function getOctokit(): InstanceType { } function getRelativePathFromWorkspace(file: string): string { - const workspaceDirectory = layout.workspaceDirectory() + const workspaceDirectory = getWorkspaceDirectory() return path.relative(workspaceDirectory, file) } diff --git a/sources/src/dependency-submission/main.ts b/sources/src/dependency-submission/main.ts index 0a1007f..e043c66 100644 --- a/sources/src/dependency-submission/main.ts +++ b/sources/src/dependency-submission/main.ts @@ -1,9 +1,7 @@ import * as core from '@actions/core' import * as setupGradle from '../setup-gradle' -import * as execution from '../execution' -import * as provisioner from '../provision' -import * as layout from '../repository-layout' +import * as gradle from '../execution/gradle' import * as dependencyGraph from '../dependency-graph' import {parseArgsStringToArgv} from 'string-argv' @@ -26,9 +24,6 @@ export async function run(): Promise { return } - // Download and install Gradle if required - const executable = await provisioner.provisionGradle() - // Only execute if arguments have been provided const additionalArgs = core.getInput('additional-arguments') const executionArgs = ` @@ -38,10 +33,8 @@ export async function run(): Promise { :ForceDependencyResolutionPlugin_resolveAllDependencies ${additionalArgs} ` - const args: string[] = parseArgsStringToArgv(executionArgs) - const buildRootDirectory = layout.buildRootDirectory() - await execution.executeGradleBuild(executable, buildRootDirectory, args) + await gradle.provisionAndMaybeExecute(args) await dependencyGraph.complete(config) } catch (error) { diff --git a/sources/src/execution.ts b/sources/src/execution.ts deleted file mode 100644 index a024b48..0000000 --- a/sources/src/execution.ts +++ /dev/null @@ -1,17 +0,0 @@ -import * as core from '@actions/core' -import * as exec from '@actions/exec' -import * as gradlew from './gradlew' - -export async function executeGradleBuild(executable: string | undefined, root: string, args: string[]): Promise { - // Use the provided executable, or look for a Gradle wrapper script to run - const toExecute = executable ?? gradlew.gradleWrapperScript(root) - - const status: number = await exec.exec(toExecute, args, { - cwd: root, - ignoreReturnCode: true - }) - - if (status !== 0) { - core.setFailed(`Gradle build failed: see console output for details`) - } -} diff --git a/sources/src/execution/gradle.ts b/sources/src/execution/gradle.ts new file mode 100644 index 0000000..0ec1108 --- /dev/null +++ b/sources/src/execution/gradle.ts @@ -0,0 +1,42 @@ +import * as core from '@actions/core' +import * as exec from '@actions/exec' +import * as path from 'path' + +import * as params from '../input-params' +import * as provisioner from './provision' +import * as gradlew from './gradlew' +import {getWorkspaceDirectory} from '../input-params' + +export async function provisionAndMaybeExecute(args: string[]): Promise { + // Download and install Gradle if required + const executable = await provisioner.provisionGradle() + + // Only execute if arguments have been provided + if (args.length > 0) { + await executeGradleBuild(executable, buildRootDirectory(), args) + } +} + +async function executeGradleBuild(executable: string | undefined, root: string, args: string[]): Promise { + // Use the provided executable, or look for a Gradle wrapper script to run + const toExecute = executable ?? gradlew.gradleWrapperScript(root) + + const status: number = await exec.exec(toExecute, args, { + cwd: root, + ignoreReturnCode: true + }) + + if (status !== 0) { + core.setFailed(`Gradle build failed: see console output for details`) + } +} + +function buildRootDirectory(): string { + const baseDirectory = getWorkspaceDirectory() + const buildRootDirectoryInput = params.getBuildRootDirectory() + const resolvedBuildRootDirectory = + buildRootDirectoryInput === '' + ? path.resolve(baseDirectory) + : path.resolve(baseDirectory, buildRootDirectoryInput) + return resolvedBuildRootDirectory +} diff --git a/sources/src/gradlew.ts b/sources/src/execution/gradlew.ts similarity index 100% rename from sources/src/gradlew.ts rename to sources/src/execution/gradlew.ts diff --git a/sources/src/provision.ts b/sources/src/execution/provision.ts similarity index 97% rename from sources/src/provision.ts rename to sources/src/execution/provision.ts index 0fd0c47..0637895 100644 --- a/sources/src/provision.ts +++ b/sources/src/execution/provision.ts @@ -7,9 +7,9 @@ import * as cache from '@actions/cache' import * as toolCache from '@actions/tool-cache' import * as gradlew from './gradlew' -import * as params from './input-params' -import {handleCacheFailure} from './caching/cache-utils' -import {CacheConfig} from './input-params' +import * as params from '../input-params' +import {handleCacheFailure} from '../caching/cache-utils' +import {CacheConfig} from '../input-params' const gradleVersionsBaseUrl = 'https://services.gradle.org/versions' diff --git a/sources/src/input-params.ts b/sources/src/input-params.ts index 6abd784..393dfcc 100644 --- a/sources/src/input-params.ts +++ b/sources/src/input-params.ts @@ -240,6 +240,10 @@ export function getGithubToken(): string { return core.getInput('github-token', {required: true}) } +export function getWorkspaceDirectory(): string { + return process.env[`GITHUB_WORKSPACE`] || '' +} + export function parseNumericInput(paramName: string, paramValue: string, paramDefault: number): number { if (paramValue.length === 0) { return paramDefault diff --git a/sources/src/repository-layout.ts b/sources/src/repository-layout.ts deleted file mode 100644 index d021828..0000000 --- a/sources/src/repository-layout.ts +++ /dev/null @@ -1,16 +0,0 @@ -import * as params from './input-params' -import * as path from 'path' - -export function workspaceDirectory(): string { - return process.env[`GITHUB_WORKSPACE`] || '' -} - -export function buildRootDirectory(): string { - const baseDirectory = workspaceDirectory() - const buildRootDirectoryInput = params.getBuildRootDirectory() - const resolvedBuildRootDirectory = - buildRootDirectoryInput === '' - ? path.resolve(baseDirectory) - : path.resolve(baseDirectory, buildRootDirectoryInput) - return resolvedBuildRootDirectory -} diff --git a/sources/src/setup-gradle.ts b/sources/src/setup-gradle.ts index ce9a989..b138e56 100644 --- a/sources/src/setup-gradle.ts +++ b/sources/src/setup-gradle.ts @@ -3,14 +3,13 @@ import * as exec from '@actions/exec' import * as path from 'path' import * as os from 'os' import * as caches from './caching/caches' -import * as layout from './repository-layout' import * as jobSummary from './job-summary' import * as buildScan from './build-scan' import {loadBuildResults} from './build-results' import {CacheListener, generateCachingReport} from './caching/cache-reporting' import {DaemonController} from './daemon-controller' -import {BuildScanConfig, CacheConfig, SummaryConfig} from './input-params' +import {BuildScanConfig, CacheConfig, SummaryConfig, getWorkspaceDirectory} from './input-params' const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED' const USER_HOME = 'USER_HOME' @@ -72,7 +71,7 @@ export async function complete(cacheConfig: CacheConfig, summaryConfig: SummaryC async function determineGradleUserHome(): Promise { const customGradleUserHome = process.env['GRADLE_USER_HOME'] if (customGradleUserHome) { - const rootDir = layout.workspaceDirectory() + const rootDir = getWorkspaceDirectory() return path.resolve(rootDir, customGradleUserHome) } diff --git a/sources/src/setup-gradle/main.ts b/sources/src/setup-gradle/main.ts index 50c7d90..6fca084 100644 --- a/sources/src/setup-gradle/main.ts +++ b/sources/src/setup-gradle/main.ts @@ -1,9 +1,7 @@ import * as core from '@actions/core' import * as setupGradle from '../setup-gradle' -import * as execution from '../execution' -import * as provisioner from '../provision' -import * as layout from '../repository-layout' +import * as gradle from '../execution/gradle' import * as dependencyGraph from '../dependency-graph' import {BuildScanConfig, CacheConfig, DependencyGraphConfig, getArguments} from '../input-params' @@ -18,15 +16,8 @@ export async function run(): Promise { // Configure the dependency graph submission await dependencyGraph.setup(new DependencyGraphConfig()) - // Download and install Gradle if required - const executable = await provisioner.provisionGradle() - - // Only execute if arguments have been provided const args: string[] = getArguments() - if (args.length > 0) { - const buildRootDirectory = layout.buildRootDirectory() - await execution.executeGradleBuild(executable, buildRootDirectory, args) - } + await gradle.provisionAndMaybeExecute(args) } catch (error) { core.setFailed(String(error)) if (error instanceof Error && error.stack) {