8000 Convert VMR tasks from Newtonsoft.Json to System.Text.Json by Copilot · Pull Request #715 · dotnet/dotnet · GitHub
[go: up one dir, main page]

Skip to content

Convert VMR tasks from Newtonsoft.Json to System.Text.Json #715

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
8000
Original file line numberDiff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
<PackageReference Include="NuGet.Protocol" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
<PackageReference Include="NuGet.ProjectModel" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
<PackageReference Include="Newtonsoft.Json" IncludeAssets="compile" NoWarn="NU1901;NU1902;NU1903;NU1904" />
</ItemGroup>

</Project>
29 changes: 19 additions & 10 deletions eng/tools/tasks/Microsoft.DotNet.UnifiedBuild.Tasks/UpdateJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand Down Expand Up @@ -37,23 +37,26 @@ public override bool Execute()

string json = File.ReadAllText(JsonFilePath);
string newLineChars = FileUtilities.DetectNewLineChars(json);
JObject jsonObj = JObject.Parse(json);
JsonNode jsonNode = JsonNode.Parse(json);

string[] escapedPathToAttributeParts = PathToAttribute.Split(Delimiter);
for (int i = 0; i < escapedPathToAttributeParts.Length; ++i)
{
escapedPathToAttributeParts[i] = escapedPathToAttributeParts[i];
}
UpdateAttribute(jsonObj, escapedPathToAttributeParts, NewAttributeValue);
UpdateAttribute(jsonNode, escapedPathToAttributeParts, NewAttributeValue);

File.WriteAllText(JsonFilePath, FileUtilities.NormalizeNewLineChars(jsonObj.ToString(), newLineChars));
var options = new JsonSerializerOptions { WriteIndented = true };
File.WriteAllText(JsonFilePath, FileUtilities.NormalizeNewLineChars(jsonNode.ToJsonString(options), newLineChars));
return true;
}

private void UpdateAttribute(JToken jsonObj, string[] path, string newValue)
private void UpdateAttribute(JsonNode jsonNode, string[] path, string newValue)
{
string pathItem = path[0];
if (jsonObj[pathItem] == null)
JsonNode current = jsonNode as JsonObject;

if (current is JsonObject jsonObject && !jsonObject.ContainsKey(pathItem))
{
string message = $"Path item [{nameof(PathToAttribute)}] not found in json file.";
if (SkipUpdateIfMissingKey)
Expand All @@ -68,16 +71,22 @@ private void UpdateAttribute(JToken jsonObj, string[] path, string newValue)
{
if (newValue == null)
{
jsonObj[pathItem].Parent.Remove();
if (current is JsonObject obj && obj.ContainsKey(pathItem))
{
obj.Remove(pathItem);
}
}
else
{
jsonObj[pathItem] = newValue;
if (current is JsonObject obj)
{
obj[pathItem] = JsonValue.Create(newValue);
}
}
return;
}

UpdateAttribute(jsonObj[pathItem], path.Skip(1).ToArray(), newValue);
UpdateAttribute(((JsonObject)current)[pathItem], path.Skip(1).ToArray(), newValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#nullable disable

using Microsoft.Build.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using System;
Expand All @@ -14,6 +12,8 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Task = Microsoft.Build.Utilities.Task;

Expand Down Expand Up @@ -181,46 +181,94 @@ public override bool Execute()
assetFiles,
assetFile =>
{
JObject jObj;
JsonDocument jsonDoc;

using (var file = File.OpenRead(Path.Combine(RootDir, assetFile)))
using (var reader = new StreamReader(file))
using (var jsonReader = new JsonTextReader(reader))
{
jObj = (JObject)JToken.ReadFrom(jsonReader);
jsonDoc = JsonDocument.Parse(file);
}

var properties = new HashSet<string>(
jObj.SelectTokens("$.targets.*").Children()
.Concat(jObj.SelectToken("$.libraries"))
.Select(t => ((JProperty)t).Name)
.Distinct(),
StringComparer.OrdinalIgnoreCase);
var root = jsonDoc.RootElement;

var properties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

// Get properties from targets
if (root.TryGetProperty("targets", out var targets))
{
foreach (var target in targets.EnumerateObject())
{
foreach (var prop in target.Value.EnumerateObject())
{
properties.Add(prop.Name);
}
}
}

// Get properties from libraries
if (root.TryGetProperty("libraries", out var libraries))
{
foreach (var library in libraries.EnumerateObject())
{
properties.Add(library.Name);
}
}

var directDependencies = jObj.SelectTokens("$.project.frameworks.*.dependencies").Children().Select(dep =>
new
var directDependencies = new List<(string name, string target, VersionRange version, bool autoReferenced)>();

// Get direct dependencies
if (root.TryGetProperty("project", out var project) &&
project.TryGetProperty("frameworks", out var frameworks))
{
foreach (var framework in frameworks.EnumerateObject())
{
name = ((JProperty)dep).Name,
target = dep.SelectToken("$..target")?.ToString(),
version = VersionRange.Parse(dep.SelectToken("$..version")?.ToString()),
autoReferenced = dep.SelectToken("$..autoReferenced")?.ToString() == "True",
})
.ToArray();
if (framework.Value.TryGetProperty("dependencies", out var dependencies))
{
foreach (var dep in dependencies.EnumerateObject())
{
string target = null;
string versionStr = null;
bool autoReferenced = false;

if (dep.Value.TryGetProperty("target", out var targetProp))
{
target = targetProp.GetString();
}

if (dep.Value.TryGetProperty("version", out var versionProp))
{
versionStr = versionProp.GetString();
}

if (dep.Value.TryGetProperty("autoReferenced", out var autoRefProp))
{
autoReferenced = autoRefProp.GetBoolean();
}

if (versionStr != null)
{
var version = VersionRange.Parse(versionStr);
directDependencies.Add((dep.Name, target, version, autoReferenced));
}
Comment on lines +247 to +251
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if branch didn't exist previously. Direct dependencies were always added, regardless of the version string being null. Not sure if it matters.

}
}
}
}

foreach (var identity in toCheck
.Where(id => properties.Contains(id.Id + "/" + id.Version.OriginalVersion)))
{
var directDependency =
directDependencies?.FirstOrDefault(
d => d.name == identity.Id &&
d.version.Satisfies(identity.Version));
var directDependency = directDependencies.FirstOrDefault(
d => d.name == identity.Id && d.version.Satisfies(identity.Version));

usages.Add(Usage.Create(
assetFile,
identity,
directDependency != null,
directDependency?.autoReferenced == true,
directDependency != default,
directDependency.autoReferenced,
possibleRids));
}

jsonDoc.Dispose();
});

Log.LogMessage(MessageImportance.Low, "Searching for unused packages...");
Expand Down Expand Up @@ -273,11 +321,14 @@ private string GetPathRelativeToRoot(string path)

private static string[] ReadRidsFromRuntimeJson(string path)
{
var root = JObject.Parse(File.ReadAllText(path));
return root["runtimes"]
.Values<JProperty>()
.Select(o => o.Name)
.ToArray();
using var jsonDoc = JsonDocument.Parse(File.ReadAllText(path));
if (jsonDoc.RootElement.TryGetProperty("runtimes", out var runtimes))
{
return runtimes.EnumerateObject()
.Select(p => p.Name)
.ToArray();
}
return Array.Empty<string>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@
#nullable disable

using Microsoft.Build.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand Down
0