1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::error::Result;
use clap::Parser;
use crossbundle_tools::{
    commands::{check_cargo_generate, create_project},
    utils::Config,
};

const TEMPLATES_REPO: &str = "https://github.com/dodorare/crossbundle-templates.git";

#[derive(Parser, Clone, Debug)]
pub struct NewCommand {
    /// Directory to create / project name; if the name isn't in kebab-case, it will be converted
    /// to kebab-case unless `--force` is given.
    pub name: String,
    /// Name of the template to create.
    #[clap(long, short)]
    pub template: Option<String>,
    /// Don't convert the project name to kebab-case before creating the directory.
    /// Note that cargo generate won't overwrite an existing directory, even if `--force` is given.
    #[clap(long, short)]
    pub force: bool,
}

impl NewCommand {
    pub fn handle_command(&self, config: &Config) -> Result<()> {
        if !check_cargo_generate() {
            config
                .shell()
                .warn("To use `crossbundle new ...` command you need to install `cargo-generate`\n         run `cargo install cargo-generate`")?;
            return Ok(());
        };
        create_project(
            config.current_dir(),
            &self.name,
            TEMPLATES_REPO,
            &self.template,
        )?;
        Ok(())
    }
}