Skip to content

Commit

Permalink
add support for building examples
Browse files Browse the repository at this point in the history
  • Loading branch information
databasedav authored and ctron committed Jan 13, 2025
1 parent dfab745 commit c5ead2c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ pub struct Build {
#[arg(default_missing_value="true", num_args=0..=1)]
pub filehash: Option<bool>,

/// Which example to build
#[arg(long, env = "TRUNK_BUILD_EXAMPLE")]
pub example: Option<String>,

/// 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<String>,
Expand Down Expand Up @@ -140,6 +144,7 @@ impl Build {
all_features,
features,
filehash,
example,
root_certificate,
accept_invalid_certs,
minify,
Expand All @@ -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 =
Expand Down
6 changes: 6 additions & 0 deletions src/config/models/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Optional pattern for the app loader script [default: None]
///
/// Patterns should include the sequences `{base}`, `{wasm}`, and `{js}` in order to
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions src/config/rt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Configuration for automatic application download.
pub tools: Tools,
/// Build process hooks.
Expand Down Expand Up @@ -202,6 +204,7 @@ impl RtcBuild {
staging_dist,
final_dist,
cargo_features,
cargo_example: build.example,
tools,
hooks,
inject_autoloader,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c5ead2c

Please sign in to comment.