Skip to content

Commit 6d3c575

Browse files
committed
Some work on the updater
1 parent 3be2824 commit 6d3c575

File tree

3 files changed

+123
-61
lines changed

3 files changed

+123
-61
lines changed

src/CodeUpdater/CodeUpdater/Options/UpdateOptions.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public class UpdateOptions
4141
/// Settings to use for configuring Nuget Audit settings in csproj files.
4242
/// You can read more at https://learn.microsoft.com/en-us/nuget/concepts/auditing-packages#configuring-nuget-audit
4343
/// </summary>
44-
[Required]
45-
public required NugetAuditOptions NugetAudit { get; set; }
44+
public NugetAuditOptions? NugetAudit { get; set; }
4645

4746
/// <summary>
4847
/// True to run the `dotnet format` command
@@ -59,11 +58,6 @@ public class UpdateOptions
5958
/// Verbosity level to log. Valid values are: Verbose, Info, Warn, Error. Default value: verbose.
6059
/// </summary>
6160
public string? LogLevel { get; set; } = "verbose";
62-
63-
public record NugetAuditOptions(
64-
[property: Required] bool NuGetAudit,
65-
[property: Required] string AuditMode,
66-
[property: Required] string AuditLevel);
6761
}
6862

6963
public class DotNetVersioningOptions
@@ -101,3 +95,24 @@ public class DotNetAnalyzerOptions
10195
[Required]
10296
public required bool EnforceCodeStyleInBuild { get; set; }
10397
}
98+
99+
public class NugetAuditOptions
100+
{
101+
/// <summary>
102+
/// What value to set for the `NuGetAudit` property in the csproj file.
103+
/// </summary>
104+
[Required]
105+
public required bool NuGetAudit { get; set; }
106+
107+
/// <summary>
108+
/// What value to set for the `NuGetAuditMode` property in the csproj file. Valid values are `direct` and `all`.
109+
/// </summary>
110+
[Required]
111+
public required string AuditMode { get; set; }
112+
113+
/// <summary>
114+
/// What value to set for the `NuGetAuditLevel` property in the csproj file. Valid values are: `low`, `moderate`, `high`, and `critical`
115+
/// </summary>
116+
[Required]
117+
public required string AuditLevel { get; set; }
118+
}

src/CodeUpdater/CodeUpdater/Results.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace ProgrammerAL.Tools.CodeUpdater;
1010
public record CSharpUpdateResult(
1111
string CsprojFile,
1212
NugetUpdateResults NugetUpdates,
13-
CsprojValueUpdateResultType LangVersionUpdate,
14-
CsprojValueUpdateResultType TargetFrameworkUpdate,
13+
CsprojValueUpdateResultType? LangVersionUpdate,
14+
CsprojValueUpdateResultType? TargetFrameworkUpdate,
1515
DotnetFormatResult DotnetFormatUpdate);
1616

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

20-
public record CsProjUpdateResult(string CsProjFile, CsprojValueUpdateResultType LangVersionUpdate, CsprojValueUpdateResultType TargetFrameworkUpdate);
20+
public record CsProjUpdateResult(string CsProjFile, CsprojValueUpdateResultType? LangVersionUpdate, CsprojValueUpdateResultType? TargetFrameworkUpdate);
2121
public record DotnetFormatResult(string CsProjFile, DotnetFormatResultType Result);
2222

2323
public record NpmUpdates(ImmutableArray<string> NpmDirectories);

src/CodeUpdater/CodeUpdater/Updaters/CsProjUpdater.cs

Lines changed: 98 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.Immutable;
4+
using System.Diagnostics;
45
using System.Linq;
56
using System.Text;
67
using System.Threading.Tasks;
@@ -18,40 +19,83 @@ public CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
1819

1920
var propertyGroups = csProjXmlDoc.Descendants("PropertyGroup").ToList();
2021

22+
var projectUpdateGroups = DetermineProjectUpdateGroups();
2123

2224
UpdateOrAddCsProjValues(
2325
csProjXmlDoc,
2426
propertyGroups,
25-
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.DoNothing,
26-
[
27-
targetFrameworkUpdates,
28-
]),
29-
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.AddElementToFirstPropertyGroup,
30-
[
31-
langUpdates,
32-
enableNETAnalyzersUpdates,
33-
enforceCodeStyleInBuildUpdates,
34-
]),
35-
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.AddElementToNewPropertyGroup,
36-
[
37-
nuGetAuditUpdates,
38-
nugetAuditModeUpdates,
39-
nugetAuditLevelUpdates
40-
]));
27+
projectUpdateGroups);
4128

