mirror of
https://github.com/moonrepo/setup-rust.git
synced 2025-04-29 13:30:03 +00:00
Add restore cache.
This commit is contained in:
parent
7b19c2c567
commit
8cbd5dda6b
3 changed files with 54 additions and 18 deletions
|
@ -3,3 +3,11 @@ import path from 'path';
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||||
export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo');
|
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')];
|
||||||
|
}
|
||||||
|
|
33
index.ts
33
index.ts
|
@ -1,10 +1,11 @@
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import * as cache from '@actions/cache';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as exec from '@actions/exec';
|
import * as exec from '@actions/exec';
|
||||||
import * as tc from '@actions/tool-cache';
|
import * as tc from '@actions/tool-cache';
|
||||||
import TOML from '@ltd/j-toml';
|
import TOML from '@ltd/j-toml';
|
||||||
import { CARGO_HOME } from './helpers';
|
import { CARGO_HOME, getPathsToCache, getPrimaryCacheKey } from './helpers';
|
||||||
|
|
||||||
interface Toolchain {
|
interface Toolchain {
|
||||||
channel: string;
|
channel: string;
|
||||||
|
@ -185,6 +186,35 @@ async function installBins() {
|
||||||
await exec.exec('cargo', ['binstall', '--no-confirm', '--log-level', 'info', ...bins]);
|
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() {
|
async function run() {
|
||||||
core.info('Setting cargo environment variables');
|
core.info('Setting cargo environment variables');
|
||||||
|
|
||||||
|
@ -195,6 +225,7 @@ async function run() {
|
||||||
core.exportVariable('CARGO_TERM_COLOR', 'always');
|
core.exportVariable('CARGO_TERM_COLOR', 'always');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await restoreCache();
|
||||||
await installToolchain(detectToolchain());
|
await installToolchain(detectToolchain());
|
||||||
await installBins();
|
await installBins();
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
|
31
post.ts
31
post.ts
|
@ -1,33 +1,30 @@
|
||||||
import fs from 'fs';
|
|
||||||
import path from 'path';
|
|
||||||
import * as cache from '@actions/cache';
|
import * as cache from '@actions/cache';
|
||||||
import * as core from '@actions/core';
|
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');
|
const enabled = core.getBooleanInput('cache');
|
||||||
|
|
||||||
if (!cache.isFeatureAvailable() || !enabled) {
|
if (!cache.isFeatureAvailable() || !enabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(CARGO_HOME)) {
|
const primaryKey = getPrimaryCacheKey();
|
||||||
core.warning(`~/.cargo does not exist, not saving cache`);
|
const cacheHitKey = core.getState('cacheHitKey');
|
||||||
|
|
||||||
|
if (cacheHitKey === primaryKey) {
|
||||||
|
core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
core.info(`Saving cache with key ${primaryKey}`);
|
||||||
|
|
||||||
|
await cache.saveCache(getPathsToCache(), primaryKey, {}, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function run() {
|
||||||
try {
|
try {
|
||||||
const primaryKey = 'v0-test';
|
await saveCache();
|
||||||
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);
|
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
core.setFailed((error as Error).message);
|
core.setFailed((error as Error).message);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue