From 3ee80f473461f9ce8bb970fd3d72de34ffc37878 Mon Sep 17 00:00:00 2001 From: bluepython508 Date: Tue, 12 Dec 2023 21:51:23 +0000 Subject: [PATCH] Add finalising commands for things like git --- src/context.rs | 30 ++++++++++++++++++++---------- src/manifest.rs | 10 +++++++++- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/context.rs b/src/context.rs index 013e333..4fb3565 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,7 +1,7 @@ use std::path::{Path, PathBuf}; -use crate::manifest::{FileDefinition, Manifest, Source, Transform, Params}; -use eyre::Result; +use crate::manifest::{FileDefinition, Manifest, Params, Source, Transform}; +use eyre::{Result, ensure}; use tracing::instrument; #[derive(Debug)] @@ -17,7 +17,9 @@ impl Context { let params = Params::load_user()?.merge(params); Ok(Context { manifest: load_manifest(&source)?, - root: source.parent().ok_or_else(|| eyre::eyre!("Expect manifest to have a parent"))?, + root: source + .parent() + .ok_or_else(|| eyre::eyre!("Expect manifest to have a parent"))?, params, }) } @@ -30,12 +32,25 @@ impl Context { self.generate_file(&dest, path, def)?; } + for command in &self.manifest.commands { + let success = std::process::Command::new(&command.command) + .args(&command.args) + .current_dir(&dest) + .status()? + .success(); + ensure!(success, "Command {:?} failed", command.command); + } + Ok(()) } #[instrument] fn generate_file(&self, dest: &Path, path: &PathBuf, def: &FileDefinition) -> Result<()> { - std::fs::create_dir_all(dest.join(path).parent().expect("`dest` should be an absolute path with a parent"))?; + std::fs::create_dir_all( + dest.join(path) + .parent() + .expect("`dest` should be an absolute path with a parent"), + )?; let mut input = self .root .join( @@ -67,13 +82,8 @@ fn load_manifest(src: &Source) -> Result { Ok(toml::from_str(std::str::from_utf8(&src.load()?)?)?) } - #[instrument(skip(input))] -fn run_template( - args: &Params, - params: &Params, - input: Vec, -) -> Result> { +fn run_template(args: &Params, params: &Params, input: Vec) -> Result> { let engine = upon::Engine::new(); let context = args.clone().merge(params.clone()); Ok(engine diff --git a/src/manifest.rs b/src/manifest.rs index a9e330a..0ffe04f 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -3,10 +3,18 @@ use serde::{Deserialize, Serialize}; use std::{collections::HashMap, convert::Infallible, path::PathBuf, str::FromStr}; use tracing::instrument; -#[derive(Debug, Clone, PartialEq, Deserialize)] +#[derive(Debug, Clone, PartialEq, Deserialize, Default)] +#[serde(default)] pub struct Manifest { pub parameters: HashMap, pub files: HashMap, + pub commands: Vec +} + +#[derive(Debug, Clone, PartialEq, Deserialize)] +pub struct Command { + pub command: String, + pub args: Vec } #[derive(Debug, Clone, PartialEq, Deserialize)]