Skip to content

Commit 8563ebe

Browse files
authored
Merge pull request #21 from bonsai-rx/feature-dev
Generate serializer library provenance
2 parents 66bdded + 66585e8 commit 8563ebe

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

Bonsai.Sgen/Bonsai.Sgen.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1314
<PackageReference Include="NJsonSchema.CodeGeneration" Version="10.9.0" />
1415
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="10.9.0" />
1516
<PackageReference Include="NJsonSchema.Yaml" Version="10.9.0" />

Bonsai.Sgen/CSharpCodeDomTemplate.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Bonsai.Sgen
88
internal abstract class CSharpCodeDomTemplate : ITemplate
99
{
1010
private static readonly AssemblyName GeneratorAssemblyName = Assembly.GetExecutingAssembly().GetName();
11+
private static readonly AssemblyName NewtonsoftJsonAssemblyName = typeof(Newtonsoft.Json.JsonConvert).Assembly.GetName();
12+
private static readonly AssemblyName YamlDotNetAssemblyName = typeof(YamlDotNet.Core.Parser).Assembly.GetName();
1113

1214
public CSharpCodeDomTemplate(
1315
CodeDomProvider provider,
@@ -29,13 +31,32 @@ public CSharpCodeDomTemplate(
2931

3032
public abstract void BuildType(CodeTypeDeclaration type);
3133

34+
private static string GetVersionString(AssemblyName assemblyName)
35+
{
36+
return $"{assemblyName.Name} v{assemblyName.Version}";
37+
}
38+
39+
private string GetVersionString()
40+
{
41+
var serializerLibraries = new List<string>();
42+
if (Settings.SerializerLibraries.HasFlag(SerializerLibraries.NewtonsoftJson))
43+
{
44+
serializerLibraries.Add(GetVersionString(NewtonsoftJsonAssemblyName));
45+
}
46+
if (Settings.SerializerLibraries.HasFlag(SerializerLibraries.YamlDotNet))
47+
{
48+
serializerLibraries.Add(GetVersionString(YamlDotNetAssemblyName));
49+
}
50+
return $"{GeneratorAssemblyName.Version} ({string.Join(", ", serializerLibraries)})";
51+
}
52+
3253
public string Render()
3354
{
3455
var type = new CodeTypeDeclaration(TypeName) { IsPartial = true };
3556
type.CustomAttributes.Add(new CodeAttributeDeclaration(
3657
new CodeTypeReference(typeof(GeneratedCodeAttribute)),
3758
new CodeAttributeArgument(new CodePrimitiveExpression(GeneratorAssemblyName.Name)),
38-
new CodeAttributeArgument(new CodePrimitiveExpression(GeneratorAssemblyName.Version?.ToString()))));
59+
new CodeAttributeArgument(new CodePrimitiveExpression(GetVersionString()))));
3960
BuildType(type);
4061

4162
using var writer = new StringWriter();

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# sgen
2-
Tool for automatically generating YML serialization classes from schema files
1+
# Serializer Generator Tool
2+
3+
Tool for automatically generating YAML / JSON serialization classes and constructor operators from schema files.
34

45
## Getting Started
56

6-
1. Navigate to the [Bonsai.Sgen toolbox NuGet package](https://www.nuget.org/packages/Bonsai.Sgen/)
7-
2. Click `NET CLI (Local)` and copy the two suggested commands. E.g.:
7+
1. Navigate to the [Bonsai.Sgen NuGet tool package](https://www.nuget.org/packages/Bonsai.Sgen/)
8+
2. Click `.NET CLI (Local)` and copy the two suggested commands. E.g.:
89

910
```cmd
1011
dotnet new tool-manifest # if you are setting up this repo
11-
dotnet tool install --local Bonsai.Sgen --version 0.1.0
12+
dotnet tool install --local Bonsai.Sgen --version 0.2.0
1213
```
1314
1415
3. To view the tool help reference documentation, run:
@@ -17,25 +18,31 @@ Tool for automatically generating YML serialization classes from schema files
1718
dotnet bonsai.sgen --help
1819
```
1920
20-
4. To generate serialization classes from a schema file:
21+
4. To generate YAML serialization classes from a schema file:
22+
23+
```cmd
24+
dotnet bonsai.sgen --schema schema.json --serializer YamlDotNet
25+
```
26+
27+
5. To generate JSON serialization classes from a schema file:
2128
2229
```cmd
23-
dotnet bonsai.sgen --schema schema.json
30+
dotnet bonsai.sgen --schema schema.json --serializer NewtonsoftJson
2431
```
2532
26-
5. Copy the generated class file to your project `Extensions` folder.
33+
6. Copy the generated class file to your project `Extensions` folder.
2734
28-
6. Add the necessary package references to your `Extensions.csproj` file. For instance:
35+
7. Add the necessary package references to your `Extensions.csproj` file. For example:
2936
3037
```xml
3138
<ItemGroup>
3239
<PackageReference Include="Bonsai.Core" Version="2.8.0" />
33-
<PackageReference Include="YamlDotNet" Version="12.0.2" />
40+
<PackageReference Include="YamlDotNet" Version="13.7.1" />
3441
</ItemGroup>
3542
</Project>
3643
```
3744
38-
7. To restore the tool at any point, run:
45+
8. To restore the tool at any point, run:
3946
4047
```cmd
4148
dotnet tool restore

0 commit comments

Comments
 (0)