Skip to content

Commit 23e3536

Browse files
committed
Added .NET Format to list of actions
1 parent dcfc91d commit 23e3536

File tree

12 files changed

+98
-15
lines changed

12 files changed

+98
-15
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ indent_size = 4
1111
indent_style = space
1212
tab_width = 4
1313

14+
# Organize usings
15+
dotnet_separate_import_directive_groups = true
16+
dotnet_sort_system_directives_first = true
17+
file_header_template = unset
18+
1419
# New line preferences
1520
end_of_line = crlf
1621
insert_final_newline = true

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ There are 2 ways to run this. As a .NET Tool installed on your machine, or downl
2929

3030
## CLI Options
3131

32-
- --config-file, -d
32+
- `-d|--config-file`
3333
- Required
3434
- Path to the file to use for config values when updating code
35-
- --help, -h
35+
- `-h|--help`
3636
- Outputs CLI help
3737

3838
## Config File

src/CodeUpdater/CodeUpdater/Helpers/RunProcessHelper.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
using System.Text;
66
using System.Threading.Tasks;
77

8-
using Serilog;
9-
108
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
11-
using static ProgrammerAL.Tools.CodeUpdater.Helpers.RunProcessHelper;
9+
10+
using Serilog;
1211

1312
namespace ProgrammerAL.Tools.CodeUpdater.Helpers;
1413

src/CodeUpdater/CodeUpdater/Options/UpdateOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public class UpdateOptions
5656
[Required]
5757
public required bool EnforceCodeStyleInBuild { get; set; }
5858

59+
/// <summary>
60+
/// True to run the `dotnet format` command
61+
/// </summary>
62+
[Required]
63+
public required bool RunDotnetFormat { get; set; }
64+
5965
/// <summary>
6066
/// If this is set, it will be the file to write logs to, in addition to the console
6167
/// </summary>

src/CodeUpdater/CodeUpdater/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static async ValueTask RunAsync(CommandOptions commandOptions)
2929
var workLocator = new WorkLocator(logger);
3030
var validator = new PreRunValidator(logger, runProcessHelper);
3131
var cSharpUpdater = new CSharpUpdater(logger, runProcessHelper, updateOptions);
32-
var npmUpdater = new NpmUpdater(runProcessHelper);
32+
var npmUpdater = new NpmUpdater(logger, runProcessHelper);
3333
var compileRunner = new CompileRunner(logger, runProcessHelper);
3434

3535
var skipPaths = workLocator.DetermineSkipPaths(updateOptions.IgnorePatterns);

src/CodeUpdater/CodeUpdater/Results.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88
namespace ProgrammerAL.Tools.CodeUpdater;
99

10-
public record CSharpUpdateResult(string CsprojFile, NugetUpdateResults NugetUpdates, CsprojValueUpdateResultType LangVersionUpdate, CsprojValueUpdateResultType TargetFrameworkUpdate);
10+
public record CSharpUpdateResult(
11+
string CsprojFile,
12+
NugetUpdateResults NugetUpdates,
13+
CsprojValueUpdateResultType LangVersionUpdate,
14+
CsprojValueUpdateResultType TargetFrameworkUpdate,
15+
DotnetFormatResult DotnetFormatUpdate);
1116

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

1520
public record CsProjUpdateResult(string CsProjFile, CsprojValueUpdateResultType LangVersionUpdate, CsprojValueUpdateResultType TargetFrameworkUpdate);
21+
public record DotnetFormatResult(string CsProjFile, DotnetFormatResultType Result);
1622

1723
public record NpmUpdates(ImmutableArray<string> NpmDirectories);
1824

@@ -30,7 +36,7 @@ public enum CompileResultType
3036
ProcessDidNotStart,
3137
BuildTimeout,
3238
BuildErrors
33-
}
39+
}
3440

3541
public enum CsprojValueUpdateResultType
3642
{
@@ -41,3 +47,11 @@ public enum CsprojValueUpdateResultType
4147
Updated,
4248
AddedElement
4349
}
50+
51+
public enum DotnetFormatResultType
52+
{
53+
Unknown,
54+
DidNotRun,
55+
RanSuccessfully,
56+
Erroreded
57+
}

