Skip to content

Commit

Permalink
Separate backend and CLI (#25)
Browse files Browse the repository at this point in the history
Closes #21.

This PR implements the advise given by @sixfold-origami on Discord. It
makes `bevy_cli` is proper library, moving all `clap` logic to
`main.rs`. It also moves `main.rs` and `lint_driver.rs` to the `src/bin`
folder, so they are distinct from library modules.

I also added a few comments to `Cargo.toml` in
7d22ab7 in a drive-by pass.

---------

Co-authored-by: Rose Peck <rosepeck1997@gmail.com>
  • Loading branch information
BD103 and sixfold-origami authored Sep 2, 2024
1 parent f23835f commit 8b718fa
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 48 deletions.
18 changes: 14 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
File renamed without changes.
33 changes: 14 additions & 19 deletions src/args.rs → src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -41,21 +54,3 @@ pub struct NewArgs {
#[arg(short, long)]
pub template: Option<String>,
}

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()
}
}
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! The library backend for the Bevy CLI.
pub mod lint;
pub mod template;
25 changes: 0 additions & 25 deletions src/main.rs

This file was deleted.

38 changes: 38 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> {
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()
}
}
}

0 comments on commit 8b718fa

Please sign in to comment.