Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
feat(sdk,core): Use async runtime instead of blocking.
Browse files Browse the repository at this point in the history
Default to using async runtime instead of blocking. I.e.

```rust
fn main() -> eyre::Result<()> {
  // ...

  client.container().from("rust").publish("somewhere")?;

  // ...
}

// to

async fn main() -> eyre::Result<()> {
  // ...

  client.container().from("rust").publish("somewhere").await?;

  // ...
}
```
  • Loading branch information
kjuulh committed Feb 19, 2023
1 parent c35c104 commit 9be6f43
Show file tree
Hide file tree
Showing 12 changed files with 778 additions and 395 deletions.
52 changes: 47 additions & 5 deletions crates/dagger-codegen/src/rust/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::functions::{
};
use crate::utility::OptionExt;

use super::templates::object_tmpl::render_optional_field_args;

pub fn format_name(s: &str) -> String {
s.to_case(Case::Pascal)
}
Expand All @@ -29,10 +31,33 @@ pub fn field_options_struct_name(field: &FullTypeFields) -> Option<String> {
}

pub fn format_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
let is_async = field.type_.pipe(|t| &t.type_ref).pipe(|t| {
if type_ref_is_object(&t) || type_ref_is_list_of_objects(&t) {
return None;
} else {
return Some(quote! {
async
});
};
});

let signature = quote! {
pub fn $(field.name.pipe(|n | format_struct_name(n)))
pub $(is_async) fn $(field.name.pipe(|n | format_struct_name(n)))
};
let args = format_function_args(funcs, field);

let lifecycle = format_optional_args(funcs, field)
.pipe(|(_, contains_lifecycle)| contains_lifecycle)
.and_then(|c| {
if *c {
Some(quote! {
<'a>
})
} else {
None
}
});

let args = format_function_args(funcs, field, lifecycle.as_ref());

let output_type = field
.type_
Expand All @@ -52,7 +77,7 @@ pub fn format_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Optio
$(render_execution(funcs, field))
}

