-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
257 lines (215 loc) · 10.6 KB
/
Program.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* *********************************************************************
* PawnBuild
* *********************************************************************
* Copyright (c) 2021 - Sasinosoft Games
* *********************************************************************
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *********************************************************************
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace PawnBuild
{
public class Program
{
public static void Main(string[] args)
{
if(args.Length < 1)
{
Console.WriteLine("Usage: PawnBuild <buildFileName.json> [options]");
Console.WriteLine(@"
Options:
-r (--run): executes the run instruction after building
-v (--verbose): prints more information
-f (--force): does not skip any file
");
return;
}
string fname = args[0];
if (!File.Exists(fname))
{
Console.WriteLine("The specified file does not exist.");
return;
}
Directory.SetCurrentDirectory(new FileInfo(fname).Directory.FullName);
// parse args
bool run = args.Contains("-r") || args.Contains("--run");
bool verbose = args.Contains("-v") || args.Contains("--verbose");
bool force = args.Contains("-f") || args.Contains("--force");
if (verbose)
Console.WriteLine($"----- Parsing {fname}... -----");
BuildFile buildFile = null;
try
{
buildFile = JsonConvert.DeserializeObject<BuildFile>(File.ReadAllText(fname));
}
catch (JsonSerializationException e)
{
Console.WriteLine($"\nSyntax error: invalid build file format ({fname}).");
Console.WriteLine(e.ToString());
}
try
{
if (verbose)
{
Console.WriteLine("\n----- Source -> Output -----");
foreach (BuildFolder build in buildFile.BuildFolders)
Console.WriteLine($"{build.SourceFolder} ===> {build.OutputFolder}");
Console.WriteLine("\n----- Include Folders -----");
foreach (string inc in buildFile.IncludeFolders)
Console.WriteLine($"{inc}");
Console.WriteLine("\n----- Files to Compile -----");
foreach (string file in buildFile.Files)
Console.WriteLine($"{file}");
Console.WriteLine("\n----- Starting build... -----");
}
int succeeded = 0;
int failed = 0;
int skipped = 0;
Console.WriteLine($"------ Build started - Project: {buildFile.ProjectName} ------");
foreach (string file in buildFile.Files)
{
foreach (BuildFolder build in buildFile.BuildFolders)
{
string srcPath = Path.Combine(build.SourceFolder, file);
if (File.Exists(srcPath))
{
string outPath = Path.Combine(build.OutputFolder, Path.GetFileNameWithoutExtension(file) + ".amx");
if (!force)
{
//
DateTime outTime = File.GetLastWriteTime(outPath);
// recursively check if include files changed
var checkedFiles = new List<string>();
DateTime srcTime = DateTime.MaxValue;
void checkLastCompileTimeRecursive(string path)
{
// skip if already checked
if (checkedFiles.Contains(path))
return;
if (!File.Exists(path))
return;
// check last write time
var t = File.GetLastWriteTime(path);
if (t > outTime)
if (t < srcTime)
srcTime = t;
checkedFiles.Add(path);
// check includes of this file
using (var fileReader = new StreamReader(path))
{
string line;
while ((line = fileReader.ReadLine()) != null)
{
if (!line.StartsWith('#') && !line.StartsWith(' ') && !line.StartsWith('\t'))
continue;
if (line.Contains("#include") || line.Contains("#tryinclude"))
{
var idx = line.IndexOf(' ', line.IndexOf('#')) + 1;
var includePath = line
.Substring(idx)
.Trim()
.Replace("<", "")
.Replace(">", "")
.Replace("\"", "");
checkLastCompileTimeRecursive(includePath);
}
}
}
}
checkLastCompileTimeRecursive(srcPath);
if (srcTime < outTime)
{
Console.WriteLine($"* Skipping {srcPath}");
skipped++;
continue;
}
}
string compilerArgs = $"\"{srcPath}\" -o\"{outPath}\" ";
foreach (string incPath in buildFile.IncludeFolders)
compilerArgs += $"-i\"{incPath}\" ";
if (!string.IsNullOrWhiteSpace(buildFile.Args))
compilerArgs += $"{buildFile.Args} ";
Console.WriteLine($"* Compiling {srcPath} -> {outPath}");
var pawnccOutput = new StringBuilder();
var startInfo = new ProcessStartInfo()
{
FileName = "pawncc",
Arguments = compilerArgs,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (var process = new Process() { StartInfo = startInfo })
{
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
{
pawnccOutput.AppendLine(e.Data);
};
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
{
pawnccOutput.AppendLine(e.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(5000);
string finalOutput = pawnccOutput
.ToString()
.Insert(0, " ")
.Replace("\n", "\n ")
.TrimEnd()
;
if (finalOutput.Contains(" Error.") || finalOutput.Contains(" Errors."))
failed++;
else
succeeded++;
if (finalOutput.Contains(" Error.") || finalOutput.Contains(" Errors.")
|| finalOutput.Contains(" Warning.") || finalOutput.Contains(" Warnings."))
{
Console.WriteLine(finalOutput + "\n");
}
}
}
}
}
Console.WriteLine($"========== Build: {succeeded} succeeded, {failed} failed, {skipped} skipped ==========");
if (run)
{
if (buildFile.Run.Length > 0)
{
foreach (string runFile in buildFile.Run)
{
if (!string.IsNullOrWhiteSpace(runFile))
{
var runProcess = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = new FileInfo(runFile).FullName,
WorkingDirectory = new FileInfo(runFile).Directory.FullName
}
};
runProcess.Start();
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine("\nGeneric error:");
Console.WriteLine(e.ToString());
}
}
}
}