Skip to content

Commit

Permalink
Add support for dotnet-format v4
Browse files Browse the repository at this point in the history
  • Loading branch information
xt0rted committed Apr 28, 2021
1 parent 3df2d14 commit ee94d96
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Added support for `dotnet-format` v4. To use this version set `version: 4`.

## Version 1.2.0

- Bumped `@actions/core` from 1.2.6 to 1.2.7
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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.
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.

## Usage

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

### Optional
Expand Down
63 changes: 56 additions & 7 deletions src/dotnet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { readFile } from "fs/promises";
import { resolve } from "path";

import {
debug,
info,
Expand All @@ -9,8 +12,6 @@ import { which } from "@actions/io";

import { getPullRequestFiles } from "./files";

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

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

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

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

async function hadChangedFiles(report: string): Promise<boolean> {
const reportContents = await readFile(report, "utf8");
const formatResults = JSON.parse(reportContents) as [];

return !!formatResults.length;
}

async function formatVersion3(options: FormatOptions): Promise<boolean> {
const dotnetFormatOptions = ["format", "--check"];

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

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

// if there weren't any files to check then we need to bail
if (!filesToCheck.length) {
debug("No files found for formatting");
debug("No files found to format");

return false;
}

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

const dotnetPath: string = await which("dotnet", true);
const dotnetResult = await exec(`"${dotnetPath}"`, dotnetFormatOptions, execOptions);
const dotnetResult = await exec(`"${dotnetPath}"`, dotnetFormatOptions, { ignoreReturnCode: true });

return !!dotnetResult;
}

async function formatVersion4(options: FormatOptions): Promise<boolean> {
const dotnetFormatReport = tempReportFile();
const dotnetFormatOptions = ["format", `--report '${dotnetFormatReport}'`];

if (options.dryRun) {
dotnetFormatOptions.push("--check");
}

if (formatOnlyChangedFiles(options.onlyChangedFiles)) {
const filesToCheck = await getPullRequestFiles();

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

if (!filesToCheck.length) {
debug("No files found to format");

return false;
}

const files = filesToCheck
.map((file) => `'${file}'`)
.join(" ");

dotnetFormatOptions.push("--files", files);
}

const dotnetPath: string = await which("dotnet", true);
await exec(`"${dotnetPath}"`, dotnetFormatOptions, { ignoreReturnCode: true });

return await hadChangedFiles(dotnetFormatReport);
}

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

case "4":
return formatVersion4;

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

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

export function checkVersion(version: string): DotNetFormatVersion {
Expand Down

0 comments on commit ee94d96

Please sign in to comment.