Skip to content
Merged
48 changes: 21 additions & 27 deletions src/ModularPipelines.Build/Modules/CodeFormattedNicelyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,10 @@ public Task<SkipDecision> ShouldSkip(IPipelineContext context)

try
{
await context.DotNet().Format(new DotNetFormatOptions
{
Arguments = ["whitespace"],
WorkingDirectory = context.Git().RootDirectory,
VerifyNoChanges = true,
Severity = "info",
}, cancellationToken);
await RunDotNetFormat(context, whitespaceOnly: true, verifyNoChanges: true, cancellationToken);

// The code hasn't been formatted nicely!
return await context.DotNet().Format(new DotNetFormatOptions
{
WorkingDirectory = context.Git().RootDirectory,
VerifyNoChanges = true,
Severity = "info",
}, cancellationToken);
return await RunDotNetFormat(context, whitespaceOnly: false, verifyNoChanges: true, cancellationToken);
}
catch (Exception)
{
Expand All @@ -70,20 +59,8 @@ await context.DotNet().Format(new DotNetFormatOptions

ArgumentNullException.ThrowIfNull(_githubSettings.Value.StandardToken);

await context.DotNet().Format(new DotNetFormatOptions
{
WorkingDirectory = context.Git().RootDirectory,
VerifyNoChanges = false,
Severity = "info",
}, cancellationToken);

await context.DotNet().Format(new DotNetFormatOptions
{
Arguments = ["whitespace"],
WorkingDirectory = context.Git().RootDirectory,
VerifyNoChanges = false,
Severity = "info",
}, cancellationToken);
await RunDotNetFormat(context, whitespaceOnly: false, verifyNoChanges: false, cancellationToken);
await RunDotNetFormat(context, whitespaceOnly: true, verifyNoChanges: false, cancellationToken);

var branchTriggeringPullRequest = context.GitHub().EnvironmentVariables.HeadRef!;

Expand All @@ -98,4 +75,21 @@ await GitHelpers.CommitAndPush(context, branchTriggeringPullRequest, DotnetForma
throw new Exception("Formatting code. This run will abort. Another run will trigger with the formatted code.");
}
}

private static Task<CommandResult> RunDotNetFormat(
IModuleContext context,
bool whitespaceOnly,
bool verifyNoChanges,
CancellationToken cancellationToken)
{
var options = new DotNetFormatOptions
{
Arguments = whitespaceOnly ? ["whitespace"] : null,
WorkingDirectory = context.Git().RootDirectory,
VerifyNoChanges = verifyNoChanges,
Severity = "info",
};

return context.DotNet().Format(options, cancellationToken);
}
}
Loading