Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
@@ -35,3 +35,5 @@ libz-sys = { version = "1.1.9", default-features = false, features = [
|
||||
inquire = { version = "0.6.2", features = ["console"] }
|
||||
tempfile = { version = "3.6.0" }
|
||||
serde_json = "1.0.97"
|
||||
rlua = "0.19.5"
|
||||
rlua-searcher = { git = "https://git.front.kjuulh.io/kjuulh/rlua-searcher.git", rev = "2b29a9d0e86ec7f91b31dd844a58168969b7b74b" }
|
||||
|
@@ -1,6 +1,9 @@
|
||||
use std::path::PathBuf;
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
|
||||
use anyhow::Context;
|
||||
use clap::ArgMatches;
|
||||
use rlua::Lua;
|
||||
use rlua_searcher::AddSearcher;
|
||||
|
||||
use crate::{
|
||||
actions::shell::ShellAction,
|
||||
@@ -93,6 +96,88 @@ impl CuddleAction {
|
||||
};
|
||||
}
|
||||
CuddleScript::Dagger(_d) => Err(anyhow::anyhow!("not implemented yet!")),
|
||||
CuddleScript::Lua(l) => {
|
||||
let lua = Lua::new();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
//map.insert("init".into(), "print(\"something\")".into());
|
||||
|
||||
let lua_dir = PathBuf::new().join(&self.path).join("scripts").join("lua");
|
||||
if lua_dir.exists() {
|
||||
let absolute_lua_dir = lua_dir.canonicalize()?;
|
||||
for entry in walkdir::WalkDir::new(&lua_dir)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
{
|
||||
if entry.metadata()?.is_file() {
|
||||
let full_file_path = entry.path().canonicalize()?;
|
||||
let relative_module_path =
|
||||
full_file_path.strip_prefix(&absolute_lua_dir)?;
|
||||
let module_path = relative_module_path
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
.trim_end_matches("/init.lua")
|
||||
.trim_end_matches(".lua")
|
||||
.replace("/", ".");
|
||||
let contents = std::fs::read_to_string(entry.path())?;
|
||||
tracing::trace!(module_path = &module_path, "adding lua file");
|
||||
|
||||
map.insert(module_path.into(), contents.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
let lua_rocks_dir = PathBuf::new()
|
||||
.join(&self.path)
|
||||
.join("lua_modules")
|
||||
.join("share")
|
||||
.join("lua")
|
||||
.join("5.4");
|
||||
if lua_rocks_dir.exists() {
|
||||
let absolute_lua_dir = lua_rocks_dir.canonicalize()?;
|
||||
for entry in walkdir::WalkDir::new(&lua_rocks_dir)
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
{
|
||||
if entry.metadata()?.is_file() {
|
||||
let full_file_path = entry.path().canonicalize()?;
|
||||
let relative_module_path =
|
||||
full_file_path.strip_prefix(&absolute_lua_dir)?;
|
||||
let module_path = relative_module_path
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
.trim_end_matches("/init.lua")
|
||||
.trim_end_matches(".lua")
|
||||
.replace("/", ".");
|
||||
let contents = std::fs::read_to_string(entry.path())?;
|
||||
tracing::trace!(module_path = &module_path, "adding lua file");
|
||||
|
||||
map.insert(module_path.into(), contents.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lua.context::<_, anyhow::Result<()>>(|lua_ctx| {
|
||||
lua_ctx.add_searcher(map)?;
|
||||
let globals = lua_ctx.globals();
|
||||
|
||||
let lua_script_entry = std::fs::read_to_string(
|
||||
PathBuf::new()
|
||||
.join(&self.path)
|
||||
.join("scripts")
|
||||
.join(format!("{}.lua", &self.name)),
|
||||
)
|
||||
.context("failed to find lua script")?;
|
||||
|
||||
lua_ctx
|
||||
.load(&lua_script_entry)
|
||||
.set_name(&self.name)?
|
||||
.exec()?;
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -110,6 +110,12 @@ impl CuddleCli {
|
||||
))
|
||||
}
|
||||
CuddleScript::Dagger(_) => todo!(),
|
||||
CuddleScript::Lua(l) => self.scripts.push(CuddleAction::new(
|
||||
script.clone(),
|
||||
ctx.path.clone(),
|
||||
name,
|
||||
l.description.clone(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -60,6 +60,7 @@ pub fn build_scripts(cli: CuddleCli) -> Vec<Command> {
|
||||
}
|
||||
}
|
||||
crate::model::CuddleScript::Dagger(_) => todo!(),
|
||||
crate::model::CuddleScript::Lua(l) => {}
|
||||
}
|
||||
|
||||
cmds.push(cmd)
|
||||
|
@@ -42,6 +42,11 @@ pub struct CuddleDaggerScript {
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
pub struct CuddleLuaScript {
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum CuddleScript {
|
||||
@@ -49,6 +54,8 @@ pub enum CuddleScript {
|
||||
Shell(CuddleShellScript),
|
||||
#[serde(alias = "dagger")]
|
||||
Dagger(CuddleDaggerScript),
|
||||
#[serde(alias = "lua")]
|
||||
Lua(CuddleLuaScript),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
|
Reference in New Issue
Block a user