amethyst_cli/
new.rs

1use std::{
2    fs::{create_dir, remove_dir_all},
3    path::Path,
4    process::Command,
5};
6
7use crate::{
8    error::{ErrorKind, Result, ResultExt},
9    templates,
10};
11
12/// Options for the New subcommand. If `version` is None, then it uses
13/// the latest version available
14#[derive(Clone, Debug)]
15pub struct New {
16    pub project_name: String,
17    pub version: Option<String>,
18    pub no_defaults: bool,
19}
20
21impl New {
22    /// Creates the project template.
23    ///
24    /// # Errors
25    ///
26    /// Fails if the project directory already exists, or when failing to create the directory.
27    pub fn execute(&self) -> Result<()> {
28        self.execute_inner()
29            .chain_err(|| ErrorKind::New(self.project_name.clone()))
30    }
31
32    fn execute_inner(&self) -> Result<()> {
33        let path = Path::new(&self.project_name);
34        if path.exists() {
35            bail!("project directory {:?} already exists", path);
36        }
37        create_dir(path).chain_err(|| "could not create project folder")?;
38
39        let mut params = templates::Parameters::new();
40        params.insert(
41            "project_name".into(),
42            templates::Value::scalar(self.project_name.to_owned()),
43        );
44
45        if let Err(err) = templates::deploy("main", &self.version, self.no_defaults, path, &params)
46        {
47            remove_dir_all(path).chain_err(|| "could not clean up project folder")?;
48            Err(err)
49        } else {
50            Command::new("git")
51                .arg("init")
52                .current_dir(path)
53                .spawn()?
54                .try_wait()?;
55            Ok(())
56        }
57    }
58}
59
60impl Default for New {
61    #[must_use]
62    fn default() -> Self {
63        Self {
64            project_name: "game".to_owned(),
65            version: None,
66            no_defaults: false,
67        }
68    }
69}