diff --git a/helpers.ts b/helpers.ts index 1f18206..772676b 100644 --- a/helpers.ts +++ b/helpers.ts @@ -3,3 +3,11 @@ import path from 'path'; // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo'); + +export function getPrimaryCacheKey(): string { + return `setup-rustcargo-${process.platform}`; +} + +export function getPathsToCache(): string[] { + return [path.join(CARGO_HOME, 'registry')]; +} diff --git a/index.ts b/index.ts index 5b9b347..7bb6789 100644 --- a/index.ts +++ b/index.ts @@ -1,10 +1,11 @@ import fs from 'fs'; import path from 'path'; +import * as cache from '@actions/cache'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as tc from '@actions/tool-cache'; import TOML from '@ltd/j-toml'; -import { CARGO_HOME } from './helpers'; +import { CARGO_HOME, getPathsToCache, getPrimaryCacheKey } from './helpers'; interface Toolchain { channel: string; @@ -185,6 +186,35 @@ async function installBins() { await exec.exec('cargo', ['binstall', '--no-confirm', '--log-level', 'info', ...bins]); } +async function restoreCache() { + const enabled = core.getBooleanInput('cache'); + + if (!cache.isFeatureAvailable() || !enabled) { + return; + } + + core.info('Attempting to restore cache'); + + const primaryKey = await getPrimaryCacheKey(); + const cacheKey = await cache.restoreCache( + getPathsToCache(), + primaryKey, + [`setup-rustcargo-${process.platform}`, 'setup-rustcargo'], + {}, + false, + ); + + if (cacheKey) { + core.saveState('cacheHitKey', cacheKey); + core.info(`Cache restored using key ${primaryKey}`); + } else { + core.warning(`Cache does not exist using key ${primaryKey}`); + } + + core.setOutput('cache-key', cacheKey ?? primaryKey); + core.setOutput('cache-hit', !!cacheKey); +} + async function run() { core.info('Setting cargo environment variables'); @@ -195,6 +225,7 @@ async function run() { core.exportVariable('CARGO_TERM_COLOR', 'always'); try { + await restoreCache(); await installToolchain(detectToolchain()); await installBins(); } catch (error: unknown) { diff --git a/post.ts b/post.ts index 28400f2..651cfde 100644 --- a/post.ts +++ b/post.ts @@ -1,33 +1,30 @@ -import fs from 'fs'; -import path from 'path'; import * as cache from '@actions/cache'; import * as core from '@actions/core'; -import { CARGO_HOME } from './helpers'; +import { getPathsToCache, getPrimaryCacheKey } from './helpers'; -async function run() { +async function saveCache() { const enabled = core.getBooleanInput('cache'); if (!cache.isFeatureAvailable() || !enabled) { return; } - if (!fs.existsSync(CARGO_HOME)) { - core.warning(`~/.cargo does not exist, not saving cache`); + const primaryKey = getPrimaryCacheKey(); + const cacheHitKey = core.getState('cacheHitKey'); + + if (cacheHitKey === primaryKey) { + core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`); return; } + core.info(`Saving cache with key ${primaryKey}`); + + await cache.saveCache(getPathsToCache(), primaryKey, {}, false); +} + +async function run() { try { - const primaryKey = 'v0-test'; - const cacheHitKey = core.getState('cacheHitKey'); - - if (cacheHitKey === primaryKey) { - core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`); - return; - } - - core.info(`Saving cache with key ${primaryKey}`); - - await cache.saveCache([path.join(CARGO_HOME, 'registry')], primaryKey, {}, false); + await saveCache(); } catch (error: unknown) { core.setFailed((error as Error).message); }