Skip to content

Commit 3be2824

Browse files
committed
In progress updates. Trying to change how settings are configured so things are more optional
1 parent 357e91e commit 3be2824

File tree

3 files changed

+121
-55
lines changed

3 files changed

+121
-55
lines changed

src/CodeUpdater/CodeUpdater/Options/UpdateOptions.cs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99

1010
namespace ProgrammerAL.Tools.CodeUpdater;
11+
1112
public class UpdateOptions
1213
{
1314
/// <summary>
@@ -32,29 +33,9 @@ public class UpdateOptions
3233
[Required(AllowEmptyStrings = false)]
3334
public required string NpmBuildCommand { get; set; }
3435

35-
/// <summary>
36-
/// Target Framework to set in all *.CsProj files
37-
/// </summary>
38-
[Required(AllowEmptyStrings = false)]
39-
public required string DotNetTargetFramework { get; set; }
40-
41-
/// <summary>
42-
/// C# language version to set in all *.CsProj files
43-
/// </summary>
44-
[Required(AllowEmptyStrings = false)]
45-
public required string DotNetLangVersion { get; set; }
46-
47-
/// <summary>
48-
/// True to set the `EnableNetAnalyzers` csproj value to true, false to set it to false
49-
/// </summary>
50-
[Required]
51-
public required bool EnableNetAnalyzers { get; set; }
36+
public DotNetVersioningOptions? DotNetVersioningOptions { get; set; }
5237

53-
/// <summary>
54-
/// True to set the `EnforceCodeStyleInBuild` csproj value to true, false to set it to false
55-
/// </summary>
56-
[Required]
57-
public required bool EnforceCodeStyleInBuild { get; set; }
38+
public DotNetAnalyzerOptions? DotNetAnalyzerOptions { get; set; }
5839

5940
/// <summary>
6041
/// Settings to use for configuring Nuget Audit settings in csproj files.
@@ -84,3 +65,39 @@ public record NugetAuditOptions(
8465
[property: Required] string AuditMode,
8566
[property: Required] string AuditLevel);
8667
}
68+
69+
public class DotNetVersioningOptions
70+
{
71+
/// <summary>
72+
/// Target Framework to set in all *.csproj files
73+
/// </summary>
74+
[Required(AllowEmptyStrings = false)]
75+
public required string DotNetTargetFramework { get; set; }
76+
77+
/// <summary>
78+
/// C# language version to set in all *.csproj files
79+
/// </summary>
80+
[Required(AllowEmptyStrings = false)]
81+
public required string DotNetLangVersion { get; set; }
82+
83+
/// <summary>
84+
/// The value to set for the TreatWarningsAsErrors flag in all *.csproj files
85+
/// </summary>
86+
[Required]
87+
public required bool TreatWarningsAsErrors { get; set; }
88+
}
89+
90+
public class DotNetAnalyzerOptions
91+
{
92+
/// <summary>
93+
/// True to set the `EnableNetAnalyzers` csproj value to true, false to set it to false
94+
/// </summary>
95+
[Required]
96+
public required bool EnableNetAnalyzers { get; set; }
97+
98+
/// <summary>
99+
/// True to set the `EnforceCodeStyleInBuild` csproj value to true, false to set it to false
100+
/// </summary>
101+
[Required]
102+
public required bool EnforceCodeStyleInBuild { get; set; }
103+
}

src/CodeUpdater/CodeUpdater/Updaters/CsProjUpdater.cs

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,6 @@ public CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
1818

1919
var propertyGroups = csProjXmlDoc.Descendants("PropertyGroup").ToList();
2020

21-
var targetFrameworkUpdates = new CsprojUpdateTracker(
22-
CsprojUpdateTracker.TargetFramework,
23-
UpdateOptions.DotNetTargetFramework,
24-
addIfElementNotFound: false,
25-
skipStartsWithValues: ["netstandard"]);//Project is set to .NET Standard are there for a reason, don't change it
26-
var langUpdates = new CsprojUpdateTracker(
27-
CsprojUpdateTracker.LangVersion,
28-
UpdateOptions.DotNetLangVersion,
29-
addIfElementNotFound: true);
30-
var enableNETAnalyzersUpdates = new CsprojUpdateTracker(
31-
CsprojUpdateTracker.EnableNETAnalyzers,
32-
UpdateOptions.EnableNetAnalyzers.ToString().ToLower(),
33-
addIfElementNotFound: true);
34-
var enforceCodeStyleInBuildUpdates = new CsprojUpdateTracker(
35-
CsprojUpdateTracker.EnforceCodeStyleInBuild,
36-
UpdateOptions.EnforceCodeStyleInBuild.ToString().ToLower(),
37-
addIfElementNotFound: true);
38-
var nuGetAuditUpdates = new CsprojUpdateTracker(
39-
CsprojUpdateTracker.NuGetAudit,
40-
UpdateOptions.NugetAudit.NuGetAudit.ToString().ToLower(),
41-
addIfElementNotFound: true);
42-
var nugetAuditModeUpdates = new CsprojUpdateTracker(
43-
CsprojUpdateTracker.NuGetAuditMode,
44-
UpdateOptions.NugetAudit.AuditMode.ToString().ToLower(),
45-
addIfElementNotFound: true);
46-
var nugetAuditLevelUpdates = new CsprojUpdateTracker(
47-
CsprojUpdateTracker.NuGetAuditLevel,
48-
UpdateOptions.NugetAudit.AuditLevel,
49-
addIfElementNotFound: true);
5021

