From 938680cb7520ab62a2d579d58fcceade7e31b3ef Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 26 Oct 2024 21:32:09 +0200 Subject: [PATCH] feat: refactor actions Signed-off-by: kjuulh --- crates/cuddle/src/actions.rs | 58 --------------------- crates/cuddle/src/actions/rust_builder.rs | 63 ++++++++++++++++++++++- 2 files changed, 61 insertions(+), 60 deletions(-) diff --git a/crates/cuddle/src/actions.rs b/crates/cuddle/src/actions.rs index ff5013e..df14532 100644 --- a/crates/cuddle/src/actions.rs +++ b/crates/cuddle/src/actions.rs @@ -107,16 +107,6 @@ impl Actions { } } -#[derive(Debug, Deserialize)] -struct CuddleActionsSchema { - actions: Vec, -} - -#[derive(Debug, Deserialize)] -struct CuddleActionSchema { - name: String, -} - pub enum ActionVariant { Rust { root_path: PathBuf }, Docker, @@ -149,54 +139,6 @@ pub enum ExecutableActionFlag { Bool, } -impl CuddleActionsSchema { - fn to_executable(self, action_path: &Path) -> anyhow::Result { - Ok(ExecutableActions { - actions: self - .actions - .into_iter() - .map(|a| { - let name = a.name.clone(); - let action_path = action_path.to_string_lossy().to_string(); - - ExecutableAction { - name: a.name, - description: String::new(), - flags: BTreeMap::default(), - call_fn: LazyResolve::new(Box::new(move || { - let name = name.clone(); - let action_path = action_path.clone(); - - Box::pin(async move { - if let Some(parent) = PathBuf::from(&action_path).parent() { - tokio::process::Command::new("touch") - .arg(parent) - .output() - .await?; - } - - tracing::debug!("calling: {}", name); - let mut cmd = tokio::process::Command::new(action_path); - cmd.args(["do", &name]); - - let output = cmd.output().await?; - let stdout = std::str::from_utf8(&output.stdout)?; - for line in stdout.lines() { - println!("{}: {}", &name, line); - } - - tracing::debug!("finished call for output: {}", &name); - - Ok(()) - }) - })), - } - }) - .collect(), - }) - } -} - struct LazyResolve( Arc Pin> + Send>> + Send + Sync>, ); diff --git a/crates/cuddle/src/actions/rust_builder.rs b/crates/cuddle/src/actions/rust_builder.rs index 9504f25..948e2c1 100644 --- a/crates/cuddle/src/actions/rust_builder.rs +++ b/crates/cuddle/src/actions/rust_builder.rs @@ -1,11 +1,12 @@ use std::{ + collections::BTreeMap, env::temp_dir, path::{Path, PathBuf}, }; -use crate::actions::CuddleActionsSchema; +use serde::Deserialize; -use super::ExecutableActions; +use super::{ExecutableAction, ExecutableActions, LazyResolve}; pub struct RustActionsBuilder { root_path: PathBuf, @@ -171,3 +172,61 @@ impl Drop for TempGuard { } } } + +#[derive(Debug, Deserialize)] +struct CuddleActionsSchema { + actions: Vec, +} + +#[derive(Debug, Deserialize)] +struct CuddleActionSchema { + name: String, +} + +impl CuddleActionsSchema { + fn to_executable(self, action_path: &Path) -> anyhow::Result { + Ok(ExecutableActions { + actions: self + .actions + .into_iter() + .map(|a| { + let name = a.name.clone(); + let action_path = action_path.to_string_lossy().to_string(); + + ExecutableAction { + name: a.name, + description: String::new(), + flags: BTreeMap::default(), + call_fn: LazyResolve::new(Box::new(move || { + let name = name.clone(); + let action_path = action_path.clone(); + + Box::pin(async move { + if let Some(parent) = PathBuf::from(&action_path).parent() { + tokio::process::Command::new("touch") + .arg(parent) + .output() + .await?; + } + + tracing::debug!("calling: {}", name); + let mut cmd = tokio::process::Command::new(action_path); + cmd.args(["do", &name]); + + let output = cmd.output().await?; + let stdout = std::str::from_utf8(&output.stdout)?; + for line in stdout.lines() { + println!("{}: {}", &name, line); + } + + tracing::debug!("finished call for output: {}", &name); + + Ok(()) + }) + })), + } + }) + .collect(), + }) + } +}