diff --git a/crates/cuddle-ci/src/dagger_middleware.rs b/crates/cuddle-ci/src/dagger_middleware.rs index e059155..1c6c2ab 100644 --- a/crates/cuddle-ci/src/dagger_middleware.rs +++ b/crates/cuddle-ci/src/dagger_middleware.rs @@ -1,6 +1,7 @@ use async_trait::async_trait; use dagger_sdk::Container; use std::{future::Future, pin::Pin}; +pub type DynMiddleware = Box; #[async_trait] pub trait DaggerMiddleware { diff --git a/crates/cuddle-ci/src/rust_service.rs b/crates/cuddle-ci/src/rust_service.rs index 05d0884..25ced32 100644 --- a/crates/cuddle-ci/src/rust_service.rs +++ b/crates/cuddle-ci/src/rust_service.rs @@ -5,9 +5,12 @@ use dagger_rust::source::RustSource; use dagger_sdk::Container; use futures::{stream, StreamExt}; -use crate::{dagger_middleware::DaggerMiddleware, MainAction, PullRequestAction}; +use crate::{ + dagger_middleware::{DaggerMiddleware, DynMiddleware}, + MainAction, PullRequestAction, +}; -pub type DynMiddleware = Box; +use self::architecture::{Architecture, Os}; pub enum RustServiceStage { BeforeDeps(DynMiddleware), @@ -30,6 +33,8 @@ pub struct RustService { source: Option, crates: Vec, bin_name: String, + arch: Option, + os: Option, } impl From for RustService { @@ -42,6 +47,8 @@ impl From for RustService { source: None, crates: Vec::new(), bin_name: String::new(), + arch: None, + os: None, } } } @@ -56,6 +63,8 @@ impl RustService { source: None, crates: Vec::new(), bin_name: String::new(), + arch: None, + os: None, }) } @@ -92,12 +101,44 @@ impl RustService { self } + pub fn with_arch(&mut self, arch: Architecture) -> &mut Self { + self.arch = Some(arch); + + self + } + + pub fn with_os(&mut self, os: Os) -> &mut Self { + self.os = Some(os); + + self + } + fn get_src(&self) -> PathBuf { self.source .clone() .unwrap_or(std::env::current_dir().unwrap()) } + fn get_arch(&self) -> Architecture { + self.arch + .clone() + .unwrap_or_else(|| match std::env::consts::ARCH { + "x86" | "x86_64" | "amd64" => Architecture::Amd64, + "arm" => Architecture::Arm64, + arch => panic!("unsupported architecture: {arch}"), + }) + } + + fn get_os(&self) -> Os { + self.os + .clone() + .unwrap_or_else(|| match std::env::consts::OS { + "linux" => Os::Linux, + "macos" => Os::MacOS, + os => panic!("unsupported os: {os}"), + }) + } + async fn run_stage( &self, stages: impl IntoIterator>, @@ -278,24 +319,32 @@ impl MainAction for RustService { } pub mod architecture { - #[derive(Debug)] + #[derive(Debug, Clone)] pub enum Architecture { Amd64, Arm64, } - #[derive(Debug)] + #[derive(Debug, Clone)] pub enum Os { Linux, MacOS, } } -pub mod apt; -pub mod cargo_binstall; -pub mod clap_sanity_test; -pub mod mold; -pub mod sqlx; +mod apt; +mod cargo_binstall; +mod clap_sanity_test; +mod mold; +mod sqlx; + +pub mod extensions { + pub use super::apt::*; + pub use super::cargo_binstall::*; + pub use super::clap_sanity_test::*; + pub use super::mold::*; + pub use super::sqlx::*; +} #[cfg(test)] mod test { @@ -320,12 +369,14 @@ mod test { let root_dir = std::path::PathBuf::from("../../").canonicalize()?; let container = RustService::from(client.clone()) + .with_arch(Architecture::Amd64) + .with_os(Os::Linux) .with_apt(&["git"]) - .with_cargo_binstall(Architecture::Amd64, Os::Linux, "latest", ["sqlx-cli"]) + .with_cargo_binstall("latest", ["sqlx-cli"]) .with_source(root_dir) .with_bin_name("ci") .with_crates(["crates/*", "examples/*", "ci"]) - .with_mold(Architecture::Amd64, Os::Linux, "2.3.3") + .with_mold("2.3.3") .with_stage(RustServiceStage::BeforeDeps(middleware(|c| { async move { // Noop diff --git a/crates/cuddle-ci/src/rust_service/cargo_binstall.rs b/crates/cuddle-ci/src/rust_service/cargo_binstall.rs index 0c9e9e6..5754a84 100644 --- a/crates/cuddle-ci/src/rust_service/cargo_binstall.rs +++ b/crates/cuddle-ci/src/rust_service/cargo_binstall.rs @@ -83,8 +83,6 @@ impl DaggerMiddleware for CargoBInstall { pub trait CargoBInstallExt { fn with_cargo_binstall( &mut self, - arch: Architecture, - os: Os, version: impl Into, crates: impl IntoIterator>, ) -> &mut Self { @@ -95,15 +93,13 @@ pub trait CargoBInstallExt { impl CargoBInstallExt for RustService { fn with_cargo_binstall( &mut self, - arch: Architecture, - os: Os, version: impl Into, crates: impl IntoIterator>, ) -> &mut Self { let crates: Vec = crates.into_iter().map(|s| s.into()).collect(); self.with_stage(super::RustServiceStage::BeforeDeps( - Box::new(CargoBInstall::new(arch, os, version, crates)) + Box::new(CargoBInstall::new(self.get_arch(), self.get_os(), version, crates)) )) } } diff --git a/crates/cuddle-ci/src/rust_service/mold.rs b/crates/cuddle-ci/src/rust_service/mold.rs index 7df1ea6..ae26ba8 100644 --- a/crates/cuddle-ci/src/rust_service/mold.rs +++ b/crates/cuddle-ci/src/rust_service/mold.rs @@ -88,25 +88,15 @@ impl DaggerMiddleware for MoldInstall { } pub trait MoldActionExt { - fn with_mold( - &mut self, - architecture: Architecture, - os: Os, - version: impl Into, - ) -> &mut Self { + fn with_mold(&mut self, version: impl Into) -> &mut Self { self } } impl MoldActionExt for RustService { - fn with_mold( - &mut self, - architecture: Architecture, - os: Os, - version: impl Into, - ) -> &mut Self { + fn with_mold(&mut self, version: impl Into) -> &mut Self { self.with_stage(super::RustServiceStage::AfterDeps( - Box::new(MoldInstall::new(architecture, os, version)) + Box::new(MoldInstall::new(self.get_arch(), self.get_os(), version)) )) } }