Skip to content

Commit 156c73b

Browse files
Load Exported Modules (#77)
2 parents cf1089b + 64b0869 commit 156c73b

File tree

5 files changed

+59
-34
lines changed

5 files changed

+59
-34
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
**/*compose*
33
**/**/db-data
44
**/**/node_modules
5-
**/**/**dev**
5+
**/**/**dev**
6+
.astra

.luarc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"workspace.library": [
44
"src"
55
],
6-
"runtime.version": "LuaJIT" // runtime language (Should be) Generated by Astra
6+
"runtime.version": "LuaJIT"
77
}

src/commands.rs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ use clap::crate_version;
33
use 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
}

src/lua_libs/astra.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,3 @@
44
Astra = {
55
version = "@ASTRA_VERSION",
66
}
7-
8-
-- This is to prevent a small undefined behavior in Lua
9-
---@diagnostic disable-next-line: redundant-parameter
10-
setmetatable(_G, {
11-
---@diagnostic disable-next-line: redundant-parameter, unused-local
12-
__index = function(T, k, v)
13-
error("Called non-existing variable")
14-
end,
15-
})

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ enum AstraCLI {
3535
Run {
3636
/// Path to the Lua script file.
3737
file_path: String,
38+
/// Path to the standard library folder
39+
#[arg(short, long)]
40+
stdlib_path: Option<String>,
3841
/// Extra arguments to pass to the script.
3942
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
4043
extra_args: Option<Vec<String>>,
@@ -69,8 +72,9 @@ pub async fn main() {
6972
match AstraCLI::parse() {
7073
AstraCLI::Run {
7174
file_path,
75+
stdlib_path,
7276
extra_args,
73-
} => commands::run_command(file_path, extra_args).await,
77+
} => commands::run_command(file_path, stdlib_path, extra_args).await,
7478
AstraCLI::ExportBundle { path } => commands::export_bundle_command(path).await,
7579
AstraCLI::Upgrade { user_agent } => {
7680
if let Err(e) = commands::upgrade_command(user_agent).await {

0 commit comments

Comments
 (0)