@@ -3,7 +3,11 @@ use clap::crate_version;
33use std:: str:: FromStr ;
44
55/// Runs a Lua script.
6- pub async fn run_command ( file_path : String , extra_args : Option < Vec < String > > ) {
6+ pub async fn run_command (
7+ file_path : String ,
8+ stdlib_path : Option < String > ,
9+ extra_args : Option < Vec < String > > ,
10+ ) {
711 let lua = & LUA ;
812
913 // ! Move VM preparation into a separate function
@@ -19,23 +23,23 @@ pub async fn run_command(file_path: String, extra_args: Option<Vec<String>>) {
1923 . expect ( "Could not set the script path to OnceCell" ) ;
2024
2125 // Register Lua components.
22- registration ( lua) . await ;
26+ registration ( lua, stdlib_path ) . await ;
2327
2428 // Handle extra arguments.
2529 if let Some ( extra_args) = extra_args {
2630 if let Ok ( args) = lua. create_table ( ) {
2731 if let Err ( e) = args. set ( 0 , file_path. clone ( ) ) {
28- eprintln ! ( "Error adding arg to the args list: {e:?}" ) ;
32+ tracing :: error !( "Error adding arg to the args list: {e:?}" ) ;
2933 }
3034
3135 for ( index, value) in extra_args. into_iter ( ) . enumerate ( ) {
3236 if let Err ( e) = args. set ( ( index + 1 ) as i32 , value) {
33- eprintln ! ( "Error adding arg to the args list: {e:?}" ) ;
37+ tracing :: error !( "Error adding arg to the args list: {e:?}" ) ;
3438 }
3539 }
3640
3741 if let Err ( e) = lua. globals ( ) . set ( "arg" , args) {
38- eprintln ! ( "Error setting the global variable ARGS: {e:?}" ) ;
42+ tracing :: error !( "Error setting the global variable ARGS: {e:?}" ) ;
3943 }
4044 }
4145 }
@@ -44,7 +48,7 @@ pub async fn run_command(file_path: String, extra_args: Option<Vec<String>>) {
4448 #[ allow( clippy:: expect_used) ]
4549 let user_file = std:: fs:: read_to_string ( & file_path) . expect ( "Couldn't read file" ) ;
4650 if let Err ( e) = lua. load ( user_file) . set_name ( file_path) . exec_async ( ) . await {
47- eprintln ! ( "{e}" ) ;
51+ tracing :: error !( "{e}" ) ;
4852 }
4953
5054 // Wait for all Tokio tasks to finish.
@@ -196,23 +200,48 @@ pub async fn upgrade_command(user_agent: Option<String>) -> Result<(), Box<dyn s
196200}
197201
198202/// Registers Lua components.
199- async fn registration ( lua : & mlua:: Lua ) {
200- let mut lua_lib = prepare_prelude ( ) ;
201-
202- #[ allow( clippy:: expect_used) ]
203- let std_lib = crate :: components:: register_components ( lua)
204- . await
205- . expect ( "Error setting up the standard library" ) ;
206-
207- // Set Astra version in Lua globals.
208- if let Err ( e) = lua
209- . globals ( )
210- . set ( "astra_internal__version" , crate_version ! ( ) )
211- {
212- eprintln ! ( "Error adding version to Astra: {e:#?}" ) ;
203+ async fn registration ( lua : & mlua:: Lua , stdlib_path : Option < String > ) {
204+ let mut lua_lib: Vec < ( String , String ) > = Vec :: new ( ) ;
205+
206+ let folder_path = stdlib_path. unwrap_or (
207+ // get the folder path from .luarc.json
208+ // { "workspace.library": ["./folder_path"] }
209+ if let Ok ( file) = tokio:: fs:: read_to_string ( ".luarc.json" ) . await
210+ && let Ok ( parsed) = serde_json:: from_str :: < serde_json:: Value > ( & file)
211+ && let Some ( parsed) = parsed. as_object ( )
212+ && let Some ( parsed) = parsed. get ( "workspace.library" )
213+ && let Some ( parsed) = parsed. as_array ( )
214+ && let Some ( folder_path) = parsed. first ( )
215+ && let Some ( folder_path) = folder_path. as_str ( )
216+ {
217+ folder_path. to_string ( )
218+ } else {
219+ ".astra" . to_string ( )
220+ } ,
221+ ) ;
222+ if let Ok ( mut files) = tokio:: fs:: read_dir ( folder_path) . await {
223+ // add them to the lua_lib for being sent to interpretation
224+ #[ allow( for_loops_over_fallibles) ]
225+ for file in files. next_entry ( ) . await {
226+ if let Some ( file) = file
227+ && let Ok ( content) = tokio:: fs:: read_to_string ( file. path ( ) ) . await
228+ {
229+ lua_lib. push ( ( file. path ( ) . to_string_lossy ( ) . to_string ( ) , content) ) ;
230+ }
231+ }
232+ } else {
233+ // if the folder couldn't be opened or issues existed
234+ lua_lib = prepare_prelude ( )
213235 }
214236
215- lua_lib. extend ( std_lib) ;
237+ // Try to make astra.lua the first to get interpreted
238+ if let Some ( index) = lua_lib. iter ( ) . position ( |entry| {
239+ let name = entry. 0 . to_ascii_lowercase ( ) ;
240+ name == "astra.lua"
241+ } ) {
242+ let value = lua_lib. remove ( index) ;
243+ lua_lib. insert ( 0 , value) ;
244+ }
216245
217246 for ( file_name, content) in lua_lib {
218247 if let Err ( e) = lua
@@ -221,7 +250,7 @@ async fn registration(lua: &mlua::Lua) {
221250 . exec_async ( )
222251 . await
223252 {
224- eprintln ! ( "Couldn't add prelude:\n {e}" ) ;
253+ tracing :: error !( "Couldn't add prelude:\n {e}" ) ;
225254 }
226255 }
227256}
0 commit comments