11use :: rhai:: { Engine , Scope , packages:: Package } ;
2- use hemtt_rhai:: libraries:: { RfsPackage , VfsPackage } ;
2+ use hemtt_rhai:: libraries:: VfsPackage ;
33use hemtt_workspace:: WorkspacePath ;
4- use rhai:: { Dynamic , EvalAltResult } ;
4+ use rhai:: Dynamic ;
55use whoami:: Result ;
66
77use crate :: {
8- context:: Context , error:: Error , modules:: hook:: libraries:: hemtt:: RhaiHemtt , report:: Report ,
8+ context:: Context ,
9+ error:: Error ,
10+ modules:: hook:: { error:: bhe5_runtime_fatal:: RuntimeFatal , libraries:: hemtt:: RhaiHemtt } ,
11+ report:: Report ,
912} ;
1013
1114use self :: error:: {
@@ -25,34 +28,34 @@ mod libraries;
2528///
2629/// # Panics
2730/// If the build folder does not exist
28- pub fn scope ( ctx : & ' _ Context , vfs : bool ) -> Result < Scope < ' _ > , Error > {
31+ pub fn scope ( ctx : & ' _ Context , hemtt : bool , vfs : bool ) -> Result < Scope < ' _ > , Error > {
2932 let mut scope = Scope :: new ( ) ;
3033 if vfs {
3134 scope. push_constant ( "HEMTT_VFS" , ctx. workspace_path ( ) . vfs ( ) . clone ( ) ) ;
3235 }
33- scope. push_constant ( "HEMTT_DIRECTORY" , ctx. project_folder ( ) . clone ( ) ) ;
34- scope. push_constant (
35- "HEMTT_OUTPUT" ,
36- ctx. build_folder ( ) . expect ( "build folder exists" ) . clone ( ) ,
37- ) ;
3836 scope. push_constant ( "HEMTT_RFS" , ctx. project_folder ( ) . clone ( ) ) ;
39- scope. push_constant (
40- "HEMTT_OUT" ,
41- ctx. build_folder ( ) . expect ( "build folder exists" ) . clone ( ) ,
42- ) ;
4337
44- scope. push_constant ( "HEMTT" , RhaiHemtt :: new ( ctx) ) ;
38+ if hemtt {
39+ scope. push_constant (
40+ "HEMTT_OUTPUT" ,
41+ ctx. build_folder ( ) . expect ( "build folder exists" ) . clone ( ) ,
42+ ) ;
43+ scope. push_constant (
44+ "HEMTT_OUT" ,
45+ ctx. build_folder ( ) . expect ( "build folder exists" ) . clone ( ) ,
46+ ) ;
47+ scope. push_constant ( "HEMTT" , RhaiHemtt :: new ( ctx) ) ;
48+ }
4549
4650 Ok ( scope)
4751}
4852
49- fn engine ( vfs : bool ) -> Engine {
50- let mut engine = hemtt_rhai:: engine ( ) ;
53+ fn engine ( name : String , vfs : bool ) -> Engine {
54+ let mut engine = hemtt_rhai:: engine ( name ) ;
5155 if vfs {
5256 let virt = VfsPackage :: new ( ) ;
5357 engine. register_static_module ( "hemtt_vfs" , virt. as_shared_module ( ) ) ;
5458 }
55- engine. register_static_module ( "hemtt_rfs" , RfsPackage :: new ( ) . as_shared_module ( ) ) ;
5659 engine. register_static_module ( "hemtt" , libraries:: HEMTTPackage :: new ( ) . as_shared_module ( ) ) ;
5760 engine
5861}
@@ -102,6 +105,9 @@ impl Hooks {
102105 ) ;
103106 report. merge ( Self :: run ( ctx, file, vfs) ?. 0 ) ;
104107 ctx. config ( ) . version ( ) . invalidate ( ) ;
108+ if report. failed ( ) {
109+ break ;
110+ }
105111 }
106112 Ok ( report)
107113 }
@@ -132,45 +138,24 @@ impl Hooks {
132138 #[ allow( clippy:: needless_pass_by_value) ] // rhai things
133139 fn run ( ctx : & Context , path : WorkspacePath , vfs : bool ) -> Result < ( Report , Dynamic ) , Error > {
134140 let mut report = Report :: new ( ) ;
135- let mut engine = engine ( vfs) ;
136- let mut scope = scope ( ctx, vfs) ?;
137141 let parts = path. as_str ( ) . split ( '/' ) ;
138142 let name = parts
139143 . clone ( )
140144 . skip ( parts. count ( ) . saturating_sub ( 2 ) )
141145 . collect :: < Vec < _ > > ( )
142146 . join ( "/" ) ;
143- let inner_name = name. clone ( ) ;
144- engine. on_debug ( move |x, _src, _pos| {
145- debug ! ( "[{inner_name}] {x}" ) ;
146- } ) ;
147- let inner_name = name. clone ( ) ;
148- engine. on_print ( move |s| {
149- info ! ( "[{inner_name}] {s}" ) ;
150- } ) ;
151- let inner_name = name. clone ( ) ;
152- engine. register_fn ( "info" , move |s : & str | {
153- info ! ( "[{inner_name}] {s}" ) ;
154- } ) ;
155- let inner_name = name. clone ( ) ;
156- engine. register_fn ( "warn" , move |s : & str | {
157- warn ! ( "[{inner_name}] {s}" ) ;
158- } ) ;
159- let inner_name = name. clone ( ) ;
160- engine. register_fn ( "error" , move |s : & str | {
161- error ! ( "[{inner_name}] {s}" ) ;
162- } ) ;
163- let inner_name = name;
164- engine. register_fn ( "fatal" , move |s : & str | -> Result < ( ) , Box < EvalAltResult > > {
165- error ! ( "[{inner_name}] {s}" ) ;
166- Err ( Box :: new ( EvalAltResult :: ErrorRuntime (
167- "Script called fatal" . into ( ) ,
168- rhai:: Position :: NONE ,
169- ) ) )
170- } ) ;
147+ let engine = engine ( name, vfs) ;
148+ let mut scope = scope ( ctx, true , vfs) ?;
171149 match engine. eval_with_scope ( & mut scope, & path. read_to_string ( ) ?) {
172150 Err ( e) => {
173- report. push ( RuntimeError :: code ( path, & e) ) ;
151+ match * e {
152+ rhai:: EvalAltResult :: ErrorTerminated ( message, pos) => {
153+ report. push ( RuntimeFatal :: code ( path, message. to_string ( ) , pos) ) ;
154+ }
155+ _ => {
156+ report. push ( RuntimeError :: code ( path, & e) ) ;
157+ }
158+ }
174159 Ok ( ( report, Dynamic :: UNIT ) )
175160 }
176161 Ok ( ret) => Ok ( ( report, ret) ) ,
@@ -188,7 +173,7 @@ impl Module for Hooks {
188173 self . 0 = ctx. hemtt_folder ( ) . join ( "hooks" ) . exists ( ) ;
189174 if self . 0 {
190175 for phase in & [ "pre_build" , "post_build" , "pre_release" , "post_release" ] {
191- let engine = engine ( phase != & "post_release" ) ;
176+ let engine = engine ( String :: from ( * phase ) , phase != & "post_release" ) ;
192177 let dir = ctx. hemtt_folder ( ) . join ( "hooks" ) . join ( phase) ;
193178 if !dir. exists ( ) {
194179 continue ;
0 commit comments