From b4afb88a2e172a9453585d8b756172e0f3e456f0 Mon Sep 17 00:00:00 2001 From: databasedav <31483365+databasedav@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:09:37 -0800 Subject: [PATCH] add support for building examples --- src/cmd/build.rs | 6 ++++++ src/config/models/build.rs | 6 ++++++ src/config/rt/build.rs | 4 ++++ src/pipelines/rust/mod.rs | 17 +++++++++++++++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index f47c24d3..a2f6595e 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -82,6 +82,10 @@ pub struct Build { #[arg(default_missing_value="true", num_args=0..=1)] pub filehash: Option, + /// Which example to build + #[arg(long, env = "TRUNK_BUILD_EXAMPLE")] + pub example: Option, + /// When desired, set a custom root certificate chain (same format as Cargo's config.toml http.cainfo) #[arg(long, env = "TRUNK_BUILD_ROOT_CERTIFICATE")] pub root_certificate: Option, @@ -140,6 +144,7 @@ impl Build { all_features, features, filehash, + example, root_certificate, accept_invalid_certs, minify, @@ -166,6 +171,7 @@ impl Build { config.build.features = features.unwrap_or(config.build.features); config.build.filehash = filehash.unwrap_or(config.build.filehash); + config.build.example = example.or(config.build.example); config.build.root_certificate = root_certificate.or(config.build.root_certificate); config.build.accept_invalid_certs = diff --git a/src/config/models/build.rs b/src/config/models/build.rs index 603bc05c..e508d8fe 100644 --- a/src/config/models/build.rs +++ b/src/config/models/build.rs @@ -76,6 +76,11 @@ pub struct Build { #[serde(default = "default::filehash")] pub filehash: bool, + /// Whether to build an example. + #[serde(default)] + #[serde(skip_serializing_if = "Option::is_none")] + pub example: Option, + /// Optional pattern for the app loader script [default: None] /// /// Patterns should include the sequences `{base}`, `{wasm}`, and `{js}` in order to @@ -214,6 +219,7 @@ impl Default for Build { no_default_features: false, all_features: false, features: vec![], + example: None, filehash: default::filehash(), pattern_script: None, inject_scripts: default::inject_scripts(), diff --git a/src/config/rt/build.rs b/src/config/rt/build.rs index 133dd657..91096b51 100644 --- a/src/config/rt/build.rs +++ b/src/config/rt/build.rs @@ -56,6 +56,8 @@ pub struct RtcBuild { pub staging_dist: PathBuf, /// The configuration of the features passed to cargo. pub cargo_features: Features, + /// Optional example to be passed to cargo. + pub cargo_example: Option, /// Configuration for automatic application download. pub tools: Tools, /// Build process hooks. @@ -202,6 +204,7 @@ impl RtcBuild { staging_dist, final_dist, cargo_features, + cargo_example: build.example, tools, hooks, inject_autoloader, @@ -246,6 +249,7 @@ impl RtcBuild { final_dist, staging_dist, cargo_features: Features::All, + cargo_example: None, tools: Default::default(), hooks: Vec::new(), inject_autoloader: true, diff --git a/src/pipelines/rust/mod.rs b/src/pipelines/rust/mod.rs index 92f09a05..68af6bc2 100644 --- a/src/pipelines/rust/mod.rs +++ b/src/pipelines/rust/mod.rs @@ -420,6 +420,10 @@ impl RustApp { args.push("--bin"); args.push(bin); } + if let Some(example) = &self.cfg.cargo_example { + args.push("--example"); + args.push(example); + } match &self.cargo_features { Features::All => args.push("--all-features"), @@ -818,13 +822,22 @@ impl RustApp { return false; } - // must be cdylib or bin + // must be cdylib, bin, or example if !(art.target.kind.contains(&"bin".to_string()) - || art.target.kind.contains(&"cdylib".to_string())) + || art.target.kind.contains(&"cdylib".to_string()) + || art.target.kind.contains(&"example".to_string())) { return false; } + // Are we building an example? + if let Some(example) = &self.cfg.cargo_example { + // it must match + if example != &art.target.name { + return false; + } + } + // if we have the --bin argument if let Some(bin) = &self.bin { // it must match