-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZXProjectBuilder.cs
More file actions
823 lines (678 loc) · 32.2 KB
/
ZXProjectBuilder.cs
File metadata and controls
823 lines (678 loc) · 32.2 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using ZXBasicStudio.Classes;
using ZXBasicStudio.Controls;
using ZXBasicStudio.Dialogs;
using ZXBasicStudio.DocumentModel.Classes;
using ZXBasicStudio.IntegratedDocumentTypes.CodeDocuments.Basic;
namespace ZXBasicStudio.BuildSystem
{
public class ZXProjectBuilder
{
public static ZXProgram? Build(TextWriter OutputLogWritter)
{
try
{
if (ZXProjectManager.Current == null)
{
OutputLogWritter.WriteLine("No open project, aborting...");
return null;
}
var project = ZXProjectManager.Current;
Cleanup(project.ProjectPath);
ZXBuildSettings? settings = null;
settings = null;
string? mainFile = null;
if (string.IsNullOrWhiteSpace(ZXOptions.Current.ZxbcPath) || string.IsNullOrWhiteSpace(ZXOptions.Current.ZxbasmPath))
{
OutputLogWritter.WriteLine("Paths for ZXBASM and ZXBC not configured, aborting...");
return null;
}
if (!File.Exists(ZXOptions.Current.ZxbcPath) || !File.Exists(ZXOptions.Current.ZxbasmPath))
{
OutputLogWritter.WriteLine("ZXBASM/ZXBC not found, aborting...");
return null;
}
settings = project.GetProjectSettings();
mainFile = project.GetMainFile();
// Prebuild
if (settings.PreBuild)
{
OutputLogWritter.WriteLine($"PreBuild: {settings.PreBuildValue}");
if (!ExecuteFile(settings.PreBuildValue, new string[]
{
project.ProjectPath,
Path.GetFileName(mainFile)
},
project.ProjectPath,
OutputLogWritter))
{
return null;
}
}
if (mainFile == null)
{
OutputLogWritter.WriteLine("Cannot find main file, check that it exists and if there are more than one that is specified in the build settings.");
return null;
}
if (!PreBuild(false, project.ProjectPath, OutputLogWritter))
return null;
Process? proc = null;
var startTime = DateTime.Now;
OutputLogWritter.WriteLine("Project path: " + project.ProjectPath);
OutputLogWritter.WriteLine("Building program " + mainFile);
OutputLogWritter.WriteLine("Building starts at " + startTime);
var args = settings.GetSettings();
if (settings.CustomCompiler)
{
OutputLogWritter.WriteLine("Custom compiler settings");
args = settings.CustomCompilerValue;
}
OutputLogWritter.WriteLine($"zxbc \"{mainFile}\" {args}");
proc = Process.Start(new ProcessStartInfo(Path.GetFullPath(ZXOptions.Current.ZxbcPath), $"\"{mainFile}\" " + args) { WorkingDirectory = project.ProjectPath, RedirectStandardError = true, CreateNoWindow = true });
string logOutput;
OutputProcessLog(OutputLogWritter, proc, out logOutput);
var ecode = proc.ExitCode;
if (ecode != 0)
{
Cleanup(project.ProjectPath);
OutputLogWritter.WriteLine("Error building program, aborting...");
return null;
}
string binFile = Path.Combine(project.ProjectPath, Path.GetFileNameWithoutExtension(mainFile) + ".bin");
// Postbuild
if (settings.PostBuild)
{
OutputLogWritter.WriteLine($"PostBuild: {settings.PostBuildValue}");
if (!ExecuteFile(settings.PostBuildValue, new string[]
{
project.ProjectPath,
Path.GetFileName(binFile)
},
project.ProjectPath,
OutputLogWritter))
{
return null;
}
}
byte[] binary = File.ReadAllBytes(binFile);
Cleanup(project.ProjectPath, binFile);
ZXProgram program = ZXProgram.CreateReleaseProgram(binary, settings.Origin ?? 32768);
if (binary.Length + program.Org > 0xFFFF)
{
OutputLogWritter.WriteLine("Program too long, change base address or reduce code size.");
return null;
}
if (!PostBuild(false, project.ProjectPath, program, OutputLogWritter))
return null;
OutputLogWritter.WriteLine($"Program size: {binary.Length} bytes");
OutputLogWritter.WriteLine("Program built successfully (Elapsed time: " + (startTime - DateTime.Now).Duration().ToString(@"hh\:mm\:ss") + " seconds)");
if (settings.NextMode)
{
if (!BuildNexFile(binary, settings, project, OutputLogWritter))
{
return null;
}
}
return program;
}
catch (Exception ex)
{
OutputLogWritter.WriteLine($"Exception: {ex.Message} {ex.StackTrace}");
return null;
}
}
private static bool BuildNexFile(byte[] binary, ZXBuildSettings settings, ZXProjectManager project, TextWriter outputLogWritter)
{
try
{
outputLogWritter.WriteLine("Building .nex file...");
string binFile = "";
string cfgFile = "";
// Create .bin file
{
outputLogWritter.WriteLine("Creating .bin file...");
binFile = Path.Combine(project.ProjectPath, Path.GetFileNameWithoutExtension(settings.MainFile) + ".bin");
if (File.Exists(binFile))
{
File.Delete(binFile);
}
File.WriteAllBytes(binFile, binary);
}
// Create nex.cfg file
{
outputLogWritter.WriteLine("Creating nex.cfg configuration file...");
var sb = new StringBuilder();
sb.AppendLine("; Minimum core version");
sb.AppendLine("!COR3,0,0");
// sysvars.inc
{
var sysVarsPath = Path.Combine(Environment.CurrentDirectory, "Resources", "sysvars.inc");
var sysVarsDest = Path.Combine(project.ProjectPath, "sysvars.inc");
if (!File.Exists(sysVarsDest))
{
File.Copy(sysVarsPath, sysVarsDest);
}
sb.AppendLine("!MMU./sysvars.inc,10,$1C00");
}
// Origin
int org = settings.Origin == null ? 32768 : settings.Origin.Value;
sb.AppendLine(string.Format("!PCSP${0:X2},${1:X2}", org, org - 2));
// Main file
{
int[] nextBank16K = { 255, 5, 2, 0 };
int bank = org / 16384;
int offset = org - (bank * 16384);
if (bank < 0 || bank > 3)
{
outputLogWritter.WriteLine("Error: Invalid ORG direction, must be >0 and <65535");
return false;
}
bank = nextBank16K[bank];
sb.AppendLine(string.Format(".\\{0},{1},${2:X2}",
Path.Combine(Path.GetFileNameWithoutExtension(settings.MainFile) + ".bin"),
bank,
offset));
}
// Save nex.cfg file
{
cfgFile = Path.Combine(project.ProjectPath, "nex.cfg");
if (File.Exists(cfgFile))
{
File.Delete(cfgFile);
}
File.WriteAllText(cfgFile, sb.ToString());
}
}
// Build nex file
{
string nextDriveFolder = "nextdrive";
// Delete old .nex file
string nexFile = Path.Combine(project.ProjectPath, Path.GetFileNameWithoutExtension(settings.MainFile) + ".nex");
if (File.Exists(nexFile))
{
File.Delete(nexFile);
}
// Check if nextdata folder exists
string dataFolder = Path.Combine(project.ProjectPath, nextDriveFolder);
if (!Directory.Exists(dataFolder))
{
Directory.CreateDirectory(dataFolder);
}
// Delete old .next file in data folder
string nexDataFile = Path.Combine(project.ProjectPath, nextDriveFolder, Path.GetFileNameWithoutExtension(settings.MainFile) + ".nex");
if (File.Exists(nexDataFile))
{
File.Delete(nexDataFile);
}
outputLogWritter.WriteLine("Building .nex file...");
Process process = new Process();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
CheckNextCreator();
process.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(ZXOptions.Current.ZxbcPath), "python", "python.exe");
process.StartInfo.Arguments = string.Format("{0} nex.cfg {1}",
Path.Combine(Path.GetDirectoryName(ZXOptions.Current.ZxbcPath), "tools", "nextcreator.py"),
Path.GetFileNameWithoutExtension(settings.MainFile) + ".nex");
process.StartInfo.WorkingDirectory = project.ProjectPath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
}
else
{
process.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(ZXOptions.Current.ZxbcPath), "tools", "nextcreator.py");
process.StartInfo.Arguments = "nex.cfg " + Path.GetFileNameWithoutExtension(settings.MainFile) + ".nex";
process.StartInfo.WorkingDirectory = project.ProjectPath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
}
outputLogWritter.WriteLine(string.Format("{0} {1}",
process.StartInfo.FileName,
process.StartInfo.Arguments));
process.Start();
process.WaitForExit();
if (!File.Exists(nexFile))
{
outputLogWritter.WriteLine("Error building .nex file");
outputLogWritter.WriteLine(process.StartInfo.WorkingDirectory);
using (StreamReader reader = process.StandardOutput)
{
string output = reader.ReadToEnd();
outputLogWritter.WriteLine(output);
}
return false;
}
// Copy .nex file to data folder
File.Copy(nexFile, nexDataFile);
}
return true;
}
catch (Exception ex)
{
outputLogWritter.WriteLine($"Exception: {ex.Message} {ex.StackTrace}");
return false;
}
}
private static void CheckNextCreator()
{
var fNCexe = Path.Combine(Path.GetDirectoryName(ZXOptions.Current.ZxbcPath), "tools", "nextcreator.exe");
if (File.Exists(fNCexe))
{
return;
}
File.Copy(ZXOptions.Current.ZxbcPath, fNCexe);
return;
}
public static ZXProgram? BuildDebug(TextWriter OutputLogWritter)
{
string cmd = "";
try
{
if (ZXProjectManager.Current == null)
{
OutputLogWritter.WriteLine("No open project, aborting...");
return null;
}
var project = ZXProjectManager.Current;
Cleanup(project.ProjectPath);
ZXBuildSettings? settings = null;
string? mainFile = null;
if (string.IsNullOrWhiteSpace(ZXOptions.Current.ZxbcPath) || string.IsNullOrWhiteSpace(ZXOptions.Current.ZxbasmPath))
{
OutputLogWritter.WriteLine("Paths for ZXBASM and ZXBC not configured, aborting...");
return null;
}
if (!File.Exists(ZXOptions.Current.ZxbcPath) || !File.Exists(ZXOptions.Current.ZxbasmPath))
{
OutputLogWritter.WriteLine("ZXBASM/ZXBC not found, aborting...");
return null;
}
settings = project.GetProjectSettings();
mainFile = project.GetMainFile();
// Prebuild
if (settings.PreBuild)
{
OutputLogWritter.WriteLine($"PreBuild: {settings.PreBuildValue}");
if (!ExecuteFile(settings.PreBuildValue, new string[]
{
project.ProjectPath,
Path.GetFileName(mainFile)
},
project.ProjectPath,
OutputLogWritter))
{
return null;
}
}
if (mainFile == null)
{
OutputLogWritter.WriteLine("Cannot find main file, check that it exists and if there are more than one that is specified in the build settings.");
return null;
}
if (!PreBuild(true, project.ProjectPath, OutputLogWritter))
return null;
var files = ScanFolder(project.ProjectPath);
if (files.Count() == 0)
{
OutputLogWritter.WriteLine("No file to build, aborting...");
return null;
}
string logOutput;
var args = settings.GetDebugSettings();
if (settings.CustomCompiler)
{
OutputLogWritter.WriteLine("Custom compiler settings");
args = settings.CustomCompilerValue;
}
OutputLogWritter.WriteLine("Building map files...");
foreach (var file in files)
{
file.CreateBuildFile(files, OutputLogWritter);
}
OutputLogWritter.WriteLine("Building program map...");
var codeFile = files.FirstOrDefault(f => f.AbsolutePath == Path.GetFullPath(mainFile));
if (codeFile == null)
{
Cleanup(project.ProjectPath);
OutputLogWritter.WriteLine("Main file path not found. More than 256 chars?");
return null;
}
cmd = $"\"{Path.Combine(codeFile.Directory, codeFile.TempFileName)}\" -M MEMORY_MAP " + args;
OutputLogWritter.WriteLine("zxbc " + cmd);
var proc = Process.Start(
new ProcessStartInfo(
Path.GetFullPath(ZXOptions.Current.ZxbcPath), cmd)
{ WorkingDirectory = project.ProjectPath, RedirectStandardError = true, CreateNoWindow = true });
OutputProcessLog(OutputLogWritter, proc, out logOutput);
var ecode = proc.ExitCode;
if (ecode != 0)
{
Cleanup(project.ProjectPath);
OutputLogWritter.WriteLine("Error building map, aborting...");
return null;
}
var progMap = new ZXMemoryMap(Path.Combine(project.ProjectPath, "MEMORY_MAP"), files);
string binFile = Path.Combine(project.ProjectPath, Path.GetFileNameWithoutExtension(codeFile.TempFileName) + ".bin");
byte[] binary = File.ReadAllBytes(binFile);
OutputLogWritter.WriteLine("Building variable map...");
// DUEFECTU: 2024.09.11 -> Force .ic extension for debug
//proc = Process.Start(new ProcessStartInfo(Path.GetFullPath(ZXOptions.Current.ZxbcPath), $"\"{Path.Combine(codeFile.Directory, codeFile.TempFileName)}\" -E " + args) { WorkingDirectory = project.ProjectPath, RedirectStandardError = true, CreateNoWindow = true });
var pi = new ProcessStartInfo();
pi.WorkingDirectory = project.ProjectPath;
pi.RedirectStandardError = true;
pi.CreateNoWindow = true;
// Compile command
var tempFileName = Path.Combine(codeFile.Directory, codeFile.TempFileName);
var debugFile = Path.GetFileNameWithoutExtension(tempFileName) + ".ic"; // force .ic extension
pi.FileName = Path.GetFullPath(ZXOptions.Current.ZxbcPath); // ZXBC.exe
pi.Arguments = string.Format("\"{0}\" -E -o {1} {2}",
tempFileName, // Main project file
debugFile, // Debug file
args); // user arguments
// Go for it
proc = Process.Start(pi);
OutputProcessLog(OutputLogWritter, proc, out logOutput);
ecode = proc.ExitCode;
if (ecode != 0)
{
Cleanup(project.ProjectPath, binFile);
OutputLogWritter.WriteLine("Error building variable map, aborting...");
return null;
}
/// DUEFECTU: 2023.06.04 -> Bug
//var mainCodeFile = files.Where(f => Path.GetFullPath(mainFile.ToLower()) == Path.GetFullPath(f.AbsolutePath.ToLower())).First();
var mainCodeFile = files.Where(f => Path.GetFullPath(mainFile.ToLower()) == Path.GetFullPath(f.AbsolutePath.ToLower())).FirstOrDefault();
if (mainCodeFile == null)
{
return null;
}
ZXBasicMap bMap = new ZXBasicMap(mainCodeFile, files, logOutput);
string varFile = Path.Combine(project.ProjectPath, Path.GetFileNameWithoutExtension(codeFile.TempFileName) + ".ic");
string mapFile = Path.Combine(project.ProjectPath, "MEMORY_MAP");
var varMap = new ZXVariableMap(varFile, mapFile, bMap);
OutputLogWritter.WriteLine("Building disassembly...");
cmd = $"\"{Path.Combine(codeFile.Directory, codeFile.TempFileName)}\" -A " + args;
OutputLogWritter.WriteLine(cmd);
proc = Process.Start(
new ProcessStartInfo(
Path.GetFullPath(ZXOptions.Current.ZxbcPath), cmd)
{
WorkingDirectory = project.ProjectPath,
RedirectStandardError = true,
CreateNoWindow = true
});
OutputProcessLog(OutputLogWritter, proc, out logOutput);
ecode = proc.ExitCode;
if (ecode != 0)
{
Cleanup(project.ProjectPath, binFile);
OutputLogWritter.WriteLine("Error building disassembly, aborting...");
return null;
}
OutputLogWritter.WriteLine("Building disassembly map...");
string disFile = Path.Combine(project.ProjectPath, Path.GetFileNameWithoutExtension(Path.Combine(codeFile.Directory, codeFile.TempFileName)) + ".asm");
var disasFile = new ZXCodeFile(disFile, true);
disasFile.CreateBuildFile(files, OutputLogWritter);
cmd = $"\"{Path.Combine(disasFile.Directory, disasFile.TempFileName)}\" -M MEMORY_MAP";
proc = Process.Start(
new ProcessStartInfo(
Path.GetFullPath(ZXOptions.Current.ZxbasmPath), cmd)
{
WorkingDirectory = project.ProjectPath,
RedirectStandardError = true,
CreateNoWindow = true
});
OutputProcessLog(OutputLogWritter, proc, out logOutput);
ecode = proc.ExitCode;
if (ecode != 0)
{
Cleanup(project.ProjectPath, binFile, disFile);
OutputLogWritter.WriteLine("Error building disassembly map, aborting...");
return null;
}
var asmMap = new ZXMemoryMap(Path.Combine(project.ProjectPath, "MEMORY_MAP"), new ZXCodeFile[] { disasFile });
Cleanup(project.ProjectPath, binFile, disFile);
ushort org = disasFile.FindOrg();
ZXProgram program = ZXProgram.CreateDebugProgram(files, disasFile, progMap, asmMap, varMap, binary, org);
if (binary.Length + program.Org > 0xFFFF)
{
OutputLogWritter.WriteLine("Program too long, change base address or reduce code size.");
return null;
}
if (!PostBuild(true, project.ProjectPath, program, OutputLogWritter))
return null;
OutputLogWritter.WriteLine($"Program size: {binary.Length} bytes");
OutputLogWritter.WriteLine("Program built successfully.");
if (settings.NextMode)
{
OutputLogWritter.WriteLine("Debugging in Next not supported.");
return null;
}
return program;
}
catch (LineOutOfRangeException ex)
{
OutputLogWritter.WriteLine($"Found invalid program: {ex.Message}");
return null;
}
catch (Exception ex)
{
OutputLogWritter.WriteLine($"Exception: {ex.Message} {ex.StackTrace}");
return null;
}
}
public static bool Export(ZXExportOptions Export, TextWriter OutputLogWritter)
{
try
{
if (ZXProjectManager.Current == null)
{
OutputLogWritter.WriteLine("No open project, aborting...");
return false;
}
var project = ZXProjectManager.Current;
Cleanup(project.ProjectPath);
ZXBuildSettings? settings;
string? mainFile = null;
if (string.IsNullOrWhiteSpace(ZXOptions.Current.ZxbcPath) || string.IsNullOrWhiteSpace(ZXOptions.Current.ZxbasmPath))
{
OutputLogWritter.WriteLine("Paths for ZXBASM and ZXBC not configured, aborting...");
return false;
}
if (!File.Exists(ZXOptions.Current.ZxbcPath) || !File.Exists(ZXOptions.Current.ZxbasmPath))
{
OutputLogWritter.WriteLine("ZXBASM/ZXBC not found, aborting...");
return false;
}
settings = project.GetProjectSettings();
mainFile = project.GetMainFile();
if (mainFile == null)
{
OutputLogWritter.WriteLine("Cannot find main file, check that it exists and if there are more than one that is specified in the build settings.");
return false;
}
var args = settings.GetSettings();
OutputLogWritter.WriteLine("Exporting program...");
var proc = Process.Start(new ProcessStartInfo(Path.GetFullPath(ZXOptions.Current.ZxbcPath), $"\"{mainFile}\" " + args + " " + Export.GetExportOptions()) { WorkingDirectory = project.ProjectPath, RedirectStandardError = true, CreateNoWindow = true });
string logOutput;
OutputProcessLog(OutputLogWritter, proc, out logOutput);
var ecode = proc.ExitCode;
if (ecode != 0)
{
Cleanup(project.ProjectPath);
OutputLogWritter.WriteLine("Error building program, aborting...");
return false;
}
OutputLogWritter.WriteLine("Program exported successfully.");
return true;
}
catch (LineOutOfRangeException ex)
{
OutputLogWritter.WriteLine($"Found invalid program: {ex.Message}");
return false;
}
catch (Exception ex)
{
OutputLogWritter.WriteLine($"Exception: {ex.Message} {ex.StackTrace}");
return false;
}
}
private static void Cleanup(string Folder, string? BinFile = null, string? DisassemblyFile = null)
{
if (BinFile != null && File.Exists(BinFile))
File.Delete(BinFile);
if (DisassemblyFile != null && File.Exists(DisassemblyFile))
File.Delete(BinFile);
var toDelete = Directory.GetFiles(Folder, "*.buildtemp.*");
foreach (var file in toDelete)
File.Delete(file);
if (File.Exists(Path.Combine(Folder, "MEMORY_MAP")))
File.Delete(Path.Combine(Folder, "MEMORY_MAP"));
var dirs = Directory.GetDirectories(Folder);
foreach (var dir in dirs)
Cleanup(dir);
}
private static IEnumerable<ZXCodeFile> ScanFolder(string folder)
{
List<ZXCodeFile> files = new List<ZXCodeFile>();
var fFiles = Directory.GetFiles(folder);
foreach (var fFile in fFiles)
{
var docType = ZXDocumentProvider.GetDocumentType(fFile);
if (docType is ZXBasicDocument || docType is ZXAssemblerDocument)
files.Add(new ZXCodeFile(fFile));
}
var fDirs = Directory.GetDirectories(folder);
foreach (var fDir in fDirs)
files.AddRange(ScanFolder(fDir));
return files;
}
private static void OutputProcessLog(TextWriter OutputLogWritter, Process proc, out string Log)
{
StringBuilder sbLog = new StringBuilder();
while (!proc.HasExited)
{
if (!proc.StandardError.EndOfStream)
{
try
{
string? line = proc.StandardError.ReadLine();
OutputLogWritter.WriteLine(line);
if (line != null)
sbLog.AppendLine(line);
}
catch { }
}
}
if (!proc.StandardError.EndOfStream)
{
string? line = proc.StandardError.ReadToEnd();
OutputLogWritter.WriteLine(line);
if (line != null)
sbLog.AppendLine(line);
}
Log = sbLog.ToString();
}
private static bool PreBuild(bool debug, string path, TextWriter outLog)
{
outLog.WriteLine("Building precompilation documents...");
var builders = ZXDocumentProvider.GetPrecompilationDocumentBuilders();
foreach (var builder in builders)
{
if (!builder.Build(path, DocumentModel.Enums.ZXBuildStage.PreBuild, debug ? DocumentModel.Enums.ZXBuildType.Debug : DocumentModel.Enums.ZXBuildType.Release, null, outLog))
{
outLog.WriteLine("Error on pre-build stage, aborting...");
return false;
}
}
return true;
}
private static bool PostBuild(bool debug, string path, ZXProgram CompiledProgram, TextWriter outLog)
{
outLog.WriteLine("Building postcompilation documents...");
var builders = ZXDocumentProvider.GetPostcompilationDocumentBuilders();
foreach (var builder in builders)
{
if (!builder.Build(path, DocumentModel.Enums.ZXBuildStage.PostBuild, debug ? DocumentModel.Enums.ZXBuildType.Debug : DocumentModel.Enums.ZXBuildType.Release, CompiledProgram, outLog))
{
outLog.WriteLine("Error on post-build stage, aborting...");
return false;
}
}
return true;
}
public static bool ExecuteFile(string preBuildValue, string[] parameters, string workingPath, TextWriter outLog)
{
try
{
var proc = Process.Start(
new ProcessStartInfo(
preBuildValue,
parameters)
{
WorkingDirectory = workingPath,
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true
});
string logOutput = "";
ProcessRedirect(proc, outLog);
var ecode = proc.ExitCode;
if (ecode != 0)
{
Cleanup(workingPath);
outLog.WriteLine("Error building program, aborting...");
return false;
}
return true;
}
catch (Exception ex)
{
outLog.WriteLine("ERROR executing file: " + ex.Message);
return false;
}
}
private static void ProcessRedirect(Process proc, TextWriter outLog)
{
try
{
while (!proc.HasExited)
{
if (!proc.StandardOutput.EndOfStream)
{
string? line = proc.StandardOutput.ReadLine();
outLog.WriteLine(line);
}
if (!proc.StandardError.EndOfStream)
{
string? line = proc.StandardError.ReadLine();
outLog.WriteLine(line);
}
}
while (!proc.StandardOutput.EndOfStream)
{
string? line = proc.StandardOutput.ReadLine();
outLog.WriteLine(line);
}
while (!proc.StandardError.EndOfStream)
{
string? line = proc.StandardError.ReadLine();
outLog.WriteLine(line);
}
}
catch { }
}
}
}