From 482bad08f949eaedbd61e2cb7c53870271d47997 Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Jan 11 2019 15:45:26 +0000 Subject: Revise `hide` command. Hiding initializes a dependency by creating it's config file in the `dep_config` directory. Instead of naming the config file `ambassade.json`, the file will be named '.json'. The actual initialization hasn't been implemented yet. The `backend::dep_config::scan()` function can search for config files in a dependency directory and in `dep_config` (NOT in the project's root directory!). --- diff --git a/Cargo.lock b/Cargo.lock index 072f7aa..0679f2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,4 +1,13 @@ [[package]] +name = "ambassade" +version = "0.1.0" +dependencies = [ + "git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustyline 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "argon2rs" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -43,15 +52,6 @@ dependencies = [ ] [[package]] -name = "ambassade" -version = "0.1.0" -dependencies = [ - "git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rustyline 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] name = "bitflags" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/src/backend/add.rs b/src/backend/add.rs index d94694a..61efc0e 100644 --- a/src/backend/add.rs +++ b/src/backend/add.rs @@ -41,7 +41,7 @@ fn update_module(key: String, value: String) -> Result { None => return Err(String::from("No config file in module found.")) } - match super::config::get_json(path.clone()) { + match super::config::get_json_from_dir(path.clone()) { Ok(mut json) => { json["deps"]["linux"][key.clone()] = json!(value); json["deps"]["os-x"][key.clone()] = json!(value); diff --git a/src/backend/build.rs b/src/backend/build.rs index be084c6..d7d87e8 100644 --- a/src/backend/build.rs +++ b/src/backend/build.rs @@ -7,7 +7,7 @@ use std::env; pub fn build(config_file: PathBuf) -> Result { let config; - match super::config::get_json(config_file) { + match super::config::get_json_from_dir(config_file) { Ok(result) => config = result, Err(e) => return Err(e) } diff --git a/src/backend/config.rs b/src/backend/config.rs index 98b1644..092bee7 100644 --- a/src/backend/config.rs +++ b/src/backend/config.rs @@ -11,7 +11,7 @@ pub fn create(mut path: PathBuf) -> Result<(), Error> { init(&path) } -fn init(path: &PathBuf) -> Result<(), Error> { +pub fn init(path: &PathBuf) -> Result<(), Error> { let content = json!({ "build": { "windows": "", @@ -54,10 +54,9 @@ pub fn update(mut path: PathBuf, value: serde_json::Value) -> Result<(), String> } } -fn read(path: &mut PathBuf) -> Result { +fn read(path: &PathBuf) -> Result { let mut config = String::new(); - path.push("ambassade.json"); check(&path); match File::open(path.to_str().unwrap()) { @@ -70,8 +69,8 @@ fn read(path: &mut PathBuf) -> Result { Ok(config) } -pub fn get_json(mut path: PathBuf) -> Result { - match read(&mut path) { +pub fn get_json(path: &PathBuf) -> Result { + match read(path) { Ok(config) => super::dep::json(config), Err(e) => { println!("Error on config file: '{}'.", path.to_str().unwrap()); @@ -84,6 +83,11 @@ pub fn get_json(mut path: PathBuf) -> Result { } } +pub fn get_json_from_dir(mut path: PathBuf) -> Result { + path.push("ambassade.json"); + get_json(&path) +} + fn check(config: &PathBuf) { if !config.is_file() { let mut input = String::new(); diff --git a/src/backend/delete.rs b/src/backend/delete.rs index 87818db..5af0d40 100644 --- a/src/backend/delete.rs +++ b/src/backend/delete.rs @@ -42,12 +42,12 @@ fn dep_check_rec(tree: &Node) -> Result, String> { fn dep_check(node: &Node) -> Result<(), String> { let mut input = String::new(); - let mut config_path = PathBuf::new(); + let mut super_module = String::new(); match dep_check_rec(node) { Ok(nodes) => { for dep in nodes { - config_path = dep.path; + super_module = dep.name; } }, Err(e) => return Err(e) @@ -61,15 +61,20 @@ fn dep_check(node: &Node) -> Result<(), String> { Err(e) => return Err(e.to_string()) } - if config_path == PathBuf::new() { + if super_module == String::new() { return Ok(()); } - rm_from_config(config_path, node.name.clone()) + rm_from_config(super_module, node.name.clone()) } -fn rm_from_config(super_module: PathBuf, dep_name: String) -> Result<(), String> { - let mut json = match super::config::get_json(super_module.clone()) { +fn rm_from_config(super_module: String, dep_name: String) -> Result<(), String> { + let super_module = match super::dep_config::scan(super_module) { + Ok(path) => path, + Err(e) => return Err(e) + }; + + let mut json = match super::config::get_json(&super_module) { Ok(config) => config, Err(e) => return Err(e) }; diff --git a/src/backend/dep_config.rs b/src/backend/dep_config.rs new file mode 100644 index 0000000..081f62e --- /dev/null +++ b/src/backend/dep_config.rs @@ -0,0 +1,44 @@ +use std::path::PathBuf; + +pub fn init(mut dep_name: String) -> Result<(), String> { + let mut config = match super::filesystem::get_dep_config_root() { + Ok(path) => path, + Err(e) => return Err(e.to_string()) + }; + + dep_name.push_str(".json"); + config.push(dep_name); + + match super::config::init(&config) { + Ok(_) => Ok(()), + Err(e) => Err(e.to_string()) + } +} + +pub fn scan(dep_name: String) -> Result { + println!("Scanning for {}..", dep_name); + + let mut config_path = match super::filesystem::get_dep_config_root() { + Ok(path) => path, + Err(e) => return Err(e.to_string()) + }; + + let mut config_name = dep_name.clone(); + config_name.push_str(".json"); + config_path.push(config_name); + + if config_path.is_file() { + return Ok(config_path); + } + + match super::filesystem::get_current_dep_root() { + Ok(mut path) => { + path.push(dep_name); + match super::config::get_json_from_dir(path.clone()) { // Inconventient, should be changed later + Ok(_) => Ok(path), + Err(e) => Err(e) + } + }, + Err(e) => Err(e.to_string()) + } +} diff --git a/src/backend/deptree.rs b/src/backend/deptree.rs index 1c35108..7f2ce70 100644 --- a/src/backend/deptree.rs +++ b/src/backend/deptree.rs @@ -48,8 +48,14 @@ impl PartialEq for Node { pub fn print(os: &OS, path: PathBuf) -> Result { let mut deps: Vec = Vec::new(); let mut nodes: Vec = Vec::new(); + let dep_name = String::from(path.file_name().unwrap().to_str().unwrap()); - let deps_json = match super::config::get_json(path.clone()) { + let config_path = match super::dep_config::scan(dep_name.clone()) { + Ok(path) => path, + Err(e) => return Err(e) + }; + + let deps_json = match super::config::get_json(&config_path) { Ok(config) => config, Err(e) => return Err(e) }; @@ -77,8 +83,8 @@ pub fn print(os: &OS, path: PathBuf) -> Result { } let root = Node { - name: String::from(path.file_name().unwrap().to_str().unwrap()), - path: path, + name: dep_name, + path: path, // What if backend::config::get_json_from_dir(root.path) gets called? depends_on: nodes }; diff --git a/src/backend/fetch.rs b/src/backend/fetch.rs index 2ddfbe7..9c7fd2a 100644 --- a/src/backend/fetch.rs +++ b/src/backend/fetch.rs @@ -6,30 +6,34 @@ use std::path::PathBuf; use std::result::Result; pub fn build(dep: PathBuf, mut command: String) -> Result { + let dep_name = String::from(dep.file_name().unwrap().to_str().unwrap()); + if command == String::new() { - match set_command(&dep, true) { + match set_command(&dep_name, true) { Some(cmd) => command = cmd, None => return Err(String::from("Fetching failed: no appropriate command set.")) } } - fetch(dep, command) + fetch(dep_name, &dep, command) } pub fn run(dep: PathBuf, mut command: String) -> Result { + let dep_name = String::from(dep.file_name().unwrap().to_str().unwrap()); + if command == String::new() { - match set_command(&dep, false) { + match set_command(&dep_name, false) { Some(cmd) => command = cmd, None => return Err(String::from("Fetching failed: no appropriate command set.")) } } - fetch(dep, command) + fetch(dep_name, &dep, command) } -fn fetch(dep: PathBuf, command: String) -> Result { +fn fetch(dep_name: String, path: &PathBuf, command: String) -> Result { let mut args: Vec<&str> = command.split(' ').collect(); let command = args.remove(0); let out = Command::new(command) - .current_dir(&dep) + .current_dir(path) .args(args) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) @@ -43,8 +47,10 @@ fn fetch(dep: PathBuf, command: String) -> Result { } }, Err(e) => { - let mut config_file = dep.clone(); - config_file.push("ambassade.json"); + let config_path = match super::dep_config::scan(dep_name) { + Ok(p) => p, + Err(e) => return Err(e) + }; let mut error = String::from("Fetching failed: command '"); error.push_str(command); @@ -52,9 +58,9 @@ fn fetch(dep: PathBuf, command: String) -> Result { error.push_str(&e.to_string()); error.push_str("\n\nConsider changing the above command in the '"); - match config_file.to_str() { - Some(path) => error.push_str(path), - None => error.push_str("ambassade.json") + match config_path.to_str() { + Some(p) => error.push_str(p), + None => error.push_str("configuration") } error.push_str("' file."); @@ -63,7 +69,7 @@ fn fetch(dep: PathBuf, command: String) -> Result { } } -fn set_command(dep: &PathBuf, build_cmd: bool) -> Option { +fn set_command(dep: &String, build_cmd: bool) -> Option { let mut editor = rustyline::Editor::<()>::new(); let msg = match build_cmd { @@ -94,10 +100,15 @@ fn set_command(dep: &PathBuf, build_cmd: bool) -> Option { } } -fn update_module(root: &PathBuf, key: String, value: String) -> Result { +fn update_module(dep_name: &String, key: String, value: String) -> Result { let config: serde_json::Value; - match super::config::get_json(root.clone()) { + let path = match super::dep_config::scan(dep_name.clone()) { + Ok(path) => path, + Err(e) => return Err(e) + }; + + match super::config::get_json(&path) { Ok(mut json) => { json[key.clone()]["linux"] = json!(value); json[key.clone()]["os-x"] = json!(value); @@ -107,7 +118,7 @@ fn update_module(root: &PathBuf, key: String, value: String) -> Result return Err(e) } - match super::config::update(root.clone(), 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 3467053..5190c5f 100644 --- a/src/backend/filesystem.rs +++ b/src/backend/filesystem.rs @@ -66,3 +66,20 @@ pub fn get_dep_root(from_dir: path::PathBuf) -> Result { None => Err(Error::new(ErrorKind::NotFound, "No project file found. Aborted.")) } } + +pub fn get_dep_config_root() -> Result { + match get_current_project_root() { + Some(mut path) => { + path.push("dep_config"); + if !path.exists() { + println!("\t No dep_config folder found. Creating folder.."); + match fs::create_dir(path.clone()) { + Ok(_) => println!("\tCreated dir {}.", path.clone().to_str().unwrap()), + Err(e) => return Err(e) + } + } + Ok(path) + }, + None => Err(Error::new(ErrorKind::NotFound, "No project file found. Aborted.")) + } +} diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 7bdad7e..e7b9123 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,6 +1,7 @@ pub mod add; pub mod delete; pub mod config; +pub mod dep_config; pub mod deptree; pub mod project; pub mod filesystem; diff --git a/src/backend/project.rs b/src/backend/project.rs index d4affa5..a42a6b6 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -41,7 +41,7 @@ pub fn exe(args: &mut I) -> Result where I: Iterator return Err(String::from("not in a project (sub)directory.")) } - match super::config::get_json(output_dir) { + match super::config::get_json_from_dir(output_dir) { Ok(config) => { if cfg!(target_os = "linux") { match config["run"]["linux"].as_str() { @@ -113,7 +113,7 @@ pub fn add(args: &Vec) -> Result { Err(e) => return Err(e) } - match super::config::get_json(dir) { + match super::config::get_json_from_dir(dir) { Ok(json) => { super::dep::check(json) },