Skip to content

Commit ee94d96

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

4 files changed

Lines changed: 62 additions & 9 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: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { readFile } from "fs/promises";
2+
import { resolve } from "path";
3+
14
import {
25
debug,
36
info,
@@ -9,8 +12,6 @@ import { which } from "@actions/io";
912

1013
import { getPullRequestFiles } from "./files";
1114

12-
import type { ExecOptions } from "@actions/exec/lib/interfaces";
13-
1415
import type { DotNetFormatVersion } from "./version";
1516

1617
export type FormatFunction = (options: FormatOptions) => Promise<boolean>;
@@ -34,9 +35,22 @@ function formatOnlyChangedFiles(onlyChangedFiles: boolean): boolean {
3435
return false;
3536
}
3637

37-
async function formatVersion3(options: FormatOptions): Promise<boolean> {
38-
const execOptions: ExecOptions = { ignoreReturnCode: true };
38+
function tempReportFile(): string {
39+
return resolve(
40+
process.cwd(),
41+
"../",
42+
`dotnet-format.${new Date().getTime()}.json`
43+
);
44+
}
3945

46+
async function hadChangedFiles(report: string): Promise<boolean> {
47+
const reportContents = await readFile(report, "utf8");
48+
const formatResults = JSON.parse(reportContents) as [];
49+
50+
return !!formatResults.length;
51+
}
52+
53+
async function formatVersion3(options: FormatOptions): Promise<boolean> {
4054
const dotnetFormatOptions = ["format", "--check"];
4155

4256
if (options.dryRun) {
@@ -48,26 +62,61 @@ async function formatVersion3(options: FormatOptions): Promise<boolean> {
4862

4963
info(`Checking ${filesToCheck.length} files`);
5064

51-
// if there weren't any files to check then we need to bail
5265
if (!filesToCheck.length) {
53-
debug("No files found for formatting");
66+
debug("No files found to format");
67+
5468
return false;
5569
}
5670

5771
dotnetFormatOptions.push("--files", filesToCheck.join(","));
5872
}
5973

6074
const dotnetPath: string = await which("dotnet", true);
61-
const dotnetResult = await exec(`"${dotnetPath}"`, dotnetFormatOptions, execOptions);
75+
const dotnetResult = await exec(`"${dotnetPath}"`, dotnetFormatOptions, { ignoreReturnCode: true });
6276

6377
return !!dotnetResult;
6478
}
6579

80+
async function formatVersion4(options: FormatOptions): Promise<boolean> {
81+
const dotnetFormatReport = tempReportFile();
82+
const dotnetFormatOptions = ["format", `--report '${dotnetFormatReport}'`];
83+
84+
if (options.dryRun) {
85+
dotnetFormatOptions.push("--check");
86+
}
87+
88+
if (formatOnlyChangedFiles(options.onlyChangedFiles)) {
89+
const filesToCheck = await getPullRequestFiles();
90+
91+
info(`Checking ${filesToCheck.length} files`);
92+
93+
if (!filesToCheck.length) {
94+
debug("No files found to format");
95+
96+
return false;
97+
}
98+
99+
const files = filesToCheck
100+
.map((file) => `'${file}'`)
101+
.join(" ");
102+
103+
dotnetFormatOptions.push("--files", files);
104+
}
105+
106+
const dotnetPath: string = await which("dotnet", true);
107+
await exec(`"${dotnetPath}"`, dotnetFormatOptions, { ignoreReturnCode: true });
108+
109+
return await hadChangedFiles(dotnetFormatReport);
110+
}
111+
66112
export function format(version: DotNetFormatVersion): FormatFunction {
67113
switch (version || "") {
68114
case "3":
69115
return formatVersion3;
70116

117+
case "4":
118+
return formatVersion4;
119+
71120
default:
72121
throw Error(`dotnet-format version "${version}" is unsupported`);
73122
}

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)