Skip to content

Commit 5508199

Browse files
committed
Changed how xml is output. Just re-do the formatting
1 parent 82fda36 commit 5508199

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/CodeUpdater/CodeUpdater/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static async Task<UpdateOptions> LoadUpdateOptionsAsync(string configFilePath)
103103
var updateOptions = JsonSerializer.Deserialize<UpdateOptions>(updateOptionJson, new JsonSerializerOptions
104104
{
105105
PropertyNameCaseInsensitive = true,
106+
AllowTrailingCommas = true,
106107
});
107108

108109
if (updateOptions is null)

src/CodeUpdater/CodeUpdater/Updaters/CsCodeUpdater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async ValueTask<DotnetFormatResult> RunDotnetFormatAsync(string csProjFil
1414
return new DotnetFormatResult(csProjFilePath, DotnetFormatResultType.DidNotRun);
1515
}
1616

17-
string commandArgs = $"format \"{csProjFilePath}\"";
17+
string commandArgs = $"format \"{csProjFilePath}\" --no-restore --verbosity diagnostic";
1818
try
1919
{
2020
var processResult = await RunProcessHelper.RunProcessToCompletionAndGetOutputAsync("dotnet", commandArgs);

src/CodeUpdater/CodeUpdater/Updaters/CsProjUpdater.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Text;
77
using System.Threading.Tasks;
8+
using System.Xml;
89
using System.Xml.Linq;
910

1011
using Serilog;
@@ -15,20 +16,20 @@ public class CsProjUpdater(ILogger Logger)
1516
{
1617
public CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath, CSharpOptions cSharpOptions)
1718
{
18-
var csProjXmlDoc = XDocument.Load(csProjFilePath, LoadOptions.PreserveWhitespace);
19-
19+
var csProjXmlDoc = XDocument.Load(csProjFilePath);
20+
var rootElm = csProjXmlDoc.Root ?? throw new Exception($"*.csproj file has no root xml element: {csProjFilePath}");
2021
var propertyGroups = csProjXmlDoc.Descendants("PropertyGroup").ToList();
2122

2223
var projectUpdateGroups = DetermineProjectUpdateGroups(cSharpOptions);
2324

2425
UpdateOrAddCsProjValues(
25-
csProjXmlDoc,
26+
rootElm,
2627
propertyGroups,
2728
projectUpdateGroups);
2829

2930
//Write the file back out
3031
//Note: Use File.WriteAllText instead of Save() because calling XDocument.ToString() doesn't include the xml header
31-
File.WriteAllText(csProjFilePath, csProjXmlDoc.ToString(), Encoding.UTF8);
32+
File.WriteAllText(csProjFilePath, rootElm.ToString(), Encoding.UTF8);
3233

3334
var langUpdates = projectUpdateGroups.SelectMany(x => x).SelectMany(x => x.UpdateTrackers).FirstOrDefault(x => x.ElementName == CsprojUpdateTracker.LangVersion);
3435
var targetFrameworkUpdates = projectUpdateGroups.SelectMany(x => x).SelectMany(x => x.UpdateTrackers).FirstOrDefault(x => x.ElementName == CsprojUpdateTracker.TargetFramework);
@@ -158,7 +159,7 @@ private ImmutableArray<CsprojUpdateGroupTracker> GenerateUpdateGroupForDotNetAna
158159
return builder.ToImmutableArray();
159160
}
160161

161-
private void UpdateOrAddCsProjValues(XDocument csProjXmlDoc, List<XElement> propertyGroupsElements, ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> updateGroups)
162+
private void UpdateOrAddCsProjValues(XElement csProjXml, List<XElement> propertyGroupsElements, ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> updateGroups)
162163
{
163164
//Separate updates into groups
164165
// This way, when the groups are added to the csproj, they are grouped together to the same PropetyGroup
@@ -182,12 +183,12 @@ private void UpdateOrAddCsProjValues(XDocument csProjXmlDoc, List<XElement> prop
182183
}
183184
}
184185

185-
AddMissingElements(csProjXmlDoc, tracker);
186+
AddMissingElements(csProjXml, tracker);
186187
}
187188
}
188189
}
189190

190-
private void AddMissingElements(XDocument csProjXmlDoc, CsprojUpdateGroupTracker updateGroup)
191+
private void AddMissingElements(XElement csProjXml, CsprojUpdateGroupTracker updateGroup)
191192
{
192193
var toUpdate = updateGroup.UpdateTrackers.Where(x => !x.HasMadeRequiredUpdate()).ToImmutableArray();
193194

@@ -199,13 +200,14 @@ private void AddMissingElements(XDocument csProjXmlDoc, CsprojUpdateGroupTracker
199200
XElement? propertyGroupToAddTo = null;
200201
if (updateGroup.NotFoundAction == CsprojUpdateGroupTracker.NotFoundActionType.AddElementToFirstPropertyGroup)
201202
{
202-
propertyGroupToAddTo = csProjXmlDoc.Descendants("PropertyGroup").FirstOrDefault();
203+
propertyGroupToAddTo = csProjXml.Descendants("PropertyGroup").FirstOrDefault();
203204
}
204205
else if (updateGroup.NotFoundAction == CsprojUpdateGroupTracker.NotFoundActionType.AddElementToNewPropertyGroup)
205206
{
206207
Logger.Information("Adding new PropertyGroup element for other required elements");
207208
propertyGroupToAddTo = new XElement("PropertyGroup");
208-
csProjXmlDoc.Root!.Add(propertyGroupToAddTo);
209+
210+
csProjXml.Add(propertyGroupToAddTo);
209211
}
210212

211213
//Add the trackers that haven't already set the final value
@@ -216,7 +218,6 @@ private void AddMissingElements(XDocument csProjXmlDoc, CsprojUpdateGroupTracker
216218
Logger.Information($"Adding {trackers.ElementName} element to csproj");
217219
var newElement = new XElement(trackers.ElementName, trackers.NewValue);
218220
propertyGroupToAddTo.Add(newElement);
219-
220221
trackers.SetResults.Add(CsprojValueUpdateResultType.AddedElement);
221222
}
222223
}

0 commit comments

Comments
 (0)