Compute wrapper cache key

This commit is contained in:
Paul Merlin 2020-06-13 15:40:10 +02:00
parent 392bcac1c1
commit 9675f09de6
5 changed files with 76 additions and 12 deletions

33
__tests__/cache.test.ts Normal file
View file

@ -0,0 +1,33 @@
import * as cache from '../src/cache'
import * as path from 'path'
describe('cache', () => {
describe('can extract gradle wrapper slug', () => {
it('from wrapper properties file', async () => {
const version = cache.extractGradleWrapperSlugFrom(
path.resolve(
'__tests__/data/basic/gradle/wrapper/gradle-wrapper.properties'
)
)
expect(version).toBe('6.5-bin')
})
it('for -bin dist', async () => {
const version = cache.extractGradleWrapperSlugFromDistUri(
'distributionUrl=https\\://services.gradle.org/distributions/gradle-6.5-bin.zip'
)
expect(version).toBe('6.5-bin')
})
it('for -all dist', async () => {
const version = cache.extractGradleWrapperSlugFromDistUri(
'distributionUrl=https\\://services.gradle.org/distributions/gradle-6.5-all.zip'
)
expect(version).toBe('6.5-all')
})
it('for milestone', async () => {
const version = cache.extractGradleWrapperSlugFromDistUri(
'distributionUrl=https\\://services.gradle.org/distributions/gradle-6.6-milestone-1-all.zip'
)
expect(version).toBe('6.6-milestone-1-all')
})
})
})

View file

@ -1,3 +0,0 @@
describe('TODO - Add a test suite', () => {
it('TODO - Add a test', async () => {})
})

2
dist/main/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/post/index.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1,18 +1,52 @@
import * as core from '@actions/core'
import * as path from 'path'
import * as fs from 'fs'
const WRAPPER_CACHE_KEY = 'WRAPPER_CACHE_KEY'
const WRAPPER_CACHE_PATH = 'WRAPPER_CACHE_PATH'
export async function restoreCachedWrapperDist(
executableDirectory: string
): Promise<void> {
core.saveState('WRAPPER_BASE_DIR', path.resolve(executableDirectory))
core.info(`WRAPPER_BASE_DIR = ${core.getState('WRAPPER_BASE_DIR')}`)
const wrapperProperties = path.join(
executableDirectory,
const wrapperSlug = extractGradleWrapperSlugFrom(
path.join(
path.resolve(executableDirectory),
'gradle/wrapper/gradle-wrapper.properties'
)
core.info(`wrapper properties = ${wrapperProperties}`)
)
const wrapperCacheKey = `wrapper-${wrapperSlug}`
const wrapperCachePath = path.join(
process.env.HOME!,
`.gradle/wrapper/dists/gradle-${wrapperSlug}`
)
core.info(`${WRAPPER_CACHE_KEY} = ${wrapperCacheKey}`)
core.info(`${WRAPPER_CACHE_PATH} = ${wrapperCachePath}`)
core.saveState(WRAPPER_CACHE_KEY, wrapperCacheKey)
core.saveState(WRAPPER_CACHE_PATH, wrapperCachePath)
return
}
export async function cacheWrapperDist(): Promise<void> {
core.info(`WRAPPER_BASE_DIR = ${core.getState('WRAPPER_BASE_DIR')}`)
core.info(`${WRAPPER_CACHE_KEY} = ${core.getState(WRAPPER_CACHE_KEY)}`)
core.info(`${WRAPPER_CACHE_PATH} = ${core.getState(WRAPPER_CACHE_PATH)}`)
return
}
export function extractGradleWrapperSlugFrom(
wrapperProperties: string
): string | null {
const props = fs.readFileSync(wrapperProperties, {encoding: 'utf8'})
const distUrlLine = props
.split('\n')
.find(line => line.startsWith('distributionUrl'))
if (!distUrlLine) return null
return extractGradleWrapperSlugFromDistUri(distUrlLine.substr(16).trim())
}
export function extractGradleWrapperSlugFromDistUri(
distUri: string
): string | null {
const regex = /.*gradle-(.*-(bin|all))\.zip/
const match = distUri.match(regex)
return match ? match[1] : null
}