From 823712e2bf4be5b8e740e529898eab5ab67f2419 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 30 Mar 2024 22:33:25 +0100 Subject: [PATCH] feat: with rust workspace members Signed-off-by: kjuulh --- crates/cuddle-ci/Cargo.toml | 1 + crates/cuddle-ci/src/lib.rs | 33 ++++++++++ crates/cuddle-ci/src/rust_lib.rs | 35 ++++------ crates/cuddle-ci/src/rust_service.rs | 2 + .../src/rust_service/rust_workspace.rs | 66 +++++++++++++++++++ 5 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 crates/cuddle-ci/src/rust_service/rust_workspace.rs diff --git a/crates/cuddle-ci/Cargo.toml b/crates/cuddle-ci/Cargo.toml index 9887bb6..b490308 100644 --- a/crates/cuddle-ci/Cargo.toml +++ b/crates/cuddle-ci/Cargo.toml @@ -24,6 +24,7 @@ serde_yaml.workspace = true serde.workspace = true tracing = {version = "0.1.40", features = ["log"]} chrono = {version = "0.4.37"} +toml = "0.8.12" [dev-dependencies] tokio.workspace = true diff --git a/crates/cuddle-ci/src/lib.rs b/crates/cuddle-ci/src/lib.rs index 5a0d955..a41176c 100644 --- a/crates/cuddle-ci/src/lib.rs +++ b/crates/cuddle-ci/src/lib.rs @@ -7,6 +7,39 @@ pub mod rust_lib; pub mod rust_service; pub mod cuddle_file; +pub mod rust_workspace { + use serde::Deserialize; + + #[derive(Deserialize, Clone, Debug)] + pub struct Workspace { + pub members: Vec, + } + + #[derive(Deserialize, Clone, Debug)] + pub struct File { + pub workspace: Option, + } + + impl File { + pub async fn read_file() -> eyre::Result> { + let file = match tokio::fs::read_to_string("Cargo.toml").await { + Ok(file) => file, + Err(e) => { + tracing::warn!("Cargo.toml was not found: {}", e); + return Ok(None); + } + }; + + let workspace_file: File = toml::from_str(&file)?; + + Ok(Some(workspace_file)) + } + + pub fn get_workspace_members(&self) -> Option> { + self.workspace.as_ref().map(|w| w.members.clone()) + } + } +} pub mod cuddle_please; pub mod cuddle_releaser; pub mod cuddle_x; diff --git a/crates/cuddle-ci/src/rust_lib.rs b/crates/cuddle-ci/src/rust_lib.rs index 5c61c96..aee6b92 100644 --- a/crates/cuddle-ci/src/rust_lib.rs +++ b/crates/cuddle-ci/src/rust_lib.rs @@ -3,12 +3,11 @@ use std::path::PathBuf; use async_trait::async_trait; use dagger_rust::source::RustSource; use dagger_sdk::Container; -use futures::StreamExt; use crate::{ cli, rust_service::architecture::{Architecture, Os}, - MainAction, PullRequestAction, + rust_workspace, MainAction, PullRequestAction, }; #[derive(Clone)] @@ -24,7 +23,7 @@ pub struct RustLib { impl RustLib { pub fn new(value: dagger_sdk::Query) -> Self { Self { - client: value, + client: value.pipeline("rust-lib"), source: None, crates: Vec::new(), arch: None, @@ -54,6 +53,16 @@ impl RustLib { self } + pub async fn with_workspace_crates(&mut self) -> &mut Self { + if let Ok(Some(file)) = rust_workspace::File::read_file().await { + if let Some(members) = file.get_workspace_members() { + return self.with_crates(members); + } + } + + self + } + pub fn with_arch(&mut self, arch: Architecture) -> &mut Self { self.arch = Some(arch); @@ -72,26 +81,6 @@ impl RustLib { .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}"), - }) - } - pub async fn build_base(&self) -> eyre::Result { let rust_src = RustSource::new(self.client.clone()); diff --git a/crates/cuddle-ci/src/rust_service.rs b/crates/cuddle-ci/src/rust_service.rs index 52a53af..277be63 100644 --- a/crates/cuddle-ci/src/rust_service.rs +++ b/crates/cuddle-ci/src/rust_service.rs @@ -446,6 +446,7 @@ mod docker_cache; mod docker_cli; mod kubectl; mod mold; +mod rust_workspace; mod sqlx; mod ssh_agent; @@ -461,6 +462,7 @@ pub mod extensions { pub use super::docker_cli::*; pub use super::kubectl::*; pub use super::mold::*; + pub use super::rust_workspace::*; pub use super::sqlx::*; pub use super::ssh_agent::*; } diff --git a/crates/cuddle-ci/src/rust_service/rust_workspace.rs b/crates/cuddle-ci/src/rust_service/rust_workspace.rs new file mode 100644 index 0000000..63fde60 --- /dev/null +++ b/crates/cuddle-ci/src/rust_service/rust_workspace.rs @@ -0,0 +1,66 @@ +use std::sync::Arc; + +use async_trait::async_trait; +use dagger_sdk::Container; + +use crate::{dagger_middleware::DaggerMiddleware, leptos_service::LeptosService, rust_workspace}; + +use super::RustService; + +pub struct RustWorkspace {} + +impl Default for RustWorkspace { + fn default() -> Self { + Self::new() + } +} + +impl RustWorkspace { + pub fn new() -> Self { + Self {} + } +} + +#[async_trait] +impl DaggerMiddleware for RustWorkspace { + async fn handle(&self, container: Container) -> eyre::Result { + Ok(container) + } +} + +#[async_trait] +pub trait RustWorkspaceExt { + async fn with_workspace_crates(&mut self) -> &mut Self { + self + } +} + +#[async_trait] +impl RustWorkspaceExt for RustService { + async fn with_workspace_crates(&mut self) -> &mut Self { + self.with_crates(get_members().await) + .with_stage(super::RustServiceStage::BeforeDeps(Arc::new( + RustWorkspace::new(), + ))) + } +} + +#[async_trait] +impl RustWorkspaceExt for LeptosService { + async fn with_workspace_crates(&mut self) -> &mut Self { + self.with_crates(get_members().await) + .with_stage(super::RustServiceStage::BeforeDeps(Arc::new( + RustWorkspace::new(), + ))) + } +} + +async fn get_members() -> Vec { + if let Ok(Some(file)) = rust_workspace::File::read_file().await { + if let Some(members) = file.get_workspace_members() { + return members; + } + } + + Vec::new() +}