From e644288a4210d07fb7f966eda19e9b6a7f60518f Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sun, 5 Jun 2022 08:34:07 -0600 Subject: [PATCH] Use build-results file for root project dirs Instead of using a separate mechanism and init script, reuse the information captured in the build-results file. --- src/cache-base.ts | 8 +- src/cache-extract-entries.ts | 14 ++-- src/job-summary.ts | 4 +- .../project-root-capture.init.gradle | 10 --- .../project-root-capture.plugin.groovy | 40 ---------- .../TestProjectRootCapture.groovy | 77 ------------------- 6 files changed, 8 insertions(+), 145 deletions(-) delete mode 100644 src/resources/project-root-capture.init.gradle delete mode 100644 src/resources/project-root-capture.plugin.groovy delete mode 100644 test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestProjectRootCapture.groovy diff --git a/src/cache-base.ts b/src/cache-base.ts index 43ba3a0..cef16db 100644 --- a/src/cache-base.ts +++ b/src/cache-base.ts @@ -9,7 +9,6 @@ import {ConfigurationCacheEntryExtractor, GradleHomeEntryExtractor} from './cach const RESTORED_CACHE_KEY_KEY = 'restored-cache-key' export const META_FILE_DIR = '.gradle-build-action' -export const PROJECT_ROOTS_FILE = 'project-roots.txt' const INCLUDE_PATHS_PARAMETER = 'gradle-home-cache-includes' const EXCLUDE_PATHS_PARAMETER = 'gradle-home-cache-excludes' @@ -170,12 +169,7 @@ export class GradleStateCache { const propertiesFile = path.resolve(gradleUserHome, 'gradle.properties') fs.appendFileSync(propertiesFile, 'org.gradle.daemon=false') - const initScriptFilenames = [ - 'build-result-capture.init.gradle', - 'build-result-capture-service.plugin.groovy', - 'project-root-capture.init.gradle', - 'project-root-capture.plugin.groovy' - ] + const initScriptFilenames = ['build-result-capture.init.gradle', 'build-result-capture-service.plugin.groovy'] for (const initScriptFilename of initScriptFilenames) { const initScriptContent = this.readResourceAsString(initScriptFilename) const initScriptPath = path.resolve(initScriptsDir, initScriptFilename) diff --git a/src/cache-extract-entries.ts b/src/cache-extract-entries.ts index fbe7072..e9d5c58 100644 --- a/src/cache-extract-entries.ts +++ b/src/cache-extract-entries.ts @@ -3,7 +3,7 @@ import fs from 'fs' import * as core from '@actions/core' import * as glob from '@actions/glob' -import {META_FILE_DIR, PROJECT_ROOTS_FILE} from './cache-base' +import {META_FILE_DIR} from './cache-base' import {CacheEntryListener, CacheListener} from './cache-reporting' import { cacheDebug, @@ -14,6 +14,7 @@ import { saveCache, tryDelete } from './cache-utils' +import {loadBuildResults} from './job-summary' const SKIP_RESTORE_VAR = 'GRADLE_BUILD_ACTION_SKIP_RESTORE' @@ -387,13 +388,8 @@ export class ConfigurationCacheEntryExtractor extends AbstractEntryExtractor { * set of project roots, to allow saving of configuration-cache entries for each. */ private getProjectRoots(): string[] { - const projectList = path.resolve(process.env['RUNNER_TEMP']!, PROJECT_ROOTS_FILE) - if (!fs.existsSync(projectList)) { - core.info(`Missing project list file ${projectList}`) - return [] - } - const projectRoots = fs.readFileSync(projectList, 'utf-8') - core.info(`Found project roots '${projectRoots}' in ${projectList}`) - return projectRoots.trim().split('\n') + const buildResults = loadBuildResults() + const projectRootDirs = buildResults.map(x => x.rootProjectDir) + return [...new Set(projectRootDirs)] // Remove duplicates } } diff --git a/src/job-summary.ts b/src/job-summary.ts index 2c5b5fe..dbed552 100644 --- a/src/job-summary.ts +++ b/src/job-summary.ts @@ -3,7 +3,7 @@ import fs from 'fs' import path from 'path' import {logCachingReport, CacheListener} from './cache-reporting' -interface BuildResult { +export interface BuildResult { get rootProjectName(): string get rootProjectDir(): string get requestedTasks(): string @@ -28,7 +28,7 @@ export function writeJobSummary(cacheListener: CacheListener): void { core.summary.write() } -function loadBuildResults(): BuildResult[] { +export function loadBuildResults(): BuildResult[] { const buildResultsDir = path.resolve(process.env['RUNNER_TEMP']!, '.build-results') if (!fs.existsSync(buildResultsDir)) { return [] diff --git a/src/resources/project-root-capture.init.gradle b/src/resources/project-root-capture.init.gradle deleted file mode 100644 index ea3e3d8..0000000 --- a/src/resources/project-root-capture.init.gradle +++ /dev/null @@ -1,10 +0,0 @@ -import org.gradle.util.GradleVersion - -// Only run against root build. Do not run against included builds. -def isTopLevelBuild = gradle.getParent() == null -// Only record configuration-cache entries for Gradle 7+ -def isAtLeastGradle7 = GradleVersion.current() >= GradleVersion.version('7.0') - -if (isTopLevelBuild && isAtLeastGradle7) { - apply from: 'project-root-capture.plugin.groovy' -} diff --git a/src/resources/project-root-capture.plugin.groovy b/src/resources/project-root-capture.plugin.groovy deleted file mode 100644 index 0886c64..0000000 --- a/src/resources/project-root-capture.plugin.groovy +++ /dev/null @@ -1,40 +0,0 @@ - -/* - * Capture the build root directory for each executed Gradle build. - * This is used to save/restore configuration-cache files, so: - * - The implementation only makes sense if it's config-cache compatible - * - We only need to support Gradle 7+ - */ - -import org.gradle.tooling.events.* - -settingsEvaluated { settings -> - def rootDir = settings.rootDir.absolutePath - def rootListLocation = new File(System.getenv("RUNNER_TEMP"), "project-roots.txt").absolutePath - - def projectTracker = gradle.sharedServices.registerIfAbsent("gradle-build-action-projectRootTracker", ProjectTracker, { spec -> - spec.getParameters().getRootDir().set(rootDir); - spec.getParameters().getRootListLocation().set(rootListLocation); - }) - - gradle.services.get(BuildEventsListenerRegistry).onTaskCompletion(projectTracker) -} - -abstract class ProjectTracker implements BuildService, OperationCompletionListener, AutoCloseable { - interface Params extends BuildServiceParameters { - Property getRootDir(); - Property getRootListLocation(); - } - - public void onFinish(FinishEvent finishEvent) {} - - @Override - public void close() { - def rootDir = getParameters().getRootDir().get() - def rootDirEntry = rootDir + '\n' - def rootListFile = new File(getParameters().getRootListLocation().get()) - if (!rootListFile.exists() || !rootListFile.text.contains(rootDirEntry)) { - rootListFile << rootDirEntry - } - } -} \ No newline at end of file diff --git a/test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestProjectRootCapture.groovy b/test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestProjectRootCapture.groovy deleted file mode 100644 index c55aa67..0000000 --- a/test/test-init-scripts/src/test/groovy/com/gradle/gradlebuildaction/TestProjectRootCapture.groovy +++ /dev/null @@ -1,77 +0,0 @@ -package com.gradle.gradlebuildaction - -import static org.junit.Assume.assumeTrue - -class TestProjectRootCapture extends BaseInitScriptTest { - def initScript = 'project-root-capture.init.gradle' - - def "captures project root on #testGradleVersion"() { - assumeTrue testGradleVersion.compatibleWithCurrentJvm - - when: - run(['help'], initScript, testGradleVersion.gradleVersion) - - then: - assertCapturesProjectRoot() - - where: - testGradleVersion << CONFIGURATION_CACHE_VERSIONS - } - - def "captures project root on #testGradleVersion when build fails"() { - assumeTrue testGradleVersion.compatibleWithCurrentJvm - - addFailingTaskToBuild() - - when: - runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion) - - then: - assertCapturesProjectRoot() - - where: - testGradleVersion << CONFIGURATION_CACHE_VERSIONS - } - - def "captures project root on #testGradleVersion with --configuration-cache"() { - assumeTrue testGradleVersion.compatibleWithCurrentJvm - - when: - run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion) - - then: - assertCapturesProjectRoot() - assert projectRootList.delete() - - when: - run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion) - - then: - assertCapturesProjectRoot() - - where: - testGradleVersion << CONFIGURATION_CACHE_VERSIONS - } - - def "has no effect on #testVersion"() { - assumeTrue testVersion.compatibleWithCurrentJvm - - when: - run(['help'], initScript, testVersion.gradleVersion) - - then: - assert !projectRootList.exists() - - where: - testVersion << (ALL_VERSIONS - CONFIGURATION_CACHE_VERSIONS) - } - - private void assertCapturesProjectRoot() { - assert projectRootList.exists() - assert new File(projectRootList.text.trim()).canonicalPath == testProjectDir.canonicalPath - } - - private File getProjectRootList() { - new File(testProjectDir, 'project-roots.txt') - } -}