Move execution-related code into subpackage

This commit is contained in:
daz 2024-04-08 14:04:29 -06:00
parent 528fe78d31
commit 3335c16182
No known key found for this signature in database
10 changed files with 62 additions and 67 deletions

View file

@ -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<void> {
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<void> {
}
async function findGeneratedDependencyGraphFiles(): Promise<string[]> {
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<void> {
}
async function downloadDependencyGraphs(): Promise<string[]> {
const workspaceDirectory = layout.workspaceDirectory()
const workspaceDirectory = getWorkspaceDirectory()
const findBy = github.context.payload.workflow_run
? {
@ -220,7 +219,7 @@ function getOctokit(): InstanceType<typeof GitHub> {
}
function getRelativePathFromWorkspace(file: string): string {
const workspaceDirectory = layout.workspaceDirectory()
const workspaceDirectory = getWorkspaceDirectory()
return path.relative(workspaceDirectory, file)
}

View file

@ -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<void> {
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<void> {
: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) {

View file

@ -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<void> {
// 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`)
}
}

View file

@ -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<void> {
// 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<void> {
// 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
}

View file

@ -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'

View file

@ -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

View file

@ -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
}

View file

@ -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<string> {
const customGradleUserHome = process.env['GRADLE_USER_HOME']
if (customGradleUserHome) {
const rootDir = layout.workspaceDirectory()
const rootDir = getWorkspaceDirectory()
return path.resolve(rootDir, customGradleUserHome)
}

View file

@ -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<void> {
// 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) {