mirror of
https://github.com/moonrepo/setup-rust.git
synced 2025-04-29 13:30:03 +00:00
Move to cargo file.
This commit is contained in:
parent
81acac128f
commit
1df398a624
4 changed files with 67 additions and 69 deletions
17
helpers.ts
17
helpers.ts
|
@ -1,17 +0,0 @@
|
|||
import os from 'os';
|
||||
import path from 'path';
|
||||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo');
|
||||
|
||||
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
|
||||
|
||||
export function getPrimaryCacheKey(): string {
|
||||
return `setup-rustcargo-${process.platform}`;
|
||||
}
|
||||
|
||||
export function getPathsToCache(): string[] {
|
||||
return [path.join(CARGO_HOME, 'registry')];
|
||||
}
|
28
index.ts
28
index.ts
|
@ -1,11 +1,10 @@
|
|||
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 { CACHE_ENABLED, CARGO_HOME, getPathsToCache, getPrimaryCacheKey } from './helpers';
|
||||
import { CACHE_ENABLED, CARGO_HOME, restoreCache } from './src/cargo';
|
||||
|
||||
interface Toolchain {
|
||||
channel: string;
|
||||
|
@ -190,31 +189,6 @@ async function installBins() {
|
|||
await exec.exec('cargo', ['binstall', '--no-confirm', '--log-level', 'info', ...bins]);
|
||||
}
|
||||
|
||||
async function restoreCache() {
|
||||
if (!CACHE_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',
|
||||
]);
|
||||
|
||||
if (cacheKey) {
|
||||
core.saveState('cache-hit-key', 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');
|
||||
|
||||
|
|
26
post.ts
26
post.ts
|
@ -1,29 +1,5 @@
|
|||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
import { CACHE_ENABLED, getPathsToCache, getPrimaryCacheKey } from './helpers';
|
||||
|
||||
async function saveCache() {
|
||||
if (!CACHE_ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
const primaryKey = getPrimaryCacheKey();
|
||||
const cacheHitKey = core.getState('cache-hit-key');
|
||||
|
||||
if (cacheHitKey === primaryKey) {
|
||||
core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`);
|
||||
return;
|
||||
}
|
||||
|
||||
core.info('Cleaning cache before saving');
|
||||
|
||||
await exec.exec('cargo', ['cache', '--autoclean']);
|
||||
|
||||
core.info(`Saving cache with key ${primaryKey}`);
|
||||
|
||||
await cache.saveCache(getPathsToCache(), primaryKey);
|
||||
}
|
||||
import { saveCache } from './src/cargo';
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
|
|
65
src/cargo.ts
Normal file
65
src/cargo.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import os from 'os';
|
||||
import path from 'path';
|
||||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||
export const CARGO_HOME = process.env.CARGO_HOME || path.join(os.homedir(), '.cargo');
|
||||
|
||||
export const CACHE_ENABLED = core.getBooleanInput('cache') || cache.isFeatureAvailable();
|
||||
|
||||
export function getPrimaryCacheKey(): string {
|
||||
return `setup-rustcargo-${process.platform}`;
|
||||
}
|
||||
|
||||
export function getPathsToCache(): string[] {
|
||||
return [path.join(CARGO_HOME, 'registry')];
|
||||
}
|
||||
|
||||
export async function saveCache() {
|
||||
if (!CACHE_ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
const primaryKey = getPrimaryCacheKey();
|
||||
const cacheHitKey = core.getState('cache-hit-key');
|
||||
|
||||
if (cacheHitKey === primaryKey) {
|
||||
core.info(`Cache hit occured on the key ${cacheHitKey}, not saving cache`);
|
||||
return;
|
||||
}
|
||||
|
||||
core.info('Cleaning cache before saving');
|
||||
|
||||
await exec.exec('cargo', ['cache', '--autoclean']);
|
||||
|
||||
core.info(`Saving cache with key ${primaryKey}`);
|
||||
|
||||
await cache.saveCache(getPathsToCache(), primaryKey);
|
||||
}
|
||||
|
||||
export async function restoreCache() {
|
||||
if (!CACHE_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',
|
||||
]);
|
||||
|
||||
if (cacheKey) {
|
||||
core.saveState('cache-hit-key', 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue