Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 2 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
<ItemGroup>
<PackageVersion Include="FakeItEasy" Version="8.1.0" />
<PackageVersion Include="AwesomeAssertions" Version="8.0.2" />
<PackageVersion Include="Newtonsoft.Json.Schema" Version="4.0.1" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="JsonSchema.Net" Version="7.3.2" />
Comment thread
NikolaMilosavljevic marked this conversation as resolved.
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.556" />
<PackageVersion Include="Verify.DiffPlex" Version="3.0.0" />
<PackageVersion Include="Verify.XUnit" Version="25.0.2" />
Expand All @@ -62,7 +61,7 @@
<PackageVersion Update="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsLoggingAbstractionsVersion)" Condition="'$(MicrosoftExtensionsLoggingAbstractionsVersion)' != ''" />
<PackageVersion Update="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsoleVersion)" Condition="'$(MicrosoftExtensionsLoggingConsoleVersion)' != ''" />
<PackageVersion Update="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingVersion)" Condition="'$(MicrosoftExtensionsLoggingVersion)' != ''" />
<PackageVersion Update="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" Condition="'$(NewtonsoftJsonVersion)' != ''" />

<PackageVersion Update="NuGet.Configuration" Version="$(NuGetConfigurationVersion)" Condition="'$(NuGetConfigurationVersion)' != ''" />
<PackageVersion Update="NuGet.Credentials" Version="$(NuGetCredentialsVersion)" Condition="'$(NuGetCredentialsVersion)' != ''" />
<PackageVersion Update="NuGet.Protocol" Version="$(NuGetProtocolVersion)" Condition="'$(NuGetProtocolVersion)' != ''" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;

using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Installer;
using Microsoft.TemplateEngine.Edge.Settings;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.TemplateEngine.Edge.BuiltInManagedProvider
{
Expand Down Expand Up @@ -82,20 +83,20 @@ public async Task<IReadOnlyList<TemplatePackageData>> GetInstalledTemplatePackag
var jObject = _environmentSettings.Host.FileSystem.ReadObject(_globalSettingsFile);
var packages = new List<TemplatePackageData>();

foreach (var package in jObject.Get<JArray>(nameof(GlobalSettingsData.Packages)) ?? new JArray())
foreach (var package in jObject.Get<JsonArray>(nameof(GlobalSettingsData.Packages)) ?? new JsonArray())
{
packages.Add(new TemplatePackageData(
package.ToGuid(nameof(TemplatePackageData.InstallerId)),
package.Value<string>(nameof(TemplatePackageData.MountPointUri)) ?? string.Empty,
((DateTime?)package[nameof(TemplatePackageData.LastChangeTime)]) ?? default,
package!.ToGuid(nameof(TemplatePackageData.InstallerId)),
package.ToString(nameof(TemplatePackageData.MountPointUri)) ?? string.Empty,
package![nameof(TemplatePackageData.LastChangeTime)]?.GetValue<DateTime>() ?? default,
package.ToStringDictionary(propertyName: nameof(TemplatePackageData.Details))));
}

return packages;
}
catch (JsonReaderException ex)
catch (JsonException ex)
{
var wrappedEx = new JsonReaderException(string.Format(LocalizableStrings.GlobalSettings_Error_CorruptedSettings, _globalSettingsFile, ex.Message), ex);
var wrappedEx = new JsonException(string.Format(LocalizableStrings.GlobalSettings_Error_CorruptedSettings, _globalSettingsFile, ex.Message), ex.Path, ex.LineNumber, ex.BytePositionInLine, ex);
throw wrappedEx;
}
catch (Exception)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Serialization;
using Microsoft.TemplateEngine.Abstractions.Installer;
using Newtonsoft.Json;

namespace Microsoft.TemplateEngine.Edge.BuiltInManagedProvider
{
Expand All @@ -16,7 +16,7 @@ internal GlobalSettingsData(IReadOnlyList<TemplatePackageData> packages)
Packages = packages;
}