4229
//Write the file back out
4330
//Note: Use File.WriteAllText instead of Save() because calling XDocument.ToString() doesn't include the xml header
4431
File.WriteAllText(csProjFilePath, csProjXmlDoc.ToString(), Encoding.UTF8);
4532

46-
var langVersionUpdateType = langUpdates.GetFinalResult();
47-
var targetFrameworkUpdate = targetFrameworkUpdates.GetFinalResult();
33+
var langUpdates = projectUpdateGroups.SelectMany(x => x).SelectMany(x => x.UpdateTrackers).FirstOrDefault(x => x.ElementName == CsprojUpdateTracker.LangVersion);
34+
var targetFrameworkUpdates = projectUpdateGroups.SelectMany(x => x).SelectMany(x => x.UpdateTrackers).FirstOrDefault(x => x.ElementName == CsprojUpdateTracker.TargetFramework);
35+
36+
var langVersionUpdateType = langUpdates?.GetFinalResult();
37+
var targetFrameworkUpdate = targetFrameworkUpdates?.GetFinalResult();
4838
return new CsProjUpdateResult(csProjFilePath, langVersionUpdateType, targetFrameworkUpdate);
4939
}
5040

5141
private ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> DetermineProjectUpdateGroups()
5242
{
53-
var buidler = ImmutableArray.CreateBuilder<ImmutableArray<CsprojUpdateGroupTracker>>();
43+
var builder = ImmutableArray.CreateBuilder<ImmutableArray<CsprojUpdateGroupTracker>>();
44+
45+
var dotNetVersioningUpdates = GenerateUpdateGroupForDotNetVersioning();
46+
if (dotNetVersioningUpdates.Any())
47+
{
48+
builder.Add(dotNetVersioningUpdates);
49+
}
50+
51+
var dotNetAnalyzerUpdates = GenerateUpdateGroupForDotNetAnalyzers();
52+
if (dotNetAnalyzerUpdates.Any())
53+
{
54+
builder.Add(dotNetAnalyzerUpdates);
55+
}
56+
57+
var nugetAuditUpdates = GenerateUpdateGroupForNugetAudit();
58+
59+
if (nugetAuditUpdates.Any())
60+
{
61+
builder.Add(nugetAuditUpdates);
62+
}
63+
64+
return builder.ToImmutableArray();
65+
}
66+
67+
private ImmutableArray<CsprojUpdateGroupTracker> GenerateUpdateGroupForNugetAudit()
68+
{
69+
if (UpdateOptions.NugetAudit is object)
70+
{
71+
var nuGetAuditUpdates = new CsprojUpdateTracker(
72+
CsprojUpdateTracker.NuGetAudit,
73+
UpdateOptions.NugetAudit.NuGetAudit.ToString().ToLower(),
74+
addIfElementNotFound: true);
75+
var nugetAuditModeUpdates = new CsprojUpdateTracker(
76+
CsprojUpdateTracker.NuGetAuditMode,
77+
UpdateOptions.NugetAudit.AuditMode.ToString().ToLower(),
78+
addIfElementNotFound: true);
79+
var nugetAuditLevelUpdates = new CsprojUpdateTracker(
80+
CsprojUpdateTracker.NuGetAuditLevel,
81+
UpdateOptions.NugetAudit.AuditLevel,
82+
addIfElementNotFound: true);
5483

84+
var nugetUpdatesGroup = new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.AddElementToNewPropertyGroup,
85+
[
86+
nuGetAuditUpdates,
87+
nugetAuditModeUpdates,
88+
nugetAuditLevelUpdates
89+
]);
90+
91+
return [nugetUpdatesGroup];
92+
}
93+
94+
return ImmutableArray<CsprojUpdateGroupTracker>.Empty;
95+
}
96+
97+
private ImmutableArray<CsprojUpdateGroupTracker> GenerateUpdateGroupForDotNetVersioning()
98+
{
5599
if (UpdateOptions.DotNetVersioningOptions is object)
56100
{
57101
var targetFrameworkUpdates = new CsprojUpdateTracker(
@@ -70,24 +114,29 @@ private ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> DetermineProjec
70114
UpdateOptions.DotNetVersioningOptions.TreatWarningsAsErrors.ToString().ToLower(),
71115
addIfElementNotFound: true);
72116

73-
var theseBuilder = ImmutableArray.CreateBuilder<CsprojUpdateGroupTracker>();
117+
var builder = ImmutableArray.CreateBuilder<CsprojUpdateGroupTracker>();
74118

75-
theseBuilder.Add(
119+
builder.Add(
76120
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.DoNothing,
77121
[
78122
targetFrameworkUpdates,
79123
]));
80124

81-
theseBuilder.Add(
125+
builder.Add(
82126
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.AddElementToFirstPropertyGroup,
83127
[
84128
langUpdates,
85129
warningsAsErrorsUpdates,
86130
]));
87131

88-
buidler.Add(theseBuilder.ToImmutableArray());
132+
return builder.ToImmutableArray();
89133
}
90-
134+
135+
return ImmutableArray<CsprojUpdateGroupTracker>.Empty;
136+
}
137+
138+
private ImmutableArray<CsprojUpdateGroupTracker> GenerateUpdateGroupForDotNetAnalyzers()
139+
{
91140
if (UpdateOptions.DotNetAnalyzerOptions is object)
92141
{
93142
var enableNETAnalyzersUpdates = new CsprojUpdateTracker(
@@ -100,49 +149,47 @@ private ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> DetermineProjec
100149
UpdateOptions.DotNetAnalyzerOptions.EnforceCodeStyleInBuild.ToString().ToLower(),
101150
addIfElementNotFound: true);
102151

103-
var theseBuilder = ImmutableArray.CreateBuilder<CsprojUpdateGroupTracker>();
104-
}
152+
var builder = ImmutableArray.CreateBuilder<CsprojUpdateGroupTracker>();
105153

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);
154+
builder.Add(
155+
new CsprojUpdateGroupTracker(CsprojUpdateGroupTracker.NotFoundActionType.AddElementToNewPropertyGroup,
156+
[
157+
enableNETAnalyzersUpdates,
158+
enforceCodeStyleInBuildUpdates
159+
]));
118160

