From e81d57882036a6475718b0653169c609d23b1c30 Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Dec 25 2018 13:06:00 +0000 Subject: Resolve missing/erroneous build/run commands. --- diff --git a/README.md b/README.md index 3f087ab..c04e7ca 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ # Beheer + +## Notes + +`backend::fetch::update_module()` kan wellicht met `backend::add::update_module()` samengevoegd worden. diff --git a/src/backend/build.rs b/src/backend/build.rs index 7231f73..2db696b 100644 --- a/src/backend/build.rs +++ b/src/backend/build.rs @@ -63,7 +63,7 @@ fn build_module(config: serde_json::Value) -> Result { return Err(String::from("beheer.json: 'build->linux' should be a string.")); } - super::fetch::fetch(env::current_dir().unwrap(), String::from(build_cmd.as_str().unwrap())) + super::fetch::build(env::current_dir().unwrap(), String::from(build_cmd.as_str().unwrap())) } #[cfg(target_os="macos")] diff --git a/src/backend/config.rs b/src/backend/config.rs index e362119..495c65e 100644 --- a/src/backend/config.rs +++ b/src/backend/config.rs @@ -7,16 +7,15 @@ use std::result::Result; pub fn create(mut path: PathBuf) -> Result<(), Error> { let content = json!({ - "version": 0.1, "build": { - "windows": "echo \"No build config set.\"", - "os-x": "echo \"No build config set.\"", - "linux": "echo \"No build config set.\"" + "windows": "", + "os-x": "", + "linux": "" }, "run": { - "windows": "echo \"No run config set.\"", - "os-x": "echo \"No run config set.\"", - "linux": "echo \"No run config set.\"" + "windows": "", + "os-x": "", + "linux": "" } }); diff --git a/src/backend/dep.rs b/src/backend/dep.rs index 21b27f8..6f7d34b 100644 --- a/src/backend/dep.rs +++ b/src/backend/dep.rs @@ -195,7 +195,7 @@ fn dir_check(dependency: String, command: String) -> Result { dep_dir.push(dependency); if !dep_dir.exists() { dep_dir.pop(); - return super::fetch::fetch(dep_dir, command); + return super::fetch::build(dep_dir, command); } Ok(String::from("Dependency found.")) }, diff --git a/src/backend/fetch.rs b/src/backend/fetch.rs index 2ef99f1..e98a1f3 100644 --- a/src/backend/fetch.rs +++ b/src/backend/fetch.rs @@ -1,14 +1,114 @@ -use std::process::Command; -use std::path; +extern crate rustyline; +extern crate serde_json; + +use std::process::{Command, Stdio}; +use std::path::PathBuf; use std::result::Result; -pub fn fetch(dep: path::PathBuf, command: String) -> Result { +pub fn build(dep: PathBuf, mut command: String) -> Result { + if command == String::new() { + match set_command(&dep, true) { + Some(cmd) => command = cmd, + None => return Err(String::from("Fetching failed: no appropriate command set.")) + } + } + fetch(dep, command) +} + +pub fn run(dep: PathBuf, mut command: String) -> Result { + if command == String::new() { + match set_command(&dep, false) { + Some(cmd) => command = cmd, + None => return Err(String::from("Fetching failed: no appropriate command set.")) + } + } + fetch(dep, command) +} + +fn fetch(dep: 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).args(args).output().expect(""); + let out = Command::new(command) + .current_dir(&dep) + .args(args) + .stdin(Stdio::inherit()) + .stdout(Stdio::inherit()) + .output(); + + match out { + Ok(response) => { + match response.status.success() { + true => Ok(String::from_utf8_lossy(&response.stdout).to_string()), + false => Err(String::from_utf8_lossy(&response.stderr).to_string()) + } + }, + Err(e) => { + let mut config_file = dep.clone(); + config_file.push("beheer.json"); + + let mut error = String::from("Fetching failed: command '"); + error.push_str(command); + error.push_str("' invalid. Details: "); + 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("beheer.json") + } + + error.push_str("' file."); + Err(error) + } + } +} + +fn set_command(dep: &PathBuf, build_cmd: bool) -> Option { + let mut editor = rustyline::Editor::<()>::new(); + + let msg = match build_cmd { + true => "No build command found. Please enter new command: ", + false => "No run command found. Please enter new command: " + }; + + let cmd = match editor.readline(msg) { + Ok(ref input) if input == &String::new() => return None, + Err(_) => return None, + Ok(input) => input, + }; + + let result = match build_cmd { + true => update_module(dep, String::from("build"), cmd.clone()), + false => update_module(dep, String::from("run"), cmd.clone()) + }; + + match result { + Ok(response) => { + println!("{}", response); + Some(cmd) + }, + Err(e) => { + println!("{}", e); + None + } + } +} + +fn update_module(root: &PathBuf, key: String, value: String) -> Result { + let config: serde_json::Value; + + match super::config::get_json(root.clone()) { + Ok(mut json) => { + json[key.clone()]["linux"] = json!(value); + json[key.clone()]["os-x"] = json!(value); + json[key]["windows"] = json!(value); + config = json.clone(); + }, + Err(e) => return Err(e) + } - match out.status.success() { - true => Ok(String::from_utf8_lossy(&out.stdout).to_string()), - false => Err(String::from_utf8_lossy(&out.stderr).to_string()) + match super::config::update(root.clone(), config) { + Ok(_) => Ok(String::from("Command updated!")), + Err(e) => Err(e) } } diff --git a/src/backend/project.rs b/src/backend/project.rs index c13406c..be666ca 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -67,20 +67,7 @@ pub fn exe(args: &mut I) -> Result where I: Iterator = args.split(' ').collect(); - let command = arguments.remove(0); - let out = Command::new(command) - .args(arguments) - .stdin(Stdio::inherit()) - .stdout(Stdio::inherit()) - .output() - .expect(""); - - match out.status.success() { - true => Ok(String::from_utf8_lossy(&out.stdout).to_string()), - false => Err(String::from_utf8_lossy(&out.stderr).to_string()) - } + super::fetch::run(env::current_dir().unwrap(), args) } pub fn run(args: &mut I) -> Result where I: Iterator {