From 5ffd40604755d1c2dde9353c74f06a006deea33c Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Dec 07 2018 20:50:42 +0000 Subject: Implement non-recursive dependency check. --- diff --git a/src/backend/check.rs b/src/backend/check.rs index 6885a45..08d2bca 100644 --- a/src/backend/check.rs +++ b/src/backend/check.rs @@ -1,6 +1,6 @@ extern crate serde_json; -use std::io::{Result, Error, ErrorKind, Write}; +use std::io::{Result, Error, ErrorKind}; pub fn dep(config: String) -> Result { let config_json: serde_json::Value; @@ -37,23 +37,30 @@ pub fn dep(config: String) -> Result { } } - pkg_check(config_json) + config_check(config_json) } #[cfg(target_os="linux")] -fn pkg_check(config: serde_json::Value) -> Result { +fn config_check(config: serde_json::Value) -> Result { match config["deps"]["linux"] { json!(null) => return Ok(String::from("No dependencies found!")), ref deps => { if !deps.is_object() { return Err(Error::new(ErrorKind::InvalidData, "beheer.json: 'deps->linux' should be an object.")); } + for dep in deps.as_object().unwrap().iter() { + if !dep.1.is_string() { return Err(Error::new(ErrorKind::InvalidData, "beheer.json: all deps should be strings!")); } + println!("Checking for {}..\n\t{}", dep.0, dep.1); - super::fetch::fetch(dep.0.to_string(), String::from(dep.1.as_str().unwrap())); + + match dir_check(dep.0.to_string(), String::from(dep.1.as_str().unwrap())) { + Ok(_) => {}, + Err(e) => return Err(e) + } } } } @@ -61,18 +68,25 @@ fn pkg_check(config: serde_json::Value) -> Result { } #[cfg(target_os="macos")] -fn pkg_check(config: serde_json::Value) -> Result { +fn config_check(config: serde_json::Value) -> Result { match config["deps"]["os-x"] { json!(null) => return Ok(String::from("No dependencies found!")), ref sys_deps => { if !sys_deps.is_object() { return Err(Error::new(ErrorKind::InvalidData, "beheer.json: 'deps->os-x' should be an object.")); } - for dep in sys_deps.as_object().unwrap().iter() { + + for dep in deps.as_object().unwrap().iter() { + if !dep.1.is_string() { return Err(Error::new(ErrorKind::InvalidData, "beheer.json: all deps should be strings!")); } - println!("Checking for {} version {}..", dep.0, dep.1); + + println!("Checking for {}..\n\t{}", dep.0, dep.1); + + if dir_check(dep.0.to_string(), String::from(dep.1.as_str().unwrap())) == Err(e) { + return Err(e); + } } } } @@ -80,20 +94,42 @@ fn pkg_check(config: serde_json::Value) -> Result { } #[cfg(target_os="windows")] -fn pkg_check(config: serde_json::Value) -> Result { +fn config_check(config: serde_json::Value) -> Result { match config["deps"]["windows"] { json!(null) => return Ok(String::from("No dependencies found!")), ref sys_deps => { if !sys_deps.is_object() { return Err(Error::new(ErrorKind::InvalidData, "beheer.json: 'deps->windows' should be an object.")); } - for dep in sys_deps.as_object().unwrap().iter() { + + for dep in deps.as_object().unwrap().iter() { if !dep.1.is_string() { return Err(Error::new(ErrorKind::InvalidData, "beheer.json: all deps should be strings!")); } - println!("Checking for {} version {}..", dep.0, dep.1); + + println!("Checking for {}..\n\t{}", dep.0, dep.1); + + if dir_check(dep.0.to_string(), String::from(dep.1.as_str().unwrap())) == Err(e) { + return Err(e); + } } } } Ok(String::from("Dependencies OK!")) } + +fn dir_check(dependency: String, command: String) -> Result<()> { + let dir = super::filesystem::get_dep_dir(); + + match dir { + Ok(mut dep_dir) => { + dep_dir.push(dependency); + if !dep_dir.is_dir() { + dep_dir.pop(); + super::fetch::fetch(dep_dir, command); + } + Ok(()) + }, + Err(e) => Err(e) + } +} diff --git a/src/backend/fetch.rs b/src/backend/fetch.rs index 6160248..613e432 100644 --- a/src/backend/fetch.rs +++ b/src/backend/fetch.rs @@ -1,7 +1,10 @@ use std::process::Command; +use std::path; -pub fn fetch(dep: String, command: String) { - let out = Command::new(command).output().expect(""); +pub fn fetch(dep: path::PathBuf, command: String) { + let mut args: Vec<&str> = command.split(' ').collect(); + let command = args.remove(0); + let out = Command::new(command).current_dir(dep).args(args).output().expect(""); println!("{:#?}", out.stderr); } diff --git a/src/backend/filesystem.rs b/src/backend/filesystem.rs index e61a3c6..71578e5 100644 --- a/src/backend/filesystem.rs +++ b/src/backend/filesystem.rs @@ -1,4 +1,5 @@ -use std::{path, env}; +use std::{path, env, fs}; +use std::io::{Result, Error, ErrorKind}; fn get_root (mut path: path::PathBuf) -> Option { loop { @@ -36,3 +37,20 @@ pub fn get_project_root() -> Option { } } } + +pub fn get_dep_dir() -> Result { + match get_project_root() { + Some(mut path) => { + path.push("dep"); + if !path.is_dir() { + println!("No dep folder found. Creating folder.."); + match fs::create_dir(path.clone()) { + Ok(_) => println!("Created dir {}.", path.clone().to_str().unwrap()), + Err(e) => return Err(e) + } + } + Ok(path) + }, + None => Err(Error::new(ErrorKind::NotFound, "No project file found. Aborted.")) + } +}