Add helpers for appending to Lua's package.path
This commit is contained in:
1 parent
29e2958dd3
commit
aa1bfb768e
3 files changed
+33
-22
No files matched your search
+29
-2
@@ -1,11 +1,11 @@
|
||||
use ahash::AHasher;
|
||||
use mlua::{Lua, Result as LuaResult, Table};
|
||||
use std::{
|
||||
env,
|
||||
hash::{Hash, Hasher},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use ahash::AHasher;
|
||||
|
||||
pub fn get_executable_dir() -> anyhow::Result<PathBuf> {
|
||||
let exe_path = env::current_exe()?;
|
||||
let exe_parent_dir = exe_path.parent().unwrap().canonicalize()?;
|
||||
@@ -52,3 +52,30 @@ pub fn replace_in_matching_lines(
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// Appends a search pattern to Lua's `package.path`.
|
||||
pub fn append_lua_package_path(lua: &Lua, pattern: &str) -> LuaResult<()> {
|
||||
let package: Table = lua.globals().get("package")?;
|
||||
let mut package_path: String = package.get("path")?;
|
||||
|
||||
if !package_path.is_empty() {
|
||||
package_path.push(';');
|
||||
}
|
||||
|
||||
package_path.push_str(pattern);
|
||||
package.set("path", package_path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Adds a directory to Lua's `package.path`.
|
||||
///
|
||||
/// This adds the following patterns:
|
||||
/// - `<path>/?.lua`
|
||||
/// - `<path>/?/init.lua`
|
||||
pub fn append_lua_package_dir(lua: &Lua, path: &Path) -> LuaResult<()> {
|
||||
let path = path.to_string_lossy();
|
||||
append_lua_package_path(lua, &format!("{path}/?.lua"))?;
|
||||
append_lua_package_path(lua, &format!("{path}/?/init.lua"))?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user