2023-04-04 15:26:44 +02:00
|
|
|
import os = require("os");
|
|
|
|
|
|
|
|
import * as path from "path";
|
2023-04-23 20:24:06 +02:00
|
|
|
import * as crypto from "crypto";
|
2023-04-04 15:26:44 +02:00
|
|
|
import * as cache from "@actions/cache";
|
|
|
|
import * as core from "@actions/core";
|
|
|
|
import * as rustCore from "@actions-rs/core";
|
|
|
|
|
2023-04-30 21:37:33 +02:00
|
|
|
import {
|
|
|
|
getCargoSemverChecksVersion,
|
|
|
|
getRustcVersion,
|
|
|
|
hashFiles,
|
|
|
|
hashFolderContent,
|
|
|
|
} from "./utils";
|
2023-04-04 15:26:44 +02:00
|
|
|
|
|
|
|
export class RustdocCache {
|
|
|
|
private readonly cargo;
|
|
|
|
private readonly cachePath;
|
|
|
|
private readonly workspaceRoot;
|
|
|
|
private restoredCacheKey = "";
|
|
|
|
private __cacheKey = "";
|
|
|
|
|
|
|
|
constructor(cargo: rustCore.Cargo, cachePath: string, workspaceRoot: string) {
|
|
|
|
this.cargo = cargo;
|
|
|
|
this.cachePath = path.resolve(cachePath);
|
|
|
|
this.workspaceRoot = path.resolve(workspaceRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
async save(): Promise<void> {
|
2023-04-30 21:37:33 +02:00
|
|
|
const cacheKeyWithLocalHash = `${await this.cacheKey()}-${await this.getLocalCacheHash()}`;
|
2023-04-04 15:26:44 +02:00
|
|
|
if (this.restoredCacheKey == cacheKeyWithLocalHash) {
|
|
|
|
core.info("Rustdoc cache is up to date, skipping saving.");
|
|
|
|
} else {
|
|
|
|
core.info(`Saving rustdoc cache using key ${cacheKeyWithLocalHash}`);
|
|
|
|
await cache.saveCache([this.cachePath], cacheKeyWithLocalHash);
|
|
|
|
this.restoredCacheKey = cacheKeyWithLocalHash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async restore(): Promise<boolean> {
|
|
|
|
core.info("Restoring rustdoc cache...");
|
|
|
|
core.info(`Rustdoc cache path: ${this.cachePath}.`);
|
|
|
|
core.info(`Rustdoc cache key: ${await this.cacheKey()}.`);
|
|
|
|
|
|
|
|
const key = await cache.restoreCache([this.cachePath], await this.cacheKey(), [
|
|
|
|
await this.cacheKey(),
|
|
|
|
]);
|
|
|
|
if (key) {
|
|
|
|
core.info(`Restored rustdoc cache using key ${key}.`);
|
|
|
|
this.restoredCacheKey = key;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
core.info(`Rustdoc cache not found.`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async cacheKey(): Promise<string> {
|
|
|
|
if (!this.__cacheKey) {
|
|
|
|
this.__cacheKey = [
|
|
|
|
rustCore.input.getInput("prefix-key") || "",
|
2023-04-23 10:37:17 +02:00
|
|
|
rustCore.input.getInput("shared-key") || this.getRunDependentKey(),
|
2023-04-04 15:26:44 +02:00
|
|
|
os.platform() as string,
|
2023-04-17 14:45:37 +02:00
|
|
|
await getRustcVersion(),
|
|
|
|
await getCargoSemverChecksVersion(this.cargo),
|
2023-04-30 21:37:33 +02:00
|
|
|
await this.getCargoLocksHash(),
|
2023-04-04 15:26:44 +02:00
|
|
|
"semver-checks-rustdoc",
|
|
|
|
].join("-");
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.__cacheKey;
|
|
|
|
}
|
|
|
|
|
2023-04-23 10:37:17 +02:00
|
|
|
private getRunDependentKey(): string {
|
2023-04-23 20:24:06 +02:00
|
|
|
const hasher = crypto.createHash("md5");
|
|
|
|
hasher.update(JSON.stringify({ package: rustCore.input.getInputList("package").sort() }));
|
|
|
|
hasher.update(JSON.stringify({ exclude: rustCore.input.getInputList("exclude").sort() }));
|
|
|
|
hasher.update(JSON.stringify({ manifest_path: rustCore.input.getInput("manifest-path") }));
|
|
|
|
|
|
|
|
return [process.env["GITHUB_JOB"] || "", hasher.digest("hex").substring(0, 16)].join("-");
|
2023-04-23 10:37:17 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:37:33 +02:00
|
|
|
private async getLocalCacheHash(): Promise<string> {
|
|
|
|
return await hashFolderContent(this.cachePath);
|
2023-04-04 15:26:44 +02:00
|
|
|
}
|
|
|
|
|
2023-04-30 21:37:33 +02:00
|
|
|
private async getCargoLocksHash(): Promise<string> {
|
|
|
|
return await hashFiles([path.join(this.workspaceRoot, "**", "Cargo.lock")]);
|
2023-04-04 15:26:44 +02:00
|
|
|
}
|
|
|
|
}
|