fix: Rework rust state.

This commit is contained in:
Miles Johnson 2023-04-17 15:54:00 -07:00
parent c49c6099c9
commit fbe11a6179
3 changed files with 46 additions and 48 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@moonrepo/setup-rust",
"version": "0.3.1",
"version": "0.3.2",
"description": "A GitHub action for setting up Rust and Cargo.",
"main": "dist/index.js",
"scripts": {

View file

@ -1,4 +1,5 @@
import crypto from 'crypto';
import fs from 'fs';
import os from 'os';
import path from 'path';
import * as cache from '@actions/cache';
@ -6,31 +7,35 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as glob from '@actions/glob';
import * as io from '@actions/io';
import { RUST_HASH, RUST_VERSION } from './rust';
export const CARGO_HOME = process.env.CARGO_HOME ?? path.join(os.homedir(), '.cargo');
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
let CACHE_KEY = '';
export function getCachePaths(): string[] {
return [
// ~/.cargo/registry
path.join(CARGO_HOME, 'registry'),
// /workspace/target/debug
path.join(process.env.GITHUB_WORKSPACE ?? process.cwd(), 'target/debug'),
];
}
export function getCachePrefixes(): string[] {
return [`setup-rustcargo-${process.platform}`, 'setup-rustcargo'];
}
export async function getPrimaryCacheKey() {
if (CACHE_KEY) {
return CACHE_KEY;
}
const hasher = crypto.createHash('sha1');
core.info('Generating cache key');
const rustVersion = core.getState('rust-version');
core.debug(`Hashing Rust version = ${RUST_VERSION}`);
hasher.update(RUST_VERSION);
core.debug(`Hashing Rust version = ${rustVersion}`);
hasher.update(rustVersion);
const rustHash = core.getState('rust-hash');
core.debug(`Hashing Rust commit hash = ${rustHash}`);
hasher.update(rustHash);
core.debug(`Hashing Rust commit hash = ${RUST_HASH}`);
hasher.update(RUST_HASH);
const lockHash = await glob.hashFiles('Cargo.lock');
@ -44,19 +49,7 @@ export async function getPrimaryCacheKey() {
hasher.update(job);
}
// eslint-disable-next-line require-atomic-updates
CACHE_KEY = `setup-rustcargo-${process.platform}-${hasher.digest('hex')}`;
return CACHE_KEY;
}
export function getPathsToCache(): string[] {
return [
// ~/.cargo/registry
path.join(CARGO_HOME, 'registry'),
// /workspace/target/debug
path.join(process.env.GITHUB_WORKSPACE ?? process.cwd(), 'target/debug'),
];
return `${getCachePrefixes()[0]}-${hasher.digest('hex')}`;
}
export async function cleanCargoRegistry() {
@ -68,11 +61,18 @@ export async function cleanCargoRegistry() {
await exec.exec('cargo', ['cache', '--autoclean']);
// .cargo/registry/index - Delete .cache directories
const globber = await glob.create(path.join(registryDir, 'index/**/.cache'));
const globber = await glob.create(path.join(registryDir, 'index/*/.cache'));
for await (const file of globber.globGenerator()) {
core.debug(`Deleting ${file}`);
await io.rmRF(file);
if (fs.existsSync(path.join(path.dirname(file), '.git'))) {
core.debug(`Deleting ${file}`);
try {
await io.rmRF(file);
} catch (error: unknown) {
core.warning(`Failed to delete ${file}: ${error}`);
}
}
}
// .cargo/registry/cache - Do nothing?
@ -95,7 +95,7 @@ export async function saveCache() {
core.info(`Saving cache with key ${primaryKey}`);
await cache.saveCache(getPathsToCache(), primaryKey);
await cache.saveCache(getCachePaths(), primaryKey);
}
export async function restoreCache() {
@ -106,11 +106,7 @@ export async function restoreCache() {
core.info('Attempting to restore cache');
const primaryKey = await getPrimaryCacheKey();
const cacheKey = await cache.restoreCache(getPathsToCache(), primaryKey, [
`setup-rustcargo-${process.platform}`,
'setup-rustcargo',
]);
const cacheKey = await cache.restoreCache(getCachePaths(), primaryKey, getCachePrefixes());
if (cacheKey) {
core.saveState('cache-hit-key', cacheKey);

View file

@ -1,6 +1,11 @@
/* eslint-disable import/no-mutable-exports */
import * as core from '@actions/core';
import * as exec from '@actions/exec';
export let RUST_VERSION = '';
export let RUST_HASH = '';
export async function extractRustVersion(toolchain: string) {
let out = '';
@ -13,19 +18,16 @@ export async function extractRustVersion(toolchain: string) {
});
out.split('\n').forEach((line) => {
let key = '';
if (line.startsWith('commit-hash')) {
key = 'rust-hash';
} else if (line.startsWith('release')) {
key = 'rust-version';
} else {
return;
}
const value = line.split(':')[1].trim();
core.saveState(key, value);
core.setOutput(key, value);
if (line.startsWith('commit-hash')) {
core.setOutput('rust-hash', value);
RUST_HASH = value;
// version
} else if (line.startsWith('release')) {
core.setOutput('rust-version', value);
RUST_VERSION = value;
}
});
}