$(&signature)_opts(
$(&signature)_opts$(lifecycle)(
$args
) -> $(output_type) {
let mut query = self.selection.select($(quoted(field.name.as_ref())));
Expand Down Expand Up @@ -235,13 +260,14 @@ fn render_execution(funcs: &CommonFunctions, field: &FullTypeFields) -> rust::To
let graphql_client = rust::import("crate::client", "graphql_client");

quote! {
query.execute(&$graphql_client(&self.conn))
query.execute(&$graphql_client(&self.conn)).await
}
}

fn format_function_args(
funcs: &CommonFunctions,
field: &FullTypeFields,
lifecycle: Option<&rust::Tokens>,
) -> Option<(rust::Tokens, bool)> {
if let Some(args) = field.args.as_ref() {
let args = args
Expand Down Expand Up @@ -271,7 +297,7 @@ fn format_function_args(
Some((
quote! {
$(required_args)
opts: $(field_options_struct_name(field))
opts: $(field_options_struct_name(field))$(lifecycle)
},
true,
))
Expand Down Expand Up @@ -316,3 +342,19 @@ fn format_required_function_args(
None
}
}

pub fn format_optional_args(
funcs: &CommonFunctions,
field: &FullTypeFields,
) -> Option<(rust::Tokens, bool)> {
field
.args
.pipe(|t| t.into_iter().flatten().collect::<Vec<_>>())
.map(|t| {
t.into_iter()
.filter(|t| type_ref_is_optional(&t.input_value.type_))
.collect::<Vec<_>>()
})
.pipe(|t| render_optional_field_args(funcs, t))
.flatten()
}
16 changes: 4 additions & 12 deletions crates/dagger-codegen/src/rust/templates/object_tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use itertools::Itertools;

use crate::functions::{type_ref_is_optional, CommonFunctions};
use crate::rust::functions::{
field_options_struct_name, format_function, format_name, format_struct_name,
field_options_struct_name, format_function, format_name, format_optional_args,
format_struct_name,
};
use crate::utility::OptionExt;

Expand Down Expand Up @@ -51,16 +52,7 @@ fn render_optional_args(

fn render_optional_arg(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
let output_type = field_options_struct_name(field);
let fields = field
.args
.pipe(|t| t.into_iter().flatten().collect::<Vec<_>>())
.map(|t| {
t.into_iter()
.filter(|t| type_ref_is_optional(&t.input_value.type_))
.collect::<Vec<_>>()
})
.pipe(|t| render_optional_field_args(funcs, t))
.flatten();
let fields = format_optional_args(funcs, field);

let builder = rust::import("derive_builder", "Builder");
let _phantom_data = rust::import("std::marker", "PhantomData");
Expand All @@ -79,7 +71,7 @@ fn render_optional_arg(funcs: &CommonFunctions, field: &FullTypeFields) -> Optio
}
}

fn render_optional_field_args(
pub fn render_optional_field_args(
funcs: &CommonFunctions,
args: &Vec<&FullTypeFieldsArgs>,
) -> Option<(rust::Tokens, bool)> {
Expand Down
7 changes: 4 additions & 3 deletions crates/dagger-sdk/examples/build-the-application/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dagger_sdk::HostDirectoryOpts;

fn main() -> eyre::Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect()?;

let host_source_dir = client.host().directory_opts(
Expand All @@ -14,7 +15,7 @@ fn main() -> eyre::Result<()> {
let source = client
.container()
.from("node:16")
.with_mounted_directory("/src", host_source_dir.id()?);
.with_mounted_directory("/src", host_source_dir.id().await?);

let runner = source
.with_workdir("/src")
Expand All @@ -28,7 +29,7 @@ fn main() -> eyre::Result<()> {

let _ = build_dir.export("./build");

let entries = build_dir.entries();
let entries = build_dir.entries().await;

println!("build dir contents: \n {:?}", entries);

Expand Down
11 changes: 6 additions & 5 deletions crates/dagger-sdk/examples/caching/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rand::Rng;

fn main() -> eyre::Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect()?;

let host_source_dir = client.host().directory_opts(
Expand All @@ -10,12 +11,12 @@ fn main() -> eyre::Result<()> {
.build()?,
);

let node_cache = client.cache_volume("node").id()?;
let node_cache = client.cache_volume("node").id().await?;

let source = client
.container()
.from("node:16")
.with_mounted_directory("/src", host_source_dir.id()?)
.with_mounted_directory("/src", host_source_dir.id().await?)
.with_mounted_cache("/src/node_modules", node_cache);

let runner = source
Expand All @@ -33,8 +34,8 @@ fn main() -> eyre::Result<()> {
let ref_ = client
.container()
.from("nginx")
.with_directory("/usr/share/nginx/html", build_dir.id()?)
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>()))?;
.with_directory("/usr/share/nginx/html", build_dir.id().await?)
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>())).await?;

println!("published image to: {}", ref_);

Expand Down
7 changes: 4 additions & 3 deletions crates/dagger-sdk/examples/existing-dockerfile/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rand::Rng;

fn main() -> eyre::Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
let mut rng = rand::thread_rng();

let client = dagger_sdk::connect()?;
Expand All @@ -11,8 +12,8 @@ fn main() -> eyre::Result<()> {

let ref_ = client
.container()
.build(context_dir.id()?)
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>()))?;
.build(context_dir.id().await?)
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>())).await?;

println!("published image to: {}", ref_);

Expand Down
7 changes: 4 additions & 3 deletions crates/dagger-sdk/examples/first-pipeline/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
fn main() -> eyre::Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect()?;

let version = client
.container()
.from("golang:1.19")
.with_exec(vec!["go", "version".into()])
.stdout()?;
.with_exec(vec!["go", "version"])
.stdout().await?;

println!("Hello from Dagger and {}", version.trim());

Expand Down
10 changes: 6 additions & 4 deletions crates/dagger-sdk/examples/multi-stage-build/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use dagger_sdk::HostDirectoryOpts;
use rand::Rng;

fn main() -> eyre::Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect()?;

let host_source_dir = client.host().directory_opts(
Expand All @@ -15,7 +16,7 @@ fn main() -> eyre::Result<()> {
let source = client
.container()
.from("node:16")
.with_mounted_directory("/src", host_source_dir.id()?);
.with_mounted_directory("/src", host_source_dir.id().await?);

let runner = source
.with_workdir("/src")
Expand All @@ -32,8 +33,9 @@ fn main() -> eyre::Result<()> {
let ref_ = client
.container()
.from("nginx")
.with_directory("/usr/share/nginx/html", build_dir.id()?)
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>()))?;
.with_directory("/usr/share/nginx/html", build_dir.id().await?)
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>()))
.await?;

println!("published image to: {}", ref_);

Expand Down
9 changes: 5 additions & 4 deletions crates/dagger-sdk/examples/publish-the-application/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use dagger_sdk::HostDirectoryOpts;
use rand::Rng;

fn main() -> eyre::Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect()?;
let output = "examples/publish-the-application/app/build";

Expand All @@ -16,7 +17,7 @@ fn main() -> eyre::Result<()> {
let source = client
.container()
.from("node:16")
.with_mounted_directory("/src", host_source_dir.id()?);
.with_mounted_directory("/src", host_source_dir.id().await?);

let runner = source
.with_workdir("/src")
Expand All @@ -36,9 +37,9 @@ fn main() -> eyre::Result<()> {
.from("nginx")
.with_directory(
"/usr/share/nginx/html",
client.host().directory(output).id()?,
client.host().directory(output).id().await?,
)
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>()))?;
.publish(format!("ttl.sh/hello-dagger-rs-{}:1h", rng.gen::<u64>())).await?;

println!("published image to: {}", ref_);

Expand Down
7 changes: 4 additions & 3 deletions crates/dagger-sdk/examples/test-the-application/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dagger_sdk::HostDirectoryOpts;

fn main() -> eyre::Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect()?;

let host_source_dir = client.host().directory_opts(
Expand All @@ -14,15 +15,15 @@ fn main() -> eyre::Result<()> {
let source = client
.container()
.from("node:16")
.with_mounted_directory("/src", host_source_dir.id()?);
.with_mounted_directory("/src", host_source_dir.id().await?);

let runner = source
.with_workdir("/src")
.with_exec(vec!["npm", "install"]);

let out = runner
.with_exec(vec!["npm", "test", "--", "--watchAll=false"])
.stderr()?;
.stderr().await?;

println!("{}", out);

Expand Down
Loading

0 comments on commit 9be6f43

Please sign in to comment.