Skip to content

Add support for passing clippy arguments in comment-able file #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ 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: actions-rs/clippy-check@v1
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 All @@ -60,10 +89,13 @@ jobs:
| `args` | | Arguments for the `cargo clippy` command | string | |
| `use-cross` | | Use [`cross`](https://github.com/rust-embedded/cross) instead of `cargo` | bool | false |
| `name` | | Name of the created GitHub check. If running this action multiple times, each run must have a unique name. | string | clippy |
| `args-file` | | Path to file containing line-delimited arguments for `cargo clippy` (`sh`-style comments supported) | string | |

For extra details about the `toolchain`, `args` and `use-cross` inputs,
see [`cargo` Action](https://github.com/actions-rs/cargo#inputs) documentation.

**NOTE**: only specify one argument source, either `args` or `args-file`.

**NOTE**: if your workflow contains multiple instances of the `clippy-check` action you will need to give each invocation a unique name, using the `name` property described above.
Check runs must have a unique name, and this prevents a later check run overriding a previous one within the same workflow.

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
default: false
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Input {
args: string[],
useCross: boolean,
name: string,
argsFilePath: string,
}

export function get(): Input {
Expand All @@ -23,12 +24,14 @@ export function get(): Input {
}
const useCross = input.getInputBool('use-cross');
const name = input.getInput('name');
const argsFilePath = input.getInput('args-file')

return {
token: input.getInput('token', {required: true}),
args: args,
useCross: useCross,
toolchain: toolchain || undefined,
name
name,
argsFilePath: argsFilePath
}
}
36 changes: 35 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {Cargo, Cross} from '@actions-rs/core';
import * as input from './input';
import {CheckRunner} from './check';

import {readFileSync} from 'fs';

export async function run(actionInput: input.Input): Promise<void> {
const startedAt = new Date().toISOString();

Expand Down Expand Up @@ -50,7 +52,17 @@ export async function run(actionInput: input.Input): Promise<void> {
// of arguments and it will mess up the output.
args.push('--message-format=json');

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

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

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

let runner = new CheckRunner();
let clippyExitCode: number = 0;
Expand Down Expand Up @@ -103,4 +115,26 @@ async function main(): Promise<void> {
}
}

/**
* 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 (var line of file.split(/\r?\n/)) {
if (!line.startsWith('#')) {
parsedArgs = parsedArgs.concat(line.split(' '));
}
}

return parsedArgs;
}

main();