Skip to content

Commit 82fda36

Browse files
committed
UPdated all config settings to use objects to make everything optional instead of opinionated like this project started. Compiles but haven't tested anything.
1 parent 6d3c575 commit 82fda36

File tree

8 files changed

+281
-191
lines changed

8 files changed

+281
-191
lines changed

src/CodeUpdater/CodeUpdater/CompileRunner.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@
1313
namespace ProgrammerAL.Tools.CodeUpdater;
1414
public class CompileRunner(ILogger Logger, IRunProcessHelper RunProcessHelper)
1515
{
16-
public async ValueTask<CompileResults> CompileProjectsAsync(UpdateWork updateWork, string npmBuildCommand)
16+
public async ValueTask<CompileResults> CompileProjectsAsync(UpdateWork updateWork, UpdateOptions updateOptions)
1717
{
1818
var csharpBuildResults = await CompileAllCSharpProjectsAsync(updateWork);
19-
var npmDirectoryBuildResults = await BuildAllNpmDirectoriesAsync(updateWork, npmBuildCommand);
19+
20+
var npmBuildCommand = updateOptions.NpmOptions?.NpmBuildCommand;
21+
var npmDirectoryBuildResults = new CompileNpmDirectoryResults(ImmutableArray<CompileNpmDirectoryResult>.Empty);
22+
if (!string.IsNullOrWhiteSpace(npmBuildCommand))
23+
{
24+
npmDirectoryBuildResults = await BuildAllNpmDirectoriesAsync(updateWork, npmBuildCommand);
25+
}
2026

2127
return new CompileResults(csharpBuildResults, npmDirectoryBuildResults);
2228
}

src/CodeUpdater/CodeUpdater/Options/UpdateOptions.cs

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11

2-
using System;
3-
using System.Collections.Generic;
4-
using System.Collections.Immutable;
52
using System.ComponentModel.DataAnnotations;
6-
using System.Linq;
7-
using System.Text;
8-
using System.Threading.Tasks;
93

104
namespace ProgrammerAL.Tools.CodeUpdater;
115

@@ -28,51 +22,72 @@ public class UpdateOptions
2822
public required IEnumerable<string> IgnorePatterns { get; set; }
2923

3024
/// <summary>
31-
/// Npm command to run to \"compile\" the npm directory. Format run is: npm run <NpmBuildCommand>.
25+
/// Options for updating C# projects and code
26+
/// </summary>
27+
public CSharpOptions? CSharpOptions { get; set; }
28+
29+
/// <summary>
30+
/// Options for updating Npm packages
31+
/// </summary>
32+
public NpmOptions? NpmOptions { get; set; }
33+
34+
/// <summary>
35+
/// Options for output logging of the operation
36+
/// </summary>
37+
public LoggingOptions? LoggingOptions { get; set; }
38+
}
39+
40+
public class NpmOptions
41+
{
42+
/// <summary>
43+
/// Npm command to \"compile\" the npm directory. Format run is: npm run <NpmBuildCommand>.
3244
/// </summary>
3345
[Required(AllowEmptyStrings = false)]
3446
public required string NpmBuildCommand { get; set; }
47+
}
3548

36-
public DotNetVersioningOptions? DotNetVersioningOptions { get; set; }
37-
38-
public DotNetAnalyzerOptions? DotNetAnalyzerOptions { get; set; }
49+
public class CSharpOptions
50+
{
51+
/// <summary>
52+
/// Versioning options for csproj files
53+
/// </summary>
54+
public CsProjVersioningOptions? CsProjVersioningOptions { get; set; }
3955

4056
/// <summary>
41-
/// Settings to use for configuring Nuget Audit settings in csproj files.
42-
/// You can read more at https://learn.microsoft.com/en-us/nuget/concepts/auditing-packages#configuring-nuget-audit
57+
/// Analyzers that are set in the csproj files
4358
/// </summary>
44-
public NugetAuditOptions? NugetAudit { get; set; }
59+
public CsProjDotNetAnalyzerOptions? CsProjDotNetAnalyzerOptions { get; set; }
4560

4661
/// <summary>
47-
/// True to run the `dotnet format` command
62+
/// Options for any code styling updates that will be performed over C# code
4863
/// </summary>
49-
[Required]
50-
public required bool RunDotnetFormat { get; set; }
64+
public CSharpStyleOptions? CSharpStyleOptions { get; set; }
5165

5266
/// <summary>
53-
/// If this is set, it will be the file to write logs to, in addition to the console
67+
/// Settings to use for configuring Nuget Audit settings in csproj files.
68+
/// You can read more at https://learn.microsoft.com/en-us/nuget/concepts/auditing-packages#configuring-nuget-audit
5469
/// </summary>
55-
public string? OutputFile { get; set; }
70+
public NugetAuditOptions? NugetAudit { get; set; }
5671

5772
/// <summary>
58-
/// Verbosity level to log. Valid values are: Verbose, Info, Warn, Error. Default value: verbose.
73+
/// Settings to use for updating NuGet packages in csproj files
5974
/// </summary>
60-
public string? LogLevel { get; set; } = "verbose";
75+
public NuGetUpdateOptions? NuGetUpdates { get; set; }
6176
}
6277

