From fc99666c48da1653b3097a0251d8219bc49886cb Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Jan 12 2019 19:12:05 +0000 Subject: Add feature: initialize hidden dependencies. --- diff --git a/README.md b/README.md index e1dd235..5b5fa7c 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,5 @@ ## Notes `backend::fetch::update_module()` kan wellicht met `backend::add::update_module()` samengevoegd worden. + +Missing feature in `backend::git::ignore` (see [related file](src/backend/git/ignore.rs)). diff --git a/src/backend/add.rs b/src/backend/add.rs index 61efc0e..2713beb 100644 --- a/src/backend/add.rs +++ b/src/backend/add.rs @@ -33,7 +33,7 @@ pub fn add(args: &Vec) -> Result { } fn update_module(key: String, value: String) -> Result { - let path: PathBuf; + let mut path: PathBuf; let config: serde_json::Value; match super::filesystem::get_current_module_root() { @@ -41,7 +41,9 @@ fn update_module(key: String, value: String) -> Result { None => return Err(String::from("No config file in module found.")) } - match super::config::get_json_from_dir(path.clone()) { + path.push("ambassade.json"); + + match super::config::get_json(&path) { Ok(mut json) => { json["deps"]["linux"][key.clone()] = json!(value); json["deps"]["os-x"][key.clone()] = json!(value); @@ -51,7 +53,7 @@ fn update_module(key: String, value: String) -> Result { Err(e) => return Err(e) } - match super::config::update(path, config) { + match super::config::update(&path, config) { Ok(_) => Ok(String::from("Dependency added!")), Err(e) => Err(e) } diff --git a/src/backend/build.rs b/src/backend/build.rs index d7d87e8..41102be 100644 --- a/src/backend/build.rs +++ b/src/backend/build.rs @@ -2,22 +2,29 @@ extern crate serde_json; use std::result::Result; use std::path::PathBuf; -use std::env; -pub fn build(config_file: PathBuf) -> Result { - let config; +pub fn build(dep_name: String) -> Result { + let dep_path = match super::filesystem::search_current_module_root(dep_name.clone()) { + Ok(path) => path, + Err(e) => return Err(e.to_string()) + }; - match super::config::get_json_from_dir(config_file) { - Ok(result) => config = result, + let config_path = match super::dep_config::scan(dep_name) { + Ok(path) => path, Err(e) => return Err(e) - } + }; + + let config = match super::config::get_json(&config_path) { + Ok(json) => json, + Err(e) => return Err(e) + }; match super::dep::check(config.clone()) { Ok(result) => println!("{}", result), Err(e) => return Err(e) } - match build_module(config) { + match build_module(dep_path, config) { Ok(result) => println!("{}", result), Err(e) => return Err(e) } @@ -45,7 +52,7 @@ pub fn build_rec(config_file: PathBuf) -> Result { } println!("Building current project '{}'..", &dep_tree.name); - match build(dep_tree.path) { + match build(dep_tree.name) { Ok(result) => println!("{}", result), Err(e) => return Err(e) } @@ -54,40 +61,40 @@ pub fn build_rec(config_file: PathBuf) -> Result { } #[cfg(target_os="linux")] -fn build_module(config: serde_json::Value) -> Result { +fn build_module(config_path: PathBuf, config_value: serde_json::Value) -> Result { println!("Building module.."); - let build_cmd = &config["build"]["linux"]; + let build_cmd = &config_value["build"]["linux"]; if !build_cmd.is_string() { return Err(String::from("ambassade.json: 'build->linux' should be a string.")); } - super::fetch::build(env::current_dir().unwrap(), String::from(build_cmd.as_str().unwrap())) + super::fetch::build(config_path, String::from(build_cmd.as_str().unwrap())) } #[cfg(target_os="macos")] -fn build_module(config: serde_json::Value) -> Result { +fn build_module(config_path: PathBuf, config_value: serde_json::Value) -> Result { println!("Building module.."); - let build_cmd = &config["build"]["os-x"]; + let build_cmd = &config_value["build"]["os-x"]; if !build_cmd.is_string() { return Err(String::from("ambassade.json: 'build->os-x' should be a string.")); } - super::fetch::fetch(env::get_current_dir().unwrap(), String::from(build_cmd.as_str().unwrap())) + super::fetch::build(config_path, String::from(build_cmd.as_str().unwrap())) } #[cfg(target_os="windows")] -fn build_module(config: serde_json::Value) -> Result { +fn build_module(config_path: PathBuf, config_value: serde_json::Value) -> Result { println!("Building module.."); - let build_cmd = &config["build"]["windows"]; + let build_cmd = &config_value["build"]["windows"]; if !build_cmd.is_string() { return Err(String::from("ambassade.json: 'build->windows' should be a string.")); } - super::fetch::fetch(env::get_current_dir().unwrap(), String::from(build_cmd.as_str().unwrap())) + super::fetch::build(config_path, String::from(build_cmd.as_str().unwrap())) } diff --git a/src/backend/config.rs b/src/backend/config.rs index 092bee7..d301561 100644 --- a/src/backend/config.rs +++ b/src/backend/config.rs @@ -40,9 +40,7 @@ pub fn init(path: &PathBuf) -> Result<(), Error> { Ok(()) } -pub fn update(mut path: PathBuf, value: serde_json::Value) -> Result<(), String> { - path.push("ambassade.json"); - +pub fn update(path: &PathBuf, value: serde_json::Value) -> Result<(), String> { match File::create(path) { Ok(mut file) => { match file.write_all(serde_json::to_string_pretty(&value).unwrap().as_bytes()) { diff --git a/src/backend/dep_config.rs b/src/backend/dep_config.rs index 081f62e..23b9f70 100644 --- a/src/backend/dep_config.rs +++ b/src/backend/dep_config.rs @@ -31,6 +31,17 @@ pub fn scan(dep_name: String) -> Result { return Ok(config_path); } + match super::filesystem::get_current_project_root() { + Some(mut path) => { + if String::from(path.file_name().unwrap().to_str().unwrap()) == dep_name { + println!("Project name equals dep name. Taking project's configfile..."); + path.push("ambassade.json"); + return Ok(path); + } + }, + None => println!("Couldn't find current project root. Skipped in scan.") + } + match super::filesystem::get_current_dep_root() { Ok(mut path) => { path.push(dep_name); diff --git a/src/backend/fetch.rs b/src/backend/fetch.rs index 9c7fd2a..6ee12ad 100644 --- a/src/backend/fetch.rs +++ b/src/backend/fetch.rs @@ -118,7 +118,7 @@ fn update_module(dep_name: &String, key: String, value: String) -> Result return Err(e) } - match super::config::update(path, config) { + match super::config::update(&path, config) { Ok(_) => Ok(String::from("Command updated!")), Err(e) => Err(e) } diff --git a/src/backend/filesystem.rs b/src/backend/filesystem.rs index 5190c5f..e3e8889 100644 --- a/src/backend/filesystem.rs +++ b/src/backend/filesystem.rs @@ -1,4 +1,4 @@ -use std::{path, env, fs}; +use std::{path, env, fs, result}; use std::io::{Result, Error, ErrorKind}; fn get_root(mut path: path::PathBuf) -> Option { @@ -67,6 +67,29 @@ pub fn get_dep_root(from_dir: path::PathBuf) -> Result { } } +pub fn search_current_module_root(dep_name: String) -> result::Result { + search_module_root(dep_name, env::current_dir().unwrap()) +} + +pub fn search_module_root(dep_name: String, from_dir: path::PathBuf) -> result::Result { + let project_path = match get_project_root(from_dir.clone()) { + Some(path) => path, + None => return Err(String::from("No project folder found!")) + }; + + if dep_name == String::from(project_path.file_name().unwrap().to_str().unwrap()) { + return Ok(project_path); + } + + match get_dep_root(from_dir) { + Ok(mut dir) => { + dir.push(dep_name); + Ok(dir) + }, + Err(e) => Err(e.to_string()) + } +} + pub fn get_dep_config_root() -> Result { match get_current_project_root() { Some(mut path) => { diff --git a/src/backend/git/ignore.rs b/src/backend/git/ignore.rs index 8f135f3..698ac0e 100644 --- a/src/backend/git/ignore.rs +++ b/src/backend/git/ignore.rs @@ -30,3 +30,5 @@ pub fn add(path: &mut PathBuf, item: &mut String) -> Result<(), String> { } } } + +// pub fn init(); // Moet evt .gitignore aanmaken en de dep folder in de .gitignore plaatsen. diff --git a/src/backend/project.rs b/src/backend/project.rs index a42a6b6..22b03b2 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -19,7 +19,10 @@ pub fn build(args: &mut I) -> Result where I: Iterator { match super::filesystem::get_current_module_root() { - Some(dir) => super::build::build(dir), + Some(dir) => { + let dep_name = String::from(dir.file_name().unwrap().to_str().unwrap()); + super::build::build(dep_name) + }, None => Err(String::from("not in a project (sub)directory.")) } }, @@ -96,14 +99,14 @@ pub fn delete(path: &mut I) -> Result where I: Iterator) -> Result { - let mut path: String = match args.get(0) { + let mut dep: String = match args.get(0) { Some(arg) => arg.clone(), - None => return Err(String::from("Missing path as argument.")) + None => return Err(String::from("Missing dependency name as argument.")) }; match super::filesystem::get_current_module_root() { Some(dir) => { - match super::git::ignore::add(&mut dir.clone(), &mut path) { + match super::git::ignore::add(&mut dir.clone(), &mut dep) { Ok(_) => {}, Err(e) => return Err(e) } @@ -124,16 +127,23 @@ pub fn add(args: &Vec) -> Result { } } -pub fn ignore(args: &Vec) -> Result<(), String> { - let mut entry: String = args.get(0).unwrap().clone(); +pub fn hide(args: &Vec) -> Result { + match add(args) { + Ok(msg) => println!("{}", msg), + Err(e) => return Err(e) + } - match super::filesystem::get_current_dep_root() { - Ok(mut dir) => { - dir.push(&entry); - super::git::ignore::add(&mut dir, &mut entry) // entry moet "ambassade.json" worden. - }, - Err(e) => Err(e.to_string()) + let dep: String = match args.get(0) { + Some(arg) => arg.clone(), + None => return Err(String::from("Missing dep name.")) + }; + + match super::dep_config::init(dep.clone()) { + Ok(_) => println!("Created '{}.json' in 'dep_config' folder.", dep), + Err(e) => return Err(e) } + + Ok(String::from("dependency successfully hidden!")) } pub fn dep_tree(args: &mut I) -> Result where I: Iterator { diff --git a/src/parser.rs b/src/parser.rs index 9d1a6ea..2415cd8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -50,13 +50,8 @@ fn parse(args: &mut I, open_shell: bool) -> bool where I: Iterator = args.collect(); - match backend::project::add(&args) { + match backend::project::hide(&args) { Ok(msg) => println!("{}", msg), - Err(e) => println!("Could not add dependency: {}", e) - } - - match backend::project::ignore(&args) { - Ok(_) => println!("DONE! Dependency hidden."), Err(e) => println!("Could not hide dependency: {}", e) } }