-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
190 lines (163 loc) · 6.62 KB
/
Program.cs
File metadata and controls
190 lines (163 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
namespace tiny_t4;
class Program
{
private class TransformResult
{
public string Code { get; }
public KeyValuePair<string, string>[] Directives { get; }
public TransformResult(string code, KeyValuePair<string, string>[] directives)
{
Code = code ?? throw new ArgumentNullException(nameof(code));
Directives = directives ?? throw new ArgumentNullException(nameof(directives));
}
}
static void Main(string[] args)
{
var forceRebgen = args.Contains("--force");
var toStdOut = false;
string[] files = null;
files = args.Where(x => !x.StartsWith("--")).ToArray();
if (files.Length > 0)
{
toStdOut = true;
Console.WriteLine("Files to process: " + string.Join(Environment.NewLine, files));
}
else
{
Console.WriteLine("Current directory: " + Directory.GetCurrentDirectory());
files = Directory.EnumerateFiles(".", "*.tt", SearchOption.AllDirectories).ToArray();
Console.WriteLine("Found files:" + Environment.NewLine + string.Join(Environment.NewLine, files));
Console.WriteLine();
}
var originalConsoleOutput = new StreamWriter(Console.OpenStandardOutput());
foreach (var file in files)
{
var targetExtension = "cs";
var targetFileName = Path.ChangeExtension(file, targetExtension);
if (File.GetLastWriteTime(file) < File.GetLastWriteTime(targetFileName) && !forceRebgen)
{
originalConsoleOutput.WriteLine("Skipped: " + file);
originalConsoleOutput.Flush();
continue;
}
var fileProcessingStopWatch = Stopwatch.StartNew();
originalConsoleOutput.WriteLine("Processing: " + file);
originalConsoleOutput.Flush();
var code = File.ReadAllText(file);
var transformResult = transform(code);
var scriptOptions = ScriptOptions.Default;
scriptOptions = scriptOptions.AddReferences(Assembly.GetExecutingAssembly());
foreach (var directive in transformResult.Directives)
{
switch (directive.Key)
{
case "import namespace":
{
scriptOptions = scriptOptions.AddImports(directive.Value);
break;
}
}
}
StreamWriter writter = null;
if (!toStdOut)
{
writter = new StreamWriter(new FileStream(targetFileName, FileMode.Create));
Console.SetOut(writter);
}
code = transformResult.Code;
code = $"static readonly tiny_t4.{nameof(Host)} Host = new tiny_t4.{nameof(Host)}(@\"{Path.GetFullPath(file)}\");\n" + code;
try
{
var script = CSharpScript.Create(code, scriptOptions);
script.Compile();
script.CreateDelegate().Invoke().ContinueWith((result) =>
{
if (!result.IsCompletedSuccessfully)
{
Console.Error.WriteLine(result.Exception);
}
else
{
Console.WriteLine(result.Result);
}
})
.Wait();
originalConsoleOutput.WriteLine("Done: " + file + ". Elapsed: " + fileProcessingStopWatch.Elapsed);
originalConsoleOutput.Flush();
}
catch (Exception e)
{
var innerException = e.InnerException;
while (innerException != null)
{
Console.Error.WriteLine(innerException.GetType().Name);
Console.Error.WriteLine(innerException.StackTrace);
Console.Error.WriteLine(innerException.Message);
innerException = e.InnerException;
}
File.WriteAllText(Path.ChangeExtension(file, "debug_error." + targetExtension), code);
Console.WriteLine("Error: " + e.Message);
throw;
}
finally
{
if (!toStdOut)
{
writter.Dispose();
}
}
}
}
private static TransformResult transform(string code)
{
var parts = ("#>" + code).Split("<#");
var output = new StringBuilder();
var directives = new List<KeyValuePair<string, string>>();
var textBegun = false;
for (var i = 0; i < parts.Length; i++)
{
var part = parts[i];
var endOfPart = part.IndexOf("#>");
if (endOfPart == -1)
throw new ArgumentException(nameof(code));
var partCode = part.Substring(0, endOfPart);
var partText = part.Substring(endOfPart + 2);
if (partCode.Length > 0)
{
if (partCode[0] == '@')
{
var directive = partCode.Substring(1).Trim();
var valuePos = directive.IndexOf("=");
var value = directive.Substring(valuePos + 1);
directive = directive.Substring(0, valuePos);
if (value[0] == '"')
value = value.Trim('"');
directives.Add(new KeyValuePair<string, string>(directive, value));
}
else if (partCode[0] == '=')
{
output.Append("System.Console.Write(" + partCode.Substring(1).Trim() + ");");
}
else
{
output.Append(partCode);
}
}
if (textBegun ? partText.Length > 0 : !string.IsNullOrWhiteSpace(partText))
{
textBegun = true;
output.AppendLine("System.Console.Write(@\"" + partText.Replace("\"", "\"\"") + "\");");
}
}
return new TransformResult(output.ToString(), directives.ToArray());
}
}