generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 86
/
setup-gradle.ts
121 lines (100 loc) · 4.46 KB
/
setup-gradle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import * as caches from './caches'
import {CacheListener} from './cache-reporting'
import {BuildResult, loadBuildResults, logJobSummary, writeJobSummary} from './job-summary'
const GRADLE_SETUP_VAR = 'GRADLE_BUILD_ACTION_SETUP_COMPLETED'
const GRADLE_USER_HOME = 'GRADLE_USER_HOME'
const CACHE_LISTENER = 'CACHE_LISTENER'
const JOB_SUMMARY_ENABLED_PARAMETER = 'generate-job-summary'
function shouldGenerateJobSummary(): boolean {
// Check if Job Summary is supported on this platform
if (!process.env[SUMMARY_ENV_VAR]) {
return false
}
return core.getBooleanInput(JOB_SUMMARY_ENABLED_PARAMETER)
}
export async function setup(buildRootDirectory: string): Promise<void> {
const gradleUserHome = await determineGradleUserHome(buildRootDirectory)
// Bypass setup on all but first action step in workflow.
if (process.env[GRADLE_SETUP_VAR]) {
core.info('Gradle setup only performed on first gradle-build-action step in workflow.')
return
}
// Record setup complete: visible to all subsequent actions and prevents duplicate setup
core.exportVariable(GRADLE_SETUP_VAR, true)
// Record setup complete: visible in post-action, to control action completion
core.saveState(GRADLE_SETUP_VAR, true)
// Save the Gradle User Home for use in the post-action step.
core.saveState(GRADLE_USER_HOME, gradleUserHome)
const cacheListener = new CacheListener()
await caches.restore(gradleUserHome, cacheListener)
core.saveState(CACHE_LISTENER, cacheListener.stringify())
}
export async function complete(): Promise<void> {
if (!core.getState(GRADLE_SETUP_VAR)) {
core.info('Gradle setup post-action only performed for first gradle-build-action step in workflow.')
return
}
const buildResults = loadBuildResults()
core.info('Stopping all Gradle daemons')
await stopAllDaemons(getUniqueGradleHomes(buildResults))
core.info('In final post-action step, saving state and writing summary')
const cacheListener: CacheListener = CacheListener.rehydrate(core.getState(CACHE_LISTENER))
const gradleUserHome = core.getState(GRADLE_USER_HOME)
await caches.save(gradleUserHome, cacheListener)
if (shouldGenerateJobSummary()) {
await writeJobSummary(buildResults, cacheListener)
} else {
logJobSummary(buildResults, cacheListener)
}
}
async function determineGradleUserHome(rootDir: string): Promise<string> {
const customGradleUserHome = process.env['GRADLE_USER_HOME']
if (customGradleUserHome) {
return path.resolve(rootDir, customGradleUserHome)
}
return path.resolve(await determineUserHome(), '.gradle')
}
/**
* Different values can be returned by os.homedir() in Javascript and System.getProperty('user.home') in Java.
* In order to determine the correct Gradle User Home, we ask Java for the user home instead of using os.homedir().
*/
async function determineUserHome(): Promise<string> {
const output = await exec.getExecOutput('java', ['-XshowSettings:properties', '-version'], {silent: true})
const regex = /user\.home = (\S*)/i
const found = output.stderr.match(regex)
if (found == null || found.length <= 1) {
core.info('Could not determine user.home from java -version output. Using os.homedir().')
return os.homedir()
}
const userHome = found[1]
core.debug(`Determined user.home from java -version output: '${userHome}'`)
return userHome
}
function getUniqueGradleHomes(buildResults: BuildResult[]): string[] {
const gradleHomes = buildResults.map(buildResult => buildResult.gradleHomeDir)
return Array.from(new Set(gradleHomes))
}
async function stopAllDaemons(gradleHomes: string[]): Promise<void> {
const executions: Promise<number>[] = []
const args = ['--stop']
for (const gradleHome of gradleHomes) {
const executable = path.resolve(gradleHome, 'bin', 'gradle')
if (!fs.existsSync(executable)) {
core.warning(`Gradle executable not found at ${executable}. Could not stop Gradle daemons.`)
continue
}
core.info(`Stopping Gradle daemons in ${gradleHome}`)
executions.push(
exec.exec(executable, args, {
ignoreReturnCode: true
})
)
}
await Promise.all(executions)
}