From 59949a07b8dac9e32cf960c4c4d63a2a4fd3156d Mon Sep 17 00:00:00 2001 From: daz Date: Wed, 3 Jan 2024 21:00:07 -0700 Subject: [PATCH] Refactor --- src/cache-base.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/cache-base.ts b/src/cache-base.ts index 467dde5b..2cbe52e4 100644 --- a/src/cache-base.ts +++ b/src/cache-base.ts @@ -188,9 +188,16 @@ export class GradleStateCache { const actionCacheDir = path.resolve(this.gradleUserHome, '.gradle-build-action') fs.mkdirSync(actionCacheDir, {recursive: true}) + this.copyInitScripts() + + // Copy the default toolchain definitions to `~/.m2/toolchains.xml` + this.registerToolchains() + } + + private copyInitScripts(): void { // Copy init scripts from src/resources to Gradle UserHome const initScriptsDir = path.resolve(this.gradleUserHome, 'init.d') - fs.mkdirSync(initScriptsDir, {recursive: true}) + fs.mkdirSync(initScriptsDir, { recursive: true }) const initScriptFilenames = [ 'gradle-build-action.build-result-capture.init.gradle', 'gradle-build-action.build-result-capture-service.plugin.groovy', @@ -203,26 +210,24 @@ export class GradleStateCache { const initScriptPath = path.resolve(initScriptsDir, initScriptFilename) fs.writeFileSync(initScriptPath, initScriptContent) } + } - // Copy the default toolchain definitions to `~/.m2/toolchains.xml` - const toolchainContent = this.readResourceFileAsString('toolchains.xml') + private registerToolchains(): void { + const preInstalledToolchains = this.readResourceFileAsString('toolchains.xml') const m2dir = path.resolve(this.userHome, '.m2') const toolchainXmlTarget = path.resolve(m2dir, 'toolchains.xml') if (!fs.existsSync(toolchainXmlTarget)) { // Write a new toolchains.xml file if it doesn't exist - fs.mkdirSync(m2dir, {recursive: true}) - fs.writeFileSync(toolchainXmlTarget, toolchainContent) + fs.mkdirSync(m2dir, { recursive: true }) + fs.writeFileSync(toolchainXmlTarget, preInstalledToolchains) core.info(`Wrote default JDK locations to ${toolchainXmlTarget}`) } else { // Merge into an existing toolchains.xml file const existingToolchainContent = fs.readFileSync(toolchainXmlTarget, 'utf8') - const appendedContent = toolchainContent.split("").pop()! + const appendedContent = preInstalledToolchains.split("").pop()! const mergedContent = existingToolchainContent.replace('', appendedContent) - core.info(`Merged toolchains.xml`) - core.info(mergedContent) - fs.writeFileSync(toolchainXmlTarget, mergedContent) core.info(`Merged default JDK locations into ${toolchainXmlTarget}`) }