Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Keep global.json formatting #25

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/NvGet/Tools/Updater/Extensions/XmlDocumentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Versioning;
using NvGet.Extensions;
using NvGet.Helpers;
using NvGet.Tools.Updater.Log;
using Uno.Extensions;

Expand Down Expand Up @@ -192,18 +192,26 @@ UpdateOperation operation
{
var operations = new List<UpdateOperation>();

var globalJson = JsonConvert.DeserializeObject<GlobalJson>(jsonDocReference.Contents);
var globalJson = JObject.Parse(jsonDocReference.Contents);

if(globalJson.MSBuildSdks.TryGetValue(operation.PackageId, out var value))
if(globalJson["msbuild-sdks"] is JObject msbuildSdks)
{
globalJson.MSBuildSdks[operation.PackageId] = operation.UpdatedVersion.ToString();
if(msbuildSdks.TryGetValue(operation.PackageId, out var value))
{
var currentOperation = operation.WithPreviousVersion(value.ToString());

var currentOperation = operation.WithPreviousVersion(value);
if(currentOperation.ShouldProceed())
{
// Use text replace to avoid changing the formatting of the file.

operations.Add(currentOperation);
}
var pattern = $"\"{operation.PackageId}\"\\s*:\\s*\"[^\"]*\"";
var replacement = $"\"{operation.PackageId}\": \"{operation.UpdatedVersion}\"";
jsonDocReference.Contents = Regex.Replace(jsonDocReference.Contents, pattern, replacement);
}

jsonDocReference.Contents = JsonConvert.SerializeObject(globalJson);
operations.Add(currentOperation);
}
}

return operations;
}
Expand Down
Loading