Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to cache all crates #137

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ sensible defaults.
# default: "false"
cache-on-failure: ""

# Determines which crates are cached.
# If `true` all crates will be cached, otherwise only dependent crates will be cached.
# Useful if additional crates are used for CI tooling.
# default: "false"
cache-all-crates: ""

# Determiners whether the cache should be saved.
# If `false`, the cache is only restored.
# Useful for jobs where the matrix is additive e.g. additional Cargo features.
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ inputs:
cache-on-failure:
description: "Cache even if the build fails. Defaults to false."
required: false
cache-all-crates:
description: "Determines which crates are cached. If `true` all crates will be cached, otherwise only dependent crates will be cached."
required: false
default: "false"
save-if:
description: "Determiners whether the cache should be saved. If `false`, the cache is only restored."
required: false
Expand Down
6 changes: 5 additions & 1 deletion dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60226,7 +60226,7 @@ async function cleanBin() {
}
}
}
async function cleanRegistry(packages) {
async function cleanRegistry(packages, crates = true) {
// `.cargo/registry/src`
// we can remove this completely, as cargo will recreate this from `cache`
await rmRF(path.join(CARGO_HOME, "registry", "src"));
Expand All @@ -60244,6 +60244,10 @@ async function cleanRegistry(packages) {
// TODO: else, clean `.cache` based on the `packages`
}
}
if (!crates) {
core.debug(`skipping crate cleanup`);
return;
}
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
// `.cargo/registry/cache`
const cacheDir = await fs.promises.opendir(path.join(CARGO_HOME, "registry", "cache"));
Expand Down
21 changes: 13 additions & 8 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60226,7 +60226,7 @@ async function cleanBin() {
}
}
}
async function cleanRegistry(packages) {
async function cleanRegistry(packages, crates = true) {
// `.cargo/registry/src`
// we can remove this completely, as cargo will recreate this from `cache`
await rmRF(external_path_default().join(CARGO_HOME, "registry", "src"));
Expand All @@ -60244,6 +60244,10 @@ async function cleanRegistry(packages) {
// TODO: else, clean `.cache` based on the `packages`
}
}
if (!crates) {
core.debug(`skipping crate cleanup`);
return;
}
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));
// `.cargo/registry/cache`
const cacheDir = await external_fs_default().promises.opendir(external_path_default().join(CARGO_HOME, "registry", "cache"));
Expand Down Expand Up @@ -60409,35 +60413,36 @@ async function run() {
await cleanTargetDir(workspace.target, packages);
}
catch (e) {
core.info(`[warning] ${e.stack}`);
core.error(`${e.stack}`);
}
}
try {
core.info(`... Cleaning cargo registry ...`);
await cleanRegistry(allPackages);
const creates = core.getInput("cache-all-crates").toLowerCase() || "false";
core.info(`... Cleaning cargo registry cache-all-crates: ${creates} ...`);
await cleanRegistry(allPackages, creates === "true");
}
catch (e) {
core.info(`[warning] ${e.stack}`);
core.error(`${e.stack}`);
}
try {
core.info(`... Cleaning cargo/bin ...`);
await cleanBin();
}
catch (e) {
core.info(`[warning] ${e.stack}`);
core.error(`${e.stack}`);
}
try {
core.info(`... Cleaning cargo git cache ...`);
await cleanGit(allPackages);
}
catch (e) {
core.info(`[warning] ${e.stack}`);
core.error(`${e.stack}`);
}
core.info(`... Saving cache ...`);
await cache.saveCache(config.cachePaths, config.cacheKey);
}
catch (e) {
core.info(`[warning] ${e.stack}`);
core.error(`${e.stack}`);
}
}
run();
Expand Down
7 changes: 6 additions & 1 deletion src/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function cleanBin() {
}
}

export async function cleanRegistry(packages: Packages) {
export async function cleanRegistry(packages: Packages, crates = true) {
// `.cargo/registry/src`
// we can remove this completely, as cargo will recreate this from `cache`
await rmRF(path.join(CARGO_HOME, "registry", "src"));
Expand All @@ -106,6 +106,11 @@ export async function cleanRegistry(packages: Packages) {
}
}

if (!crates) {
core.debug(`skipping crate cleanup`);
return;
}

const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));

// `.cargo/registry/cache`
Expand Down
15 changes: 8 additions & 7 deletions src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,36 @@ async function run() {
core.info(`... Cleaning ${workspace.target} ...`);
await cleanTargetDir(workspace.target, packages);
} catch (e) {
core.info(`[warning] ${(e as any).stack}`);
core.error(`${(e as any).stack}`);
}
}

try {
core.info(`... Cleaning cargo registry ...`);
await cleanRegistry(allPackages);
const creates = core.getInput("cache-all-crates").toLowerCase() || "false";
core.info(`... Cleaning cargo registry cache-all-crates: ${creates} ...`);
await cleanRegistry(allPackages, creates === "true");
} catch (e) {
core.info(`[warning] ${(e as any).stack}`);
core.error(`${(e as any).stack}`);
}

try {
core.info(`... Cleaning cargo/bin ...`);
await cleanBin();
} catch (e) {
core.info(`[warning] ${(e as any).stack}`);
core.error(`${(e as any).stack}`);
}

try {
core.info(`... Cleaning cargo git cache ...`);
await cleanGit(allPackages);
} catch (e) {
core.info(`[warning] ${(e as any).stack}`);
core.error(`${(e as any).stack}`);
}

core.info(`... Saving cache ...`);
await cache.saveCache(config.cachePaths, config.cacheKey);
} catch (e) {
core.info(`[warning] ${(e as any).stack}`);
core.error(`${(e as any).stack}`);
}
}

Expand Down