63-
public class DotNetVersioningOptions
78+
public class CsProjVersioningOptions
6479
{
6580
/// <summary>
6681
/// Target Framework to set in all *.csproj files
6782
/// </summary>
6883
[Required(AllowEmptyStrings = false)]
69-
public required string DotNetTargetFramework { get; set; }
84+
public required string TargetFramework { get; set; }
7085

7186
/// <summary>
7287
/// C# language version to set in all *.csproj files
7388
/// </summary>
7489
[Required(AllowEmptyStrings = false)]
75-
public required string DotNetLangVersion { get; set; }
90+
public required string LangVersion { get; set; }
7691

7792
/// <summary>
7893
/// The value to set for the TreatWarningsAsErrors flag in all *.csproj files
@@ -81,7 +96,7 @@ public class DotNetVersioningOptions
8196
public required bool TreatWarningsAsErrors { get; set; }
8297
}
8398

84-
public class DotNetAnalyzerOptions
99+
public class CsProjDotNetAnalyzerOptions
85100
{
86101
/// <summary>
87102
/// True to set the `EnableNetAnalyzers` csproj value to true, false to set it to false
@@ -96,6 +111,15 @@ public class DotNetAnalyzerOptions
96111
public required bool EnforceCodeStyleInBuild { get; set; }
97112
}
98113

114+
public class CSharpStyleOptions
115+
{
116+
/// <summary>
117+
/// True to run the `dotnet format` command
118+
/// </summary>
119+
[Required]
120+
public required bool RunDotnetFormat { get; set; }
121+
}
122+
99123
public class NugetAuditOptions
100124
{
101125
/// <summary>
@@ -107,12 +131,42 @@ public class NugetAuditOptions
107131
/// <summary>
108132
/// What value to set for the `NuGetAuditMode` property in the csproj file. Valid values are `direct` and `all`.
109133
/// </summary>
110-
[Required]
134+
[Required(AllowEmptyStrings = false)]
111135
public required string AuditMode { get; set; }
112136

113137
/// <summary>
114138
/// What value to set for the `NuGetAuditLevel` property in the csproj file. Valid values are: `low`, `moderate`, `high`, and `critical`
115139
/// </summary>
116-
[Required]
140+
[Required(AllowEmptyStrings = false)]
117141
public required string AuditLevel { get; set; }
118142
}
143+
144+
public class NuGetUpdateOptions
145+
{
146+
/// <summary>
147+
/// True to updates all referenced nugets to the latest version. These are the references in the csproj files.
148+
/// </summary>
149+
[Required]
150+
public bool UpdateTopLevelNugetsInCsProj { get; set; }
151+
152+
/// <summary>
153+
/// True to updates all indirect nugets to the latest version. These are the nugets that are referenced automatically based on SDK chosen or something like that.
154+
/// </summary>
155+
[Required]
156+
public bool UpdateTopLevelNugetsNotInCsProj { get; set; }
157+
}
158+
159+
public class LoggingOptions
160+
{
161+
/// <summary>
162+
/// If this is set, it will be the file to write logs to, in addition to the console
163+
/// </summary>
164+
[Required(AllowEmptyStrings = false)]
165+
public required string OutputFile { get; set; }
166+
167+
/// <summary>
168+
/// Verbosity level to log. Valid values are: Verbose, Info, Warn, Error. Default value: verbose.
169+
/// </summary>
170+
[Required(AllowEmptyStrings = false)]
171+
public required string LogLevel { get; set; } = "verbose";
172+
}

