Skip to content

Commit

Permalink
Merge pull request #191 from xt0rted/version-support
Browse files Browse the repository at this point in the history
More easily support new `dotnet-format` versions
  • Loading branch information
xt0rted authored Apr 27, 2021
2 parents f587d57 + 57c9d25 commit 40a26df
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Bumped `@actions/core` from 1.2.6 to 1.2.7
- Bumped `@actions/io` from 1.0.2 to 1.1.0
- 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.
- Updated the output target from `es6` to `es2019`
- 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.

## Version 1.1.0

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ jobs:
Name | Allowed values | Description
-- | -- | --
`repo-token` | `GITHUB_TOKEN` (default) or PAT | `GITHUB_TOKEN` token or a repo scoped PAT.
`version` | `3` (default) | Version of `dotnet-format` to use.
`action` | `check` (default), `fix` | Primary action `dotnet-format` should perform.

### Optional

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

Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ inputs:
required: true
default: ${{ github.token }}

version:
description: "Version of dotnet-format to use"
required: true
default: "3"

action:
description: "Primary action dotnet-format should perform (check for errors or apply fixes)"
required: false
required: true
default: "check"

only-changed-files:
Expand Down
11 changes: 9 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import {
} from "@actions/core";

import { format } from "./dotnet";
import { checkVersion } from "./version";

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

const result = await format({
const dotnetFormatVersion = checkVersion(version);

const result = await format(dotnetFormatVersion)({
dryRun: true,
onlyChangedFiles,
});
Expand All @@ -24,8 +28,11 @@ export async function check(): Promise<void> {

export async function fix(): Promise<void> {
const onlyChangedFiles = getInput("only-changed-files") === "true";
const version = getInput("version", { required: true });

const dotnetFormatVersion = checkVersion(version);

const result = await format({
const result = await format(dotnetFormatVersion)({
dryRun: false,
onlyChangedFiles,
});
Expand Down
16 changes: 15 additions & 1 deletion src/dotnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { getPullRequestFiles } from "./files";

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

import type { DotNetFormatVersion } from "./version";

export type FormatFunction = (options: FormatOptions) => Promise<boolean>;

export interface FormatOptions {
dryRun: boolean;
onlyChangedFiles: boolean;
Expand All @@ -30,7 +34,7 @@ function formatOnlyChangedFiles(onlyChangedFiles: boolean): boolean {
return false;
}

export async function format(options: FormatOptions): Promise<boolean> {
async function formatVersion3(options: FormatOptions): Promise<boolean> {
const execOptions: ExecOptions = { ignoreReturnCode: true };

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

return !!dotnetResult;
}

export function format(version: DotNetFormatVersion): FormatFunction {
switch (version || "") {
case "3":
return formatVersion3;

default:
throw Error(`dotnet-format version "${version}" is unsupported`);
}
}
18 changes: 18 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type DotNetFormatVersion =
| "3"
;

const supportedVersions: DotNetFormatVersion[] = [
"3",
];

export function checkVersion(version: string): DotNetFormatVersion {
for (let i = 0; i < supportedVersions.length; i++) {
const ver = supportedVersions[i];
if (ver === version) {
return version;
}
}

throw Error(`dotnet-format version "${version}" is unsupported`);
}

0 comments on commit 40a26df

Please sign in to comment.