MCPcopy
hub / github.com/Swatinem/rust-cache / new

Method new

src/config.ts:56–295  ·  view source on GitHub ↗

* Constructs a [`CacheConfig`] with all the paths and keys. * * This will read the action `input`s, and read and persist `state` as necessary.

()

Source from the content-addressed store, hash-verified

54 * This will read the action `input`s, and read and persist `state` as necessary.
55 */
56 static async new(): Promise<CacheConfig> {
57 const self = new CacheConfig();
58
59 let cmdFormat = core.getInput("cmd-format");
60 if (cmdFormat) {
61 const placeholderMatches = cmdFormat.match(/\{0\}/g);
62 if (!placeholderMatches || placeholderMatches.length !== 1) {
63 cmdFormat = "{0}";
64 }
65 } else {
66 cmdFormat = "{0}";
67 }
68 self.cmdFormat = cmdFormat;
69
70 // Construct key prefix:
71 // This uses either the `shared-key` input,
72 // or the `key` input combined with the `job` key.
73
74 let key = core.getInput("prefix-key") || "v0-rust";
75
76 const sharedKey = core.getInput("shared-key");
77 if (sharedKey) {
78 key += `-${sharedKey}`;
79 } else {
80 const inputKey = core.getInput("key");
81 if (inputKey) {
82 key += `-${inputKey}`;
83 }
84
85 const job = process.env.GITHUB_JOB;
86 if (job && core.getInput("add-job-id-key").toLowerCase() == "true") {
87 key += `-${job}`;
88 }
89 }
90
91 // Add runner OS and CPU architecture to the key to avoid cross-contamination of cache
92 const runnerOS = os.type();
93 const runnerArch = os.arch();
94 key += `-${runnerOS}-${runnerArch}`;
95
96 self.keyPrefix = key;
97
98 // Construct environment portion of the key:
99 // This consists of a hash that considers the rust version
100 // as well as all the environment variables as given by a default list
101 // and the `env-vars` input.
102 // The env vars are sorted, matched by prefix and hashed into the
103 // resulting environment hash.
104
105 let hasher = crypto.createHash("sha1");
106 const rustVersions = Array.from(await getRustVersions(cmdFormat));
107 // Doesn't matter how they're sorted, just as long as it's deterministic.
108 rustVersions.sort();
109
110 for (const rustVersion of rustVersions) {
111 const { release, host, "commit-hash": commitHash } = rustVersion;
112 const keyRust = `${release} ${host} ${commitHash}`;
113 hasher.update(keyRust);

Callers 1

runFunction · 0.80

Calls 7

existsFunction · 0.90
getCargoBinsFunction · 0.90
getRustVersionsFunction · 0.85
digestFunction · 0.85
globFilesFunction · 0.85
sort_and_uniqFunction · 0.85
getWorkspaceMembersMethod · 0.80

Tested by

no test coverage detected