2021-08-24 18:57:17 +00:00
|
|
|
import * as core from '@actions/core'
|
2021-09-06 03:35:17 +00:00
|
|
|
import * as crypto from 'crypto'
|
2021-09-28 03:05:17 +00:00
|
|
|
import * as path from 'path'
|
2021-10-04 21:59:08 +00:00
|
|
|
import * as fs from 'fs'
|
2021-08-24 18:57:17 +00:00
|
|
|
|
2021-10-29 13:44:45 +00:00
|
|
|
const CACHE_PROTOCOL_VERSION = 'v4-'
|
|
|
|
|
2021-10-27 22:05:07 +00:00
|
|
|
const CACHE_DISABLED_PARAMETER = 'cache-disabled'
|
|
|
|
const CACHE_READONLY_PARAMETER = 'cache-read-only'
|
|
|
|
const CACHE_DEBUG_VAR = 'GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED'
|
|
|
|
const CACHE_PREFIX_VAR = 'GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX'
|
|
|
|
|
2021-09-12 20:26:38 +00:00
|
|
|
export function isCacheDisabled(): boolean {
|
2021-10-27 22:05:07 +00:00
|
|
|
return core.getBooleanInput(CACHE_DISABLED_PARAMETER)
|
2021-08-24 18:57:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 20:26:38 +00:00
|
|
|
export function isCacheReadOnly(): boolean {
|
2021-10-27 22:05:07 +00:00
|
|
|
return core.getBooleanInput(CACHE_READONLY_PARAMETER)
|
2021-08-24 18:57:17 +00:00
|
|
|
}
|
2021-09-05 23:10:47 +00:00
|
|
|
|
2021-09-12 20:08:22 +00:00
|
|
|
export function isCacheDebuggingEnabled(): boolean {
|
2021-10-27 22:05:07 +00:00
|
|
|
return process.env[CACHE_DEBUG_VAR] ? true : false
|
2021-09-12 20:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:44:35 +00:00
|
|
|
export function getCacheKeyPrefix(): string {
|
2021-10-16 14:33:42 +00:00
|
|
|
// Prefix can be used to force change all cache keys (defaults to cache protocol version)
|
2021-10-29 13:44:45 +00:00
|
|
|
return process.env[CACHE_PREFIX_VAR] || CACHE_PROTOCOL_VERSION
|
2021-10-16 15:44:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 03:35:17 +00:00
|
|
|
export function hashStrings(values: string[]): string {
|
|
|
|
const hash = crypto.createHash('md5')
|
|
|
|
for (const value of values) {
|
|
|
|
hash.update(value)
|
|
|
|
}
|
|
|
|
return hash.digest('hex')
|
2021-09-05 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 03:05:17 +00:00
|
|
|
export function hashFileNames(fileNames: string[]): string {
|
2021-10-29 13:34:44 +00:00
|
|
|
return hashStrings(fileNames.map(x => x.replace(new RegExp(`\\${path.sep}`, 'g'), '/')))
|
2021-09-28 03:05:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:59:08 +00:00
|
|
|
/**
|
2021-10-15 20:54:29 +00:00
|
|
|
* Attempt to delete a file or directory, waiting to allow locks to be released
|
2021-10-04 21:59:08 +00:00
|
|
|
*/
|
|
|
|
export async function tryDelete(file: string): Promise<void> {
|
2021-10-15 20:54:29 +00:00
|
|
|
const stat = fs.lstatSync(file)
|
2021-10-04 21:59:08 +00:00
|
|
|
for (let count = 0; count < 3; count++) {
|
|
|
|
try {
|
2021-10-15 20:54:29 +00:00
|
|
|
if (stat.isDirectory()) {
|
|
|
|
fs.rmdirSync(file, {recursive: true})
|
|
|
|
} else {
|
|
|
|
fs.unlinkSync(file)
|
|
|
|
}
|
2021-10-04 21:59:08 +00:00
|
|
|
return
|
|
|
|
} catch (error) {
|
|
|
|
if (count === 2) {
|
|
|
|
throw error
|
|
|
|
} else {
|
|
|
|
core.warning(String(error))
|
|
|
|
await delay(1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function delay(ms: number): Promise<void> {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
|
|
}
|