src/CodeUpdater/CodeUpdater/Program.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static async ValueTask RunAsync(CommandOptions commandOptions)
4949
//After updating everything, compile all projects
5050
// Don't do this in the above loop in case a project needs an update that would cause it to not compile
5151
// So wait for all projects to be updated
52-
var compileResults = await compileRunner.CompileProjectsAsync(updateWork, updateOptions.NpmBuildCommand);
52+
var compileResults = await compileRunner.CompileProjectsAsync(updateWork, updateOptions);
5353

5454
OutputSummary(updateWork, csUpdates, npmUpdates, compileResults, logger);
5555
}
@@ -59,25 +59,28 @@ static ILogger SetupLogger(UpdateOptions updateOptions)
5959
var loggerConfig = new LoggerConfiguration()
6060
.WriteTo.Console();
6161

62-
if (!string.IsNullOrWhiteSpace(updateOptions.OutputFile))
62+
if (updateOptions.LoggingOptions is object)
6363
{
64-
loggerConfig = loggerConfig.WriteTo.File(updateOptions.OutputFile);
65-
}
64+
if (!string.IsNullOrWhiteSpace(updateOptions.LoggingOptions.OutputFile))
65+
{
66+
loggerConfig = loggerConfig.WriteTo.File(updateOptions.LoggingOptions.OutputFile);
67+
}
6668

67-
if (string.IsNullOrWhiteSpace(updateOptions.LogLevel))
68-
{
69-
loggerConfig = loggerConfig.MinimumLevel.Verbose();
70-
}
71-
else
72-
{
73-
loggerConfig = updateOptions.LogLevel.ToLower() switch
69+
if (string.IsNullOrWhiteSpace(updateOptions.LoggingOptions.LogLevel))
70+
{
71+
loggerConfig = loggerConfig.MinimumLevel.Verbose();
72+
}
73+
else
7474
{
75-
"verbose" => loggerConfig.MinimumLevel.Verbose(),
76-
"info" => loggerConfig.MinimumLevel.Information(),
77-
"warn" => loggerConfig.MinimumLevel.Warning(),
78-
"error" => loggerConfig.MinimumLevel.Error(),
79-
_ => loggerConfig.MinimumLevel.Verbose(),
80-
};
75+
loggerConfig = updateOptions.LoggingOptions.LogLevel.ToLower() switch
76+
{
77+
"verbose" => loggerConfig.MinimumLevel.Verbose(),
78+
"info" => loggerConfig.MinimumLevel.Information(),
79+
"warn" => loggerConfig.MinimumLevel.Warning(),
80+
"error" => loggerConfig.MinimumLevel.Error(),
81+
_ => loggerConfig.MinimumLevel.Verbose(),
82+
};
83+
}
8184
}
8285

8386
Log.Logger = loggerConfig.CreateLogger();

src/CodeUpdater/CodeUpdater/Updaters/CSharpUpdater.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Immutable;
4-
using System.Linq;
1+
using System.Collections.Immutable;
52

63
using ProgrammerAL.Tools.CodeUpdater.Helpers;
7-
using ProgrammerAL.Tools.CodeUpdater.Options;
84

95
using Serilog;
106

@@ -29,20 +25,26 @@ public CSharpUpdater(
2925
_runProcessHelper = runProcessHelper;
3026
_updateOptions = updateOptions;
3127
_nugetUpdater = new NugetUpdater(_logger, _runProcessHelper);
32-
_csProjUpdater = new CsProjUpdater(_logger, updateOptions);
33-
_csCodeUpdater = new CsCodeUpdater(_logger, _runProcessHelper, updateOptions);
28+
_csProjUpdater = new CsProjUpdater(_logger);
29+
_csCodeUpdater = new CsCodeUpdater(_logger, _runProcessHelper);
3430
}
3531

