fix: ci
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 2023-08-12 20:05:27 +02:00 committed by Kasper Juul Hermansen
parent e1428a8fbb
commit 776db7274a
7 changed files with 24 additions and 220 deletions

1
Cargo.lock generated
View File

@ -184,6 +184,7 @@ dependencies = [
"clap",
"color-eyre",
"dagger-cuddle-please",
"dagger-rust",
"dagger-sdk",
"dotenv",
"eyre",

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
dagger-cuddle-please.workspace = true
dagger-rust.workspace = true
dagger-sdk = "*"
eyre = "*"

View File

@ -1,4 +1,3 @@
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
@ -70,9 +69,7 @@ async fn main() -> eyre::Result<()> {
match &cli.commands {
Commands::Local { command } => match command {
LocalCommands::Test => {
let base_image =
base_rust_image(client.clone(), &cli.global, &None, &"debug".into()).await?;
test::execute(client, &cli.global, base_image).await?;
test::execute(client, &cli.global).await?;
}
LocalCommands::PleaseRelease => todo!(),
},
@ -80,12 +77,7 @@ async fn main() -> eyre::Result<()> {
async fn test(client: Arc<dagger_sdk::Query>, cli: &Command) {
let args = &cli.global;
let base_image = base_rust_image(client.clone(), args, &None, &"debug".into())
.await
.unwrap();
test::execute(client.clone(), args, base_image)
.await
.unwrap();
test::execute(client.clone(), args).await.unwrap();
}
tokio::join!(test(client.clone(), &cli),);
@ -94,12 +86,7 @@ async fn main() -> eyre::Result<()> {
async fn test(client: Arc<dagger_sdk::Query>, cli: &Command) {
let args = &cli.global;
let base_image = base_rust_image(client.clone(), args, &None, &"debug".into())
.await
.unwrap();
test::execute(client.clone(), args, base_image)
.await
.unwrap();
test::execute(client.clone(), args).await.unwrap();
}
async fn cuddle_please(client: Arc<dagger_sdk::Query>, cli: &Command) {
@ -149,195 +136,22 @@ mod please_release {
}
mod test {
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};
use dagger_rust::build::RustVersion;
use crate::GlobalArgs;
pub async fn execute(
_client: Arc<dagger_sdk::Query>,
_args: &GlobalArgs,
container: dagger_sdk::Container,
) -> eyre::Result<()> {
let test_image = container
.pipeline("rust:test")
.with_exec(vec!["apt", "update"])
.with_exec(vec!["apt", "install", "-y", "git"])
.with_exec(vec!["cargo", "test"]);
let please_out = test_image.stdout().await?;
println!("{please_out}");
let please_out = test_image.stderr().await?;
println!("{please_out}");
test_image.exit_code().await?;
pub async fn execute(client: Arc<dagger_sdk::Query>, _args: &GlobalArgs) -> eyre::Result<()> {
dagger_rust::test::RustTest::new(client)
.test(
None::<PathBuf>,
RustVersion::Nightly,
&["crates/*", "examples/*", "ci"],
&[],
)
.await?;
Ok(())
}
}
pub fn get_src(
client: Arc<dagger_sdk::Query>,
args: &GlobalArgs,
) -> eyre::Result<dagger_sdk::Directory> {
let directory = client.host().directory_opts(
args.source
.clone()
.unwrap_or(PathBuf::from("."))
.display()
.to_string(),
dagger_sdk::HostDirectoryOptsBuilder::default()
.exclude(vec!["node_modules/", ".git/", "target/", ".cuddle/"])
.build()?,
);
Ok(directory)
}
pub async fn get_rust_dep_src(
client: Arc<dagger_sdk::Query>,
args: &GlobalArgs,
) -> eyre::Result<dagger_sdk::Directory> {
let directory = client.host().directory_opts(
args.source
.clone()
.unwrap_or(PathBuf::from("."))
.display()
.to_string(),
dagger_sdk::HostDirectoryOptsBuilder::default()
.include(vec!["**/Cargo.toml", "**/Cargo.lock"])
.build()?,
);
Ok(directory)
}
pub async fn get_rust_skeleton_files(
client: Arc<dagger_sdk::Query>,
_args: &GlobalArgs,
) -> eyre::Result<(dagger_sdk::Directory, Vec<String>)> {
let mut rust_crates = vec![PathBuf::from("ci")];
let mut dirs = tokio::fs::read_dir("crates").await?;
while let Some(entry) = dirs.next_entry().await? {
if entry.metadata().await?.is_dir() {
rust_crates.push(entry.path())
}
}
let mut dirs = tokio::fs::read_dir("examples").await?;
while let Some(entry) = dirs.next_entry().await? {
if entry.metadata().await?.is_dir() {
rust_crates.push(entry.path())
}
}
fn create_skeleton_files(
directory: dagger_sdk::Directory,
path: &Path,
) -> eyre::Result<dagger_sdk::Directory> {
println!("found crates: {}", path.display());
let main_content = r#"
#[allow(dead_code)]
fn main() { panic!("should never be executed"); }"#;
let lib_content = r#"
#[allow(dead_code)]
fn some() { panic!("should never be executed"); }"#;
let directory = directory.with_new_file(
path.join("src").join("main.rs").display().to_string(),
main_content,
);
let directory = directory.with_new_file(
path.join("src").join("lib.rs").display().to_string(),
lib_content,
);
Ok(directory)
}
let mut directory = client.directory();
let mut crate_names = Vec::new();
for rust_crate in rust_crates.iter() {
if let Some(file_name) = rust_crate.file_name() {
crate_names.push(file_name.to_str().unwrap().to_string());
}
directory = create_skeleton_files(directory, rust_crate)?;
}
Ok((directory, crate_names))
}
pub async fn base_rust_image(
client: Arc<dagger_sdk::Query>,
args: &GlobalArgs,
platform: &Option<String>,
profile: &String,
) -> eyre::Result<dagger_sdk::Container> {
let dep_src = get_rust_dep_src(client.clone(), args).await?;
let (skeleton_files, crates) = get_rust_skeleton_files(client.clone(), args).await?;
let src = get_src(client.clone(), args)?;
let client = client.pipeline("rust_base_image");
let rust_target = match platform
.clone()
.unwrap_or("linux/amd64".to_string())
.as_str()
{
"linux/amd64" => "x86_64-unknown-linux-gnu",
"linux/arm64" => "aarch64-unknown-linux-gnu",
_ => eyre::bail!("architecture not supported"),
};
let rust_build_image = client
.container()
.from(
args.rust_builder_image
.as_ref()
.unwrap_or(&"rustlang/rust:nightly".into()),
)
.with_exec(vec!["rustup", "target", "add", rust_target])
.with_exec(vec!["apt", "update"])
.with_exec(vec!["apt", "install", "-y", "jq"]);
let target_cache = client.cache_volume(format!("rust_target_{}", profile));
let mut build_options = vec!["cargo", "build", "--target", rust_target, "--workspace"];
if profile == "release" {
build_options.push("--release");
}
let rust_prebuild = rust_build_image
.with_workdir("/mnt/src")
.with_directory("/mnt/src", dep_src.id().await?)
.with_directory("/mnt/src/", skeleton_files.id().await?)
.with_exec(build_options)
.with_mounted_cache("/mnt/src/target/", target_cache.id().await?);
let exclude = crates
.iter()
.filter(|c| **c != "ci")
.map(|c| format!("**/*{}*", c.replace('-', "_")))
.collect::<Vec<_>>();
let exclude = exclude.iter().map(|c| c.as_str()).collect();
let incremental_dir = client.directory().with_directory_opts(
".",
rust_prebuild.directory("target").id().await?,
dagger_sdk::DirectoryWithDirectoryOpts {
exclude: Some(exclude),
include: None,
},
);
let rust_with_src = rust_build_image
.with_workdir("/mnt/src")
.with_directory(
"/usr/local/cargo",
rust_prebuild.directory("/usr/local/cargo").id().await?,
)
.with_directory("target", incremental_dir.id().await?)
.with_directory("/mnt/src/", src.id().await?);
Ok(rust_with_src)
}

View File

@ -55,14 +55,6 @@ impl RustBuild {
let target_str = target.to_string();
let mut build_options = vec!["cargo", "build", "--target", &target_str, "--workspace"];
let entries = dep_src
.directory("crates/example_bin/src")
.entries()
.await?;
for entry in entries {
println!("entry: {}", entry);
}
if matches!(profile, BuildProfile::Release) {
build_options.push("--release");
}

View File

@ -185,7 +185,10 @@ impl RustSource {
if let Some(file_name) = rust_crate.file_name() {
crate_names.push(file_name.to_str().unwrap().to_string());
}
directory = create_skeleton_files(directory, rust_crate.strip_prefix(source_path)?)?;
directory = create_skeleton_files(
directory,
rust_crate.strip_prefix(source_path).unwrap_or(&rust_crate),
)?;
}
Ok((directory, crate_names))

View File

@ -43,15 +43,6 @@ impl RustTest {
let target_cache = self.client.cache_volume(format!("rust_target_test",));
let build_options = vec!["cargo", "build", "--workspace"];
let entries = dep_src
.directory("crates/example_bin/src")
.entries()
.await?;
for entry in entries {
println!("entry: {}", entry);
}
let rust_prebuild = rust_build_image
.with_workdir("/mnt/src")
.with_directory("/mnt/src", dep_src.id().await?)

View File

@ -8,7 +8,9 @@ pub async fn main() -> eyre::Result<()> {
let dag = dagger_rust::source::RustSource::new(client.clone());
let (_src, _rust_src) = dag.get_rust_src(None::<PathBuf>, crates).await?;
let _full_src = dag.get_rust_target_src(client.container(), crates).await?;
let _full_src = dag
.get_rust_target_src(&PathBuf::from("."), client.container(), crates)
.await?;
Ok(())
}