-
Notifications
You must be signed in to change notification settings - Fork 20
/
build.csx
172 lines (144 loc) · 4.05 KB
/
build.csx
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
#!/usr/bin/env dotnet-script
#r "nuget: Microsoft.CodeAnalysis.CSharp, 4.7.0"
#r "nuget: HedgeModManager.CodeCompiler, 0.2.37"
using HedgeModManager.CodeCompiler;
var StartDirectory = Environment.CurrentDirectory;
var TestOnly = false;
var Failures = new List<string>();
Environment.CurrentDirectory = Path.Combine(StartDirectory, "Source");
foreach(var arg in Args)
{
if (arg.Equals("test", StringComparison.OrdinalIgnoreCase))
{
TestOnly = true;
}
}
await BuildAllFolders(".");
if (Failures.Count != 0)
{
Console.WriteLine("Failed builds:");
foreach(var fail in Failures)
{
Console.WriteLine($"- {fail}");
}
Environment.Exit(1);
}
async Task BuildAllFolders(string root)
{
foreach(var dir in Directory.EnumerateDirectories(root))
{
await BuildFolder(dir);
Console.WriteLine();
}
}
async Task BuildFolder(string path, bool test = true)
{
if (TestOnly)
{
test = true;
}
var name = Path.GetFileName(path);
Console.WriteLine($"Building {name}");
var system = new BuildSystem();
if (!string.Equals(Path.GetFileName(path), "Globals", StringComparison.OrdinalIgnoreCase))
{
system.AddFolder("Globals");
}
system.AddFolder(path);
var result = system.Build();
var failed = false;
if (test)
{
Console.WriteLine($"Testing {name}");
Console.WriteLine("Detected codes:");
var codes = CodeFile.FromText(result);
foreach(var code in codes)
{
Console.WriteLine($"- {code.Name}");
}
var options = new CompilerOptions<CodeFile>(codes)
{
IncludeResolver = codes,
IncludeAllSources = true
};
var report = await CodeProvider.CompileCodes(options);
failed = report.HasErrors;
if (report.Blocks.Count != 0)
{
Console.WriteLine("Build Logs:");
foreach(var block in report.Blocks)
{
Console.WriteLine($"{block.Key}:");
foreach(var message in block.Value)
{
Console.WriteLine($"\t{message.ToString()}");
}
Console.WriteLine();
}
Console.WriteLine();
}
}
if (failed)
{
Console.WriteLine($"{name} Checks failed");
Failures.Add(name);
}
else
{
Console.WriteLine($"{name} built successfully");
if (!TestOnly)
{
string GetCodeFileName()
{
return Path.GetFileName(path)
.Replace(" ", "")
.Replace("(", "")
.Replace(")", "");
}
var outPath = Path.Combine(StartDirectory, "build", $"{GetCodeFileName()}.hmm");
Directory.CreateDirectory(Path.GetDirectoryName(outPath));
File.WriteAllText(outPath, result);
}
}
}
public class BuildSystem
{
public List<Action<StringBuilder>> BuildActions { get; } = new List<Action<StringBuilder>>();
public void AddText(string text)
{
BuildActions.Add(x => x.Append(text));
}
public void AddFile(string path)
{
var fullPath = Path.GetFullPath(path);
if (!File.Exists(fullPath))
{
return;
}
BuildActions.Add(x => x.AppendLine(File.ReadAllText(fullPath)));
}
public void AddFolder(string path, string filter = "*.hmm")
{
var fullPath = Path.GetFullPath(path);
if (!Directory.Exists(fullPath))
{
return;
}
BuildActions.Add(x =>
{
foreach(var file in Directory.EnumerateFiles(fullPath, filter, SearchOption.AllDirectories))
{
x.AppendLine(File.ReadAllText(file));
}
});
}
public string Build()
{
var builder = new StringBuilder();
foreach(var action in BuildActions)
{
action(builder);
}
return builder.ToString();
}
}