Skip to content

Commit

Permalink
Apply args-file feature to updated fork (new base) (#8)
Browse files Browse the repository at this point in the history
Apply changes from #7
  • Loading branch information
gbhand authored Dec 13, 2022
1 parent 13e5cd0 commit 5a7a17c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ jobs:
args: --all-features
```
### With `args-file`

```yaml
name: Clippy check
jobs:
clippy_check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
components: clippy
override: true
- uses: gbhand/clippy-check@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
args-file: "./clippy_args.txt"
```

Example file-based args (`clippy_args.txt`)

```sh
# here's a comment
--locked --all-targets --all-features --
-D missing_docs
-D unused_extern_crates
-D warnings
# another one
-D clippy::complexity
```

## Inputs

| Name | Required | Description | Type | Default |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ inputs:
args:
description: Arguments for the cargo command
required: false
args-file:
description: Arguments defined in external file
required: false
use-cross:
description: Use cross instead of cargo
required: false
Expand Down
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import stringArgv from "string-argv";
export interface Input {
toolchain: string | undefined;
args: string[];
argsFilePath: string | undefined;
useCross: boolean;
}

export function get(): Input {
let toolchain: string = input.getInput("toolchain");
const argsFilePath: string = input.getInput("args-file");

if (toolchain.startsWith("+")) {
toolchain = toolchain.slice(1);
}

return {
args: stringArgv(input.getInput("args")),
argsFilePath: "" !== argsFilePath ? argsFilePath : undefined,
useCross: input.getInputBool("use-cross"),
toolchain: "" !== toolchain ? toolchain : undefined,
};
Expand Down
36 changes: 35 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFileSync } from "fs";

import { Cargo, Cross } from "@actions-rs-plus/core";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
Expand Down Expand Up @@ -51,6 +53,28 @@ async function buildContext(program: Program): Promise<Context> {
return context;
}

/**
* Parses a newline-delimited file of clippy args
*
* @remark sh-style comments are supported (using #)
*
* @param filePath - path of file that contains clippy arguments to parse
* @returns parsed arguments as an array of strings
*/
function parseArgsFile(filePath: string): string[] {
let parsedArgs: string[] = [];

const file = readFileSync(filePath, "utf-8");

for (const line of file.split(/\r?\n/)) {
if (!line.startsWith("#") && line.trim() !== "") {
parsedArgs = parsedArgs.concat(line.split(" "));
}
}

return parsedArgs;
}

async function runClippy(actionInput: input.Input, program: Program): Promise<ClippyResult> {
let args: string[] = [];

Expand All @@ -66,7 +90,17 @@ async function runClippy(actionInput: input.Input, program: Program): Promise<Cl
// of arguments and it will mess up the output.
args.push("--message-format=json");

args = args.concat(actionInput.args);
if (actionInput.args.length !== 0 && actionInput.argsFilePath) {
throw new Error("Only specify one argument source: `args` or `args-file`");
}

if (actionInput.args.length !== 0) {
args = args.concat(actionInput.args);
}

if (actionInput.argsFilePath) {
args = args.concat(parseArgsFile(actionInput.argsFilePath));
}

const outputParser = new OutputParser();

Expand Down
2 changes: 1 addition & 1 deletion src/tests/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { get } from "../input";

describe("foo", () => {
test("bar", () => {
expect(get()).toStrictEqual({ args: [], toolchain: undefined, useCross: false });
expect(get()).toStrictEqual({ args: [], toolchain: undefined, useCross: false, argsFilePath: undefined });
});
});

0 comments on commit 5a7a17c

Please sign in to comment.