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
use crate::error::*;
use std::path::Path;
use std::process::Command;

/// Creates a new project.
/// Runs `cargo generate ...` with given args.
pub fn create_project(
    current_dir: &Path,
    name: &str,
    git: &str,
    branch: &Option<String>,
) -> Result<()> {
    let mut cargo_generate = Command::new("cargo");
    cargo_generate
        .current_dir(current_dir)
        .arg("generate")
        .arg("--git")
        .arg(git)
        .arg("--name")
        .arg(name);
    if let Some(branch) = branch {
        cargo_generate.arg("--branch").arg(branch);
    };
    cargo_generate.output_err(true)?;
    Ok(())
}

/// Checks if `cargo-generate` is installed in the system.
pub fn check_cargo_generate() -> bool {
    Command::new("cargo")
        .arg("generate")
        .arg("-V")
        .output()
        .map(|s| s.status.success())
        .unwrap_or(false)
}