2023-11-28 10:12:19 +01:00
|
|
|
use std::{path::PathBuf, sync::Arc};
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use dagger_sdk::{Container, HostDirectoryOpts, HostDirectoryOptsBuilder};
|
|
|
|
|
|
|
|
use crate::dagger_middleware::DaggerMiddleware;
|
|
|
|
|
|
|
|
use super::RustService;
|
|
|
|
|
|
|
|
pub struct Assets {
|
|
|
|
client: dagger_sdk::Query,
|
2023-11-28 11:27:15 +01:00
|
|
|
assets: Vec<(PathBuf, PathBuf)>,
|
2023-11-28 10:12:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Assets {
|
|
|
|
pub fn new(client: dagger_sdk::Query) -> Self {
|
|
|
|
Self {
|
|
|
|
client,
|
|
|
|
assets: Vec::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 11:27:15 +01:00
|
|
|
fn with_folders(mut self, folders: impl IntoIterator<Item = (PathBuf, PathBuf)>) -> Self {
|
|
|
|
let mut folders = folders.into_iter().collect::<Vec<(PathBuf, PathBuf)>>();
|
2023-11-28 10:12:19 +01:00
|
|
|
self.assets.append(&mut folders);
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl DaggerMiddleware for Assets {
|
|
|
|
async fn handle(&self, container: Container) -> eyre::Result<Container> {
|
2023-11-28 11:27:15 +01:00
|
|
|
let container =
|
|
|
|
self.assets
|
|
|
|
.iter()
|
|
|
|
.fold(container, |container, (src_asset_path, dest_asset_path)| {
|
|
|
|
let src_path = src_asset_path.display().to_string();
|
|
|
|
let dest_path = dest_asset_path.display().to_string();
|
|
|
|
let path = self.client.host().directory(&src_path);
|
|
|
|
container.with_directory(&dest_path, path)
|
|
|
|
});
|
2023-11-28 10:12:19 +01:00
|
|
|
|
|
|
|
Ok(container)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait AssetsExt {
|
2023-11-28 11:27:15 +01:00
|
|
|
fn with_assets(&mut self, folders: impl IntoIterator<Item = (PathBuf, PathBuf)>) -> &mut Self {
|
2023-11-28 10:12:19 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssetsExt for RustService {
|
2023-11-28 11:27:15 +01:00
|
|
|
fn with_assets(&mut self, folders: impl IntoIterator<Item = (PathBuf, PathBuf)>) -> &mut Self {
|
2023-11-28 10:12:19 +01:00
|
|
|
self.with_stage(super::RustServiceStage::AfterPackage(Arc::new(
|
|
|
|
Assets::new(self.client.clone()).with_folders(folders),
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|