88using System . Runtime . CompilerServices ;
99using System . Text ;
1010using System . Text . RegularExpressions ;
11+ using System . Threading ;
1112using System . Threading . Tasks ;
1213using ZXBasicStudio . Classes ;
14+ using ZXBasicStudio . Controls ;
1315using ZXBasicStudio . Dialogs ;
1416using ZXBasicStudio . DocumentModel . Classes ;
1517using ZXBasicStudio . IntegratedDocumentTypes . CodeDocuments . Basic ;
@@ -51,6 +53,22 @@ public class ZXProjectBuilder
5153 settings = project . GetProjectSettings ( ) ;
5254 mainFile = project . GetMainFile ( ) ;
5355
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+
5472 if ( mainFile == null )
5573 {
5674 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
6078 if ( ! PreBuild ( false , project . ProjectPath , OutputLogWritter ) )
6179 return null ;
6280
63- var args = settings . GetSettings ( ) ;
64-
81+ Process ? proc = null ;
6582 var startTime = DateTime . Now ;
6683 OutputLogWritter . WriteLine ( "Project path: " + project . ProjectPath ) ;
6784 OutputLogWritter . WriteLine ( "Building program " + mainFile ) ;
6885 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 } ) ;
7194
7295 string logOutput ;
7396
@@ -84,6 +107,22 @@ public class ZXProjectBuilder
84107
85108 string binFile = Path . Combine ( project . ProjectPath , Path . GetFileNameWithoutExtension ( mainFile ) + ".bin" ) ;
86109
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+
87126 byte [ ] binary = File . ReadAllBytes ( binFile ) ;
88127
89128 Cleanup ( project . ProjectPath , binFile ) ;
@@ -309,6 +348,22 @@ private static void CheckNextCreator()
309348 settings = project . GetProjectSettings ( ) ;
310349 mainFile = project . GetMainFile ( ) ;
311350
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+
312367 if ( mainFile == null )
313368 {
314369 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()
329384 string logOutput ;
330385
331386 var args = settings . GetDebugSettings ( ) ;
387+ if ( settings . CustomCompiler )
388+ {
389+ OutputLogWritter . WriteLine ( "Custom compiler settings" ) ;
390+ args = settings . CustomCompilerValue ;
391+ }
332392
333393 OutputLogWritter . WriteLine ( "Building map files..." ) ;
334394
@@ -339,7 +399,6 @@ private static void CheckNextCreator()
339399
340400 OutputLogWritter . WriteLine ( "Building program map..." ) ;
341401
342- // TODO: DUEFECTU 2023.05.17: Bug for long path
343402 var codeFile = files . FirstOrDefault ( f => f . AbsolutePath == Path . GetFullPath ( mainFile ) ) ;
344403 if ( codeFile == null )
345404 {
@@ -349,10 +408,10 @@ private static void CheckNextCreator()
349408 }
350409
351410 cmd = $ "\" { Path . Combine ( codeFile . Directory , codeFile . TempFileName ) } \" -M MEMORY_MAP " + args ;
352- OutputLogWritter . WriteLine ( "zxbc " + cmd ) ;
411+ OutputLogWritter . WriteLine ( "zxbc " + cmd ) ;
353412 var proc = Process . Start (
354413 new ProcessStartInfo (
355- Path . GetFullPath ( ZXOptions . Current . ZxbcPath ) , cmd )
414+ Path . GetFullPath ( ZXOptions . Current . ZxbcPath ) , cmd )
356415 { WorkingDirectory = project . ProjectPath , RedirectStandardError = true , CreateNoWindow = true } ) ;
357416
358417 OutputProcessLog ( OutputLogWritter , proc , out logOutput ) ;
@@ -692,5 +751,73 @@ private static bool PostBuild(bool debug, string path, ZXProgram CompiledProgram
692751
693752 return true ;
694753 }
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+ }
695822 }
696823}
0 commit comments