feat: with rust workspace members
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-03-30 22:33:25 +01:00
parent 0539e375b1
commit 823712e2bf
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
5 changed files with 114 additions and 23 deletions

View File

@ -24,6 +24,7 @@ serde_yaml.workspace = true
serde.workspace = true serde.workspace = true
tracing = {version = "0.1.40", features = ["log"]} tracing = {version = "0.1.40", features = ["log"]}
chrono = {version = "0.4.37"} chrono = {version = "0.4.37"}
toml = "0.8.12"
[dev-dependencies] [dev-dependencies]
tokio.workspace = true tokio.workspace = true

View File

@ -7,6 +7,39 @@ pub mod rust_lib;
pub mod rust_service; pub mod rust_service;
pub mod cuddle_file; pub mod cuddle_file;
pub mod rust_workspace {
use serde::Deserialize;
#[derive(Deserialize, Clone, Debug)]
pub struct Workspace {
pub members: Vec<String>,
}
#[derive(Deserialize, Clone, Debug)]
pub struct File {
pub workspace: Option<Workspace>,
}
impl File {
pub async fn read_file() -> eyre::Result<Option<Self>> {
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<Vec<String>> {
self.workspace.as_ref().map(|w| w.members.clone())
}
}
}
pub mod cuddle_please; pub mod cuddle_please;
pub mod cuddle_releaser; pub mod cuddle_releaser;
pub mod cuddle_x; pub mod cuddle_x;

View File

@ -3,12 +3,11 @@ use std::path::PathBuf;
use async_trait::async_trait; use async_trait::async_trait;
use dagger_rust::source::RustSource; use dagger_rust::source::RustSource;
use dagger_sdk::Container; use dagger_sdk::Container;
use futures::StreamExt;
use crate::{ use crate::{
cli, cli,
rust_service::architecture::{Architecture, Os}, rust_service::architecture::{Architecture, Os},
MainAction, PullRequestAction, rust_workspace, MainAction, PullRequestAction,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -24,7 +23,7 @@ pub struct RustLib {
impl RustLib { impl RustLib {
pub fn new(value: dagger_sdk::Query) -> Self { pub fn new(value: dagger_sdk::Query) -> Self {
Self { Self {
client: value, client: value.pipeline("rust-lib"),
source: None, source: None,
crates: Vec::new(), crates: Vec::new(),
arch: None, arch: None,
@ -54,6 +53,16 @@ impl RustLib {
self 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 { pub fn with_arch(&mut self, arch: Architecture) -> &mut Self {
self.arch = Some(arch); self.arch = Some(arch);
@ -72,26 +81,6 @@ impl RustLib {
.unwrap_or(std::env::current_dir().unwrap()) .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<Container> { pub async fn build_base(&self) -> eyre::Result<Container> {
let rust_src = RustSource::new(self.client.clone()); let rust_src = RustSource::new(self.client.clone());

View File

@ -446,6 +446,7 @@ mod docker_cache;
mod docker_cli; mod docker_cli;
mod kubectl; mod kubectl;
mod mold; mod mold;
mod rust_workspace;
mod sqlx; mod sqlx;
mod ssh_agent; mod ssh_agent;
@ -461,6 +462,7 @@ pub mod extensions {
pub use super::docker_cli::*; pub use super::docker_cli::*;
pub use super::kubectl::*; pub use super::kubectl::*;
pub use super::mold::*; pub use super::mold::*;
pub use super::rust_workspace::*;
pub use super::sqlx::*; pub use super::sqlx::*;
pub use super::ssh_agent::*; pub use super::ssh_agent::*;
} }

View File

@ -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<Container> {
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<String> {
if let Ok(Some(file)) = rust_workspace::File::read_file().await {
if let Some(members) = file.get_workspace_members() {
return members;
}
}
Vec::new()
}