Skip to content

Commit dde189c

Browse files
Import multiple modules with folders
1 parent ee3f3d8 commit dde189c

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

src/utils/sprite.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::f32::consts::*;
1515
use std::fs::File;
1616
use std::io::Write;
1717
use std::path::Path;
18+
use std::path::PathBuf;
1819

1920
use macroquad::audio::*;
2021
use macroquad::prelude::*;
@@ -261,12 +262,29 @@ impl Sprite {
261262
}
262263
Statement::Import { path } => {
263264
fn import_module(
264-
path: &str,
265-
base_dir: &str,
265+
path: PathBuf,
266266
visited: &mut Vec<String>,
267267
setup_ast: &mut Vec<Statement>,
268268
) -> HashMap<String, Function> {
269-
let path = Path::new(base_dir).join(path);
269+
let md = path.metadata().unwrap();
270+
if md.is_dir() {
271+
println!("Importing directory as module: {}", &path.display());
272+
let children = std::fs::read_dir(&path).unwrap_or_else(|_| {
273+
panic!("Failed to read directory: {}", &path.display());
274+
}).map(|entry| {
275+
entry.unwrap().path()
276+
}).collect::<Vec<_>>();
277+
let mut functions = HashMap::new();
278+
for child in children {
279+
let imported_functions = import_module(
280+
child,
281+
visited,
282+
setup_ast,
283+
);
284+
functions.extend(imported_functions);
285+
}
286+
return functions;
287+
}
270288
let code = std::fs::read_to_string(&path).unwrap_or_else(|_| {
271289
println!("Failed to load module: {}", &path.display());
272290
String::new()
@@ -322,9 +340,9 @@ impl Sprite {
322340
println!("Circular import detected: {}, skipping", path);
323341
return functions;
324342
}
343+
visited.push(path.clone());
325344
let imported_functions =
326-
import_module(&path, &base_dir, visited, setup_ast);
327-
visited.push(path);
345+
import_module(PathBuf::from(&path), visited, setup_ast);
328346
functions.extend(imported_functions);
329347
}
330348
_ => {}
@@ -334,7 +352,7 @@ impl Sprite {
334352
}
335353
let mut visited: Vec<String> = vec![];
336354
let imported_functions =
337-
import_module(&path, &base_dir, &mut visited, &mut setup_ast);
355+
import_module(PathBuf::from(&base_dir).join(&path), &mut visited, &mut setup_ast);
338356
for (name, function) in imported_functions {
339357
functions.insert(name, function);
340358
}

0 commit comments

Comments
 (0)