5122
UpdateOrAddCsProjValues(
5223
csProjXmlDoc,
@@ -77,6 +48,78 @@ public CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
7748
return new CsProjUpdateResult(csProjFilePath, langVersionUpdateType, targetFrameworkUpdate);
7849
}
7950

51+
private ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> DetermineProjectUpdateGroups()
52+
{
53+
var buidler = ImmutableArray.CreateBuilder<ImmutableArray<CsprojUpdateGroupTracker>>();
54+
55+
if (UpdateOptions.DotNetVersioningOptions is object)
56+
{
57+
var targetFrameworkUpdates = new CsprojUpdateTracker(
58+
CsprojUpdateTracker.TargetFramework,
59+
UpdateOptions.DotNetVersioningOptions.DotNetTargetFramework,
60+
addIfElementNotFound: false,
61+
skipStartsWithValues: ["netstandard"]);//Project is set to .NET Standard are there for a reason, don't change it
62+
63+
var langUpdates = new CsprojUpdateTracker(
64+
CsprojUpdateTracker.LangVersion,
65+
UpdateOptions.DotNetVersioningOptions.DotNetLangVersion,
66+
addIfElementNotFound: true);
67+
68+
var warningsAsErrorsUpdates = new CsprojUpdateTracker(
69+
CsprojUpdateTracker.TreatWarningsAsErrors,
70+
UpdateOptions.DotNetVersioningOptions.TreatWarningsAsErrors.ToString().ToLower(),
71+
addIfElementNotFound: true);
72+
73+
var theseBuilder = ImmutableArray.CreateBuilder<CsprojUpdateGroupTracker>();
74+
75+
theseBuilder.Add(
76+
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.DoNothing,
77+
[
78+
targetFrameworkUpdates,
79+
]));
80+
81+
theseBuilder.Add(
82+
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.AddElementToFirstPropertyGroup,
83+
[
84+
langUpdates,
85+
warningsAsErrorsUpdates,
86+
]));
87+
88+
buidler.Add(theseBuilder.ToImmutableArray());
89+
}
90+
91+
if (UpdateOptions.DotNetAnalyzerOptions is object)
92+
{
93+
var enableNETAnalyzersUpdates = new CsprojUpdateTracker(
94+
CsprojUpdateTracker.EnableNETAnalyzers,
95+
UpdateOptions.DotNetAnalyzerOptions.EnableNetAnalyzers.ToString().ToLower(),
96+
addIfElementNotFound: true);
97+
98+
var enforceCodeStyleInBuildUpdates = new CsprojUpdateTracker(
99+
CsprojUpdateTracker.EnforceCodeStyleInBuild,
100+
UpdateOptions.DotNetAnalyzerOptions.EnforceCodeStyleInBuild.ToString().ToLower(),
101+
addIfElementNotFound: true);
102+
103+
var theseBuilder = ImmutableArray.CreateBuilder<CsprojUpdateGroupTracker>();
104+
}
105+
106+
var nuGetAuditUpdates = new CsprojUpdateTracker(
107+
CsprojUpdateTracker.NuGetAudit,
108+
UpdateOptions.NugetAudit.NuGetAudit.ToString().ToLower(),
109+
addIfElementNotFound: true);
110+
var nugetAuditModeUpdates = new CsprojUpdateTracker(
111+
CsprojUpdateTracker.NuGetAuditMode,
112+
UpdateOptions.NugetAudit.AuditMode.ToString().ToLower(),
113+
addIfElementNotFound: true);
114+
var nugetAuditLevelUpdates = new CsprojUpdateTracker(
115+
CsprojUpdateTracker.NuGetAuditLevel,
116+
UpdateOptions.NugetAudit.AuditLevel,
117+
addIfElementNotFound: true);
118+
119+
120+
return buidler.ToImmutableArray();
121+
}
122+
80123
private void UpdateOrAddCsProjValues(XDocument csProjXmlDoc, List<XElement> propertyGroupsElements, params CsprojUpdateGroupTracker[] updateGroups)
81124
{
82125
//Separate updates into groups
@@ -245,6 +288,7 @@ private class CsprojUpdateTracker
245288
public const string TargetFramework = "TargetFramework";
246289
public const string TargetFrameworks = "TargetFrameworks";
247290
public const string LangVersion = "LangVersion";
291+
public const string TreatWarningsAsErrors = "TreatWarningsAsErrors";
248292
public const string EnableNETAnalyzers = "EnableNETAnalyzers";
249293
public const string EnforceCodeStyleInBuild = "EnforceCodeStyleInBuild";
250294
public const string NuGetAudit = "NuGetAudit";

src/CodeUpdater/CodeUpdater/sampleUpdateOptions.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
"RootDirectory": "C:/my-repos/my-app-1",
33
"IgnorePatterns": [],
44
"NpmBuildCommand": "publish",
5-
"DotNetTargetFramework": "net8.0",
6-
"DotNetLangVersion": "latest",
7-
"EnableNetAnalyzers": true,
8-
"EnforceCodeStyleInBuild": true,
5+
"DotNetVersioningOptions": {
6+
"TreatWarningsAsErrors": true,
7+
"DotNetTargetFramework": "net8.0",
8+
"DotNetLangVersion": "latest"
9+
},
10+
"DotNetAnalyzerOptions": {
11+
"EnableNetAnalyzers": true,
12+
"EnforceCodeStyleInBuild": true
13+
},
914
"RunDotnetFormat": true,
1015
"NugetAudit": {
1116
"NuGetAudit": true,

0 commit comments

Comments
 (0)