diff --git a/Cargo.toml b/Cargo.toml index 31802ad2..7d76d19c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,18 +6,28 @@ name = "bevy_cli" version = "0.1.0-dev" edition = "2021" license = "MIT OR Apache-2.0" +# When no binary is specific, run the main CLI by default. default-run = "bevy" +# The main CLI executable [[bin]] name = "bevy" -path = "src/main.rs" +path = "src/bin/main.rs" +# Integrates custom lints with `rustc` [[bin]] name = "bevy_lint_driver" -path = "src/lint_driver.rs" +path = "src/bin/lint_driver.rs" [dependencies] +# CLI argument parsing +clap = { version = "4.5.16", features = ["derive"] } + +# Easy error propagation and contexts anyhow = "1.0.86" -bevy_lint = { version = "0.1.0-dev", path = "./bevy_lint" } + +# Generates new Bevy projects from templates cargo-generate = "0.21.3" -clap = { version = "4.5.16", features = ["derive"] } + +# Bevy-specific lints +bevy_lint = { version = "0.1.0-dev", path = "./bevy_lint" } diff --git a/src/lint_driver.rs b/src/bin/lint_driver.rs similarity index 100% rename from src/lint_driver.rs rename to src/bin/lint_driver.rs diff --git a/src/args.rs b/src/bin/main.rs similarity index 68% rename from src/args.rs rename to src/bin/main.rs index 3faedd49..59afd6ce 100644 --- a/src/args.rs +++ b/src/bin/main.rs @@ -1,6 +1,19 @@ -use cargo_generate::TemplatePath; +use anyhow::Result; use clap::{Args, Parser, Subcommand}; +fn main() -> Result<()> { + let cli = Cli::parse(); + + match cli.subcommand { + Subcommands::New(new) => { + bevy_cli::template::generate_template(&new.name, new.template.as_deref())?; + } + Subcommands::Lint => bevy_cli::lint::lint()?, + } + + Ok(()) +} + /// Command-line interface for the Bevy Game Engine /// /// This CLI provides tools for Bevy project management, @@ -41,21 +54,3 @@ pub struct NewArgs { #[arg(short, long)] pub template: Option, } - -impl NewArgs { - /// The path to the template to use for generating the project. - pub fn template_path(&self) -> TemplatePath { - if let Some(template) = &self.template { - TemplatePath { - git: Some(template.clone()), - ..Default::default() - } - } else { - TemplatePath { - git: Some("https://github.com/TheBevyFlock/bevy_quickstart.git".to_string()), - branch: Some("cargo-generate".to_string()), - ..Default::default() - } - } - } -} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..bd6a5456 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,4 @@ +//! The library backend for the Bevy CLI. + +pub mod lint; +pub mod template; diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index a1e5f2dc..00000000 --- a/src/main.rs +++ /dev/null @@ -1,25 +0,0 @@ -use anyhow::Result; -use args::{Cli, Subcommands}; -use cargo_generate::GenerateArgs; -use clap::Parser as _; - -mod args; -mod lint; - -fn main() -> Result<()> { - let cli = Cli::parse(); - - match cli.subcommand { - Subcommands::New(new) => { - cargo_generate::generate(GenerateArgs { - template_path: new.template_path(), - name: Some(new.name), - force: true, // prevent conversion to kebab-case - ..Default::default() - })?; - } - Subcommands::Lint => lint::lint()?, - } - - Ok(()) -} diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 00000000..22d6533d --- /dev/null +++ b/src/template.rs @@ -0,0 +1,38 @@ +use cargo_generate::{GenerateArgs, TemplatePath}; +use std::path::PathBuf; + +/// Generates a new template to the returned [`PathBuf`] using the given name and Git repository. +/// +/// If `git` is [`None`], it will default to [TheBevyFlock/bevy_quickstart]. +/// +/// [TheBevyFlock/bevy_quickstart]: https://github.com/TheBevyFlock/bevy_quickstart +pub fn generate_template(name: &str, git: Option<&str>) -> anyhow::Result { + cargo_generate::generate(GenerateArgs { + template_path: template_path(git), + name: Some(name.to_string()), + // prevent conversion to kebab-case + force: true, + ..Default::default() + }) +} + +/// Returns the [`TemplatePath`] for a given Git repository. +/// +/// If `git` is [`None`], it will default to `bevy_quickstart`. +fn template_path(git: Option<&str>) -> TemplatePath { + const DEFAULT_REPOSITORY: &str = "https://github.com/TheBevyFlock/bevy_quickstart.git"; + const DEFAULT_BRANCH: &str = "cargo-generate"; + + if let Some(template) = git { + TemplatePath { + git: Some(template.to_string()), + ..Default::default() + } + } else { + TemplatePath { + git: Some(DEFAULT_REPOSITORY.to_string()), + branch: Some(DEFAULT_BRANCH.to_string()), + ..Default::default() + } + } +}