[JsonProperty]
[JsonInclude]
internal IReadOnlyList<TemplatePackageData> Packages { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.TemplateEngine.Utils;
using Newtonsoft.Json.Linq;

namespace Microsoft.TemplateEngine.Edge.Constraints
{
Expand All @@ -16,23 +17,29 @@ internal static class Extensions
/// <exception cref="ConfigurationException">Thrown on unexpected input - not a valid json string or array of string or an empty array.</exception>
public static IEnumerable<string> ParseArrayOfConstraintStrings(this string? args)
{
JToken token = ParseConstraintJToken(args);
JsonNode token = ParseConstraintJsonNode(args);

if (token.Type == JTokenType.String)
if (token.GetValueKind() == JsonValueKind.String)
{
return new[] { token.Value<string>() ?? throw new ConfigurationException(string.Format(LocalizableStrings.Constraint_Error_ArgumentHasEmptyString, args)) };
return new[] { token.GetValue<string>() ?? throw new ConfigurationException(string.Format(LocalizableStrings.Constraint_Error_ArgumentHasEmptyString, args)) };
}

JArray array = token.ToConstraintsJArray(args, true);
JsonArray array = token.ToConstraintsJsonArray(args, true);

return array.Values<string>().Select(value =>
return array.Select(value =>
{
if (string.IsNullOrEmpty(value))
if (value == null || value.GetValueKind() != JsonValueKind.String)
{
throw new ConfigurationException(string.Format(LocalizableStrings.Constraint_Error_ArgumentHasEmptyString, args));
}

string? strValue = value.GetValue<string>();
if (string.IsNullOrEmpty(strValue))
{
throw new ConfigurationException(string.Format(LocalizableStrings.Constraint_Error_ArgumentHasEmptyString, args));
}

return value!;
return strValue!;
});
}

Expand All @@ -42,14 +49,14 @@ public static IEnumerable<string> ParseArrayOfConstraintStrings(this string? arg
/// <param name="args">Input configuration string.</param>
/// <returns>Enumeration of parsed JObject tokens.</returns>
/// <exception cref="ConfigurationException">Thrown on unexpected input - not a valid json array or an empty array.</exception>
public static IEnumerable<JObject> ParseArrayOfConstraintJObjects(this string? args)
public static IEnumerable<JsonObject> ParseArrayOfConstraintJObjects(this string? args)
{
JToken token = ParseConstraintJToken(args);
JArray array = token.ToConstraintsJArray(args, false);
JsonNode token = ParseConstraintJsonNode(args);
JsonArray array = token.ToConstraintsJsonArray(args, false);

return array.Select(value =>
{
if (value is not JObject jObj)
if (value is not JsonObject jObj)
{
throw new ConfigurationException(string.Format(LocalizableStrings.Constraint_Error_InvalidJsonArray_Objects, args));
}
Expand Down Expand Up @@ -103,29 +110,29 @@ public static IVersionSpecification ParseVersionSpecification(this string versio
return versionInstance;
}

private static JToken ParseConstraintJToken(this string? args)
private static JsonNode ParseConstraintJsonNode(this string? args)
{
if (string.IsNullOrWhiteSpace(args))
{
throw new ConfigurationException(LocalizableStrings.Constraint_Error_ArgumentsNotSpecified);
}

JToken? token;
JsonNode? token;
try
{
token = JToken.Parse(args!);
token = JsonNode.Parse(args!);
}
catch (Exception e)
{
throw new ConfigurationException(string.Format(LocalizableStrings.Constraint_Error_InvalidJson, args), e);
}

return token;
return token ?? throw new ConfigurationException(string.Format(LocalizableStrings.Constraint_Error_InvalidJson, args));
}

private static JArray ToConstraintsJArray(this JToken token, string? args, bool isStringTypeAllowed)
private static JsonArray ToConstraintsJsonArray(this JsonNode token, string? args, bool isStringTypeAllowed)
{
if (token is not JArray array)
if (token is not JsonArray array)
{
throw new ConfigurationException(string.Format(
isStringTypeAllowed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Nodes;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Constraints;
using Microsoft.TemplateEngine.Utils;
using Newtonsoft.Json.Linq;

namespace Microsoft.TemplateEngine.Edge.Constraints
{
Expand Down Expand Up @@ -71,7 +71,7 @@ private static IEnumerable<HostInformation> ParseArgs(string? args)
{
List<HostInformation> hostInformation = new List<HostInformation>();

foreach (JObject jObj in args.ParseArrayOfConstraintJObjects())
foreach (JsonObject jObj in args.ParseArrayOfConstraintJObjects())
{
string? hostName = jObj.ToString("hostname");
string? version = jObj.ToString("version");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageReference Include="NuGet.Configuration" />
<PackageReference Include="NuGet.Credentials" />
<PackageReference Include="NuGet.Protocol" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.Text.Json" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
Expand Down
51 changes: 26 additions & 25 deletions src/Microsoft.TemplateEngine.Edge/Settings/SettingsStore.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.TemplateEngine.Edge.Settings
{
internal class SettingsStore
{
internal SettingsStore(JObject? obj)
internal SettingsStore(JsonObject? obj)
{
if (obj == null)
{
return;
}

if (obj.TryGetValue(nameof(ComponentGuidToAssemblyQualifiedName), StringComparison.OrdinalIgnoreCase, out JToken? componentGuidToAssemblyQualifiedNameToken))
if (obj.TryGetValue(nameof(ComponentGuidToAssemblyQualifiedName), out JsonNode? componentGuidToAssemblyQualifiedNameToken))
{
if (componentGuidToAssemblyQualifiedNameToken is JObject componentGuidToAssemblyQualifiedNameObject)
if (componentGuidToAssemblyQualifiedNameToken is JsonObject componentGuidToAssemblyQualifiedNameObject)
{
foreach (JProperty entry in componentGuidToAssemblyQualifiedNameObject.Properties())
foreach (var entry in componentGuidToAssemblyQualifiedNameObject)
{
if (entry.Value is { Type: JTokenType.String })
if (entry.Value?.GetValueKind() == JsonValueKind.String)
{
ComponentGuidToAssemblyQualifiedName[entry.Name] = entry.Value.ToString();
ComponentGuidToAssemblyQualifiedName[entry.Key] = entry.Value.GetValue<string>();
}
}
}
}

if (obj.TryGetValue(nameof(ProbingPaths), StringComparison.OrdinalIgnoreCase, out JToken? probingPathsToken))
if (obj.TryGetValue(nameof(ProbingPaths), out JsonNode? probingPathsToken))
{
if (probingPathsToken is JArray probingPathsArray)
if (probingPathsToken is JsonArray probingPathsArray)
{
foreach (JToken path in probingPathsArray)
foreach (JsonNode? path in probingPathsArray)
{
if (path is { Type: JTokenType.String })
if (path?.GetValueKind() == JsonValueKind.String)
{
ProbingPaths.Add(path.ToString());
ProbingPaths.Add(path.GetValue<string>());
}
}
}
}

if (obj.TryGetValue(nameof(ComponentTypeToGuidList), StringComparison.OrdinalIgnoreCase, out JToken? componentTypeToGuidListToken))
if (obj.TryGetValue(nameof(ComponentTypeToGuidList), out JsonNode? componentTypeToGuidListToken))
{
if (componentTypeToGuidListToken is JObject componentTypeToGuidListObject)
if (componentTypeToGuidListToken is JsonObject componentTypeToGuidListObject)
{
foreach (JProperty entry in componentTypeToGuidListObject.Properties())
foreach (var entry in componentTypeToGuidListObject)
{
if (entry.Value is JArray values)
if (entry.Value is JsonArray values)
{
HashSet<Guid> set = new HashSet<Guid>();
ComponentTypeToGuidList[entry.Name] = set;
ComponentTypeToGuidList[entry.Key] = set;

foreach (JToken value in values)
foreach (JsonNode? value in values)
{
if (value is { Type: JTokenType.String })
if (value?.GetValueKind() == JsonValueKind.String)
{
if (Guid.TryParse(value.ToString(), out Guid id))
if (Guid.TryParse(value.GetValue<string>(), out Guid id))
{
set.Add(id);
}
Expand All @@ -72,13 +73,13 @@ internal SettingsStore(JObject? obj)
}
}

[JsonProperty]
[JsonInclude]
internal Dictionary<string, string> ComponentGuidToAssemblyQualifiedName { get; } = new();

[JsonProperty]
[JsonInclude]
internal HashSet<string> ProbingPaths { get; } = new();

[JsonProperty]
[JsonInclude]
internal Dictionary<string, HashSet<Guid>> ComponentTypeToGuidList { get; } = new();

internal static SettingsStore Load(IEngineEnvironmentSettings engineEnvironmentSettings, SettingsFilePaths paths)
Expand All @@ -88,7 +89,7 @@ internal static SettingsStore Load(IEngineEnvironmentSettings engineEnvironmentS
return new SettingsStore(null);
}

JObject parsed;
JsonObject parsed;
using (Timing.Over(engineEnvironmentSettings.Host.Logger, "Parse settings"))
{
try
Expand Down
Loading
Loading