Skip to content

Commit

Permalink
Added a model generator for FluentHub.Octokit (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa authored Jul 15, 2022
1 parent a07f298 commit 30a14a2
Show file tree
Hide file tree
Showing 2,146 changed files with 68,050 additions and 15,151 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/approved.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
on: pull_request_review
name: Label approved pull requests
name: Labeler
jobs:
labelWhenApproved:
name: Label when approved
name: Approved
runs-on: ubuntu-latest
steps:
- name: Label when approved
Expand Down
50 changes: 25 additions & 25 deletions FluentHub.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32014.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentHub", "src\FluentHub\FluentHub.csproj", "{9AF1D390-4850-42E3-A884-4CA703B1A651}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C6265587-7328-4B9E-AF1F-69F0E7301C5F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentHub.Octokit", "src\FluentHub.Octokit\FluentHub.Octokit.csproj", "{893EDCC9-4345-4F49-A13F-1A426A7BA432}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentHub.Uwp", "src\FluentHub.Uwp\FluentHub.Uwp.csproj", "{9AF1D390-4850-42E3-A884-4CA703B1A651}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C6265587-7328-4B9E-AF1F-69F0E7301C5F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentHub.Octokit", "src\FluentHub.Octokit\FluentHub.Octokit.csproj", "{893EDCC9-4345-4F49-A13F-1A426A7BA432}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentHub.Core", "src\FluentHub.Core\FluentHub.Core.csproj", "{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentHub.Octokit.Generation", "src\FluentHub.Octokit.Generation\FluentHub.Octokit.Generation.csproj", "{2219FA42-F8D3-4951-9578-76E000BB86D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -73,34 +73,34 @@ Global
{893EDCC9-4345-4F49-A13F-1A426A7BA432}.Release|x64.Build.0 = Release|x64
{893EDCC9-4345-4F49-A13F-1A426A7BA432}.Release|x86.ActiveCfg = Release|x86
{893EDCC9-4345-4F49-A13F-1A426A7BA432}.Release|x86.Build.0 = Release|x86
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|ARM.ActiveCfg = Debug|ARM
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|ARM.Build.0 = Debug|ARM
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|ARM64.ActiveCfg = Debug|ARM64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|ARM64.Build.0 = Debug|ARM64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|x64.ActiveCfg = Debug|x64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|x64.Build.0 = Debug|x64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|x86.ActiveCfg = Debug|x86
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Debug|x86.Build.0 = Debug|x86
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|Any CPU.Build.0 = Release|Any CPU
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|ARM.ActiveCfg = Release|ARM
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|ARM.Build.0 = Release|ARM
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|ARM64.ActiveCfg = Release|ARM64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|ARM64.Build.0 = Release|ARM64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|x64.ActiveCfg = Release|x64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|x64.Build.0 = Release|x64
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|x86.ActiveCfg = Release|x86
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957}.Release|x86.Build.0 = Release|x86
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|ARM.ActiveCfg = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|ARM.Build.0 = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|ARM64.Build.0 = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|x64.ActiveCfg = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|x64.Build.0 = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|x86.ActiveCfg = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Debug|x86.Build.0 = Debug|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|Any CPU.Build.0 = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|ARM.ActiveCfg = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|ARM.Build.0 = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|ARM64.ActiveCfg = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|ARM64.Build.0 = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|x64.ActiveCfg = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|x64.Build.0 = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|x86.ActiveCfg = Release|Any CPU
{2219FA42-F8D3-4951-9578-76E000BB86D7}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9AF1D390-4850-42E3-A884-4CA703B1A651} = {C6265587-7328-4B9E-AF1F-69F0E7301C5F}
{893EDCC9-4345-4F49-A13F-1A426A7BA432} = {C6265587-7328-4B9E-AF1F-69F0E7301C5F}
{2DF9C7E4-8293-4DCD-9EA4-78784D98E957} = {C6265587-7328-4B9E-AF1F-69F0E7301C5F}
{2219FA42-F8D3-4951-9578-76E000BB86D7} = {C6265587-7328-4B9E-AF1F-69F0E7301C5F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {69E4A07A-D627-48B2-A72B-25F6082E0684}
Expand Down
152 changes: 0 additions & 152 deletions src/FluentHub.Core/FluentHub.Core.csproj

This file was deleted.

17 changes: 0 additions & 17 deletions src/FluentHub.Core/Properties/AssemblyInfo.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/FluentHub.Core/Properties/FluentHub.Core.rd.xml

This file was deleted.

15 changes: 15 additions & 0 deletions src/FluentHub.Octokit.Generation/AttributeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace FluentHub.Octokit.Generation
{
internal static class AttributeGenerator
{
public static string GenerateObsoleteAttribute(string reason)
{
if (string.IsNullOrWhiteSpace(reason))
{
return "[Obsolete]";
}

return $"[Obsolete(@\"{reason}\")]";
}
}
}
61 changes: 61 additions & 0 deletions src/FluentHub.Octokit.Generation/CodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.IO;
using FluentHub.Octokit.Generation.Models;
using Octokit.GraphQL.Core.Introspection;

namespace FluentHub.Octokit.Generation
{
public static class CodeGenerator
{
public static IEnumerable<GeneratedFile> Generate(SchemaModel schema, string rootNamespace, string entityNamespace = null)
{
foreach (var type in schema.Types)
{
if (!type.Name.StartsWith("__") && type.Kind != TypeKind.Scalar)
{
yield return Generate(type, rootNamespace, schema.QueryType, entityNamespace);
}
}
}

public static GeneratedFile Generate(TypeModel type, string rootNamespace, string queryType, string entityNamespace = null)
{
entityNamespace = entityNamespace ?? rootNamespace;

string result = null;
switch (type.Kind)
{
case TypeKind.Object:
if (type.Name == queryType || type.Name == "Mutation")
{
// Mutation.cs and Query.cs is not needed
}
else
{
result = EntityGenerator.Generate(type, entityNamespace, queryType, entityNamespace: entityNamespace);
}
break;

case TypeKind.Interface:
result = InterfaceGenerator.Generate(type, entityNamespace, queryType);
break;

case TypeKind.Enum:
result = EnumGenerator.Generate(type, entityNamespace);
break;

case TypeKind.InputObject:
result = InputObjectGenerator.Generate(type, entityNamespace);
break;

case TypeKind.Union:
result = UnionGenerator.Generate(type, entityNamespace);
break;
}

var fileName = type.Name + ".cs";
return new GeneratedFile(fileName, result);
}
}
}
37 changes: 37 additions & 0 deletions src/FluentHub.Octokit.Generation/DocCommentGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Text;
using System.Xml;

namespace FluentHub.Octokit.Generation
{
internal static class DocCommentGenerator
{
public static void GenerateSummary(string summary, int indentation, StringBuilder builder)
{
if (!string.IsNullOrWhiteSpace(summary))
{
var indent = new string(' ', indentation);
builder.Append(indent);
builder.AppendLine("/// <summary>");
GenerateCommentBlock(summary, indent, builder);
builder.Append(indent);
builder.AppendLine(@"/// </summary>");
}
}

private static void GenerateCommentBlock(string text, string indent, StringBuilder builder)
{
text = text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");

foreach (var line in text.Split('\r', '\n'))
{
if (!string.IsNullOrWhiteSpace(line))
{
builder.Append(indent);
builder.Append("/// ");
builder.AppendLine(line);
}
}
}
}
}
Loading

0 comments on commit 30a14a2

Please sign in to comment.