Skip to content

Commit

Permalink
feat: support custom project paths, and if target does not exist
Browse files Browse the repository at this point in the history
Closes #7.
  • Loading branch information
BD103 committed Sep 10, 2024
1 parent 9805f37 commit 06303be
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ inputs:
using `cargo install`. By default this is false.
required: false
default: false
project-path:
description: |
The path to the folder containing `Cargo.toml` and `target`, without a trailing slash. By
default this is the current working directory.
required: false
default: '.'
gh-token:
description: |
The Github token used to fetch the most recent artifact. This defaults to
Expand Down
1 change: 1 addition & 0 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115807,6 +115807,7 @@ const CARGO_SWEEP_VERSION = "^0.7.0";
const INPUTS = {
useCache: core.getBooleanInput("use-cache", { required: true }),
usePrebuilt: core.getBooleanInput("use-prebuilt", { required: true }),
projectPath: core.getInput("project-path", { required: true }),
ghToken: core.getInput("gh-token", { required: false }),
};

Expand Down
20 changes: 15 additions & 5 deletions dist/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61418,6 +61418,7 @@ const CARGO_SWEEP_VERSION = "^0.7.0";
const INPUTS = {
useCache: core.getBooleanInput("use-cache", { required: true }),
usePrebuilt: core.getBooleanInput("use-prebuilt", { required: true }),
projectPath: core.getInput("project-path", { required: true }),
ghToken: core.getInput("gh-token", { required: false }),
};

Expand Down Expand Up @@ -72439,33 +72440,42 @@ var __webpack_exports__ = {};
const cache = __nccwpck_require__(6878);
const core = __nccwpck_require__(9093);
const exec = __nccwpck_require__(7775);
const io = __nccwpck_require__(2826);

const fs = __nccwpck_require__(3292);

const shared = __nccwpck_require__(1590);

async function main() {
async function main() {
try {
// Check if the `target` folder exists.
fs.access(`${shared.INPUTS.projectPath}/target`);
} catch {
core.info(`Cargo \`target\` directory was not found at ${shared.INPUTS.projectPath}/target.`);

// Since there's nothing to cache, let's exit early.
return;
}

const cacheHit = core.getState("cache-hit");

// Recreate `sweep.timestamp` file.
core.info("Restoring timestamp from state.");
const timestamp = core.getState("timestamp");
await fs.writeFile("sweep.timestamp", timestamp);
await fs.writeFile(`${shared.INPUTS.projectPath}/sweep.timestamp`, timestamp);

core.info(`Using timestamp: ${timestamp}.`);

// Remove everything older than timestamp.
core.info("Sweeping unused build files.");
await exec.exec(`"${shared.PATH}"`, ["sweep", "--file"]);
await exec.exec(`"${shared.PATH}"`, ["sweep", "--file", shared.INPUTS.projectPath]);

if (shared.INPUTS.useCache && cacheHit === "false") {
core.info(`Saving cache with key ${shared.cacheKey()}`);

cache.saveCache(
[shared.PATH],
shared.cacheKey(),
)
);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const CARGO_SWEEP_VERSION = "^0.7.0";
export const INPUTS = {
useCache: core.getBooleanInput("use-cache", { required: true }),
usePrebuilt: core.getBooleanInput("use-prebuilt", { required: true }),
projectPath: core.getInput("project-path", { required: true }),
ghToken: core.getInput("gh-token", { required: false }),
};

Expand Down
19 changes: 14 additions & 5 deletions src/post.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
const cache = require("@actions/cache");
const core = require("@actions/core");
const exec = require("@actions/exec");
const io = require("@actions/io");

const fs = require("fs/promises");

const shared = require("./index");

async function main() {
async function main() {
try {
// Check if the `target` folder exists.
fs.access(`${shared.INPUTS.projectPath}/target`);
} catch {
core.info(`Cargo \`target\` directory was not found at ${shared.INPUTS.projectPath}/target.`);

// Since there's nothing to cache, let's exit early.
return;
}

const cacheHit = core.getState("cache-hit");

// Recreate `sweep.timestamp` file.
core.info("Restoring timestamp from state.");
const timestamp = core.getState("timestamp");
await fs.writeFile("sweep.timestamp", timestamp);
await fs.writeFile(`${shared.INPUTS.projectPath}/sweep.timestamp`, timestamp);

core.info(`Using timestamp: ${timestamp}.`);

// Remove everything older than timestamp.
core.info("Sweeping unused build files.");
await exec.exec(`"${shared.PATH}"`, ["sweep", "--file"]);
await exec.exec(`"${shared.PATH}"`, ["sweep", "--file", shared.INPUTS.projectPath]);

if (shared.INPUTS.useCache && cacheHit === "false") {
core.info(`Saving cache with key ${shared.cacheKey()}`);

cache.saveCache(
[shared.PATH],
shared.cacheKey(),
)
);
}
}

Expand Down

0 comments on commit 06303be

Please # to comment.