Skip to content

Commit

Permalink
Added .NET Format to list of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgrammerAL committed Oct 5, 2024
1 parent dcfc91d commit 23e3536
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ indent_size = 4
indent_style = space
tab_width = 4

# Organize usings
dotnet_separate_import_directive_groups = true
dotnet_sort_system_directives_first = true
file_header_template = unset

# New line preferences
end_of_line = crlf
insert_final_newline = true
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ There are 2 ways to run this. As a .NET Tool installed on your machine, or downl

## CLI Options

- --config-file, -d
- `-d|--config-file`
- Required
- Path to the file to use for config values when updating code
- --help, -h
- `-h|--help`
- Outputs CLI help

## Config File
Expand Down
5 changes: 2 additions & 3 deletions src/CodeUpdater/CodeUpdater/Helpers/RunProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
using System.Text;
using System.Threading.Tasks;

using Serilog;

using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
using static ProgrammerAL.Tools.CodeUpdater.Helpers.RunProcessHelper;

using Serilog;

namespace ProgrammerAL.Tools.CodeUpdater.Helpers;

Expand Down
6 changes: 6 additions & 0 deletions src/CodeUpdater/CodeUpdater/Options/UpdateOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public class UpdateOptions
[Required]
public required bool EnforceCodeStyleInBuild { get; set; }

/// <summary>
/// True to run the `dotnet format` command
/// </summary>
[Required]
public required bool RunDotnetFormat { get; set; }

/// <summary>
/// If this is set, it will be the file to write logs to, in addition to the console
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/CodeUpdater/CodeUpdater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static async ValueTask RunAsync(CommandOptions commandOptions)
var workLocator = new WorkLocator(logger);
var validator = new PreRunValidator(logger, runProcessHelper);
var cSharpUpdater = new CSharpUpdater(logger, runProcessHelper, updateOptions);
var npmUpdater = new NpmUpdater(runProcessHelper);
var npmUpdater = new NpmUpdater(logger, runProcessHelper);
var compileRunner = new CompileRunner(logger, runProcessHelper);

var skipPaths = workLocator.DetermineSkipPaths(updateOptions.IgnorePatterns);
Expand Down
18 changes: 16 additions & 2 deletions src/CodeUpdater/CodeUpdater/Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

namespace ProgrammerAL.Tools.CodeUpdater;

public record CSharpUpdateResult(string CsprojFile, NugetUpdateResults NugetUpdates, CsprojValueUpdateResultType LangVersionUpdate, CsprojValueUpdateResultType TargetFrameworkUpdate);
public record CSharpUpdateResult(
string CsprojFile,
NugetUpdateResults NugetUpdates,
CsprojValueUpdateResultType LangVersionUpdate,
CsprojValueUpdateResultType TargetFrameworkUpdate,
DotnetFormatResult DotnetFormatUpdate);

public record NugetUpdateResults(bool RetrievedPackageListSuccessfully, ImmutableArray<NugetUpdateResult> Updates);
public record NugetUpdateResult(string CsProjFile, string PackageId, bool UpdatedSuccessfully);

public record CsProjUpdateResult(string CsProjFile, CsprojValueUpdateResultType LangVersionUpdate, CsprojValueUpdateResultType TargetFrameworkUpdate);
public record DotnetFormatResult(string CsProjFile, DotnetFormatResultType Result);

public record NpmUpdates(ImmutableArray<string> NpmDirectories);

Expand All @@ -30,7 +36,7 @@ public enum CompileResultType
ProcessDidNotStart,
BuildTimeout,
BuildErrors
}
}

public enum CsprojValueUpdateResultType
{
Expand All @@ -41,3 +47,11 @@ public enum CsprojValueUpdateResultType
Updated,
AddedElement
}

public enum DotnetFormatResultType
{
Unknown,
DidNotRun,
RanSuccessfully,
Erroreded
}
20 changes: 18 additions & 2 deletions src/CodeUpdater/CodeUpdater/Updaters/CSharpUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CSharpUpdater
{
private readonly NugetUpdater _nugetUpdater;
private readonly CsProjUpdater _csProjUpdater;
private readonly CsCodeUpdater _csCodeUpdater;

private readonly ILogger _logger;
private readonly IRunProcessHelper _runProcessHelper;
Expand All @@ -29,6 +30,7 @@ public CSharpUpdater(
_updateOptions = updateOptions;
_nugetUpdater = new NugetUpdater(_logger, _runProcessHelper);
_csProjUpdater = new CsProjUpdater(_logger, updateOptions);
_csCodeUpdater = new CsCodeUpdater(_logger, _runProcessHelper, updateOptions);
}

public async ValueTask<ImmutableArray<CSharpUpdateResult>> UpdateAllCSharpProjectsAsync(UpdateWork updateWork)
Expand All @@ -38,15 +40,16 @@ public async ValueTask<ImmutableArray<CSharpUpdateResult>> UpdateAllCSharpProjec
{
_logger.Information($"Updating '{csProjFilePath}'");


var nugetUpdates = await UpdateNugetPackagesAsync(csProjFilePath);
var csProjUpdates = UpdateCsProjPropertyValues(csProjFilePath);
var dotnetFormatUpdate = await RunDotnetFormatAsync(csProjFilePath);

builder.Add(new CSharpUpdateResult(
csProjFilePath,
NugetUpdates: nugetUpdates,
LangVersionUpdate: csProjUpdates.LangVersionUpdate,
TargetFrameworkUpdate: csProjUpdates.TargetFrameworkUpdate));
TargetFrameworkUpdate: csProjUpdates.TargetFrameworkUpdate,
DotnetFormatUpdate: dotnetFormatUpdate));
}

