From 90bfa0340b7b874d65c08dc05c3d711abda1c469 Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Dec 18 2018 16:18:46 +0000 Subject: Improve `dep-tree` command. * Implement 'Display' Trait. * Able to process `dep-tree` arguments. * Restructure 'OS' enum in 'system.rs' module. --- diff --git a/src/backend/config.rs b/src/backend/config.rs index 339e983..e362119 100644 --- a/src/backend/config.rs +++ b/src/backend/config.rs @@ -7,7 +7,6 @@ use std::result::Result; pub fn create(mut path: PathBuf) -> Result<(), Error> { let content = json!({ - "project-name": path.file_name().unwrap().to_str().unwrap(), "version": 0.1, "build": { "windows": "echo \"No build config set.\"", diff --git a/src/backend/dep.rs b/src/backend/dep.rs index b57c7ce..e3662f2 100644 --- a/src/backend/dep.rs +++ b/src/backend/dep.rs @@ -1,13 +1,7 @@ extern crate serde_json; use std::result::Result; - -pub enum OS { - all, - linux, - macos, - windows -} +use super::system::OS; pub fn json(config: String) -> Result { let config_json: serde_json::Value; @@ -47,26 +41,26 @@ pub fn json(config: String) -> Result { Ok(config_json) } -pub fn dep(config: serde_json::Value, os: OS) -> Result, String> { +pub fn dep(config: serde_json::Value, os: &OS) -> Result, String> { let mut output: Vec<(String, String)> = Vec::new(); match os { OS::all => { - match dep(config.clone(), OS::linux) { + match dep(config.clone(), &OS::linux) { Ok(mut result) => { let mut r: Vec<(String,String)> = result; output.append(&mut r) }, Err(e) => return Err(e) } - match dep(config.clone(), OS::macos) { + match dep(config.clone(), &OS::macos) { Ok(mut result) => { let mut r: Vec<(String,String)> = result; output.append(&mut r) }, Err(e) => return Err(e) } - match dep(config.clone(), OS::windows) { + match dep(config.clone(), &OS::windows) { Ok(mut result) => { let mut r: Vec<(String,String)> = result; output.append(&mut r) @@ -130,7 +124,7 @@ pub fn dep(config: serde_json::Value, os: OS) -> Result, S pub fn check(config: serde_json::Value) -> Result { println!("Checking dependencies.."); - match dep(config, OS::linux) { + match dep(config, &OS::linux) { Ok(ref deps) if deps.is_empty() => return Ok(String::from("No dependencies found!")), Ok(ref deps) => { for dep in deps.iter() { @@ -153,7 +147,7 @@ pub fn check(config: serde_json::Value) -> Result { pub fn check(config: serde_json::Value) -> Result { println!("Checking dependencies.."); - match dep(config, OS::macos) { + match dep(config, &OS::macos) { Ok(ref deps) if deps.is_empty() => return Ok(String::from("No dependencies found!")), Ok(ref deps) => { for dep in deps.iter() { @@ -176,7 +170,7 @@ pub fn check(config: serde_json::Value) -> Result { pub fn check(config: serde_json::Value) -> Result { println!("Checking dependencies.."); - match dep(config, OS::windows) { + match dep(config, &OS::windows) { Ok(ref deps) if deps.is_empty() => return Ok(String::from("No dependencies found!")), Ok(ref deps) => { for dep in deps.iter() { diff --git a/src/backend/deptree.rs b/src/backend/deptree.rs index 027dd6b..d3c6076 100644 --- a/src/backend/deptree.rs +++ b/src/backend/deptree.rs @@ -1,50 +1,78 @@ extern crate serde_json; -use super::dep as backend; +use super::system::OS; use std::result::Result; -use std::option::Option; use std::path::PathBuf; -use std::rc::Rc; use std::fmt; pub struct Node { dep_name: String, path: PathBuf, - depends_on: Option> + depends_on: Vec } -// Incomplete impl fmt::Display for Node { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "Incomplete")); + let mut dependency = self.clone(); + let mut string = String::from(self.dep_name.clone()); + + string.push('\n'); + + for node in &dependency.depends_on { + let node = node.to_string(); + + for line in node.lines() { + let mut line = String::from(line); + + line.insert(0, '\t'); + + string.push_str(line.as_str()); + string.push('\n'); + } + } + + dependency = self.clone(); + + try!(write!(f, "{}", string)); Ok(()) } } -pub fn print(os: backend::OS) -> Result { +pub fn print(os: &OS, path: PathBuf) -> Result { let mut deps: Vec = Vec::new(); - let path: PathBuf; + let mut nodes: Vec = Vec::new(); - match super::filesystem::get_module_root() { - Some(p) => path = p, - None => return Err(String::from("Not in a project/dependency directory.")) - } - - let deps_json = match super::config::get_json(path) { + let deps_json = match super::config::get_json(path.clone()) { Ok(config) => config, Err(e) => return Err(e) }; - match backend::dep(deps_json, os) { - Ok(vector) => vector.iter().for_each(|tuple| deps.push(tuple.0.clone())), + match super::dep::dep(deps_json, os) { + Ok(vector) => vector.iter().for_each(|dep| deps.push(dep.0.clone())), Err(e) => return Err(e) } - // Incomplete + for dependency in deps { + let node: Node; + + match super::filesystem::get_dep_root() { + Ok(mut dir) => { + dir.push(dependency); + match print(&os, dir) { + Ok(dep) => node = dep, + Err(e) => return Err(e.to_string()) + } + }, + Err(e) => return Err(e.to_string()) + }; + + nodes.push(node); + } + let root = Node { - dep_name: String::from("root"), + dep_name: String::from(path.file_name().unwrap().to_str().unwrap()), path: super::filesystem::get_module_root().unwrap(), - depends_on: None + depends_on: nodes }; Ok(root) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 241dd96..6820532 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -6,3 +6,4 @@ pub mod filesystem; pub mod build; pub mod dep; pub mod fetch; +pub mod system; diff --git a/src/backend/project.rs b/src/backend/project.rs index aaaaa77..9e611da 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -84,9 +84,20 @@ pub fn run(args: &mut env::Args) -> Result { exe(args) } -// Args need to be processed.. pub fn dep_tree(args: &mut env::Args) -> Result { - deptree::print(super::dep::OS::all) + let path = match super::filesystem::get_module_root() { + Some(p) => p, + None => return Err(String::from("Not in a project/dependency directory.")) + }; + + match args.next() { + Some(ref os) if os.as_str() == "linux" => deptree::print(&super::system::OS::linux, path), + Some(ref os) if os.as_str() == "os-x" => deptree::print(&super::system::OS::macos, path), + Some(ref os) if os.as_str() == "windows" => deptree::print(&super::system::OS::windows, path), + Some(ref os) if os.as_str() == "all" => deptree::print(&super::system::OS::all, path), + Some(_) => Err(String::from("dep-tree: OS not found. Possible inputs: 'all', 'linux', 'os-x', 'windows'")), + None => deptree::print(&super::system::OS::current(), path) + } } pub fn help() { diff --git a/src/backend/system.rs b/src/backend/system.rs new file mode 100644 index 0000000..38ae019 --- /dev/null +++ b/src/backend/system.rs @@ -0,0 +1,23 @@ +pub enum OS { + all, + linux, + macos, + windows +} + +impl OS { + #[cfg(target_os="linux")] + pub fn current() -> OS { + OS::linux + } + + #[cfg(target_os="macos")] + pub fn current() -> OS { + OS::macos + } + + #[cfg(target_os="windows")] + pub fn current() -> OS { + OS::windows + } +}