161+
return builder.ToImmutableArray();
162+
}
119163

120-
return buidler.ToImmutableArray();
164+
return ImmutableArray<CsprojUpdateGroupTracker>.Empty;
121165
}
122166

123-
private void UpdateOrAddCsProjValues(XDocument csProjXmlDoc, List<XElement> propertyGroupsElements, params CsprojUpdateGroupTracker[] updateGroups)
167+
private void UpdateOrAddCsProjValues(XDocument csProjXmlDoc, List<XElement> propertyGroupsElements, ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> updateGroups)
124168
{
125169
//Separate updates into groups
126170
// This way, when the groups are added to the csproj, they are grouped together to the same PropetyGroup
127171
// Not important for functional reasons, but it makes the csproj file easier to read
128-
foreach (var group in updateGroups)
172+
foreach (var trackerGroup in updateGroups)
129173
{
130-
foreach (var propertyGroupElement in propertyGroupsElements)
174+
foreach (var tracker in trackerGroup)
131175
{
132-
foreach (var update in group.UpdateTrackers)
176+
foreach (var update in tracker.UpdateTrackers)
133177
{
134-
if (update.ElementName == CsprojUpdateTracker.TargetFramework)
135-
{
136-
UpdateTargetFrameworkValue(propertyGroupElement, update);
137-
}
138-
else
178+
foreach (var propertyGroupElement in propertyGroupsElements)
139179
{
140-
UpdateCsprojValue(propertyGroupElement, update);
180+
if (update.ElementName == CsprojUpdateTracker.TargetFramework)
181+
{
182+
UpdateTargetFrameworkValue(propertyGroupElement, update);
183+
}
184+
else
185+
{
186+
UpdateCsprojValue(propertyGroupElement, update);
187+
}
141188
}
142189
}
143-
}
144190

145-
AddMissingElements(csProjXmlDoc, group);
191+
AddMissingElements(csProjXmlDoc, tracker);
192+
}
146193
}
147194
}
148195

0 commit comments

Comments
 (0)