-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for path aliases in require
- Loading branch information
1 parent
0392c16
commit b07202a
Showing
15 changed files
with
287 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,75 @@ | ||
use console::style; | ||
use mlua::prelude::*; | ||
|
||
use crate::lune::util::{ | ||
luaurc::LuauRc, | ||
paths::{make_absolute_and_clean, CWD}, | ||
}; | ||
|
||
use super::context::*; | ||
|
||
pub(super) async fn require<'lua, 'ctx>( | ||
_ctx: &'ctx RequireContext<'lua>, | ||
ctx: &'ctx RequireContext<'lua>, | ||
source: &str, | ||
alias: &str, | ||
name: &str, | ||
path: &str, | ||
) -> LuaResult<LuaMultiValue<'lua>> | ||
where | ||
'lua: 'ctx, | ||
{ | ||
Err(LuaError::runtime(format!( | ||
"TODO: Support require for built-in libraries (tried to require '{name}' with alias '{alias}')" | ||
))) | ||
let alias = alias.to_ascii_lowercase(); | ||
let path = path.to_ascii_lowercase(); | ||
|
||
let parent = make_absolute_and_clean(source) | ||
.parent() | ||
.expect("how did a root path end up here..") | ||
.to_path_buf(); | ||
|
||
// Try to gather the first luaurc and / or error we | ||
// encounter to display better error messages to users | ||
let mut first_luaurc = None; | ||
let mut first_error = None; | ||
let predicate = |rc: &LuauRc| { | ||
if first_luaurc.is_none() { | ||
first_luaurc.replace(rc.clone()); | ||
} | ||
if let Err(e) = rc.validate() { | ||
if first_error.is_none() { | ||
first_error.replace(e); | ||
} | ||
false | ||
} else { | ||
rc.find_alias(&alias).is_some() | ||
} | ||
}; | ||
|
||
// Try to find a luaurc that contains the alias we're searching for | ||
let luaurc = LuauRc::read_recursive(parent, predicate) | ||
.await | ||
.ok_or_else(|| { | ||
if let Some(error) = first_error { | ||
LuaError::runtime(format!("error while parsing .luaurc file: {error}")) | ||
} else if let Some(luaurc) = first_luaurc { | ||
LuaError::runtime(format!( | ||
"failed to find alias '{alias}' - known aliases:\n{}", | ||
luaurc | ||
.aliases() | ||
.iter() | ||
.map(|(name, path)| format!(" {name} {} {path}", style(">").dim())) | ||
.collect::<Vec<_>>() | ||
.join("\n") | ||
)) | ||
} else { | ||
LuaError::runtime(format!("failed to find alias '{alias}' (no .luaurc)")) | ||
} | ||
})?; | ||
|
||
// We now have our aliased path, our path require function just needs it | ||
// in a slightly different format with both absolute + relative to cwd | ||
let abs_path = luaurc.find_alias(&alias).unwrap().join(path); | ||
let rel_path = pathdiff::diff_paths(&abs_path, CWD.as_path()).ok_or_else(|| { | ||
LuaError::runtime(format!("failed to find relative path for alias '{alias}'")) | ||
})?; | ||
|
||
super::path::require_abs_rel(ctx, abs_path, rel_path).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.