-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bevy build #102
Merged
Merged
Bevy build #102
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d27cb55
Add implementation of bevy build command
TimJentzsch 1a567b8
Integrate bevy build into CLI
TimJentzsch 64ee482
Enable parse feature for Cargo.toml parsing
TimJentzsch e9bb8b6
Use qualified path instead of import for clarity
TimJentzsch 6df9563
Add helper type to ensure that a command exits successfully
TimJentzsch 9a2e094
Merge branch 'main' into bevy-build
TimJentzsch d5e55ed
Suppress dead code warnings for metadata command
TimJentzsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this lint wasn't emitted before...