Skip to content
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

C sharp support #39

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
/src/**/bin
/src/New folder
**/*.user

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# Jetbrains Rider
.idea/
2 changes: 1 addition & 1 deletion src/SigSpec.AspNetCore/SigSpec.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Owners>Rico Suter</Owners>
<Authors>Rico Suter</Authors>
<Description>Specification and code generator for SignalR Core.</Description>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/RicoSuter/SigSpec</PackageProjectUrl>
<RepositoryUrl>https://github.com/RicoSuter/SigSpec.git</RepositoryUrl>
Expand Down
16 changes: 16 additions & 0 deletions src/SigSpec.CodeGeneration.CSharp/Models/FileModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace SigSpec.CodeGeneration.CSharp.Models
{
public class FileModel
{
public FileModel(IEnumerable<string> hubs, string @namespace)
{
Hubs = hubs;
Namespace = @namespace;
}
public string Namespace { get; set; }

public IEnumerable<string> Hubs { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Remove="Templates\File.liquid" />
<None Remove="Templates\Hub.liquid" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Templates\File.liquid" />
<EmbeddedResource Include="Templates\Hub.liquid" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="njsonschema.codegeneration" Version="10.3.1" />
<PackageReference Include="njsonschema.codegeneration.csharp" Version="10.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SigSpec.CodeGeneration\SigSpec.CodeGeneration.csproj" />
<ProjectReference Include="..\SigSpec.Core\SigSpec.Core.csproj" />
</ItemGroup>

</Project>
56 changes: 56 additions & 0 deletions src/SigSpec.CodeGeneration.CSharp/SigSpecToCSharpGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Linq;
using NJsonSchema.CodeGeneration;
using NJsonSchema.CodeGeneration.CSharp;
using SigSpec.CodeGeneration.CSharp.Models;
using SigSpec.CodeGeneration.Models;
using SigSpec.Core;

namespace SigSpec.CodeGeneration.CSharp
{
public class SigSpecToCSharpGenerator
{
private readonly SigSpecToCSharpGeneratorSettings _settings;

public SigSpecToCSharpGenerator(SigSpecToCSharpGeneratorSettings settings)
{
_settings = settings;
}

public IEnumerable<CodeArtifact> GenerateArtifacts(SigSpecDocument document)
{
var resolver = new CSharpTypeResolver(_settings.CSharpGeneratorSettings);
resolver.RegisterSchemaDefinitions(document.Definitions);

var artifacts = new List<CodeArtifact>();
foreach (var hub in document.Hubs)
{
var hubModel = new HubModel(hub.Key, hub.Value, resolver);
var template = _settings.CSharpGeneratorSettings.TemplateFactory.CreateTemplate("CSharp", "Hub", hubModel);
artifacts.Add(new CodeArtifact(hubModel.Name, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Client, template.Render()));
}

if (_settings.GenerateDtoTypes)
{
var generator = new NJsonSchema.CodeGeneration.CSharp.CSharpGenerator(document, _settings.CSharpGeneratorSettings, resolver); //TypeScriptGenerator(document, _settings.TypeScriptGeneratorSettings, resolver);
var types = generator.GenerateTypes();
return artifacts.Concat(types);
}
else
{
var generator = new CSharpGenerator(document, _settings.CSharpGeneratorSettings, resolver);
return artifacts.Concat(generator.GenerateTypes());
}
}

public string GenerateClients(SigSpecDocument document)
{
var artifacts = GenerateArtifacts(document);

var fileModel = new FileModel(artifacts.Select(a => a.Code), _settings.CSharpGeneratorSettings.Namespace);
var fileTemplate = _settings.CSharpGeneratorSettings.TemplateFactory.CreateTemplate("CSharp", "File", fileModel);

return fileTemplate.Render();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Reflection;
using NJsonSchema.CodeGeneration.CSharp;

namespace SigSpec.CodeGeneration.CSharp
{
public class SigSpecToCSharpGeneratorSettings : SigSpecToCSharpGeneratorSettingsBase
{
public SigSpecToCSharpGeneratorSettings()
: base(new CSharpGeneratorSettings())
{
CodeGeneratorSettings.TemplateFactory = new DefaultTemplateFactory(CSharpGeneratorSettings, new[]
{
typeof(CSharpGeneratorSettings).GetTypeInfo().Assembly,
typeof(SigSpecToCSharpGeneratorSettingsBase).GetTypeInfo().Assembly
});
//TypeScriptGeneratorSettings.TypeStyle = TypeScriptTypeStyle.Interface;
//CodeGeneratorSettings.TemplateFactory = new DefaultTemplateFactory(TypeScriptGeneratorSettings, new[]
//{
// typeof(TypeScriptGeneratorSettings).GetTypeInfo().Assembly,
// typeof(SigSpecToTypeScriptGeneratorSettingsBase).GetTypeInfo().Assembly,
//});

}

public CSharpGeneratorSettings CSharpGeneratorSettings => (CSharpGeneratorSettings)CodeGeneratorSettings;
}
}
10 changes: 10 additions & 0 deletions src/SigSpec.CodeGeneration.CSharp/Templates/File.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace {{ Namespace }}
{
using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.SignalR.Client;

{% for hub in Hubs -%}
{{ hub }}
{% endfor -%}
}
29 changes: 29 additions & 0 deletions src/SigSpec.CodeGeneration.CSharp/Templates/Hub.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class {{ Name }}HubClient {
private readonly HubConnection _connection;

public {{Name}}HubClient(HubConnection connection) {
_connection = connection;
}
{% for operation in Callbacks -%}
public delegate Task On{{ operation.Name }}({% for parameter in operation.Parameters %}{{ parameter.Type }} {{ parameter.Name }}{% if forloop.last == false %}, {% endif %}{% endfor %});
{% if operation.Parameters.size == 0 %}
public IDisposable RegisterFor{{ operation.Name }}(On{{ operation.Name }} callback)
{
return _connection.On("{{ operation.Name }}", callback.Invoke);
}
public IDisposable RegisterFor{{ operation.Name }}(Func<Task> callback)
{
return _connection.On("{{ operation.Name }}", callback);
}
{% else %}
public IDisposable RegisterFor{{ operation.Name }}(On{{ operation.Name }} callback)
{
return _connection.On<{% for parameter in operation.Parameters %}{{ parameter.Type }}{% if forloop.last == false %}, {% endif %}{% endfor %}>("{{ operation.Name }}", callback.Invoke);
}
public IDisposable RegisterFor{{ operation.Name }}(Func<{% for parameter in operation.Parameters %}{{ parameter.Type }}{% if forloop.last == false %}, {% endif %}{% endfor %}, Task> callback)
{
return _connection.On("{{ operation.Name }}", callback);
}
{% endif %}
{% endfor -%}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Owners>Rico Suter</Owners>
<Authors>Rico Suter</Authors>
<Description>Specification and code generator for SignalR Core.</Description>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/RicoSuter/SigSpec</PackageProjectUrl>
<RepositoryUrl>https://github.com/RicoSuter/SigSpec.git</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion src/SigSpec.CodeGeneration/SigSpec.CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Owners>Rico Suter</Owners>
<Authors>Rico Suter</Authors>
<Description>Specification and code generator for SignalR Core.</Description>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/RicoSuter/SigSpec</PackageProjectUrl>
<RepositoryUrl>https://github.com/RicoSuter/SigSpec.git</RepositoryUrl>
Expand Down
17 changes: 17 additions & 0 deletions src/SigSpec.CodeGeneration/SigSpecToCSharpGeneratorSettingsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
using NJsonSchema.CodeGeneration;

namespace SigSpec.CodeGeneration
{
public class SigSpecToCSharpGeneratorSettingsBase : ClientGeneratorBaseSettings
{
public SigSpecToCSharpGeneratorSettingsBase(CodeGeneratorSettingsBase codeGeneratorSettings)
{
CodeGeneratorSettings = codeGeneratorSettings;
}

protected CodeGeneratorSettingsBase CodeGeneratorSettings { get; }
}
}
11 changes: 6 additions & 5 deletions src/SigSpec.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using SigSpec.CodeGeneration;
using SigSpec.CodeGeneration.CSharp;

namespace SigSpec
{
Expand All @@ -27,16 +29,15 @@ static async Task RunAsync()
});

var json = document.ToJson();

Console.WriteLine("\nGenerated SigSpec document:");
Console.WriteLine(json);
Console.ReadKey();

var codeGeneratorSettings = new SigSpecToTypeScriptGeneratorSettings();
Copy link
Owner

Choose a reason for hiding this comment

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

Can you maybe keep the ts code here (commented) so we have both samples here?

var codeGenerator = new SigSpecToTypeScriptGenerator(codeGeneratorSettings);
var file = codeGenerator.GenerateFile(document);
var codeGeneratorSettings = new SigSpecToCSharpGeneratorSettings();
var codeGenerator = new SigSpecToCSharpGenerator(codeGeneratorSettings);
var file = codeGenerator.GenerateClients(document);

Console.WriteLine("\n\nGenerated SigSpec TypeScript code:");
Console.WriteLine("\n\nGenerated SigSpec CSharp clients:");
Console.WriteLine(file);
Console.ReadKey();
}
Expand Down
1 change: 1 addition & 0 deletions src/SigSpec.Console/SigSpec.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SigSpec.CodeGeneration.CSharp\SigSpec.CodeGeneration.CSharp.csproj" />
<ProjectReference Include="..\SigSpec.Core\SigSpec.Core.csproj" />
<ProjectReference Include="..\HelloSignalR\HelloSignalR.csproj" />
<ProjectReference Include="..\SigSpec.CodeGeneration.TypeScript\SigSpec.CodeGeneration.TypeScript.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/SigSpec.Core/SigSpec.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Owners>Rico Suter</Owners>
<Authors>Rico Suter</Authors>
<Description>Specification and code generator for SignalR Core.</Description>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/RicoSuter/SigSpec</PackageProjectUrl>
<RepositoryUrl>https://github.com/RicoSuter/SigSpec.git</RepositoryUrl>
Expand Down
6 changes: 6 additions & 0 deletions src/SigSpec.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{EC91FE8E
..\azure-pipelines.yml = ..\azure-pipelines.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SigSpec.CodeGeneration.CSharp", "SigSpec.CodeGeneration.CSharp\SigSpec.CodeGeneration.CSharp.csproj", "{FF02610E-C750-46DC-B902-D6ED2D48BF5A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -50,6 +52,10 @@ Global
{2D8AA80F-DA03-4AD6-97A3-6D465466A3C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D8AA80F-DA03-4AD6-97A3-6D465466A3C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D8AA80F-DA03-4AD6-97A3-6D465466A3C1}.Release|Any CPU.Build.0 = Release|Any CPU
{FF02610E-C750-46DC-B902-D6ED2D48BF5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF02610E-C750-46DC-B902-D6ED2D48BF5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF02610E-C750-46DC-B902-D6ED2D48BF5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF02610E-C750-46DC-B902-D6ED2D48BF5A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down