Add a selftest and support for .cargo/bin

This commit is contained in:
Arpad Borsos 2021-02-16 08:53:35 +01:00
parent 83aad8d470
commit b495963495
12 changed files with 1897 additions and 69 deletions

29
.github/workflows/selftest.yml vendored Normal file
View File

@ -0,0 +1,29 @@
name: CI
on: [push, pull_request]
jobs:
selftest:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
name: Test Action on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- uses: ./
- run: |
cargo install cargo-deny --locked
cargo check
cargo test

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules
/target

1665
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
publish = false
name = "rust-cache"
version = "0.1.0"
authors = ["Arpad Borsos <arpad.borsos@googlemail.com>"]
edition = "2018"
[dev-dependencies]
reqwest = "0.11.0"
actix-web = { git = "https://github.com/actix/actix-web.git", rev = "bd26083f333ecf63e3eb444748250364ce124f5e" }

24
dist/restore/index.js vendored
View File

@ -55710,11 +55710,13 @@ const cwd = core.getInput("working-directory");
if (cwd) {
process.chdir(cwd);
}
const stateBins = "RUST_CACHE_BINS";
const stateKey = "RUST_CACHE_KEY";
const stateHash = "RUST_CACHE_HASH";
const home = external_os_default().homedir();
const cargoHome = process.env.CARGO_HOME || external_path_default().join(home, ".cargo");
const paths = {
cargoHome,
index: external_path_default().join(cargoHome, "registry/index"),
cache: external_path_default().join(cargoHome, "registry/cache"),
git: external_path_default().join(cargoHome, "git"),
@ -55747,11 +55749,29 @@ async function getCacheConfig() {
}
key += await getRustKey();
return {
paths: [paths.index, paths.cache, paths.git, paths.target],
paths: [
external_path_default().join(cargoHome, "bin"),
external_path_default().join(cargoHome, ".crates2.json"),
external_path_default().join(cargoHome, ".crates.toml"),
paths.git,
paths.cache,
paths.index,
paths.target,
],
key: `${key}-${lockHash}`,
restoreKeys: [key],
};
}
async function getCargoBins() {
const { installs } = JSON.parse(await external_fs_default().promises.readFile(external_path_default().join(paths.cargoHome, ".crates2.json"), "utf8"));
const bins = new Set();
for (const pkg of Object.values(installs)) {
for (const bin of pkg.bins) {
bins.add(bin);
}
}
return bins;
}
async function getRustKey() {
const rustc = await getRustVersion();
return `${rustc.release}-${rustc.host}-${rustc["commit-hash"].slice(0, 12)}`;
@ -55864,6 +55884,8 @@ async function run() {
try {
core.exportVariable("CARGO_INCREMENTAL", 0);
const { paths, key, restoreKeys } = await getCacheConfig();
const bins = await getCargoBins();
core.saveState(stateBins, JSON.stringify([...bins]));
core.info(`Restoring paths:\n ${paths.join("\n ")}`);
core.info(`In directory:\n ${process.cwd()}`);
core.info(`Using keys:\n ${[key, ...restoreKeys].join("\n ")}`);

39
dist/save/index.js vendored
View File

@ -55710,11 +55710,13 @@ const cwd = core.getInput("working-directory");
if (cwd) {
process.chdir(cwd);
}
const stateBins = "RUST_CACHE_BINS";
const stateKey = "RUST_CACHE_KEY";
const stateHash = "RUST_CACHE_HASH";
const home = external_os_default().homedir();
const cargoHome = process.env.CARGO_HOME || external_path_default().join(home, ".cargo");
const paths = {
cargoHome,
index: external_path_default().join(cargoHome, "registry/index"),
cache: external_path_default().join(cargoHome, "registry/cache"),
git: external_path_default().join(cargoHome, "git"),
@ -55747,11 +55749,29 @@ async function getCacheConfig() {
}
key += await getRustKey();
return {
paths: [paths.index, paths.cache, paths.git, paths.target],
paths: [
external_path_default().join(cargoHome, "bin"),
external_path_default().join(cargoHome, ".crates2.json"),
external_path_default().join(cargoHome, ".crates.toml"),
paths.git,
paths.cache,
paths.index,
paths.target,
],
key: `${key}-${lockHash}`,
restoreKeys: [key],
};
}
async function getCargoBins() {
const { installs } = JSON.parse(await external_fs_default().promises.readFile(external_path_default().join(paths.cargoHome, ".crates2.json"), "utf8"));
const bins = new Set();
for (const pkg of Object.values(installs)) {
for (const bin of pkg.bins) {
bins.add(bin);
}
}
return bins;
}
async function getRustKey() {
const rustc = await getRustVersion();
return `${rustc.release}-${rustc.host}-${rustc["commit-hash"].slice(0, 12)}`;
@ -55880,6 +55900,10 @@ async function run() {
await cleanRegistry(registryName, packages);
}
catch { }
try {
await cleanBin();
}
catch { }
try {
await cleanGit(packages);
}
@ -55907,6 +55931,19 @@ async function getRegistryName() {
const first = files.shift();
return external_path_default().basename(external_path_default().dirname(first));
}
async function cleanBin() {
const bins = await getCargoBins();
const oldBins = JSON.parse(core.getState(stateBins));
for (const bin of oldBins) {
bins.delete(bin);
}
const dir = await external_fs_default().promises.opendir(external_path_default().join(paths.cargoHome, "bin"));
for await (const dirent of dir) {
if (dirent.isFile() && !bins.has(dirent.name)) {
await rm(dir.path, dirent);
}
}
}
async function cleanRegistry(registryName, packages) {
await io.rmRF(external_path_default().join(paths.index, registryName, ".cache"));
const pkgSet = new Set(packages.map((p) => `${p.name}-${p.version}.crate`));

129
package-lock.json generated
View File

@ -8,7 +8,7 @@
"version": "1.1.0",
"license": "LGPL-3.0",
"dependencies": {
"@actions/cache": "^1.0.5",
"@actions/cache": "^1.0.6",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"@actions/glob": "^0.1.1",
@ -16,16 +16,16 @@
},
"devDependencies": {
"@vercel/ncc": "^0.27.0",
"typescript": "^4.1.3"
"typescript": "^4.1.5"
},
"funding": {
"url": "https://github.com/sponsors/Swatinem"
}
},
"node_modules/@actions/cache": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/@actions/cache/-/cache-1.0.5.tgz",
"integrity": "sha512-TcvJOduwsPP27KLmIa5cqXsQYFK2GzILcEpnhvYmhGwi1aYx9XwhKmp6Im8X6DJMBxbvupKPsOntG6f6sSkIPA==",
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@actions/cache/-/cache-1.0.6.tgz",
"integrity": "sha512-c8CSJS6uCatX07VHazgvL4XPMpTdawOyyD+OCThRE0v486lqThYa4tayRqTyl6FSyD5LclQyB8/FQlZ5cxTNJA==",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.1",
@ -95,9 +95,9 @@
"integrity": "sha512-kmv8CGrPfN9SwMwrkiBK9VTQYxdFQEGe0BmQk+M8io56P9KNzpAxcWE/1fxJj7uouwN4kXF0BHW8DNlgx+wtCg=="
},
"node_modules/@azure/core-auth": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.1.4.tgz",
"integrity": "sha512-+j1embyH1jqf04AIfJPdLafd5SC1y6z1Jz4i+USR1XkTp6KM8P5u4/AjmWMVoEQdM/M29PJcRDZcCEWjK9S1bw==",
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.2.0.tgz",
"integrity": "sha512-KUl+Nwn/Sm6Lw5d3U90m1jZfNSL087SPcqHLxwn2T6PupNKmcgsEbDjHB25gDvHO4h7pBsTlrdJAY7dz+Qk8GA==",
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"tslib": "^2.0.0"
@ -112,9 +112,9 @@
"integrity": "sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A=="
},
"node_modules/@azure/core-http": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/@azure/core-http/-/core-http-1.2.2.tgz",
"integrity": "sha512-9eu2OcbR7e44gqBy4U1Uv8NTWgLIMwKXMEGgO2MahsJy5rdTiAhs5fJHQffPq8uX2MFh21iBODwO9R/Xlov88A==",
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/@azure/core-http/-/core-http-1.2.3.tgz",
"integrity": "sha512-g5C1zUJO5dehP2Riv+vy9iCYoS1UwKnZsBVCzanScz9A83LbnXKpZDa9wie26G9dfXUhQoFZoFT8LYWhPKmwcg==",
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-auth": "^1.1.3",
@ -137,9 +137,9 @@
}
},
"node_modules/@azure/core-http/node_modules/form-data": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
@ -240,10 +240,11 @@
"integrity": "sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A=="
},
"node_modules/@azure/ms-rest-js": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@azure/ms-rest-js/-/ms-rest-js-2.1.0.tgz",
"integrity": "sha512-4BXLVImYRt+jcUmEJ5LUWglI8RBNVQndY6IcyvQ4U8O4kIXdmlRz3cJdA/RpXf5rKT38KOoTO2T6Z1f6Z1HDBg==",
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/@azure/ms-rest-js/-/ms-rest-js-2.2.3.tgz",
"integrity": "sha512-sXOhOu/37Tr8428f32Jwuwga975Xw64pYg1UeUwOBMhkNgtn5vUuNRa3fhmem+I6f8EKoi6hOsYDFlaHeZ52jA==",
"dependencies": {
"@azure/core-auth": "^1.1.4",
"@types/node-fetch": "^2.3.7",
"@types/tunnel": "0.0.1",
"abort-controller": "^3.0.0",
@ -257,9 +258,9 @@
}
},
"node_modules/@azure/storage-blob": {
"version": "12.3.0",
"resolved": "https://registry.npmjs.org/@azure/storage-blob/-/storage-blob-12.3.0.tgz",
"integrity": "sha512-nCySzNfm782pEW3sg9GHj1zE4gBeVVMeEBdWb4MefifrCwQQOoz5cXZTNFiUJAJqAO+/72r2UjZcUwHk/QmzkA==",
"version": "12.4.1",
"resolved": "https://registry.npmjs.org/@azure/storage-blob/-/storage-blob-12.4.1.tgz",
"integrity": "sha512-RH6ru8LbnCC+m1rlVLon6mYUXdHsTcyUXFCJAWRQQM7p0XOwVKPS+UiVk2tZXfvMWd3q/qT/meOrEbHEcp/c4g==",
"dependencies": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-http": "^1.2.0",
@ -270,6 +271,9 @@
"@opentelemetry/api": "^0.10.2",
"events": "^3.0.0",
"tslib": "^2.0.0"
},
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/@azure/storage-blob/node_modules/tslib": {
@ -305,23 +309,23 @@
}
},
"node_modules/@types/node": {
"version": "14.14.20",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.20.tgz",
"integrity": "sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A=="
"version": "14.14.28",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.28.tgz",
"integrity": "sha512-lg55ArB+ZiHHbBBttLpzD07akz0QPrZgUODNakeC09i62dnrywr9mFErHuaPlB6I7z+sEbK+IYmplahvplCj2g=="
},
"node_modules/@types/node-fetch": {
"version": "2.5.7",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.7.tgz",
"integrity": "sha512-o2WVNf5UhWRkxlf6eq+jMZDu7kjgpgJfl4xVNlvryc95O/6F2ld8ztKX+qu+Rjyet93WAWm5LjeX9H5FGkODvw==",
"version": "2.5.8",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.8.tgz",
"integrity": "sha512-fbjI6ja0N5ZA8TV53RUqzsKNkl9fv8Oj3T7zxW7FGv1GSH7gwJaNF8dzCjrqKaxKeUpTz4yT1DaJFq/omNpGfw==",
"dependencies": {
"@types/node": "*",
"form-data": "^3.0.0"
}
},
"node_modules/@types/node-fetch/node_modules/form-data": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
@ -538,9 +542,9 @@
}
},
"node_modules/typescript": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz",
"integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==",
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.5.tgz",
"integrity": "sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==",
"dev": true,
"bin": {
"tsc": "bin/tsc",
@ -589,9 +593,9 @@
},
"dependencies": {
"@actions/cache": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/@actions/cache/-/cache-1.0.5.tgz",
"integrity": "sha512-TcvJOduwsPP27KLmIa5cqXsQYFK2GzILcEpnhvYmhGwi1aYx9XwhKmp6Im8X6DJMBxbvupKPsOntG6f6sSkIPA==",
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@actions/cache/-/cache-1.0.6.tgz",
"integrity": "sha512-c8CSJS6uCatX07VHazgvL4XPMpTdawOyyD+OCThRE0v486lqThYa4tayRqTyl6FSyD5LclQyB8/FQlZ5cxTNJA==",
"requires": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.1",
@ -660,9 +664,9 @@
"integrity": "sha512-kmv8CGrPfN9SwMwrkiBK9VTQYxdFQEGe0BmQk+M8io56P9KNzpAxcWE/1fxJj7uouwN4kXF0BHW8DNlgx+wtCg=="
},
"@azure/core-auth": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.1.4.tgz",
"integrity": "sha512-+j1embyH1jqf04AIfJPdLafd5SC1y6z1Jz4i+USR1XkTp6KM8P5u4/AjmWMVoEQdM/M29PJcRDZcCEWjK9S1bw==",
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.2.0.tgz",
"integrity": "sha512-KUl+Nwn/Sm6Lw5d3U90m1jZfNSL087SPcqHLxwn2T6PupNKmcgsEbDjHB25gDvHO4h7pBsTlrdJAY7dz+Qk8GA==",
"requires": {
"@azure/abort-controller": "^1.0.0",
"tslib": "^2.0.0"
@ -676,9 +680,9 @@
}
},
"@azure/core-http": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/@azure/core-http/-/core-http-1.2.2.tgz",
"integrity": "sha512-9eu2OcbR7e44gqBy4U1Uv8NTWgLIMwKXMEGgO2MahsJy5rdTiAhs5fJHQffPq8uX2MFh21iBODwO9R/Xlov88A==",
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/@azure/core-http/-/core-http-1.2.3.tgz",
"integrity": "sha512-g5C1zUJO5dehP2Riv+vy9iCYoS1UwKnZsBVCzanScz9A83LbnXKpZDa9wie26G9dfXUhQoFZoFT8LYWhPKmwcg==",
"requires": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-auth": "^1.1.3",
@ -698,9 +702,9 @@
},
"dependencies": {
"form-data": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
@ -788,10 +792,11 @@
}
},
"@azure/ms-rest-js": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@azure/ms-rest-js/-/ms-rest-js-2.1.0.tgz",
"integrity": "sha512-4BXLVImYRt+jcUmEJ5LUWglI8RBNVQndY6IcyvQ4U8O4kIXdmlRz3cJdA/RpXf5rKT38KOoTO2T6Z1f6Z1HDBg==",
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/@azure/ms-rest-js/-/ms-rest-js-2.2.3.tgz",
"integrity": "sha512-sXOhOu/37Tr8428f32Jwuwga975Xw64pYg1UeUwOBMhkNgtn5vUuNRa3fhmem+I6f8EKoi6hOsYDFlaHeZ52jA==",
"requires": {
"@azure/core-auth": "^1.1.4",
"@types/node-fetch": "^2.3.7",
"@types/tunnel": "0.0.1",
"abort-controller": "^3.0.0",
@ -805,9 +810,9 @@
}
},
"@azure/storage-blob": {
"version": "12.3.0",
"resolved": "https://registry.npmjs.org/@azure/storage-blob/-/storage-blob-12.3.0.tgz",
"integrity": "sha512-nCySzNfm782pEW3sg9GHj1zE4gBeVVMeEBdWb4MefifrCwQQOoz5cXZTNFiUJAJqAO+/72r2UjZcUwHk/QmzkA==",
"version": "12.4.1",
"resolved": "https://registry.npmjs.org/@azure/storage-blob/-/storage-blob-12.4.1.tgz",
"integrity": "sha512-RH6ru8LbnCC+m1rlVLon6mYUXdHsTcyUXFCJAWRQQM7p0XOwVKPS+UiVk2tZXfvMWd3q/qT/meOrEbHEcp/c4g==",
"requires": {
"@azure/abort-controller": "^1.0.0",
"@azure/core-http": "^1.2.0",
@ -846,23 +851,23 @@
"integrity": "sha512-hZNKjKOYsckoOEgBziGMnBcX0M7EtstnCmwz5jZUOUYwlZ+/xxX6z3jPu1XVO2Jivk0eLfuP9GP+vFD49CMetw=="
},
"@types/node": {
"version": "14.14.20",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.20.tgz",
"integrity": "sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A=="
"version": "14.14.28",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.28.tgz",
"integrity": "sha512-lg55ArB+ZiHHbBBttLpzD07akz0QPrZgUODNakeC09i62dnrywr9mFErHuaPlB6I7z+sEbK+IYmplahvplCj2g=="
},
"@types/node-fetch": {
"version": "2.5.7",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.7.tgz",
"integrity": "sha512-o2WVNf5UhWRkxlf6eq+jMZDu7kjgpgJfl4xVNlvryc95O/6F2ld8ztKX+qu+Rjyet93WAWm5LjeX9H5FGkODvw==",
"version": "2.5.8",
"resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.8.tgz",
"integrity": "sha512-fbjI6ja0N5ZA8TV53RUqzsKNkl9fv8Oj3T7zxW7FGv1GSH7gwJaNF8dzCjrqKaxKeUpTz4yT1DaJFq/omNpGfw==",
"requires": {
"@types/node": "*",
"form-data": "^3.0.0"
},
"dependencies": {
"form-data": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz",
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==",
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
@ -1027,9 +1032,9 @@
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="
},
"typescript": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz",
"integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==",
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.5.tgz",
"integrity": "sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==",
"dev": true
},
"universalify": {

View File

@ -22,7 +22,7 @@
},
"homepage": "https://github.com/Swatinem/rust-cache#readme",
"dependencies": {
"@actions/cache": "^1.0.5",
"@actions/cache": "^1.0.6",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"@actions/glob": "^0.1.1",
@ -30,7 +30,7 @@
},
"devDependencies": {
"@vercel/ncc": "^0.27.0",
"typescript": "^4.1.3"
"typescript": "^4.1.5"
},
"scripts": {
"prepare": "ncc build --target es2020 -o dist/restore src/restore.ts && ncc build --target es2020 -o dist/save src/save.ts"

View File

@ -16,12 +16,14 @@ if (cwd) {
process.chdir(cwd);
}
export const stateBins = "RUST_CACHE_BINS";
export const stateKey = "RUST_CACHE_KEY";
const stateHash = "RUST_CACHE_HASH";
const home = os.homedir();
const cargoHome = process.env.CARGO_HOME || path.join(home, ".cargo");
export const paths = {
cargoHome,
index: path.join(cargoHome, "registry/index"),
cache: path.join(cargoHome, "registry/cache"),
git: path.join(cargoHome, "git"),
@ -67,12 +69,33 @@ export async function getCacheConfig(): Promise<CacheConfig> {
key += await getRustKey();
return {
paths: [paths.index, paths.cache, paths.git, paths.target],
paths: [
path.join(cargoHome, "bin"),
path.join(cargoHome, ".crates2.json"),
path.join(cargoHome, ".crates.toml"),
paths.git,
paths.cache,
paths.index,
paths.target,
],
key: `${key}-${lockHash}`,
restoreKeys: [key],
};
}
export async function getCargoBins(): Promise<Set<string>> {
const { installs }: { installs: { [key: string]: { bins: Array<string> } } } = JSON.parse(
await fs.promises.readFile(path.join(paths.cargoHome, ".crates2.json"), "utf8"),
);
const bins = new Set<string>();
for (const pkg of Object.values(installs)) {
for (const bin of pkg.bins) {
bins.add(bin);
}
}
return bins;
}
async function getRustKey(): Promise<string> {
const rustc = await getRustVersion();
return `${rustc.release}-${rustc.host}-${rustc["commit-hash"].slice(0, 12)}`;

3
src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -1,6 +1,6 @@
import * as cache from "@actions/cache";
import * as core from "@actions/core";
import { cleanTarget, getCacheConfig, getPackages, stateKey } from "./common";
import { cleanTarget, getCacheConfig, getCargoBins, getPackages, stateBins, stateKey } from "./common";
async function run() {
try {
@ -8,6 +8,9 @@ async function run() {
const { paths, key, restoreKeys } = await getCacheConfig();
const bins = await getCargoBins();
core.saveState(stateBins, JSON.stringify([...bins]));
core.info(`Restoring paths:\n ${paths.join("\n ")}`);
core.info(`In directory:\n ${process.cwd()}`);
core.info(`Using keys:\n ${[key, ...restoreKeys].join("\n ")}`);

View File

@ -5,7 +5,17 @@ import * as glob from "@actions/glob";
import * as io from "@actions/io";
import fs from "fs";
import path from "path";
import { cleanTarget, getCacheConfig, getPackages, Packages, paths, rm, stateKey } from "./common";
import {
cleanTarget,
getCacheConfig,
getCargoBins,
getPackages,
Packages,
paths,
rm,
stateBins,
stateKey,
} from "./common";
async function run() {
try {
@ -26,6 +36,10 @@ async function run() {
await cleanRegistry(registryName, packages);
} catch {}
try {
await cleanBin();
} catch {}
try {
await cleanGit(packages);
} catch {}
@ -56,6 +70,22 @@ async function getRegistryName(): Promise<string> {
return path.basename(path.dirname(first));
}
async function cleanBin() {
const bins = await getCargoBins();
const oldBins = JSON.parse(core.getState(stateBins));
for (const bin of oldBins) {
bins.delete(bin);
}
const dir = await fs.promises.opendir(path.join(paths.cargoHome, "bin"));
for await (const dirent of dir) {
if (dirent.isFile() && !bins.has(dirent.name)) {
await rm(dir.path, dirent);
}
}
}
async function cleanRegistry(registryName: string, packages: Packages) {
await io.rmRF(path.join(paths.index, registryName, ".cache"));