|
8 | 8 | using Tocsoft.GraphQLCodeGen.Cli; |
9 | 9 | using System.Text.RegularExpressions; |
10 | 10 | using System.Runtime.InteropServices.ComTypes; |
| 11 | +using Tocsoft.GraphQLCodeGen.Models; |
11 | 12 |
|
12 | 13 | namespace Tocsoft.GraphQLCodeGen |
13 | 14 | { |
14 | 15 | public class TemplateEngine |
15 | 16 | { |
16 | 17 | private readonly ILogger logger; |
| 18 | + private readonly ViewModel model; |
17 | 19 | private IHandlebars engine; |
18 | 20 |
|
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) |
20 | 22 | { |
21 | 23 | this.logger = logger; |
| 24 | + this.model = model; |
22 | 25 | this.engine = HandlebarsDotNet.Handlebars.Create(new HandlebarsConfiguration |
23 | 26 | { |
24 | 27 | ThrowOnUnresolvedBindingExpression = true, |
@@ -47,6 +50,34 @@ public TemplateEngine(IEnumerable<string> templates, IDictionary<string, string> |
47 | 50 | writer.WriteSafeString(args[0].ToString().Replace(toReplace, toReplaceWith)); |
48 | 51 | }); |
49 | 52 |
|
| 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 | + |
50 | 81 | HandlebarsBlockHelper ifTemplateHelper = (output, options, context, args) => |
51 | 82 | { |
52 | 83 | var val = this.engine.Compile("{{> " + args[0].ToString() + "}}")((object)context)?.ToString()?.Trim(); |
@@ -115,7 +146,7 @@ public string Encode(string value) |
115 | 146 |
|
116 | 147 | private Func<object, string> template; |
117 | 148 |
|
118 | | - public string Generate(object model) |
| 149 | + public string Generate() |
119 | 150 | { |
120 | 151 | try |
121 | 152 | { |
|
0 commit comments