at 4940405
use std::path::PathBuf; use crate::builders::{ArtifactPath, Builder}; use miette::{IntoDiagnostic, Result, miette}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use tokio::process::Command; /// Configuration for [`ScriptBuilder`]. #[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)] pub struct ScriptBuilderConfig { /// Shell commands to execute in order. Each line is passed to `sh -c`, /// so any POSIX shell syntax is supported. /// /// The environment variable `ABBAYE_BUILDING_VERSION` is set to the version /// being built. (e.g. the git tag `v0.1.0` or whatever. Not Abbaye's own version) /// /// The build fails immediately if any command exits with a non-zero status. /// /// ```toml /// [[builders]] /// type = "script" /// script = [ /// "make release", /// "strip target/mybin", /// ] /// outputs = ["target/mybin"] /// ``` pub script: Vec<String>, /// Paths of the files or directories produced by the script that should be /// treated as release artifacts (copied to `dist/` and listed on the /// release page). Each path is resolved relative to the working directory /// in which `abbaye` is run. /// /// If a listed path is missing, the build fails. pub outputs: Vec<ScriptBuilderOutput>, } /// Lets the user specify a path for a script output, optionally with a custom name. /// I need to check but it should enable the user to specify a directory as output. /// If that's a good idea is an other question. #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] #[serde(untagged)] pub enum ScriptBuilderOutput { PathWithName { path: PathBuf, name: String }, Path(PathBuf), } /// Executes a script and treats the listed output paths as release artifacts. pub struct ScriptBuilder; impl Builder for ScriptBuilder { type ConfigType = ScriptBuilderConfig; async fn build(&self, config: Self::ConfigType, version: &str) -> Result<Vec<ArtifactPath>> { for line in &config.script { let status = Command::new("sh") .args(["-c", line]) .env("ABBAYE_BUILDING_VERSION", version) .status() .await .into_diagnostic()?; if !status.success() { return Err(miette!( "script command failed (exit {}):\n {}", status.code().unwrap_or(-1), line )); } } // Collect every declared output path as an artifact. let mut artifacts = Vec::new(); for output in &config.outputs { let (path, name) = match output { ScriptBuilderOutput::Path(p) => { let name = p .file_name() .ok_or_else(|| miette!("output path has no file name: {}", p.display()))? .to_string_lossy() .into_owned(); (p, name) } ScriptBuilderOutput::PathWithName { path: p, name } => (p, name.clone()), }; if !path.exists() { return Err(miette!( "declared script output does not exist: {}", path.display() )); } artifacts.push(ArtifactPath { path: path.clone(), name, hash: None, }); } Ok(artifacts) } }