3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-04-29 06:35:53 +00:00

Slightly improve docs

This commit is contained in:
Arpad Borsos 2023-08-02 12:46:45 +02:00
parent f6987ea139
commit 3312b3ab47
No known key found for this signature in database
GPG key ID: FC7BCA77824B3298
7 changed files with 126 additions and 114 deletions

View file

@ -1,15 +1,15 @@
import * as core from "@actions/core";
import * as glob from "@actions/glob";
import * as toml from "toml";
import crypto from "crypto";
import fs from "fs";
import fs_promises from "fs/promises";
import os from "os";
import path from "path";
import * as toml from "toml";
import { getCmdOutput } from "./utils";
import { Workspace } from "./workspace";
import { getCargoBins } from "./cleanup";
import { CacheProvider, getCmdOutput } from "./utils";
import { Workspace } from "./workspace";
const HOME = os.homedir();
export const CARGO_HOME = process.env.CARGO_HOME || path.join(HOME, ".cargo");
@ -146,7 +146,7 @@ export class CacheConfig {
for (const cargo_manifest of cargo_manifests) {
try {
const content = await fs_promises.readFile(cargo_manifest, { encoding: 'utf8' });
const content = await fs_promises.readFile(cargo_manifest, { encoding: "utf8" });
const parsed = toml.parse(content);
if ("package" in parsed) {
@ -167,7 +167,7 @@ export class CacheConfig {
const dep = deps[key];
if ("path" in dep) {
dep.version = '0.0.0'
dep.version = "0.0.0";
}
}
}
@ -175,7 +175,8 @@ export class CacheConfig {
hasher.update(JSON.stringify(parsed));
parsedKeyFiles.push(cargo_manifest);
} catch (_e) { // Fallback to caching them as regular file
} catch (_e) {
// Fallback to caching them as regular file
keyFiles.push(cargo_manifest);
}
}
@ -184,7 +185,7 @@ export class CacheConfig {
for (const cargo_lock of cargo_locks) {
try {
const content = await fs_promises.readFile(cargo_lock, { encoding: 'utf8' });
const content = await fs_promises.readFile(cargo_lock, { encoding: "utf8" });
const parsed = toml.parse(content);
if (parsed.version !== 3 || !("package" in parsed)) {
@ -197,13 +198,14 @@ export class CacheConfig {
// Package without `[[package]].source` and `[[package]].checksum`
// are the one with `path = "..."` to crates within the workspace.
const packages = parsed.package.filter((p: any) => {
"source" in p || "checksum" in p
"source" in p || "checksum" in p;
});
hasher.update(JSON.stringify(packages));
parsedKeyFiles.push(cargo_lock);
} catch (_e) { // Fallback to caching them as regular file
} catch (_e) {
// Fallback to caching them as regular file
keyFiles.push(cargo_lock);
}
}
@ -257,8 +259,7 @@ export class CacheConfig {
const self = new CacheConfig();
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w: any) => new Workspace(w.root, w.target));
self.workspaces = self.workspaces.map((w: any) => new Workspace(w.root, w.target));
return self;
}
@ -266,8 +267,10 @@ export class CacheConfig {
/**
* Prints the configuration to the action log.
*/
printInfo() {
printInfo(cacheProvider: CacheProvider) {
core.startGroup("Cache Configuration");
core.info(`Cache Provider:`);
core.info(` ${cacheProvider.name}`);
core.info(`Workspaces:`);
for (const workspace of this.workspaces) {
core.info(` ${workspace.root}`);
@ -345,25 +348,22 @@ async function globFiles(pattern: string): Promise<string[]> {
// fs.statSync resolve the symbolic link and returns stat for the
// file it pointed to, so isFile would make sure the resolved
// file is actually a regular file.
return (await globber.glob()).filter(file => fs.statSync(file).isFile());
return (await globber.glob()).filter((file) => fs.statSync(file).isFile());
}
function sort_and_uniq(a: string[]) {
return a
.sort((a, b) => a.localeCompare(b))
.reduce(
(accumulator: string[], currentValue: string) => {
const len = accumulator.length;
// If accumulator is empty or its last element != currentValue
// Since array is already sorted, elements with the same value
// are grouped together to be continugous in space.
//
// If currentValue != last element, then it must be unique.
if (len == 0 || accumulator[len - 1].localeCompare(currentValue) != 0) {
accumulator.push(currentValue);
}
return accumulator;
},
[]
);
return a
.sort((a, b) => a.localeCompare(b))
.reduce((accumulator: string[], currentValue: string) => {
const len = accumulator.length;
// If accumulator is empty or its last element != currentValue
// Since array is already sorted, elements with the same value
// are grouped together to be continugous in space.
//
// If currentValue != last element, then it must be unique.
if (len == 0 || accumulator[len - 1].localeCompare(currentValue) != 0) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
}

View file

@ -2,7 +2,7 @@ import * as core from "@actions/core";
import { cleanTargetDir } from "./cleanup";
import { CacheConfig } from "./config";
import { getCacheHandler, reportError } from "./utils";
import { getCacheProvider, reportError } from "./utils";
process.on("uncaughtException", (e) => {
core.error(e.message);
@ -12,9 +12,9 @@ process.on("uncaughtException", (e) => {
});
async function run() {
const cache = getCacheHandler();
const cacheProvider = getCacheProvider();
if (!cache.isFeatureAvailable()) {
if (!cacheProvider.cache.isFeatureAvailable()) {
setCacheHitOutput(false);
return;
}
@ -28,7 +28,7 @@ async function run() {
core.exportVariable("CARGO_INCREMENTAL", 0);
const config = await CacheConfig.new();
config.printInfo();
config.printInfo(cacheProvider);
core.info("");
core.info(`... Restoring cache ...`);
@ -36,7 +36,7 @@ async function run() {
// Pass a copy of cachePaths to avoid mutating the original array as reported by:
// https://github.com/actions/toolkit/pull/1378
// TODO: remove this once the underlying bug is fixed.
const restoreKey = await cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey]);
const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey]);
if (restoreKey) {
const match = restoreKey === key;
core.info(`Restored from cache key "${restoreKey}" full match: ${match}.`);

View file

@ -3,7 +3,7 @@ import * as exec from "@actions/exec";
import { cleanBin, cleanGit, cleanRegistry, cleanTargetDir } from "./cleanup";
import { CacheConfig, isCacheUpToDate } from "./config";
import { getCacheHandler, reportError } from "./utils";
import { getCacheProvider, reportError } from "./utils";
process.on("uncaughtException", (e) => {
core.error(e.message);
@ -13,11 +13,11 @@ process.on("uncaughtException", (e) => {
});
async function run() {
const cache = getCacheHandler();
const cacheProvider = getCacheProvider();
const save = core.getInput("save-if").toLowerCase() || "true";
if (!(cache.isFeatureAvailable() && save === "true")) {
if (!(cacheProvider.cache.isFeatureAvailable() && save === "true")) {
return;
}
@ -28,7 +28,7 @@ async function run() {
}
const config = CacheConfig.fromState();
config.printInfo();
config.printInfo(cacheProvider);
core.info("");
// TODO: remove this once https://github.com/actions/toolkit/pull/553 lands
@ -48,7 +48,7 @@ async function run() {
try {
const crates = core.getInput("cache-all-crates").toLowerCase() || "false";
core.info(`... Cleaning cargo registry cache-all-crates: ${crates} ...`);
core.info(`... Cleaning cargo registry (cache-all-crates: ${crates}) ...`);
await cleanRegistry(allPackages, crates !== "true");
} catch (e) {
core.debug(`${(e as any).stack}`);
@ -72,7 +72,7 @@ async function run() {
// Pass a copy of cachePaths to avoid mutating the original array as reported by:
// https://github.com/actions/toolkit/pull/1378
// TODO: remove this once the underlying bug is fixed.
await cache.saveCache(config.cachePaths.slice(), config.cacheKey);
await cacheProvider.cache.saveCache(config.cachePaths.slice(), config.cacheKey);
} catch (e) {
reportError(e);
}

View file

@ -1,7 +1,7 @@
import * as buildjetCache from "@actions/buildjet-cache";
import * as ghCache from "@actions/cache";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as buildjetCache from "@actions/buildjet-cache";
import * as ghCache from "@actions/cache";
export function reportError(e: any) {
const { commandFailed } = e;
@ -43,16 +43,21 @@ export async function getCmdOutput(
return stdout;
}
export function getCacheHandler() {
const cacheProvider = core.getInput("cache-provider");
switch (cacheProvider) {
case "github":
core.info("Using Github Cache.");
return ghCache;
case "buildjet":
core.info("Using Buildjet Cache.");
return buildjetCache;
default:
throw new Error("Only currently support github and buildjet caches");
}
export interface CacheProvider {
name: string;
cache: typeof ghCache;
}
export function getCacheProvider(): CacheProvider {
const cacheProvider = core.getInput("cache-provider");
const cache = cacheProvider === "github" ? ghCache : cacheProvider === "buildjet" ? buildjetCache : undefined;
if (!cache) {
throw new Error(`The \`cache-provider\` \`{cacheProvider}\` is not valid.`);
}
return {
name: cacheProvider,
cache: cache,
};
}