Skip to content

Commit

Permalink
feat: add support for deprecated cache-source/target
Browse files Browse the repository at this point in the history
Signed-off-by: Amin Yahyaabadi <[email protected]>
  • Loading branch information
aminya committed Mar 30, 2024
1 parent ada196f commit 73f20e6
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 51 deletions.
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ inputs:
cache-map:
required: true
description: "The map of actions source to container destination paths for the cache paths"
cache-source:
deprecationMessage: "Use `cache-map` instead"
description: "Where the cache is stored in the calling workspace. Default: `cache`"
cache-target:
deprecationMessage: "Use `cache-map` instead"
description: "Where the cache is stored in the docker container. Default: `/root/.cache/go-build`"
scratch-dir:
default: scratch
description: "Where the action is stores some temporary files for its processing. Default: `scratch`"
Expand Down
552 changes: 521 additions & 31 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
"typescript": "^5.4.3"
},
"dependencies": {
"@actions/core": "~1.5.0",
"mri": "^1.2.0",
"spawn-please": "^3.0.0"
},
"targets": {
"main": {
"includeNodeModules": true
"includeNodeModules": true,
"optimize": false
}
}
}
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/extract-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs/promises';
import * as path from 'path';
import fs from 'fs/promises';
import path from 'path';
import { Opts, getCacheMap } from './opts.js';
import { run } from './run.js';

Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { appendFile } from "fs/promises";
import { EOL } from "os";
import fs from "fs/promises";
import os from "os";
import { injectCaches } from "./inject-cache.js";
import { extractCaches } from "./extract-cache.js";
import { help, parseOpts } from "./opts.js";
Expand All @@ -13,11 +13,11 @@ async function main(args: string[]) {

if (opts.extract) {
// Run the post step
extractCaches(opts);
await extractCaches(opts);
} else {
// Otherwise, this is the main step
if (process.env.GITHUB_STATE !== undefined) {
await appendFile(process.env.GITHUB_STATE, `POST=true${EOL}`);
await fs.appendFile(process.env.GITHUB_STATE, `POST=true${os.EOL}`);
}
await injectCaches(opts);
}
Expand Down
4 changes: 2 additions & 2 deletions src/inject-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs/promises';
import * as path from 'path';
import fs from 'fs/promises';
import path from 'path';
import { Opts, getCacheMap } from './opts.js';
import { run } from './run.js';

Expand Down
27 changes: 17 additions & 10 deletions src/opts.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import mri from 'mri';
import { getInput, warning } from '@actions/core';

export type Opts = {
"extract": boolean
"cache-map": string
"scratch-dir": string
"skip-extraction": boolean
help: boolean
/** @deprecated Use `cache-map` instead */
"cache-source"?: string
/** @deprecated Use `cache-map` instead */
"cache-target"?: string
}

export function parseOpts(args: string[]): mri.Argv<Opts> {
return mri<Opts>(args, {
const opts = mri<Opts>(args, {
default: {
"cache-map": getInput("cache-map"),
"scratch-dir": getInput("scratch-dir"),
"skip-extraction": getInput("skip-extraction") === "true",
"extract": process.env[`STATE_POST`] !== undefined,
},
string: ["cache-map", "scratch-dir"],
string: ["cache-map", "scratch-dir", "cache-source", "cache-target"],
boolean: ["skip-extraction", "help", "extract"],
alias: {
"help": ["h"],
},
})

if (opts["cache-source"] && opts["cache-target"]) {
warning("The `cache-source` and `cache-target` options are deprecated. Use `cache-map` instead.")

opts["cache-map"] = JSON.stringify({
[opts["cache-source"]]: opts["cache-target"],
});
}

return opts;
}

export function help() {
Expand All @@ -37,14 +52,6 @@ Options:
`);
}

/**
* Get the action input value from the environment (INPUT_NAME)
*/
function getInput(name: string) {
const val = process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || '';
return val.trim();
}

export function getCacheMap(opts: Opts): Record<string, string> {
try {
return JSON.parse(opts["cache-map"]) as Record<string, string>;
Expand Down

0 comments on commit 73f20e6

Please sign in to comment.