-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds the remaining pieces needed for the `bevy build` command. The command is a wrapper around `cargo build`, but with an additional `web` sub command which makes it a lot easier to build an app for the browser. I also plan to add additional features in the feature, like #68. The PR contains the following changes: - Added `cargo install` wrapper to install a program if it's missing (prompting the user). - Added helper to obtain the current package name via `Cargo.toml`. There are likely better ways to do this (perhaps via `cargo metadata`), but I already had this implemented and we can improve it in a follow-up if needed. - Added helper to determine the active compilation profile, which is needed to determine the path to the build artifact. - Implemented the build command itself.
- Loading branch information
1 parent
12afdf9
commit 5cd2719
Showing
13 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use clap::{Args, Subcommand}; | ||
|
||
use crate::external_cli::{arg_builder::ArgBuilder, cargo::build::CargoBuildArgs}; | ||
|
||
#[derive(Debug, Args)] | ||
pub struct BuildArgs { | ||
/// The subcommands available for the build command. | ||
#[clap(subcommand)] | ||
pub subcommand: Option<BuildSubcommands>, | ||
|
||
/// Arguments to forward to `cargo build`. | ||
#[clap(flatten)] | ||
pub cargo_args: CargoBuildArgs, | ||
} | ||
|
||
impl BuildArgs { | ||
/// Determine if the app is being built for the web. | ||
pub(crate) fn is_web(&self) -> bool { | ||
matches!(self.subcommand, Some(BuildSubcommands::Web)) | ||
} | ||
|
||
/// The profile used to compile the app. | ||
pub(crate) fn profile(&self) -> &str { | ||
self.cargo_args.compilation_args.profile() | ||
} | ||
|
||
/// Generate arguments to forward to `cargo build`. | ||
pub(crate) fn cargo_args_builder(&self) -> ArgBuilder { | ||
self.cargo_args.args_builder(self.is_web()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum BuildSubcommands { | ||
/// Build your app for the browser. | ||
Web, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::{ | ||
external_cli::{cargo, rustup, wasm_bindgen, CommandHelpers}, | ||
manifest::package_name, | ||
}; | ||
|
||
pub use self::args::BuildArgs; | ||
|
||
mod args; | ||
|
||
pub fn build(args: &BuildArgs) -> anyhow::Result<()> { | ||
let cargo_args = args.cargo_args_builder(); | ||
|
||
if args.is_web() { | ||
ensure_web_setup()?; | ||
|
||
println!("Building for WASM..."); | ||
cargo::build::command().args(cargo_args).ensure_status()?; | ||
|
||
println!("Bundling for the web..."); | ||
wasm_bindgen::bundle(&package_name()?, args.profile()) | ||
.expect("Failed to bundle for the web"); | ||
} else { | ||
cargo::build::command().args(cargo_args).ensure_status()?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn ensure_web_setup() -> anyhow::Result<()> { | ||
// `wasm32-unknown-unknown` compilation target | ||
rustup::install_target_if_needed("wasm32-unknown-unknown")?; | ||
// `wasm-bindgen-cli` for bundling | ||
cargo::install::if_needed(wasm_bindgen::PROGRAM, wasm_bindgen::PACKAGE, true, false)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::process::{exit, Command}; | ||
|
||
use dialoguer::Confirm; | ||
|
||
/// Check if the given program is installed on the system. | ||
/// | ||
/// This assumes that the program offers a `--version` flag. | ||
fn is_installed(program: &str) -> bool { | ||
let output = Command::new(program).arg("--version").output(); | ||
|
||
if let Ok(output) = output { | ||
output.status.success() | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/// Checks if the program is installed and installs it if it isn't. | ||
/// | ||
/// Returns `true` if the program needed to be installed. | ||
pub(crate) fn if_needed( | ||
program: &str, | ||
package: &str, | ||
ask_user: bool, | ||
hidden: bool, | ||
) -> anyhow::Result<bool> { | ||
if is_installed(program) { | ||
return Ok(false); | ||
} | ||
|
||
// Abort if the user doesn't want to install it | ||
if ask_user | ||
&& !Confirm::new() | ||
.with_prompt(format!( | ||
"`{program}` is missing, should I install it for you?" | ||
)) | ||
.interact()? | ||
{ | ||
exit(1); | ||
} | ||
|
||
let mut cmd = Command::new(super::program()); | ||
cmd.arg("install").arg(package); | ||
|
||
let status = if hidden { | ||
cmd.output()?.status | ||
} else { | ||
cmd.status()? | ||
}; | ||
|
||
if !status.success() { | ||
Err(anyhow::anyhow!("Failed to install `{program}`.")) | ||
} else { | ||
Ok(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
//! Wrappers and utilities to deal with external CLI applications, like `cargo`. | ||
|
||
use std::process::{Command, ExitStatus}; | ||
|
||
pub mod arg_builder; | ||
pub(crate) mod cargo; | ||
pub(crate) mod rustup; | ||
pub(crate) mod wasm_bindgen; | ||
|
||
pub trait CommandHelpers { | ||
fn ensure_status(&mut self) -> anyhow::Result<ExitStatus>; | ||
} | ||
|
||
impl CommandHelpers for Command { | ||
/// Ensure that the command exits with a successful status code. | ||
fn ensure_status(&mut self) -> anyhow::Result<ExitStatus> { | ||
let status = self.status()?; | ||
anyhow::ensure!( | ||
status.success(), | ||
"Command {} exited with status code {}", | ||
self.get_program().to_str().unwrap_or_default(), | ||
status | ||
); | ||
Ok(status) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
//! The library backend for the Bevy CLI. | ||
|
||
pub mod build; | ||
pub mod external_cli; | ||
pub mod lint; | ||
pub mod manifest; | ||
pub mod template; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::{fs::File, io::Read as _}; | ||
|
||
use toml_edit::{DocumentMut, Item, Value}; | ||
|
||
/// Get the contents of the manifest file. | ||
fn get_cargo_toml(folder_name: &str) -> anyhow::Result<DocumentMut> { | ||
let mut file = File::open(format!("{folder_name}/Cargo.toml"))?; | ||
|
||
let mut content = String::new(); | ||
file.read_to_string(&mut content)?; | ||
|
||
Ok(content.parse()?) | ||
} | ||
|
||
/// Determine the name of the cargo package. | ||
pub(crate) fn package_name() -> anyhow::Result<String> { | ||
let cargo_toml = get_cargo_toml("./")?; | ||
|
||
if let Item::Value(Value::String(name)) = &cargo_toml["package"]["name"] { | ||
Ok(name.value().clone()) | ||
} else { | ||
Err(anyhow::anyhow!("No package name defined in Cargo.toml")) | ||
} | ||
} |