Skip to content

Commit 99eaee3

Browse files
committed
Add support for dotnet-format v4
1 parent 3df2d14 commit 99eaee3

4 files changed

Lines changed: 83 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Added support for `dotnet-format` v4. To use this version set `version: 4`.
6+
57
## Version 1.2.0
68

79
- Bumped `@actions/core` from 1.2.6 to 1.2.7

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![CI Workflow Status](https://github.com/xt0rted/dotnet-format/workflows/CI/badge.svg)](https://github.com/xt0rted/dotnet-format/actions?query=workflow%3ACI)
44

5-
Run [dotnet-format](https://github.com/dotnet/format) v3 as part of your workflow to report formatting errors or auto fix violations as part of your pull request workflow.
5+
Run [dotnet-format](https://github.com/dotnet/format) as part of your workflow to report formatting errors or auto fix violations as part of your pull request workflow.
66

77
## Usage
88

@@ -160,7 +160,7 @@ 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.
163+
`version` | `3` (default), `4` | Version of `dotnet-format` to use.
164164
`action` | `check` (default), `fix` | Primary action `dotnet-format` should perform.
165165

166166
### Optional

src/dotnet.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import {
2+
existsSync,
3+
promises,
4+
} from "fs";
5+
16
import {
27
debug,
38
info,
@@ -13,6 +18,8 @@ import type { ExecOptions } from "@actions/exec/lib/interfaces";
1318

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

21+
const { readFile } = promises;
22+
1623
export type FormatFunction = (options: FormatOptions) => Promise<boolean>;
1724

1825
export interface FormatOptions {
@@ -34,8 +41,29 @@ function formatOnlyChangedFiles(onlyChangedFiles: boolean): boolean {
3441
return false;
3542
}
3643

44+
function tempReportFile(): string {
45+
return `../dotnet-format-${new Date().getTime()}.json`;
46+
}
47+
48+
async function hadChangedFiles(report: string): Promise<boolean> {
49+
if (!existsSync(report)) {
50+
throw Error(`Report not found at ${report}`);
51+
}
52+
53+
const reportContents = await readFile(report, "utf8");
54+
const formatResults = JSON.parse(reportContents) as [];
55+
56+
debug(`Formatting issues found: ${formatResults.length}`);
57+
58+
return !!formatResults.length;
59+
}
60+
3761
async function formatVersion3(options: FormatOptions): Promise<boolean> {
38-
const execOptions: ExecOptions = { ignoreReturnCode: true };
62+
const execOptions: ExecOptions = {
63+
ignoreReturnCode: true,
64+
listeners: { debug },
65+
windowsVerbatimArguments: true,
66+
};
3967

4068
const dotnetFormatOptions = ["format", "--check"];
4169

@@ -48,9 +76,9 @@ async function formatVersion3(options: FormatOptions): Promise<boolean> {
4876

4977
info(`Checking ${filesToCheck.length} files`);
5078

51-
// if there weren't any files to check then we need to bail
5279
if (!filesToCheck.length) {
53-
debug("No files found for formatting");
80+
debug("No files found to format");
81+
5482
return false;
5583
}
5684

@@ -63,11 +91,57 @@ async function formatVersion3(options: FormatOptions): Promise<boolean> {
6391
return !!dotnetResult;
6492
}
6593

94+
async function formatVersion4(options: FormatOptions): Promise<boolean> {
95+
const execOptions: ExecOptions = {
96+
ignoreReturnCode: true,
97+
listeners: { debug },
98+
windowsVerbatimArguments: true,
99+
};
100+
101+
const dotnetFormatReport = tempReportFile();
102+
const dotnetFormatOptions = ["format", "--report", dotnetFormatReport];
103+
104+
if (options.dryRun) {
105+
dotnetFormatOptions.push("--check");
106+
}
107+
108+
if (formatOnlyChangedFiles(options.onlyChangedFiles)) {
109+
const filesToCheck = await getPullRequestFiles();
110+
111+
info(`Checking ${filesToCheck.length} files`);
112+
113+
if (!filesToCheck.length) {
114+
debug("No files found to format");
115+
116+
return false;
117+
}
118+
119+
const files = filesToCheck
120+
.map((file) => {
121+
debug(`Including file: ${file}`);
122+
123+
return `"${file}"`;
124+
});
125+
126+
dotnetFormatOptions.push("-f", "--include", ...files);
127+
}
128+
129+
// If the args are passed as args while using the --report parameter then dotnet-format thinks the
130+
// report path is the project path, but passing them as part of the command works as expected 🤷
131+
const dotnetPath: string = await which("dotnet", true);
132+
await exec(`${dotnetPath} ${dotnetFormatOptions.join(" ")}`, [], execOptions);
133+
134+
return await hadChangedFiles(dotnetFormatReport);
135+
}
136+
66137
export function format(version: DotNetFormatVersion): FormatFunction {
67138
switch (version || "") {
68139
case "3":
69140
return formatVersion3;
70141

142+
case "4":
143+
return formatVersion4;
144+
71145
default:
72146
throw Error(`dotnet-format version "${version}" is unsupported`);
73147
}

src/version.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
export type DotNetFormatVersion =
22
| "3"
3+
| "4"
34
;
45

56
const supportedVersions: DotNetFormatVersion[] = [
67
"3",
8+
"4",
79
];
810

911
export function checkVersion(version: string): DotNetFormatVersion {

0 commit comments

Comments
 (0)