Skip to content

Commit 3492cad

Browse files
committed
Add import logic
1 parent 5cd130e commit 3492cad

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

src/Assets/SimpleCodeGenerator/Editor/IntermediateTemplate.cs

Lines changed: 36 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,30 @@ 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+
if (Template.TryFindTemplateInAssets(templateName, out Template template))
109+
{
110+
input = input.Replace(importMatch.Value, template.Render(new { }));
111+
}
112+
else
113+
{
114+
input = input.Replace(importMatch.Value, "");
115+
}
116+
}
117+
118+
return input;
119+
}
120+
85121
private static string InsertValuesFromObject(string input, object objectToInsert, string propertyRootPath = "")
86122
{
87123
while (true)

src/Assets/SimpleCodeGenerator/Editor/Template.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public static Template ParseFromFile(string path)
1919
return new Template(File.ReadAllLines(path));
2020
}
2121

22+
public static Template ParseFromLines(string[] lines)
23+
{
24+
return new Template(lines);
25+
}
26+
27+
public static Template Parse(string contentWithLineBreaks)
28+
{
29+
return ParseFromLines(contentWithLineBreaks.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None));
30+
}
31+
2232
public string Render(object data)
2333
{
2434
return new IntermediateTemplate(content, data)
@@ -59,5 +69,10 @@ internal static AbsolutePath GetAbsolutePathToBuiltInTemplate(string templateNam
5969
{
6070
return GetPathToBuiltInTemplates() / $"{templateName}.txt";
6171
}
72+
73+
public Template Import(string key, Template template)
74+
{
75+
return this;
76+
}
6277
}
6378
}

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)