|
1 | | -using GraphQLParser; |
2 | | -using Newtonsoft.Json; |
3 | | -using Newtonsoft.Json.Linq; |
4 | | -using System; |
5 | | -using System.Collections.Generic; |
6 | | -using System.Diagnostics; |
7 | | -using System.IO; |
8 | | -using System.Linq; |
9 | | -using System.Reflection; |
10 | | -using System.Text; |
11 | | -using System.Text.RegularExpressions; |
12 | | -using System.Threading.Tasks; |
13 | | -using Tocsoft.GraphQLCodeGen.Cli; |
14 | | -using Tocsoft.GraphQLCodeGen.SchemaIntrospection; |
15 | | -using static Tocsoft.GraphQLCodeGen.IntrospectedSchemeParser; |
16 | | - |
17 | | -namespace Tocsoft.GraphQLCodeGen |
18 | | -{ |
19 | | - internal class CodeGenerator |
20 | | - { |
21 | | - private readonly CodeGeneratorSettings settings; |
22 | | - private readonly IEnumerable<SchemaIntrospection.IIntrosepctionProvider> introspectionProviders; |
23 | | - private readonly ILogger logger; |
24 | | - |
25 | | - public CodeGenerator(ILogger logger, CodeGeneratorSettings settings) |
26 | | - : this(logger, settings, new IIntrosepctionProvider[] { |
27 | | - new SchemaIntrospection.JsonIntrospection(), |
28 | | - new SchemaIntrospection.HttpIntrospection(), |
29 | | - new SchemaIntrospection.DllIntrospection(), |
30 | | - new SchemaIntrospection.SchemaFileIntrospection() |
31 | | - }) |
32 | | - { |
33 | | - } |
34 | | - |
35 | | - public CodeGenerator(ILogger logger, CodeGeneratorSettings settings, IEnumerable<SchemaIntrospection.IIntrosepctionProvider> introspectionProviders) |
36 | | - { |
37 | | - this.logger = logger; |
38 | | - this.settings = settings; |
39 | | - this.introspectionProviders = introspectionProviders; |
40 | | - } |
41 | | - |
42 | | - internal IEnumerable<NamedSource> Sources { get; set; } |
43 | | - internal ObjectModel.GraphQLDocument Document { get; set; } |
44 | | - internal Models.ViewModel Model { get; set; } |
45 | | - internal string GeneratedCode { get; set; } |
46 | | - internal bool HasParsingErrors { get; set; } |
47 | | - |
48 | | - internal async Task LoadSource() |
49 | | - { |
50 | | - IIntrosepctionProvider provider = this.introspectionProviders.Single(x => x.SchemaType == this.settings.Schema.SchemaType()); |
51 | | - |
52 | | - string schema = await provider.LoadSchema(this.settings.Schema); |
53 | | - // we need to load in the scheme |
54 | | - List<NamedSource> sources = new List<NamedSource>(); |
55 | | - sources.Add(new NamedSource() { Path = this.settings.Schema.Location, Body = schema }); |
56 | | - |
57 | | - foreach (var s in this.settings.SourceFiles) |
58 | | - { |
59 | | - sources.Add(s); |
60 | | - } |
61 | | - |
62 | | - Sources = sources; |
63 | | - } |
64 | | - |
65 | | - internal void Export() |
66 | | - { |
67 | | - Directory.CreateDirectory(Path.GetDirectoryName(this.settings.OutputPath)); |
68 | | - File.WriteAllText(this.settings.OutputPath, GeneratedCode); |
69 | | - } |
70 | | - |
71 | | - internal void Render() |
72 | | - { |
73 | | - this.GeneratedCode = new TemplateEngine(this.settings.Templates, this.settings.TemplateSettings, logger, Model).Generate(); |
74 | | - } |
75 | | - |
76 | | - internal void Parse() |
77 | | - { |
78 | | - Document = IntrospectedSchemeParser.Parse(Sources, this.settings); |
79 | | - |
80 | | - if (Document.Errors.Any()) |
81 | | - { |
82 | | - foreach (var error in Document.Errors) |
83 | | - { |
84 | | - logger.Error(error.ToString()); |
85 | | - } |
86 | | - HasParsingErrors = true; |
87 | | - return; |
88 | | - } |
89 | | - |
90 | | - Model = new Models.ViewModel(Document, this.settings); |
91 | | - |
92 | | - HasParsingErrors = false; |
93 | | - } |
94 | | - |
95 | | - public async Task<bool> GenerateAsync() |
96 | | - { |
97 | | - await LoadSource(); |
98 | | - |
99 | | - Parse(); |
100 | | - |
101 | | - if (HasParsingErrors) |
102 | | - { |
103 | | - return false; |
104 | | - } |
105 | | - |
106 | | - Render(); |
107 | | - |
108 | | - Export(); |
109 | | - |
110 | | - return true; |
111 | | - } |
112 | | - } |
113 | | -} |
| 1 | +using Newtonsoft.Json; |
| 2 | +using Newtonsoft.Json.Linq; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Reflection; |
| 9 | +using System.Text; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Tocsoft.GraphQLCodeGen.Cli; |
| 13 | +using Tocsoft.GraphQLCodeGen.ObjectModel; |
| 14 | +using Tocsoft.GraphQLCodeGen.SchemaIntrospection; |
| 15 | +using static Tocsoft.GraphQLCodeGen.IntrospectedSchemeParser; |
| 16 | + |
| 17 | +namespace Tocsoft.GraphQLCodeGen |
| 18 | +{ |
| 19 | + internal class CodeGenerator |
| 20 | + { |
| 21 | + private readonly CodeGeneratorSettings settings; |
| 22 | + private readonly IEnumerable<SchemaIntrospection.IIntrosepctionProvider> introspectionProviders; |
| 23 | + private readonly ILogger logger; |
| 24 | + |
| 25 | + public CodeGenerator(ILogger logger, CodeGeneratorSettings settings) |
| 26 | + : this(logger, settings, new IIntrosepctionProvider[] { |
| 27 | + new SchemaIntrospection.JsonIntrospection(), |
| 28 | + new SchemaIntrospection.HttpIntrospection(), |
| 29 | + #if DLL_INTROSPECTION |
| 30 | + new SchemaIntrospection.DllIntrospection(), |
| 31 | + #endif |
| 32 | + new SchemaIntrospection.SchemaFileIntrospection() |
| 33 | + }) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + public CodeGenerator(ILogger logger, CodeGeneratorSettings settings, IEnumerable<SchemaIntrospection.IIntrosepctionProvider> introspectionProviders) |
| 38 | + { |
| 39 | + this.logger = logger; |
| 40 | + this.settings = settings; |
| 41 | + this.introspectionProviders = introspectionProviders; |
| 42 | + } |
| 43 | + |
| 44 | + internal IEnumerable<NamedSource> Sources { get; set; } |
| 45 | + internal ObjectModel.GraphQLDocument Document { get; set; } |
| 46 | + internal Models.ViewModel Model { get; set; } |
| 47 | + internal string GeneratedCode { get; set; } |
| 48 | + internal bool HasParsingErrors { get; set; } |
| 49 | + internal bool HasRenderingErrors { get; set; } |
| 50 | + |
| 51 | + internal async Task LoadSource() |
| 52 | + { |
| 53 | + IIntrosepctionProvider provider = this.introspectionProviders.Single(x => x.SchemaType == this.settings.Schema.SchemaType()); |
| 54 | + |
| 55 | + string schema = await provider.LoadSchema(this.settings.Schema); |
| 56 | + // we need to load in the scheme |
| 57 | + List<NamedSource> sources = new List<NamedSource>(); |
| 58 | + sources.Add(new NamedSource() { Path = this.settings.Schema.Location, Body = schema }); |
| 59 | + |
| 60 | + foreach (var s in this.settings.SourceFiles) |
| 61 | + { |
| 62 | + sources.Add(s); |
| 63 | + } |
| 64 | + |
| 65 | + Sources = sources; |
| 66 | + } |
| 67 | + |
| 68 | + internal void Export() |
| 69 | + { |
| 70 | + Directory.CreateDirectory(Path.GetDirectoryName(this.settings.OutputPath)); |
| 71 | + File.WriteAllText(this.settings.OutputPath, GeneratedCode); |
| 72 | + } |
| 73 | + |
| 74 | + internal void Render() |
| 75 | + { |
| 76 | + if (!HasParsingErrors) |
| 77 | + { |
| 78 | + var errors = new List<GraphQLError>(); |
| 79 | + this.GeneratedCode = new TemplateEngine(this.settings.Templates, this.settings.TemplateSettings, logger, Model, errors).Generate(); |
| 80 | + |
| 81 | + if (errors.Any()) |
| 82 | + { |
| 83 | + foreach (var error in errors) |
| 84 | + { |
| 85 | + logger.Error(error.ToString()); |
| 86 | + } |
| 87 | + HasRenderingErrors = true; |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + HasRenderingErrors = false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + internal void Parse() |
| 96 | + { |
| 97 | + Document = IntrospectedSchemeParser.Parse(Sources, this.settings); |
| 98 | + |
| 99 | + if (Document.Errors.Any()) |
| 100 | + { |
| 101 | + foreach (var error in Document.Errors) |
| 102 | + { |
| 103 | + logger.Error(error.ToString()); |
| 104 | + } |
| 105 | + HasParsingErrors = true; |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + Model = new Models.ViewModel(Document, this.settings); |
| 110 | + |
| 111 | + HasParsingErrors = false; |
| 112 | + } |
| 113 | + |
| 114 | + public async Task<bool> GenerateAsync() |
| 115 | + { |
| 116 | + await LoadSource(); |
| 117 | + |
| 118 | + Parse(); |
| 119 | + |
| 120 | + if (HasParsingErrors) |
| 121 | + { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + Render(); |
| 126 | + |
| 127 | + if (HasRenderingErrors) |
| 128 | + { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + Export(); |
| 133 | + |
| 134 | + return true; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments