Skip to content

Commit 40a26df

Browse files
authored
Merge pull request #191 from xt0rted/version-support
More easily support new `dotnet-format` versions
2 parents f587d57 + 57c9d25 commit 40a26df

6 files changed

Lines changed: 52 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Bumped `@actions/core` from 1.2.6 to 1.2.7
66
- Bumped `@actions/io` from 1.0.2 to 1.1.0
77
- Updated the `repo-token` input to be optional and defaulted it to `GITHUB_TOKEN`. If you're already using this value, or not using the `only-changed-files` option, you can remove this setting from your workflow.
8+
- Updated the output target from `es6` to `es2019`
9+
- Added a new `version` input to allow picking the cli version of `dotnet-format` to use. Currently this defaults to `3` and is the only version supported. A future update will add support for versions 4 and 5.
810

911
## Version 1.1.0
1012

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ jobs:
160160
Name | Allowed values | Description
161161
-- | -- | --
162162
`repo-token` | `GITHUB_TOKEN` (default) or PAT | `GITHUB_TOKEN` token or a repo scoped PAT.
163+
`version` | `3` (default) | Version of `dotnet-format` to use.
164+
`action` | `check` (default), `fix` | Primary action `dotnet-format` should perform.
163165

164166
### Optional
165167

166168
Name | Allowed values | Description
167169
-- | -- | --
168-
`action` | `check` (default), `fix` | Primary action `dotnet-format` should perform.
169170
`only-changed-files` | `true`, `false` (default) | Only changed files in the current pull request should be formatted.
170171
`fail-fast` | `true` (default), `false` | The job should fail if there's a formatting error. Only used with the `check` action.
171172

action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ inputs:
1414
required: true
1515
default: ${{ github.token }}
1616

17+
version:
18+
description: "Version of dotnet-format to use"
19+
required: true
20+
default: "3"
21+
1722
action:
1823
description: "Primary action dotnet-format should perform (check for errors or apply fixes)"
19-
required: false
24+
required: true
2025
default: "check"
2126

2227
only-changed-files:

src/actions.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import {
44
} from "@actions/core";
55

66
import { format } from "./dotnet";
7+
import { checkVersion } from "./version";
78

89
export async function check(): Promise<void> {
910
const onlyChangedFiles = getInput("only-changed-files") === "true";
1011
const failFast = getInput("fail-fast") === "true";
12+
const version = getInput("version", { required: true });
1113

12-
const result = await format({
14+
const dotnetFormatVersion = checkVersion(version);
15+
16+
const result = await format(dotnetFormatVersion)({
1317
dryRun: true,
1418
onlyChangedFiles,
1519
});
@@ -24,8 +28,11 @@ export async function check(): Promise<void> {
2428

2529
export async function fix(): Promise<void> {
2630
const onlyChangedFiles = getInput("only-changed-files") === "true";
31+
const version = getInput("version", { required: true });
32+
33+
const dotnetFormatVersion = checkVersion(version);
2734

28-
const result = await format({
35+
const result = await format(dotnetFormatVersion)({
2936
dryRun: false,
3037
onlyChangedFiles,
3138
});

src/dotnet.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import { getPullRequestFiles } from "./files";
1111

1212
import type { ExecOptions } from "@actions/exec/lib/interfaces";
1313

14+
import type { DotNetFormatVersion } from "./version";
15+
16+
export type FormatFunction = (options: FormatOptions) => Promise<boolean>;
17+
1418
export interface FormatOptions {
1519
dryRun: boolean;
1620
onlyChangedFiles: boolean;
@@ -30,7 +34,7 @@ function formatOnlyChangedFiles(onlyChangedFiles: boolean): boolean {
3034
return false;
3135
}
3236

33-
export async function format(options: FormatOptions): Promise<boolean> {
37+
async function formatVersion3(options: FormatOptions): Promise<boolean> {
3438
const execOptions: ExecOptions = { ignoreReturnCode: true };
3539

3640
const dotnetFormatOptions = ["format", "--check"];
@@ -58,3 +62,13 @@ export async function format(options: FormatOptions): Promise<boolean> {
5862

5963
return !!dotnetResult;
6064
}
65+
66+
export function format(version: DotNetFormatVersion): FormatFunction {
67+
switch (version || "") {
68+
case "3":
69+
return formatVersion3;
70+
71+
default:
72+
throw Error(`dotnet-format version "${version}" is unsupported`);
73+
}
74+
}

src/version.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export type DotNetFormatVersion =
2+
| "3"
3+
;
4+
5+
const supportedVersions: DotNetFormatVersion[] = [
6+
"3",
7+
];
8+
9+
export function checkVersion(version: string): DotNetFormatVersion {
10+
for (let i = 0; i < supportedVersions.length; i++) {
11+
const ver = supportedVersions[i];
12+
if (ver === version) {
13+
return version;
14+
}
15+
}
16+
17+
throw Error(`dotnet-format version "${version}" is unsupported`);
18+
}

0 commit comments

Comments
 (0)