mirror of
https://github.com/Swatinem/rust-cache
synced 2025-04-29 14:45:53 +00:00
avoid calling cargo metadata on pre-cleanup
This commit is contained in:
parent
19c46583c5
commit
3f2513fdf4
6 changed files with 63 additions and 40 deletions
|
@ -180,17 +180,29 @@ export async function cleanGit(packages: Packages) {
|
|||
const ONE_WEEK = 7 * 24 * 3600 * 1000;
|
||||
|
||||
/**
|
||||
* Removes all files or directories in `dirName`, except the ones matching
|
||||
* any string in the `keepPrefix` set.
|
||||
*
|
||||
* The matching strips and trailing `-$hash` suffix.
|
||||
*
|
||||
* Removes all files or directories in `dirName` matching some criteria.
|
||||
*
|
||||
* When the `checkTimestamp` flag is set, this will also remove anything older
|
||||
* than one week.
|
||||
*
|
||||
* Otherwise, it will remove everything that does not match any string in the
|
||||
* `keepPrefix` set.
|
||||
* The matching strips and trailing `-$hash` suffix.
|
||||
*/
|
||||
async function rmExcept(dirName: string, keepPrefix: Set<string>, checkTimestamp = false) {
|
||||
const dir = await fs.promises.opendir(dirName);
|
||||
for await (const dirent of dir) {
|
||||
if (checkTimestamp) {
|
||||
const fileName = path.join(dir.path, dirent.name);
|
||||
const { mtime } = await fs.promises.stat(fileName);
|
||||
const isOutdated = Date.now() - mtime.getTime() > ONE_WEEK;
|
||||
|
||||
if (isOutdated) {
|
||||
await rm(dir.path, dirent);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let name = dirent.name;
|
||||
|
||||
// strip the trailing hash
|
||||
|
@ -199,14 +211,7 @@ async function rmExcept(dirName: string, keepPrefix: Set<string>, checkTimestamp
|
|||
name = name.slice(0, idx);
|
||||
}
|
||||
|
||||
let isOutdated = false;
|
||||
if (checkTimestamp) {
|
||||
const fileName = path.join(dir.path, dirent.name);
|
||||
const { mtime } = await fs.promises.stat(fileName);
|
||||
isOutdated = Date.now() - mtime.getTime() > ONE_WEEK;
|
||||
}
|
||||
|
||||
if (!keepPrefix.has(name) || isOutdated) {
|
||||
if (!keepPrefix.has(name)) {
|
||||
await rm(dir.path, dirent);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,9 +43,7 @@ async function run() {
|
|||
// pre-clean the target directory on cache mismatch
|
||||
for (const workspace of config.workspaces) {
|
||||
try {
|
||||
const packages = await workspace.getPackages();
|
||||
|
||||
await cleanTargetDir(workspace.target, packages, true);
|
||||
await cleanTargetDir(workspace.target, [], true);
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import * as core from "@actions/core";
|
||||
import path from "path";
|
||||
|
||||
import { getCmdOutput } from "./utils";
|
||||
|
@ -10,11 +11,13 @@ export class Workspace {
|
|||
public async getPackages(): Promise<Packages> {
|
||||
let packages: Packages = [];
|
||||
try {
|
||||
core.debug(`collecting metadata for "${this.root}"`);
|
||||
const meta: Meta = JSON.parse(
|
||||
await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1"], {
|
||||
cwd: this.root,
|
||||
}),
|
||||
);
|
||||
core.debug(`workspace "${this.root}" has ${meta.packages.length} packages`);
|
||||
for (const pkg of meta.packages) {
|
||||
if (pkg.manifest_path.startsWith(this.root)) {
|
||||
continue;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue