Skip to content

Commit 9362328

Browse files
committed
add resolve path helper to alow easier relative file path generation
1 parent fb4e5ff commit 9362328

File tree

17 files changed

+238
-4
lines changed

17 files changed

+238
-4
lines changed

Tocsoft.GraphQLCodeGen.Cli/CodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal void Export()
7070

7171
internal void Render()
7272
{
73-
this.GeneratedCode = new TemplateEngine(this.settings.Templates, this.settings.TemplateSettings, logger).Generate(Model);
73+
this.GeneratedCode = new TemplateEngine(this.settings.Templates, this.settings.TemplateSettings, logger, Model).Generate();
7474
}
7575

7676
internal void Parse()

Tocsoft.GraphQLCodeGen.Cli/CodeGeneratorSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class CodeGeneratorSettings
1313

1414
public IEnumerable<string> Templates { get; set; }
1515
public IDictionary<string, string> TemplateSettings { get; internal set; }
16+
public string RootPath { get; internal set; }
1617
internal SchemaSource Schema { get; set; }
1718
}
1819
}

Tocsoft.GraphQLCodeGen.Cli/CodeGeneratorSettingsLoader.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public IEnumerable<CodeGeneratorSettings> GenerateSettings(CodeGeneratorSettings
7878
ClassName = cn,
7979
Namespace = nspc,
8080
OutputPath = first.OutputPath,
81+
RootPath = first.RootPath,
8182
TypeNameDirective = first.TypeNameDirective,
8283
SourceFiles = x.Select(p => new NamedSource
8384
{
@@ -326,6 +327,11 @@ private bool ExpandSettings(string path, SimpleSourceFile file)
326327
}
327328
}
328329

330+
if (settings.Root)
331+
{
332+
file.RootPath = root;
333+
}
334+
329335
return settings.Root;
330336
}
331337
}

Tocsoft.GraphQLCodeGen.Cli/Models/ViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class ViewModel
1414

1515
public string Namespace { get; private set; }
1616
public string ClassName { get; private set; }
17+
public string OutputPath { get; }
18+
public string RootPath { get; }
1719

1820
private Dictionary<string, TypeViewModel> typeLookup = new Dictionary<string, TypeViewModel>();
1921
private Dictionary<IGraphQLType, string> inputTypeLookup = new Dictionary<IGraphQLType, string>();
@@ -32,6 +34,8 @@ internal ViewModel(GraphQLDocument query, CodeGeneratorSettings settings)
3234
this.query = query;
3335
this.Namespace = settings.Namespace;
3436
this.ClassName = settings.ClassName;
37+
this.OutputPath = settings.OutputPath;
38+
this.RootPath = settings.RootPath;
3539

3640
this.typeLookup = new Dictionary<string, TypeViewModel>();
3741
this.inputTypeLookup = new Dictionary<IGraphQLType, string>();

Tocsoft.GraphQLCodeGen.Cli/SimpleSourceFile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal class SimpleSourceFile
1818
public Dictionary<string, string> TemplateSettings { get; set; } = new Dictionary<string, string>();
1919
public List<string> Includes { get; set; } = new List<string>();
2020
public SchemaSource SchemaSource { get; set; }
21+
public string RootPath { get; set; }
2122

2223
internal string SettingsHash()
2324
{
@@ -27,6 +28,8 @@ internal string SettingsHash()
2728
sb.Append("~#~");
2829
sb.Append(OutputPath);
2930
sb.Append("~#~");
31+
sb.Append(RootPath);
32+
sb.Append("~#~");
3033
sb.Append(Format);
3134
sb.Append("~#~");
3235
sb.Append(TypeNameDirective);

Tocsoft.GraphQLCodeGen.Cli/TemplateEngine.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
using Tocsoft.GraphQLCodeGen.Cli;
99
using System.Text.RegularExpressions;
1010
using System.Runtime.InteropServices.ComTypes;
11+
using Tocsoft.GraphQLCodeGen.Models;
1112

1213
namespace Tocsoft.GraphQLCodeGen
1314
{
1415
public class TemplateEngine
1516
{
1617
private readonly ILogger logger;
18+
private readonly ViewModel model;
1719
private IHandlebars engine;
1820

19-
public TemplateEngine(IEnumerable<string> templates, IDictionary<string, string> templateArguments, ILogger logger)
21+
public TemplateEngine(IEnumerable<string> templates, IDictionary<string, string> templateArguments, ILogger logger, Models.ViewModel model)
2022
{
2123
this.logger = logger;
24+
this.model = model;
2225
this.engine = HandlebarsDotNet.Handlebars.Create(new HandlebarsConfiguration
2326
{
2427
ThrowOnUnresolvedBindingExpression = true,
@@ -47,6 +50,34 @@ public TemplateEngine(IEnumerable<string> templates, IDictionary<string, string>
4750
writer.WriteSafeString(args[0].ToString().Replace(toReplace, toReplaceWith));
4851
});
4952

53+
this.engine.RegisterHelper("resolve", (writer, context, args) =>
54+
{
55+
string target = args[0].ToString();
56+
57+
var currentDirectory = Path.GetDirectoryName(model.OutputPath);
58+
System.Uri uri1;
59+
if (target.StartsWith("~/"))
60+
{
61+
uri1 = new Uri(Path.GetFullPath(Path.Combine(model.RootPath, target.Substring(2))));
62+
}
63+
else
64+
{
65+
uri1 = new Uri(target, UriKind.Relative);
66+
}
67+
68+
System.Uri uri2 = new Uri(currentDirectory.TrimEnd() + "\\");
69+
70+
Uri relativeUri = uri2.MakeRelativeUri(uri1);
71+
72+
var path = relativeUri.ToString();
73+
if (path[0] != '.')
74+
{
75+
path = "./" + path;
76+
}
77+
78+
writer.WriteSafeString(path);
79+
});
80+
5081
HandlebarsBlockHelper ifTemplateHelper = (output, options, context, args) =>
5182
{
5283
var val = this.engine.Compile("{{> " + args[0].ToString() + "}}")((object)context)?.ToString()?.Trim();
@@ -115,7 +146,7 @@ public string Encode(string value)
115146

116147
private Func<object, string> template;
117148

118-
public string Generate(object model)
149+
public string Generate()
119150
{
120151
try
121152
{

Tocsoft.GraphQLCodeGen.Cli/Templates/ts/typescript.template

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{!# Includes }}
22

3+
{{!# ClientClassDecorators }}
4+
35
{{!# Main}}
46
/* tslint:disable */
57
//----------------------
@@ -35,6 +37,7 @@ function {{pascalCase ClassName}}Error(message:string, httpResponse:Response, gr
3537

3638
export { {{pascalCase ClassName}}Error }
3739

40+
{{>ClientClassDecorators}}
3841
export default class {{pascalCase ClassName}}Client {
3942

4043
constructor(public client: fetchClient, public url: string) { }
@@ -139,6 +142,12 @@ export class {{pascalCase Name}} {
139142
{{!# TypeReference_ID}}
140143
{{~> RenderTypeReference typename='string' isValueType=false fixCase=false isCollection=IsCollection nullable=CanValueBeNull collectionNullable=CanCollectionBeNull }}
141144

145+
{{!# TypeReference_GUID}}
146+
{{~> RenderTypeReference typename='string' isValueType=false fixCase=false isCollection=IsCollection nullable=CanValueBeNull collectionNullable=CanCollectionBeNull }}
147+
148+
{{!# TypeReference_Uuid}}
149+
{{~> RenderTypeReference typename='string' isValueType=false fixCase=false isCollection=IsCollection nullable=CanValueBeNull collectionNullable=CanCollectionBeNull }}
150+
142151
{{!# TypeReference_Float}}
143152
{{~> RenderTypeReference typename='number' isValueType=true fixCase=false isCollection=IsCollection nullable=CanValueBeNull collectionNullable=CanCollectionBeNull }}
144153

@@ -170,6 +179,12 @@ export class {{pascalCase Name}} {
170179
{{!# FieldSetter_ID}}
171180
result.{{camelCase Name}} = json["{{camelCase Name}}"];
172181

182+
{{!# FieldSetter_GUID}}
183+
result.{{camelCase Name}} = json["{{camelCase Name}}"];
184+
185+
{{!# FieldSetter_Uuid}}
186+
result.{{camelCase Name}} = json["{{camelCase Name}}"];
187+
173188
{{!# FieldSetter_Float}}
174189
result.{{camelCase Name}} = json["{{camelCase Name}}"];
175190

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query q{
2+
test(id: "safsa")
3+
{
4+
nullable,
5+
nonnullable
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"root": true,
3+
"schema": "schema.gqlschema",
4+
"output": "test.cs",
5+
"class": "Sample.Client.Test",
6+
7+
"templateSettings": {
8+
"interfaceBase": "IFooBase"
9+
}
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+

2+
schema {
3+
query: Query
4+
mutation: Mutation
5+
}
6+
7+
type Query {
8+
test(id: string!): Droid
9+
}
10+
11+
type Droid {
12+
nullable: Date
13+
nonnullable: Date!
14+
name: String!
15+
}
16+
17+
scalar Date
18+
19+
scalar String

0 commit comments

Comments
 (0)