8
8
using System . Runtime . CompilerServices ;
9
9
using System . Text ;
10
10
using System . Text . RegularExpressions ;
11
+ using System . Threading ;
11
12
using System . Threading . Tasks ;
12
13
using ZXBasicStudio . Classes ;
14
+ using ZXBasicStudio . Controls ;
13
15
using ZXBasicStudio . Dialogs ;
14
16
using ZXBasicStudio . DocumentModel . Classes ;
15
17
using ZXBasicStudio . IntegratedDocumentTypes . CodeDocuments . Basic ;
@@ -51,6 +53,22 @@ public class ZXProjectBuilder
51
53
settings = project . GetProjectSettings ( ) ;
52
54
mainFile = project . GetMainFile ( ) ;
53
55
56
+ // Prebuild
57
+ if ( settings . PreBuild )
58
+ {
59
+ OutputLogWritter . WriteLine ( $ "PreBuild: { settings . PreBuildValue } ") ;
60
+ if ( ! ExecuteFile ( settings . PreBuildValue , new string [ ]
61
+ {
62
+ project . ProjectPath ,
63
+ Path . GetFileName ( mainFile )
64
+ } ,
65
+ project . ProjectPath ,
66
+ OutputLogWritter ) )
67
+ {
68
+ return null ;
69
+ }
70
+ }
71
+
54
72
if ( mainFile == null )
55
73
{
56
74
OutputLogWritter . WriteLine ( "Cannot find main file, check that it exists and if there are more than one that is specified in the build settings." ) ;
@@ -60,14 +78,19 @@ public class ZXProjectBuilder
60
78
if ( ! PreBuild ( false , project . ProjectPath , OutputLogWritter ) )
61
79
return null ;
62
80
63
- var args = settings . GetSettings ( ) ;
64
-
81
+ Process ? proc = null ;
65
82
var startTime = DateTime . Now ;
66
83
OutputLogWritter . WriteLine ( "Project path: " + project . ProjectPath ) ;
67
84
OutputLogWritter . WriteLine ( "Building program " + mainFile ) ;
68
85
OutputLogWritter . WriteLine ( "Building starts at " + startTime ) ;
69
-
70
- var proc = Process . Start ( new ProcessStartInfo ( Path . GetFullPath ( ZXOptions . Current . ZxbcPath ) , $ "\" { mainFile } \" " + args ) { WorkingDirectory = project . ProjectPath , RedirectStandardError = true , CreateNoWindow = true } ) ;
86
+ var args = settings . GetSettings ( ) ;
87
+ if ( settings . CustomCompiler )
88
+ {
89
+ OutputLogWritter . WriteLine ( "Custom compiler settings" ) ;
90
+ args = settings . CustomCompilerValue ;
91
+ }
92
+ OutputLogWritter . WriteLine ( $ "zxbc \" { mainFile } \" { args } ") ;
93
+ proc = Process . Start ( new ProcessStartInfo ( Path . GetFullPath ( ZXOptions . Current . ZxbcPath ) , $ "\" { mainFile } \" " + args ) { WorkingDirectory = project . ProjectPath , RedirectStandardError = true , CreateNoWindow = true } ) ;
71
94
72
95
string logOutput ;
73
96
@@ -84,6 +107,22 @@ public class ZXProjectBuilder
84
107
85
108
string binFile = Path . Combine ( project . ProjectPath , Path . GetFileNameWithoutExtension ( mainFile ) + ".bin" ) ;
86
109
110
+ // Postbuild
111
+ if ( settings . PostBuild )
112
+ {
113
+ OutputLogWritter . WriteLine ( $ "PostBuild: { settings . PostBuildValue } ") ;
114
+ if ( ! ExecuteFile ( settings . PostBuildValue , new string [ ]
115
+ {
116
+ project . ProjectPath ,
117
+ Path . GetFileName ( binFile )
118
+ } ,
119
+ project . ProjectPath ,
120
+ OutputLogWritter ) )
121
+ {
122
+ return null ;
123
+ }
124
+ }
125
+
87
126
byte [ ] binary = File . ReadAllBytes ( binFile ) ;
88
127
89
128
Cleanup ( project . ProjectPath , binFile ) ;
@@ -309,6 +348,22 @@ private static void CheckNextCreator()
309
348
settings = project . GetProjectSettings ( ) ;
310
349
mainFile = project . GetMainFile ( ) ;
311
350
351
+ // Prebuild
352
+ if ( settings . PreBuild )
353
+ {
354
+ OutputLogWritter . WriteLine ( $ "PreBuild: { settings . PreBuildValue } ") ;
355
+ if ( ! ExecuteFile ( settings . PreBuildValue , new string [ ]
356
+ {
357
+ project . ProjectPath ,
358
+ Path . GetFileName ( mainFile )
359
+ } ,
360
+ project . ProjectPath ,
361
+ OutputLogWritter ) )
362
+ {
363
+ return null ;
364
+ }
365
+ }
366
+
312
367
if ( mainFile == null )
313
368
{
314
369
OutputLogWritter . WriteLine ( "Cannot find main file, check that it exists and if there are more than one that is specified in the build settings." ) ;
@@ -329,6 +384,11 @@ private static void CheckNextCreator()
329
384
string logOutput ;
330
385
331
386
var args = settings . GetDebugSettings ( ) ;
387
+ if ( settings . CustomCompiler )
388
+ {
389
+ OutputLogWritter . WriteLine ( "Custom compiler settings" ) ;
390
+ args = settings . CustomCompilerValue ;
391
+ }
332
392
333
393
OutputLogWritter . WriteLine ( "Building map files..." ) ;
334
394
@@ -339,7 +399,6 @@ private static void CheckNextCreator()
339
399
340
400
OutputLogWritter . WriteLine ( "Building program map..." ) ;
341
401
342
- // TODO: DUEFECTU 2023.05.17: Bug for long path
343
402
var codeFile = files . FirstOrDefault ( f => f . AbsolutePath == Path . GetFullPath ( mainFile ) ) ;
344
403
if ( codeFile == null )
345
404
{
@@ -349,10 +408,10 @@ private static void CheckNextCreator()
349
408
}
350
409
351
410
cmd = $ "\" { Path . Combine ( codeFile . Directory , codeFile . TempFileName ) } \" -M MEMORY_MAP " + args ;
352
- OutputLogWritter . WriteLine ( "zxbc " + cmd ) ;
411
+ OutputLogWritter . WriteLine ( "zxbc " + cmd ) ;
353
412
var proc = Process . Start (
354
413
new ProcessStartInfo (
355
- Path . GetFullPath ( ZXOptions . Current . ZxbcPath ) , cmd )
414
+ Path . GetFullPath ( ZXOptions . Current . ZxbcPath ) , cmd )
356
415
{ WorkingDirectory = project . ProjectPath , RedirectStandardError = true , CreateNoWindow = true } ) ;
357
416
358
417
OutputProcessLog ( OutputLogWritter , proc , out logOutput ) ;
@@ -692,5 +751,73 @@ private static bool PostBuild(bool debug, string path, ZXProgram CompiledProgram
692
751
693
752
return true ;
694
753
}
754
+
755
+
756
+ public static bool ExecuteFile ( string preBuildValue , string [ ] parameters , string workingPath , TextWriter outLog )
757
+ {
758
+ try
759
+ {
760
+ var proc = Process . Start (
761
+ new ProcessStartInfo (
762
+ preBuildValue ,
763
+ parameters )
764
+ {
765
+ WorkingDirectory = workingPath ,
766
+ RedirectStandardError = true ,
767
+ RedirectStandardOutput = true ,
768
+ CreateNoWindow = true
769
+ } ) ;
770
+
771
+ string logOutput = "" ;
772
+ ProcessRedirect ( proc , outLog ) ;
773
+
774
+ var ecode = proc . ExitCode ;
775
+
776
+ if ( ecode != 0 )
777
+ {
778
+ Cleanup ( workingPath ) ;
779
+ outLog . WriteLine ( "Error building program, aborting..." ) ;
780
+ return false ;
781
+ }
782
+ return true ;
783
+ }
784
+ catch ( Exception ex )
785
+ {
786
+ outLog . WriteLine ( "ERROR executing file: " + ex . Message ) ;
787
+ return false ;
788
+ }
789
+ }
790
+
791
+
792
+ private static void ProcessRedirect ( Process proc , TextWriter outLog )
793
+ {
794
+ try
795
+ {
796
+ while ( ! proc . HasExited )
797
+ {
798
+ if ( ! proc . StandardOutput . EndOfStream )
799
+ {
800
+ string ? line = proc . StandardOutput . ReadLine ( ) ;
801
+ outLog . WriteLine ( line ) ;
802
+ }
803
+ if ( ! proc . StandardError . EndOfStream )
804
+ {
805
+ string ? line = proc . StandardError . ReadLine ( ) ;
806
+ outLog . WriteLine ( line ) ;
807
+ }
808
+ }
809
+ while ( ! proc . StandardOutput . EndOfStream )
810
+ {
811
+ string ? line = proc . StandardOutput . ReadLine ( ) ;
812
+ outLog . WriteLine ( line ) ;
813
+ }
814
+ while ( ! proc . StandardError . EndOfStream )
815
+ {
816
+ string ? line = proc . StandardError . ReadLine ( ) ;
817
+ outLog . WriteLine ( line ) ;
818
+ }
819
+ }
820
+ catch { }
821
+ }
695
822
}
696
823
}
0 commit comments