From 45ef0226072514efa1672932744ce693b1fd5071 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sat, 27 Nov 2021 20:35:01 -0700 Subject: [PATCH] Always initialize Gradle User Home with init script The generated gradle.properties file and init script are not cached, so need to be generated even when restoring from cache. --- src/build-scan-capture.ts | 41 ---------------------- src/cache-base.ts | 3 -- src/cache-gradle-user-home.ts | 64 +++++++++++++++++++++++++++-------- src/caches.ts | 8 +++-- 4 files changed, 55 insertions(+), 61 deletions(-) delete mode 100644 src/build-scan-capture.ts diff --git a/src/build-scan-capture.ts b/src/build-scan-capture.ts deleted file mode 100644 index 2732b7f..0000000 --- a/src/build-scan-capture.ts +++ /dev/null @@ -1,41 +0,0 @@ -import fs from 'fs' - -export function writeBuildScanCaptureInitScript(initScript: string): void { - fs.writeFileSync( - initScript, - ` -import org.gradle.util.GradleVersion - -// Don't run against the included builds (if the main build has any). -def isTopLevelBuild = gradle.getParent() == null -if (isTopLevelBuild) { - def version = GradleVersion.current().baseVersion - def atLeastGradle4 = version >= GradleVersion.version("4.0") - def atLeastGradle6 = version >= GradleVersion.version("6.0") - - if (atLeastGradle6) { - settingsEvaluated { settings -> - if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) { - registerCallbacks(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject.name) - } - } - } else if (atLeastGradle4) { - projectsEvaluated { gradle -> - if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) { - registerCallbacks(gradle.rootProject.extensions["buildScan"], gradle.rootProject.name) - } - } - } -} - -def registerCallbacks(buildScanExtension, rootProjectName) { - buildScanExtension.with { - def scanFile = new File("gradle-build-scan.txt") - buildScanPublished { buildScan -> - scanFile.text = buildScan.buildScanUri - } - } -} -` - ) -} diff --git a/src/cache-base.ts b/src/cache-base.ts index be07516..5fa6914 100644 --- a/src/cache-base.ts +++ b/src/cache-base.ts @@ -147,7 +147,6 @@ export abstract class AbstractCache { if (!cacheResult) { core.info(`${this.cacheDescription} cache not found. Will initialize empty.`) - await this.initializeState() return } @@ -182,8 +181,6 @@ export abstract class AbstractCache { } } - protected async initializeState(): Promise {} - protected async afterRestore(_listener: CacheListener): Promise {} async save(listener: CacheListener): Promise { diff --git a/src/cache-gradle-user-home.ts b/src/cache-gradle-user-home.ts index 092a479..0854534 100644 --- a/src/cache-gradle-user-home.ts +++ b/src/cache-gradle-user-home.ts @@ -7,7 +7,6 @@ import * as exec from '@actions/exec' import {AbstractCache, CacheEntryListener, CacheListener} from './cache-base' import {getCacheKeyPrefix, hashFileNames, tryDelete} from './cache-utils' -import {writeBuildScanCaptureInitScript} from './build-scan-capture' const META_FILE_DIR = '.gradle-build-action' @@ -23,20 +22,9 @@ export class GradleUserHomeCache extends AbstractCache { this.gradleUserHome = this.determineGradleUserHome(rootDir) } - async initializeState(): Promise { - this.initializeGradleUserHome(this.gradleUserHome) - } - - private initializeGradleUserHome(gradleUserHome: string): void { - fs.mkdirSync(gradleUserHome, {recursive: true}) - - const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties') - this.debug(`Initializing gradle.properties to disable daemon: ${propertiesFile}`) - fs.writeFileSync(propertiesFile, 'org.gradle.daemon=false') - - const initScript = path.resolve(gradleUserHome, 'init.gradle') - this.debug(`Adding init script to capture build scans: ${initScript}`) - writeBuildScanCaptureInitScript(initScript) + init(): void { + this.debug(`Initializing Gradle User Home with properties and init script: ${this.gradleUserHome}`) + initializeGradleUserHome(this.gradleUserHome) } async afterRestore(listener: CacheListener): Promise { @@ -271,3 +259,49 @@ export class GradleUserHomeCache extends AbstractCache { core.info('-----------------------') } } + +function initializeGradleUserHome(gradleUserHome: string): void { + fs.mkdirSync(gradleUserHome, {recursive: true}) + + const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties') + fs.writeFileSync(propertiesFile, 'org.gradle.daemon=false') + + const initScript = path.resolve(gradleUserHome, 'init.gradle') + fs.writeFileSync( + initScript, + ` +import org.gradle.util.GradleVersion + +// Don't run against the included builds (if the main build has any). +def isTopLevelBuild = gradle.getParent() == null +if (isTopLevelBuild) { + def version = GradleVersion.current().baseVersion + def atLeastGradle4 = version >= GradleVersion.version("4.0") + def atLeastGradle6 = version >= GradleVersion.version("6.0") + + if (atLeastGradle6) { + settingsEvaluated { settings -> + if (settings.pluginManager.hasPlugin("com.gradle.enterprise")) { + registerCallbacks(settings.extensions["gradleEnterprise"].buildScan, settings.rootProject.name) + } + } + } else if (atLeastGradle4) { + projectsEvaluated { gradle -> + if (gradle.rootProject.pluginManager.hasPlugin("com.gradle.build-scan")) { + registerCallbacks(gradle.rootProject.extensions["buildScan"], gradle.rootProject.name) + } + } + } +} + +def registerCallbacks(buildScanExtension, rootProjectName) { + buildScanExtension.with { + def scanFile = new File("gradle-build-scan.txt") + buildScanPublished { buildScan -> + scanFile.text = buildScan.buildScanUri + } + } +} +` + ) +} diff --git a/src/caches.ts b/src/caches.ts index 5555549..b968f0f 100644 --- a/src/caches.ts +++ b/src/caches.ts @@ -8,6 +8,11 @@ const BUILD_ROOT_DIR = 'BUILD_ROOT_DIR' const CACHE_LISTENER = 'CACHE_LISTENER' export async function restore(buildRootDirectory: string): Promise { + const gradleUserHomeCache = new GradleUserHomeCache(buildRootDirectory) + const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory) + + gradleUserHomeCache.init() + if (isCacheDisabled()) { core.info('Cache is disabled: will not restore state from previous builds.') return @@ -17,9 +22,8 @@ export async function restore(buildRootDirectory: string): Promise { core.saveState(BUILD_ROOT_DIR, buildRootDirectory) const cacheListener = new CacheListener() - await new GradleUserHomeCache(buildRootDirectory).restore(cacheListener) + await gradleUserHomeCache.restore(cacheListener) - const projectDotGradleCache = new ProjectDotGradleCache(buildRootDirectory) if (cacheListener.fullyRestored) { // Only restore the configuration-cache if the Gradle Home is fully restored await projectDotGradleCache.restore(cacheListener)