src/CodeUpdater/CodeUpdater/Updaters/CSharpUpdater.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class CSharpUpdater
1414
{
1515
private readonly NugetUpdater _nugetUpdater;
1616
private readonly CsProjUpdater _csProjUpdater;
17+
private readonly CsCodeUpdater _csCodeUpdater;
1718

1819
private readonly ILogger _logger;
1920
private readonly IRunProcessHelper _runProcessHelper;
@@ -29,6 +30,7 @@ public CSharpUpdater(
2930
_updateOptions = updateOptions;
3031
_nugetUpdater = new NugetUpdater(_logger, _runProcessHelper);
3132
_csProjUpdater = new CsProjUpdater(_logger, updateOptions);
33+
_csCodeUpdater = new CsCodeUpdater(_logger, _runProcessHelper, updateOptions);
3234
}
3335

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

41-
4243
var nugetUpdates = await UpdateNugetPackagesAsync(csProjFilePath);
4344
var csProjUpdates = UpdateCsProjPropertyValues(csProjFilePath);
45+
var dotnetFormatUpdate = await RunDotnetFormatAsync(csProjFilePath);
4446

4547
builder.Add(new CSharpUpdateResult(
4648
csProjFilePath,
4749
NugetUpdates: nugetUpdates,
4850
LangVersionUpdate: csProjUpdates.LangVersionUpdate,
49-
TargetFrameworkUpdate: csProjUpdates.TargetFrameworkUpdate));
51+
TargetFrameworkUpdate: csProjUpdates.TargetFrameworkUpdate,
52+
DotnetFormatUpdate: dotnetFormatUpdate));
5053
}
5154

5255
return builder.ToImmutableArray();
@@ -77,4 +80,17 @@ private CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
7780
return new CsProjUpdateResult(csProjFilePath, CsprojValueUpdateResultType.Unknown, CsprojValueUpdateResultType.Unknown);
7881
}
7982
}
83+
84+
private async Task<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath)
85+
{
86+
try
87+
{
88+
return await _csCodeUpdater.RunDotnetFormatAsync(csProjFilePath);
89+
}
90+
catch (Exception ex)
91+
{
92+
_logger.Error("Error running dotnet format on csproj file '{CsProjFilePath}'. {Ex}", csProjFilePath, ex.ToString());
93+
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.Erroreded);
94+
}
95+
}
8096
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Xml.Linq;
8+
9+
using ProgrammerAL.Tools.CodeUpdater.Helpers;
10+
11+
using Serilog;
12+
13+
namespace ProgrammerAL.Tools.CodeUpdater.Updaters;
14+
15+
public class CsCodeUpdater(ILogger Logger, IRunProcessHelper RunProcessHelper, UpdateOptions UpdateOptions)
16+
{
17+
public async ValueTask<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath)
18+
{
19+
if (!UpdateOptions.RunDotnetFormat)
20+
{
21+
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.DidNotRun);
22+
}
23+
24+
string commandArgs = $"format \"{csProjFilePath}\"";
25+
try
26+
{
27+
var processResult = await RunProcessHelper.RunProcessToCompletionAndGetOutputAsync("dotnet", commandArgs);
28+
if (processResult.CompletedSuccessfully)
29+
{
30+
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.RanSuccessfully);
31+
}
32+
}
33+
catch (Exception ex)
34+
{
35+
Logger.Error(ex, "Error running dotnet format with command args '{CommandArgs}'", commandArgs);
36+
}
37+
38+
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.Erroreded);
39+
}
40+
}

src/CodeUpdater/CodeUpdater/Updaters/CsProjUpdater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private void UpdateTargetFrameworksValue(XElement childElm, CsprojUpdateTracker
197197
}
198198
}
199199

200-
public class CsprojUpdateTracker
200+
private class CsprojUpdateTracker
201201
{
202202
public const string TargetFramework = "TargetFramework";
203203
public const string TargetFrameworks = "TargetFrameworks";

src/CodeUpdater/CodeUpdater/Updaters/NpmUpdater.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using Serilog;
1414

1515
namespace ProgrammerAL.Tools.CodeUpdater.Updaters;
16-
public class NpmUpdater(IRunProcessHelper RunProcessHelper)
16+
public class NpmUpdater(ILogger Logger, IRunProcessHelper RunProcessHelper)
1717
{
1818
public NpmUpdates UpdateNpmPackages(UpdateWork updateWork)
1919
{
@@ -33,7 +33,7 @@ public NpmUpdates UpdateNpmPackages(UpdateWork updateWork)
3333
}
3434
catch (Exception ex)
3535
{
36-
Log.Error(ex, "Error updating npm packages at path'{ProjectPath}'. Command was '{Command}'", projectPath, command);
36+
Logger.Error(ex, "Error updating npm packages at path'{ProjectPath}'. Command was '{Command}'", projectPath, command);
3737
}
3838
}
3939

0 commit comments

Comments
 (0)