Skip to content

Commit

Permalink
Merge pull request #2729 from itowlson/build-commands-plural
Browse files Browse the repository at this point in the history
Allow multiple commands in a component build section
  • Loading branch information
itowlson authored Aug 21, 2024
2 parents e0e39a9 + 7b6c5c8 commit aa5d74c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
61 changes: 29 additions & 32 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,36 @@ fn build_components(
fn build_component(build_info: ComponentBuildInfo, app_dir: &Path) -> Result<()> {
match build_info.build {
Some(b) => {
terminal::step!(
"Building",
"component {} with `{}`",
build_info.id,
b.command
);
let workdir = construct_workdir(app_dir, b.workdir.as_ref())?;
if b.workdir.is_some() {
println!("Working directory: {}", quoted_path(&workdir));
}

let exit_status = Exec::shell(&b.command)
.cwd(workdir)
.stdout(Redirection::None)
.stderr(Redirection::None)
.stdin(Redirection::None)
.popen()
.map_err(|err| {
anyhow!(
"Cannot spawn build process '{:?}' for component {}: {}",
&b.command,
for command in b.commands() {
terminal::step!("Building", "component {} with `{}`", build_info.id, command);
let workdir = construct_workdir(app_dir, b.workdir.as_ref())?;
if b.workdir.is_some() {
println!("Working directory: {}", quoted_path(&workdir));
}

let exit_status = Exec::shell(command)
.cwd(workdir)
.stdout(Redirection::None)
.stderr(Redirection::None)
.stdin(Redirection::None)
.popen()
.map_err(|err| {
anyhow!(
"Cannot spawn build process '{:?}' for component {}: {}",
&b.command,
build_info.id,
err
)
})?
.wait()?;

if !exit_status.success() {
bail!(
"Build command for component {} failed with status {:?}",
build_info.id,
err
)
})?
.wait()?;

if !exit_status.success() {
bail!(
"Build command for component {} failed with status {:?}",
build_info.id,
exit_status,
);
exit_status,
);
}
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/doctor/src/rustlang/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Diagnostic for TargetDiagnostic {
let uses_rust = manifest.components.values().any(|c| {
c.build
.as_ref()
.map(|b| b.command.starts_with("cargo"))
.map(|b| b.commands().any(|c| c.starts_with("cargo")))
.unwrap_or_default()
});

Expand Down
23 changes: 22 additions & 1 deletion crates/manifest/src/schema/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub enum WasiFilesMount {
#[serde(deny_unknown_fields)]
pub struct ComponentBuildConfig {
/// `command = "cargo build"`
pub command: String,
pub command: Commands,
/// `workdir = "components/main"
#[serde(default, skip_serializing_if = "Option::is_none")]
pub workdir: Option<String>,
Expand All @@ -92,6 +92,27 @@ pub struct ComponentBuildConfig {
pub watch: Vec<String>,
}

impl ComponentBuildConfig {
/// The commands to execute for the build
pub fn commands(&self) -> impl Iterator<Item = &String> {
let as_vec = match &self.command {
Commands::Single(cmd) => vec![cmd],
Commands::Multiple(cmds) => cmds.iter().collect(),
};
as_vec.into_iter()
}
}

/// Component build command or commands
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Commands {
/// `command = "cargo build"`
Single(String),
/// `command = ["cargo build", "wac encode compose-deps.wac -d my:pkg=app.wasm --registry fermyon.com"]`
Multiple(Vec<String>),
}

fn is_false(v: &bool) -> bool {
!*v
}

0 comments on commit aa5d74c

Please sign in to comment.