From b62d946f8324edc1294638b05ba5290226a2a91e Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Wed, 9 Jan 2019 11:23:00 -0800 Subject: [PATCH] Stub out new 'init' command --- server/src/bin.rs | 18 ++++++++++--- server/src/commands/init.rs | 54 ++++++++++++++++++++++++++++--------- server/src/project.rs | 6 ++++- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/server/src/bin.rs b/server/src/bin.rs index db0900b5..e6f43df3 100644 --- a/server/src/bin.rs +++ b/server/src/bin.rs @@ -32,6 +32,7 @@ fn main() { (@subcommand init => (about: "Creates a new Rojo project") (@arg PATH: "Path to the place to create the project. Defaults to the current directory.") + (@arg kind: --kind +takes_value "The kind of project to create, 'place' or 'model'. Defaults to place.") ) (@subcommand serve => @@ -59,10 +60,21 @@ fn main() { match matches.subcommand() { ("init", Some(sub_matches)) => { - let project_path = Path::new(sub_matches.value_of("PATH").unwrap_or(".")); - let full_path = make_path_absolute(project_path); + let fuzzy_project_path = make_path_absolute(Path::new(sub_matches.value_of("PATH").unwrap_or("."))); + let kind = sub_matches.value_of("kind"); - commands::init(&full_path); + let options = commands::InitOptions { + fuzzy_project_path, + kind, + }; + + match commands::init(&options) { + Ok(_) => {}, + Err(e) => { + error!("{}", e); + process::exit(1); + }, + } }, ("serve", Some(sub_matches)) => { let fuzzy_project_path = match sub_matches.value_of("PROJECT") { diff --git a/server/src/commands/init.rs b/server/src/commands/init.rs index 186d531c..a764ccb0 100644 --- a/server/src/commands/init.rs +++ b/server/src/commands/init.rs @@ -1,16 +1,46 @@ -use std::path::PathBuf; -use std::process; +use std::{ + path::PathBuf, +}; -use crate::project::Project; +use failure::Fail; -pub fn init(project_path: &PathBuf) { - match Project::init(project_path) { - Ok(_) => { - println!("Created new empty project at {}", project_path.display()); - }, - Err(e) => { - error!("Failed to create new project.\n{}", e); - process::exit(1); - }, +use crate::project::{Project, ProjectInitError}; + +#[derive(Debug, Fail)] +pub enum InitError { + #[fail(display = "Invalid project kind '{}', valid kinds are 'place' and 'model'", _0)] + InvalidKind(String), + + #[fail(display = "Project init error: {}", _0)] + ProjectInitError(#[fail(cause)] ProjectInitError) +} + +impl From for InitError { + fn from(error: ProjectInitError) -> InitError { + InitError::ProjectInitError(error) } +} + +#[derive(Debug)] +pub struct InitOptions<'a> { + pub fuzzy_project_path: PathBuf, + pub kind: Option<&'a str>, +} + +pub fn init(options: &InitOptions) -> Result<(), InitError> { + let (project_path, project_kind) = match options.kind { + Some("place") | None => { + let path = Project::init_place(&options.fuzzy_project_path)?; + (path, "place") + }, + Some("model") => { + let path = Project::init_model(&options.fuzzy_project_path)?; + (path, "model") + }, + Some(invalid) => return Err(InitError::InvalidKind(invalid.to_string())), + }; + + println!("Created new {} project at {}", project_kind, project_path.display()); + + Ok(()) } \ No newline at end of file diff --git a/server/src/project.rs b/server/src/project.rs index 61cce205..46c4d7be 100644 --- a/server/src/project.rs +++ b/server/src/project.rs @@ -180,7 +180,11 @@ pub struct Project { } impl Project { - pub fn init(_project_folder_location: &Path) -> Result<(), ProjectInitError> { + pub fn init_place(_project_fuzzy_location: &Path) -> Result { + unimplemented!(); + } + + pub fn init_model(_project_fuzzy_location: &Path) -> Result { unimplemented!(); }