Skip to content

Commit

Permalink
v2.0.0 (#19)
Browse files Browse the repository at this point in the history
* Deprecate Newtonsoft.Json

* Update ci.yml

Signed-off-by: Eric Sibly [chullybun] <[email protected]>

---------

Signed-off-by: Eric Sibly [chullybun] <[email protected]>
  • Loading branch information
chullybun authored Jan 6, 2024
1 parent 3272de2 commit a55a3ef
Show file tree
Hide file tree
Showing 26 changed files with 569 additions and 460 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '3.1.x'
dotnet-version: |
6.0.x
7.0.x
8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
run: dotnet test --no-build --verbosity normal
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Represents the **NuGet** versions.

## v2.0.0
- *Enhancement:* **Breaking change** - underlying JSON serialization has been changed from `Newtonsoft.Json` to `System.Text.Json`, with new `Utility.JsonSerializer` encapsulating logic to enable. The following steps are required to migrate existing usage:
- Rename all attribute references from `JsonProperty` to `JsonPropertyName`.
- Remove all attribute references to `[JsonObject(MemberSerialization = MemberSerialization.OptIn)]`; opt-in is the default behavior when leveraging the `CodeGenClassAttribute` attribute.
- *Fixed:* All dependencies updated to the latest version.

## v1.0.8
- *Fixed:* Added comparison context where reporting a file update as a result of using `ExpectNoChanges`.

Expand Down
11 changes: 3 additions & 8 deletions src/OnRamp/Config/CodeGenPropertyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ namespace OnRamp.Config
/// <summary>
/// Represents the <i>code-generation</i> property configuration.
/// </summary>
/// <param name="category">The grouping category.</param>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class CodeGenPropertyAttribute : Attribute
public sealed class CodeGenPropertyAttribute(string category) : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="CodeGenPropertyAttribute"/> class.
/// </summary>
/// <param name="category">The grouping category.</param>
public CodeGenPropertyAttribute(string category) => Category = category ?? throw new ArgumentNullException(nameof(category));

/// <summary>
/// Gets or sets the category.
/// </summary>
public string Category { get; }
public string Category { get; } = category ?? throw new ArgumentNullException(nameof(category));

/// <summary>
/// Gets or sets the title.
Expand Down
21 changes: 10 additions & 11 deletions src/OnRamp/Config/ConfigBase.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/OnRamp

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace OnRamp.Config
{
/// <summary>
/// Provides base configuration capabilities.
/// </summary>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public abstract class ConfigBase
{
#region static
Expand Down Expand Up @@ -215,35 +214,35 @@ public static bool IsSubclassOfBaseType(Type baseType, Type type)
/// Gets or sets the <see cref="Dictionary{TKey, TValue}"/> that houses any additional/extra properties/attributes deserialized within the configuration.
/// </summary>
[JsonExtensionData]
public Dictionary<string, JToken>? ExtraProperties { get; set; }
public Dictionary<string, JsonElement>? ExtraProperties { get; set; }

/// <summary>
/// Gets the property value from <see cref="ExtraProperties"/> using the specified <paramref name="key"/> as <see cref="Type"/> <typeparamref name="T"/>.
/// Gets the property value from <see cref="ExtraProperties"/> using the specified <paramref name="key"/> and <see cref="Type"/> <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The property <see cref="Type"/>.</typeparam>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value where the property is not found.</param>
/// <returns>The value.</returns>
public T GetExtraProperty<T>(string key, T defaultValue = default!) where T : JToken
public T? GetExtraProperty<T>(string key, T? defaultValue = default!)
{
if (ExtraProperties != null && ExtraProperties.TryGetValue(key, out var val))
return (T)Convert.ChangeType(val, typeof(T));
return val.Deserialize<T>()!;
else
return defaultValue!;
}

/// <summary>
/// Trys to get the property value from <see cref="ExtraProperties"/> using the specified <paramref name="key"/> as <see cref="Type"/> <typeparamref name="T"/>.
/// Trys to get the property value from <see cref="ExtraProperties"/> using the specified <paramref name="key"/> and <see cref="Type"/> <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The property <see cref="Type"/>.</typeparam>
/// <param name="key">The key.</param>
/// <param name="value">The corresponding value.</param>
/// <returns><c>true</c> if the <paramref name="key"/> is found; otherwise, <c>false</c>.</returns>
public bool TryGetExtraProperty<T>(string key, out T value) where T : JToken
public bool TryGetExtraProperty<T>(string key, out T value)
{
if (ExtraProperties != null && ExtraProperties.TryGetValue(key, out var val))
{
value = (T)Convert.ChangeType(val, typeof(T));
value = val.Deserialize<T>()!;
return true;
}
else
Expand All @@ -257,7 +256,7 @@ public bool TryGetExtraProperty<T>(string key, out T value) where T : JToken
/// Gets the <see cref="Dictionary{TKey, TValue}"/> that allows for custom property values to be manipulated at runtime.
/// </summary>
[JsonIgnore]
public Dictionary<string, object> CustomProperties { get; } = new Dictionary<string, object>();
public Dictionary<string, object> CustomProperties { get; } = [];

/// <summary>
/// Gets the property value from <see cref="CustomProperties"/> using the specified <paramref name="key"/> as <see cref="Type"/> <typeparamref name="T"/>.
Expand Down
2 changes: 0 additions & 2 deletions src/OnRamp/Config/ConfigBaseT.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/OnRamp

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,7 +13,6 @@ namespace OnRamp.Config
/// </summary>
/// <typeparam name="TRoot">The root <see cref="Type"/>.</typeparam>
/// <typeparam name="TParent">The parent <see cref="Type"/>.</typeparam>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public abstract class ConfigBase<TRoot, TParent> : ConfigBase where TRoot : ConfigBase where TParent : ConfigBase
{
/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/OnRamp/Config/ConfigRootBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/OnRamp

using Newtonsoft.Json;
using OnRamp.Generators;
using System;
using System.Collections.Generic;
Expand All @@ -11,7 +10,6 @@ namespace OnRamp.Config
/// Provides the <b>root</b> base <see cref="ConfigBase.PrepareAsync(object, object)"/> configuration capabilities.
/// </summary>
/// <typeparam name="TRoot">The root <see cref="Type"/>.</typeparam>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public abstract class ConfigRootBase<TRoot> : ConfigBase<TRoot, TRoot>, IRootConfig where TRoot : ConfigRootBase<TRoot>
{
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/OnRamp/Console/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class ConsoleLogger : ILogger
public ConsoleLogger(IConsole? console = null) => _console = console;

/// <inheritdoc />
public IDisposable BeginScope<TState>(TState state) => NullScope.Default;
public IDisposable BeginScope<TState>(TState state) where TState : notnull => NullScope.Default;

/// <inheritdoc />
public bool IsEnabled(LogLevel logLevel) => true;

/// <inheritdoc />
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (formatter == null)
throw new ArgumentNullException(nameof(formatter));
Expand Down
12 changes: 6 additions & 6 deletions src/OnRamp/OnRamp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>OnRamp</RootNamespace>
<Version>1.0.8</Version>
<Version>2.0.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down Expand Up @@ -48,13 +48,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Handlebars.Net" Version="2.0.9" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Handlebars.Net" Version="2.1.4" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Pluralize.NET" Version="1.0.2" />
<PackageReference Include="StrongNamer" Version="0.2.5" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>

</Project>
11 changes: 5 additions & 6 deletions src/OnRamp/Scripts/CodeGenScript.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/OnRamp

using Newtonsoft.Json;
using OnRamp.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace OnRamp.Scripts
{
/// <summary>
/// Represents the root that encapsulates the underlying <see cref="Generators"/>.
/// </summary>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[CodeGenClass("Script", Title = "'Script' object.", Description = "The `Script` object scripts the code-generation execution.")]
[CodeGenCategory("Key", Title = "Provides the _Key_ configuration.")]
[CodeGenCategory("Collections", Title = "Provides related child (hierarchical) configuration.")]
Expand All @@ -24,28 +23,28 @@ public class CodeGenScript : ConfigRootBase<CodeGenScript>
/// <summary>
/// Gets or sets the .NET <see cref="ConfigRootBase{TRoot}"/> Type for the underlying <see cref="Generators"/>.
/// </summary>
[JsonProperty("configType")]
[JsonPropertyName("configType")]
[CodeGenProperty("Key", Title = "The .NET ConfigRootBase Type for the underlying 'Generators'.", IsMandatory = true)]
public string? ConfigType { get; set; }

/// <summary>
/// Gets or sets the list of additional script resource names to inherit.
/// </summary>
[JsonProperty("inherits")]
[JsonPropertyName("inherits")]
[CodeGenPropertyCollection("Key", Title = "The list of additional script resource names to inherit.")]
public List<string>? Inherits { get; set; }

/// <summary>
/// Gets or sets the <see cref="IConfigEditor"/> <see cref="Type"/>.
/// </summary>
[JsonProperty("editorType")]
[JsonPropertyName("editorType")]
[CodeGenPropertyCollection("Key", Title = "The .NET IConfigEditor Type for performing extended custom configuration prior to generation.")]
public string? EditorType { get; set; }

/// <summary>
/// Gets or sets the <see cref="CodeGenScriptItem"/> collection.
/// </summary>
[JsonProperty("generators")]
[JsonPropertyName("generators")]
[CodeGenPropertyCollection("Collections", Title = "The corresponding `Generator` collection.")]
public List<CodeGenScriptItem>? Generators { get; set; }

Expand Down
19 changes: 9 additions & 10 deletions src/OnRamp/Scripts/CodeGenScriptItem.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/OnRamp

using Newtonsoft.Json;
using OnRamp.Config;
using OnRamp.Generators;
using OnRamp.Utility;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace OnRamp.Scripts
{
/// <summary>
/// Represents the <see cref="HandlebarsCodeGenerator"/> script arguments used to define a <see cref="CodeGeneratorBase"/> (as specified by the <see cref="Type"/>) and other associated code-generation arguments.
/// </summary>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[CodeGenClass("Generate", Title = "'Generate' command.", Description = "The `Generate` command defines the execution parameters for a code-generation execution.")]
[CodeGenCategory("Key", Title = "Provides the _Key_ configuration.")]
public class CodeGenScriptItem : ConfigBase<CodeGenScript, CodeGenScript>
Expand All @@ -29,52 +28,52 @@ public class CodeGenScriptItem : ConfigBase<CodeGenScript, CodeGenScript>
/// <summary>
/// Gets or sets the <see cref="CodeGeneratorBase"/> <see cref="System.Type"/>.
/// </summary>
[JsonProperty("type")]
[JsonPropertyName("type")]
[CodeGenProperty("Key", Title = "The .NET Generator (CodeGeneratorBase) Type.", IsMandatory = true)]
public string? Type { get; set; }

/// <summary>
/// Gets or sets the template resource name.
/// </summary>
[JsonProperty("template")]
[JsonPropertyName("template")]
[CodeGenProperty("Key", Title = "The template resource name.", IsMandatory = true)]
public string? Template { get; set; }

/// <summary>
/// Gets or sets the file name.
/// </summary>
/// <remarks>Supports <b>Handlebars</b> syntax.</remarks>
[JsonProperty("file")]
[JsonPropertyName("file")]
[CodeGenProperty("Key", Title = "The file name.", IsMandatory = true, Description = "Supports _Handlebars_ syntax.")]
public string? File { get; set; }

/// <summary>
/// Gets or sets the directory name.
/// </summary>
/// <remarks>Supports <b>Handlebars</b> syntax.</remarks>
[JsonProperty("directory")]
[JsonPropertyName("directory")]
[CodeGenProperty("Key", Title = "The directory name.", Description = "Supports _Handlebars_ syntax.")]
public string? Directory { get; set; }

/// <summary>
/// Indicates whether the file is only generated once; i.e. only created where it does not already exist.
/// </summary>
[JsonProperty("genOnce")]
[JsonPropertyName("genOnce")]
[CodeGenProperty("Key", Title = "Indicates whether the file is only generated once; i.e. only created where it does not already exist.")]
public bool IsGenOnce { get; set; }

/// <summary>
/// Gets or sets the gen-once file name pattern to check (can include wildcard '<c>*</c>' characters).
/// </summary>
/// <remarks>Supports <b>Handlebars</b> syntax.</remarks>
[JsonProperty("genOncePattern")]
[JsonPropertyName("genOncePattern")]
[CodeGenProperty("Key", Title = "The gen-once file name pattern to check (can include wildcard `*` characters).", Description = "Supports _Handlebars_ syntax. Defaults to `File` where not specified.")]
public string? GenOncePattern { get; set; }

/// <summary>
/// Gets or sets the help text.
/// </summary>
[JsonProperty("text")]
[JsonPropertyName("text")]
[CodeGenProperty("Key", Title = "The additional text written to the log to enable additional context.")]
public string? Text { get; set; }

Expand Down Expand Up @@ -127,7 +126,7 @@ protected override Task PrepareAsync()
{
foreach (var json in ExtraProperties)
{
RuntimeParameters.Add(json.Key, json.Value.ToObject<string?>());
RuntimeParameters.Add(json.Key, json.Value.ToString());
}
}
}
Expand Down
Loading

0 comments on commit a55a3ef

Please sign in to comment.