Skip to content

Commit

Permalink
Merge pull request #25 from unoplatform/dev/jela/globaljson-format
Browse files Browse the repository at this point in the history
fix: Keep global.json formatting
  • Loading branch information
jeromelaban authored Nov 9, 2024
2 parents f2fea1d + 8fb6a00 commit 0350fd1
Showing 1 changed file with 17 additions and 9 deletions.
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

0 comments on commit 0350fd1

Please sign in to comment.