return builder.ToImmutableArray();
Expand Down Expand Up @@ -77,4 +80,17 @@ private CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
return new CsProjUpdateResult(csProjFilePath, CsprojValueUpdateResultType.Unknown, CsprojValueUpdateResultType.Unknown);
}
}

private async Task<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath)
{
try
{
return await _csCodeUpdater.RunDotnetFormatAsync(csProjFilePath);
}
catch (Exception ex)
{
_logger.Error("Error running dotnet format on csproj file '{CsProjFilePath}'. {Ex}", csProjFilePath, ex.ToString());
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.Erroreded);
}
}
}
40 changes: 40 additions & 0 deletions src/CodeUpdater/CodeUpdater/Updaters/CsCodeUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

using ProgrammerAL.Tools.CodeUpdater.Helpers;

using Serilog;

namespace ProgrammerAL.Tools.CodeUpdater.Updaters;

public class CsCodeUpdater(ILogger Logger, IRunProcessHelper RunProcessHelper, UpdateOptions UpdateOptions)
{
public async ValueTask<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath)
{
if (!UpdateOptions.RunDotnetFormat)
{
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.DidNotRun);
}

string commandArgs = $"format \"{csProjFilePath}\"";
try
{
var processResult = await RunProcessHelper.RunProcessToCompletionAndGetOutputAsync("dotnet", commandArgs);
if (processResult.CompletedSuccessfully)
{
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.RanSuccessfully);
}
}
catch (Exception ex)
{
Logger.Error(ex, "Error running dotnet format with command args '{CommandArgs}'", commandArgs);
}

return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.Erroreded);
}
}
2 changes: 1 addition & 1 deletion src/CodeUpdater/CodeUpdater/Updaters/CsProjUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void UpdateTargetFrameworksValue(XElement childElm, CsprojUpdateTracker
}
}

public class CsprojUpdateTracker
private class CsprojUpdateTracker
{
public const string TargetFramework = "TargetFramework";
public const string TargetFrameworks = "TargetFrameworks";
Expand Down
4 changes: 2 additions & 2 deletions src/CodeUpdater/CodeUpdater/Updaters/NpmUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using Serilog;

namespace ProgrammerAL.Tools.CodeUpdater.Updaters;
public class NpmUpdater(IRunProcessHelper RunProcessHelper)
public class NpmUpdater(ILogger Logger, IRunProcessHelper RunProcessHelper)
{
public NpmUpdates UpdateNpmPackages(UpdateWork updateWork)
{
Expand All @@ -33,7 +33,7 @@ public NpmUpdates UpdateNpmPackages(UpdateWork updateWork)
}
catch (Exception ex)
{
Log.Error(ex, "Error updating npm packages at path'{ProjectPath}'. Command was '{Command}'", projectPath, command);
Logger.Error(ex, "Error updating npm packages at path'{ProjectPath}'. Command was '{Command}'", projectPath, command);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/CodeUpdater/CodeUpdater/WorkLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ImmutableArray<string> DetermineSkipPaths(IEnumerable<string> additionalS
}

public UpdateWork DetermineUpdateWork(string rootDirectory, ImmutableArray<string> skipPaths)
{
{
var csProjFiles = FindCsProjFiles(rootDirectory, skipPaths);
var npmDirectories = FindNpmDirectories(rootDirectory, skipPaths);

Expand Down
5 changes: 4 additions & 1 deletion src/CodeUpdater/CodeUpdater/sampleUpdateOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"DotNetTargetFramework": "net8.0",
"DotNetLangVersion": "latest",
"EnableNetAnalyzers": true,
"EnforceCodeStyleInBuild": true
"EnforceCodeStyleInBuild": true,
"RunDotnetFormat": true,
"OutputFile": "./code-updater-output.txt",
"LogLevel": "Verbose"
}

0 comments on commit 23e3536

Please sign in to comment.