Skip to content

Commit

Permalink
Allow to pass some extra content to be hashed for the cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Sep 28, 2020
1 parent 09b91a0 commit 22e714f
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 15 deletions.
22 changes: 22 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,43 @@ inputs:
- multiple paths in a JSON array, encoded in a string
Blobs are accepted (processed by [glob-all](https://www.npmjs.com/package/glob-all)).
default: ''
extraHashedContent:
required: false
description: >
Extra content to take into account in the cache key.
See extraFiles for more details.
The content of extraHashedContent is taken into account in the hash for the coursier cache key.
default: ''
extraSbtFiles:
required: false
description: >
Extra sbt files to take into account in the sbt cache key. Same format as extraFiles.
default: ''
extraSbtHashedContent:
required: false
description: >
Extra content to take into account in the sbt cache key. Same format as extraHashedContent.
default: ''
extraMillFiles:
required: false
description: >
Extra mill files to take into account in the mill cache key. Same format as extraFiles.
default: ''
extraMillHashedContent:
required: false
description: >
Extra content to take into account in the mill cache key. Same format as extraHashedContent.
default: ''
ammoniteScripts:
required: false
description: >
Ammonite scripts to take into account in the Ammonite cache key. Same format as extraFiles.
default: ''
extraAmmoniteHashedContent:
required: false
description: >
Extra content to take into account in the Ammonite cache key. Same format as extraHashedContent.
default: ''
runs:
using: 'node12'
main: 'dist/restore/index.js'
Expand Down
102 changes: 87 additions & 15 deletions src/restore.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as cache from '@actions/cache'
import * as core from '@actions/core'
import * as exec from '@actions/exec'
const hashFiles = require('hash-files')
const fs = require('fs')
const glob = require('glob-all')
const hashFiles = require('hash-files')

let _unameValue = ''

Expand Down Expand Up @@ -68,13 +69,35 @@ async function doGlob(globs: string[]): Promise<string[]> {
async function restoreCache(
id: string,
paths: string[],
inputFiles: string[]
inputFiles: string[],
extraHashedContent: string
): Promise<void> {
const os = await uname()

const upperId = id.toLocaleUpperCase('en-US')

const hash = await doHashFiles(inputFiles)
let hash = ''

if (extraHashedContent.length === 0) {
hash = await doHashFiles(inputFiles)
} else {
const tmpFilePath = '.tmp-cs-cache-key'
const writeTmpFile = new Promise<void>((resolve, reject) => {
fs.writeFile(tmpFilePath, extraHashedContent, (err: any) => {
if (err) reject(err)
else resolve()
})
})
await writeTmpFile
hash = await doHashFiles(inputFiles.concat([tmpFilePath]))
const removeTmpFile = new Promise<void>((resolve, reject) => {
fs.unlink(tmpFilePath, (err: any) => {
if (err) reject(err)
else resolve()
})
})
await removeTmpFile
}

const key = `${os}-${id}-${hash}`
const restoreKeys = [`${os}-${id}-`]
Expand All @@ -93,7 +116,10 @@ async function restoreCache(
core.saveState(`${upperId}_CACHE_RESULT`, cacheHitKey)
}

async function restoreCoursierCache(inputFiles: string[]): Promise<void> {
async function restoreCoursierCache(
inputFiles: string[],
extraHashedContent: string
): Promise<void> {
let paths: string[] = []

const userSpecifiedCachePath = core.getInput('path')
Expand All @@ -104,19 +130,33 @@ async function restoreCoursierCache(inputFiles: string[]): Promise<void> {
paths = [getCachePath(getOs(await uname()))]
}

await restoreCache('coursier', paths, inputFiles)
await restoreCache('coursier', paths, inputFiles, extraHashedContent)
}

async function restoreSbtCache(inputFiles: string[]): Promise<void> {
await restoreCache('sbt-ivy2-cache', ['~/.sbt', '~/.ivy2/cache'], inputFiles)
async function restoreSbtCache(
inputFiles: string[],
extraHashedContent: string
): Promise<void> {
await restoreCache(
'sbt-ivy2-cache',
['~/.sbt', '~/.ivy2/cache'],
inputFiles,
extraHashedContent
)
}

async function restoreMillCache(inputFiles: string[]): Promise<void> {
await restoreCache('mill', ['~/.mill'], inputFiles)
async function restoreMillCache(
inputFiles: string[],
extraHashedContent: string
): Promise<void> {
await restoreCache('mill', ['~/.mill'], inputFiles, extraHashedContent)
}

async function restoreAmmoniteCache(inputFiles: string[]): Promise<void> {
await restoreCache('ammonite', ['~/.ammonite'], inputFiles)
async function restoreAmmoniteCache(
inputFiles: string[],
extraHashedContent: string
): Promise<void> {
await restoreCache('ammonite', ['~/.ammonite'], inputFiles, extraHashedContent)
}

function readExtraFiles(variableName: string): string[] {
Expand All @@ -132,6 +172,12 @@ function readExtraFiles(variableName: string): string[] {
return extraFiles
}

function readExtraKeys(variableName: string): string {
let extraFilesStr = core.getInput(variableName)
if (!extraFilesStr) extraFilesStr = ''
return extraFilesStr
}

async function run(): Promise<void> {
let root = core.getInput('root')
if (!root.endsWith('/')) {
Expand All @@ -143,6 +189,11 @@ async function run(): Promise<void> {
const extraMillFiles = readExtraFiles('extraMillFiles')
const extraAmmoniteFiles = readExtraFiles('ammoniteScripts')

const extraHashedContent = readExtraKeys('extraHashedContent')
const extraSbtHashedContent = readExtraKeys('extraSbtHashedContent')
const extraMillHashedContent = readExtraKeys('extraMillHashedContent')
const extraAmmoniteHashedContent = readExtraKeys('extraAmmoniteHashedContent')

const sbtGlobs = [
`${root}*.sbt`,
`${root}project/**.sbt`,
Expand All @@ -166,19 +217,40 @@ async function run(): Promise<void> {
const hasAmmoniteFiles = (await doGlob(ammoniteGlobs)).length > 0

await restoreCoursierCache(
sbtGlobs.concat(millGlobs).concat(ammoniteGlobs).concat(extraFiles)
sbtGlobs.concat(millGlobs).concat(ammoniteGlobs).concat(extraFiles),
JSON.stringify({
sbt: extraSbtHashedContent,
mill: extraMillHashedContent,
amm: extraAmmoniteHashedContent,
other: extraHashedContent
})
)

if (hasSbtFiles) {
await restoreSbtCache(sbtGlobs)
await restoreSbtCache(
sbtGlobs,
JSON.stringify({
sbt: extraSbtHashedContent
})
)
}

if (hasMillFiles) {
await restoreMillCache(millGlobs)
await restoreMillCache(
millGlobs,
JSON.stringify({
mill: extraMillHashedContent
})
)
}

if (hasAmmoniteFiles) {
await restoreAmmoniteCache(ammoniteGlobs)
await restoreAmmoniteCache(
ammoniteGlobs,
JSON.stringify({
amm: extraAmmoniteHashedContent
})
)
}
}

Expand Down

0 comments on commit 22e714f

Please sign in to comment.