Skip to content

Commit

Permalink
Use faster D: drive on windows for Gradle User Home and Gradle downlo…
Browse files Browse the repository at this point in the history
…ads (#299)

Fixes #290
  • Loading branch information
bigdaz authored Jul 19, 2024
2 parents 20e923b + f8aa3ed commit 514ac34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 7 additions & 2 deletions sources/src/execution/provision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async function installGradleVersion(versionInfo: GradleVersionInfo): Promise<str
}

async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo): Promise<string> {
const installsDir = path.join(os.homedir(), 'gradle-installations/installs')
const installsDir = path.join(getProvisionDir(), 'installs')
const installDir = path.join(installsDir, `gradle-${versionInfo.version}`)
if (fs.existsSync(installDir)) {
core.info(`Gradle installation already exists at ${installDir}`)
Expand All @@ -119,7 +119,7 @@ async function locateGradleAndDownloadIfRequired(versionInfo: GradleVersionInfo)
}

async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo): Promise<string> {
const downloadPath = path.join(os.homedir(), `gradle-installations/downloads/gradle-${versionInfo.version}-bin.zip`)
const downloadPath = path.join(getProvisionDir(), `downloads/gradle-${versionInfo.version}-bin.zip`)

// TODO: Convert this to a class and inject config
const cacheConfig = new CacheConfig()
Expand Down Expand Up @@ -152,6 +152,11 @@ async function downloadAndCacheGradleDistribution(versionInfo: GradleVersionInfo
return downloadPath
}

function getProvisionDir(): string {
const tmpDir = process.env['RUNNER_TEMP'] ?? os.tmpdir()
return path.join(tmpDir, `.gradle-actions/gradle-installations`)
}

async function downloadGradleDistribution(versionInfo: GradleVersionInfo, downloadPath: string): Promise<void> {
await toolCache.downloadTool(versionInfo.downloadUrl, downloadPath)
core.info(`Downloaded ${versionInfo.downloadUrl} to ${downloadPath} (size ${fs.statSync(downloadPath).size})`)
Expand Down
18 changes: 17 additions & 1 deletion sources/src/setup-gradle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import * as caches from './caching/caches'
Expand Down Expand Up @@ -79,7 +80,22 @@ async function determineGradleUserHome(): Promise<string> {
return path.resolve(rootDir, customGradleUserHome)
}

return path.resolve(await determineUserHome(), '.gradle')
const defaultGradleUserHome = path.resolve(await determineUserHome(), '.gradle')
// Use the default Gradle User Home if it already exists
if (fs.existsSync(defaultGradleUserHome)) {
core.info(`Gradle User Home already exists at ${defaultGradleUserHome}`)
return defaultGradleUserHome
}

// Switch Gradle User Home to faster 'D:' drive if possible
if (os.platform() === 'win32' && defaultGradleUserHome.startsWith('C:\\') && fs.existsSync('D:\\a\\')) {
const fasterGradleUserHome = 'D:\\a\\.gradle'
core.info(`Setting GRADLE_USER_HOME to ${fasterGradleUserHome} to leverage (potentially) faster drive.`)
core.exportVariable('GRADLE_USER_HOME', fasterGradleUserHome)
return fasterGradleUserHome
}

return defaultGradleUserHome
}

/**
Expand Down

0 comments on commit 514ac34

Please sign in to comment.