relative include paths now get resolved properly

This commit is contained in:
Sven Vogel 2024-04-17 22:31:32 +02:00
parent a567fc75b9
commit 5a3c4bffd6
2 changed files with 15 additions and 8 deletions

View File

@ -1,6 +1,7 @@
use std::{collections::VecDeque, path::Path}; use std::{collections::VecDeque, env, path::Path};
use crate::{token::{Token, MessageType}, builtin::modules::Module}; use crate::{token::{Token, MessageType}, builtin::modules::Module};
use crate::builtin::modules::io::print;
#[derive(Default)] #[derive(Default)]
pub struct LangSpecs { pub struct LangSpecs {
@ -58,8 +59,6 @@ pub fn from_list(text: &str) -> Vec<String> {
fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs, origin: &String) { fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs, origin: &String) {
std::env::set_current_dir(Path::new(origin).parent().unwrap()).expect("unable to change cwd");
for cap in DIRECTIVE_REGEX.captures_iter(text) { for cap in DIRECTIVE_REGEX.captures_iter(text) {
let mut enumerator = cap.iter().enumerate(); let mut enumerator = cap.iter().enumerate();
loop { loop {
@ -91,11 +90,19 @@ fn parse_directive(text: &str, token_idx: usize, specs: &mut LangSpecs, origin:
return; return;
}, },
4 => { 4 => {
let cwd = Path::new(origin).parent().unwrap().canonicalize().unwrap();
for path in from_list(mat.as_str()).iter() { for path in from_list(mat.as_str()).iter() {
if let Ok(str) = std::fs::read_to_string(path) { let mut include_path = cwd.clone();
specs.embedded_files.push((token_idx, str, path.to_owned())); include_path.push(path);
if let Ok(path) = include_path.canonicalize() {
let include_file = path.to_str().unwrap().to_string();
if let Ok(str) = std::fs::read_to_string(&include_file) {
specs.embedded_files.push((token_idx, str, include_file));
} else { } else {
crate::message(MessageType::Warning, format!("Unable to read embedded file: {path}")); crate::message(MessageType::Warning, format!("Unable to read embedded file: {include_file}"));
}
} }
} }
return; return;