3632
public async ValueTask<ImmutableArray<CSharpUpdateResult>> UpdateAllCSharpProjectsAsync(UpdateWork updateWork)
3733
{
34+
if (_updateOptions.CSharpOptions is null)
35+
{
36+
_logger.Information("No C# options provided, skipping C# updates");
37+
return ImmutableArray<CSharpUpdateResult>.Empty;
38+
}
39+
3840
var builder = ImmutableArray.CreateBuilder<CSharpUpdateResult>();
3941
foreach (var csProjFilePath in updateWork.CsProjectFiles)
4042
{
4143
_logger.Information($"Updating '{csProjFilePath}'");
4244

43-
var csProjUpdates = UpdateCsProjPropertyValues(csProjFilePath);
44-
var nugetUpdates = await UpdateNugetPackagesAsync(csProjFilePath);
45-
var dotnetFormatUpdate = await RunDotnetFormatAsync(csProjFilePath);
45+
var csProjUpdates = UpdateCsProjPropertyValues(csProjFilePath, _updateOptions.CSharpOptions);
46+
var dotnetFormatUpdate = await RunDotnetFormatAsync(csProjFilePath, _updateOptions.CSharpOptions);
47+
var nugetUpdates = await UpdateNugetPackagesAsync(csProjFilePath, _updateOptions.CSharpOptions);
4648

4749
builder.Add(new CSharpUpdateResult(
4850
csProjFilePath,
@@ -55,11 +57,17 @@ public async ValueTask<ImmutableArray<CSharpUpdateResult>> UpdateAllCSharpProjec
5557
return builder.ToImmutableArray();
5658
}
5759

58-
private async ValueTask<NugetUpdateResults> UpdateNugetPackagesAsync(string csProjFilePath)
60+
private async ValueTask<NugetUpdateResults> UpdateNugetPackagesAsync(string csProjFilePath, CSharpOptions cSharpOptions)
5961
{
6062
try
6163
{
62-
return await _nugetUpdater.UpdateNugetPackagesAsync(csProjFilePath);
64+
if (cSharpOptions.NuGetUpdates is null)
65+
{
66+
_logger.Information("No NuGet options provided, skipping NuGet updates");
67+
return new NugetUpdateResults(RetrievedPackageListSuccessfully: true, ImmutableArray<NugetUpdateResult>.Empty);
68+
}
69+
70+
return await _nugetUpdater.UpdateNugetPackagesAsync(csProjFilePath, cSharpOptions.NuGetUpdates);
6371
}
6472
catch (Exception ex)
6573
{
@@ -68,11 +76,13 @@ private async ValueTask<NugetUpdateResults> UpdateNugetPackagesAsync(string csPr
6876
}
6977
}
7078

71-
private CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
79+
private CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath, CSharpOptions cSharpOptions)
7280
{
7381
try
7482
{
75-
return _csProjUpdater.UpdateCsProjPropertyValues(csProjFilePath);
83+
return _csProjUpdater.UpdateCsProjPropertyValues(
84+
csProjFilePath,
85+
cSharpOptions);
7686
}
7787
catch (Exception ex)
7888
{
@@ -81,11 +91,17 @@ private CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
8191
}
8292
}
8393

84-
private async Task<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath)
94+
private async Task<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath, CSharpOptions cSharpOptions)
8595
{
8696
try
8797
{
88-
return await _csCodeUpdater.RunDotnetFormatAsync(csProjFilePath);
98+
if (cSharpOptions.CSharpStyleOptions is null)
99+
{
100+
_logger.Information("No C# style options provided, skipping dotnet format");
101+
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.DidNotRun);
102+
}
103+
104+
return await _csCodeUpdater.RunDotnetFormatAsync(csProjFilePath, cSharpOptions.CSharpStyleOptions);
89105
}
90106
catch (Exception ex)
91107
{

src/CodeUpdater/CodeUpdater/Updaters/CsCodeUpdater.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
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-
1+

92
using ProgrammerAL.Tools.CodeUpdater.Helpers;
103

114
using Serilog;
125

136
namespace ProgrammerAL.Tools.CodeUpdater.Updaters;
147

15-
public class CsCodeUpdater(ILogger Logger, IRunProcessHelper RunProcessHelper, UpdateOptions UpdateOptions)
8+
public class CsCodeUpdater(ILogger Logger, IRunProcessHelper RunProcessHelper)
169
{
17-
public async ValueTask<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath)
10+
public async ValueTask<DotnetFormatResult> RunDotnetFormatAsync(string csProjFilePath, CSharpStyleOptions cSharpStyleOptions)
1811
{
19-
if (!UpdateOptions.RunDotnetFormat)
12+
if (!cSharpStyleOptions.RunDotnetFormat)
2013
{
2114
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.DidNotRun);
2215
}

0 commit comments

Comments
 (0)