Skip to content

Commit c0ca946

Browse files
committed
Add import logic
1 parent 5cd130e commit c0ca946

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

src/Assets/SimpleCodeGenerator/Editor/IntermediateTemplate.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ public IntermediateTemplate(IEnumerable<string> template, object data)
1818
content = new List<string>(template);
1919
}
2020

21+
public IntermediateTemplate RenderImports()
22+
{
23+
var result = new IntermediateTemplate(content, data);
24+
25+
for (int i = 0; i < result.content.Count; i++)
26+
{
27+
result.content[i] = InsertImportedTemplates(result.content[i]);
28+
}
29+
30+
return result;
31+
}
32+
2133
public IntermediateTemplate RenderStandaloneValues()
2234
{
2335
var result = new IntermediateTemplate(content, data);
@@ -82,6 +94,23 @@ public IntermediateTemplate RenderForLoops()
8294
return result;
8395
}
8496

97+
private static string InsertImportedTemplates(string input)
98+
{
99+
while (true)
100+
{
101+
Match importMatch = Regex.Match(input, @"\{\{\s*import\s+(?<templateName>\S+)\s*\}\}");
102+
103+
if (!importMatch.Success)
104+
break;
105+
106+
string templateName = importMatch.Groups["templateName"].Value;
107+
108+
// TODO
109+
}
110+
111+
return input;
112+
}
113+
85114
private static string InsertValuesFromObject(string input, object objectToInsert, string propertyRootPath = "")
86115
{
87116
while (true)

src/Assets/SimpleCodeGenerator/Editor/Template.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.IO;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using JetBrains.Annotations;
25
using SimpleCodeGenerator.Core;
36
using UnityEditor;
47
using UnityEngine;
@@ -8,20 +11,36 @@ namespace SimpleCodeGenerator.Editor
811
public class Template
912
{
1013
private readonly string[] content;
14+
private readonly Dictionary<string, Template> importedTemplates = new();
1115

1216
private Template(string[] content)
1317
{
1418
this.content = content;
1519
}
1620

21+
[PublicAPI]
1722
public static Template ParseFromFile(string path)
1823
{
1924
return new Template(File.ReadAllLines(path));
2025
}
2126

27+
[PublicAPI]
28+
public static Template ParseFromLines(string[] lines)
29+
{
30+
return new Template(lines);
31+
}
32+
33+
[PublicAPI]
34+
public static Template Parse(string contentWithLineBreaks)
35+
{
36+
return ParseFromLines(contentWithLineBreaks.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None));
37+
}
38+
39+
[PublicAPI]
2240
public string Render(object data)
2341
{
2442
return new IntermediateTemplate(content, data)
43+
.RenderImports()
2544
.RenderStandaloneValues()
2645
.RenderForLoops()
2746
.Build();
@@ -32,6 +51,7 @@ internal static Template FindBuiltInTemplate(string templateName)
3251
return ParseFromFile(GetAbsolutePathToBuiltInTemplate(templateName));
3352
}
3453

54+
[PublicAPI]
3555
public static bool TryFindTemplateInAssets(string templateAssetPath, out Template template)
3656
{
3757
if (!templateAssetPath.StartsWith("Assets/"))
@@ -59,5 +79,12 @@ internal static AbsolutePath GetAbsolutePathToBuiltInTemplate(string templateNam
5979
{
6080
return GetPathToBuiltInTemplates() / $"{templateName}.txt";
6181
}
82+
83+
[PublicAPI]
84+
public Template Import(string key, Template template)
85+
{
86+
importedTemplates.Add(key, template);
87+
return this;
88+
}
6289
}
6390
}

tests/TemplateTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,21 @@ public Task Render_RenderValidTemplate2_TemplateIsRenderedCorrectly()
8585
settings.UseDirectory("Verify");
8686
return Verify(result, settings);
8787
}
88-
88+
8989
[Fact]
9090
public Task Render_TemplateWithImportedTemplate_TemplateIsRenderedCorrectly()
9191
{
92-
Template rootTemplate = Template.ParseFromFile(TemplateFileImport);
92+
Template template = Template.ParseFromFile(TemplateFileImport);
93+
template.Import("Template_To_Import", Template.ParseFromFile(TemplateFileImportPartial));
94+
template.Import("This_Can_Be_Any_Key", Template.Parse("1234567890"));
9395

94-
var rootData = new
96+
var data = new
9597
{
9698
Template = "Import",
9799
SomeNumbers = new[] { 1, 2, 3, 4, 5 }
98100
};
99101

100-
string result = rootTemplate.Render(rootData);
102+
string result = template.Render(data);
101103

102104
var settings = new VerifySettings();
103105
settings.UseDirectory("Verify");

tests/Templates/Test_Template_Import.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{{ import "Test_Template_1" }}
1+
{{ import "Template_To_Import" }}
22

33
// Test Template {{ Template }} with imported template
44

5-
{{ import "Test_Template_1" }}
5+
{{ import "Template_To_Import" }}
66
{{ import "This_Can_Be_Any_Key" }}
77

88
{{ for number in SomeNumbers }}

0 commit comments

Comments
 (0)