File src/backend/fetch.rs changed (mode: 100644) (index 2ef99f1..e98a1f3) |
1 |
|
use std::process::Command; |
|
2 |
|
use std::path; |
|
|
1 |
|
extern crate rustyline; |
|
2 |
|
extern crate serde_json; |
|
3 |
|
|
|
4 |
|
use std::process::{Command, Stdio}; |
|
5 |
|
use std::path::PathBuf; |
3 |
6 |
use std::result::Result; |
use std::result::Result; |
4 |
7 |
|
|
5 |
|
pub fn fetch(dep: path::PathBuf, command: String) -> Result<String, String> { |
|
|
8 |
|
pub fn build(dep: PathBuf, mut command: String) -> Result<String, String> { |
|
9 |
|
if command == String::new() { |
|
10 |
|
match set_command(&dep, true) { |
|
11 |
|
Some(cmd) => command = cmd, |
|
12 |
|
None => return Err(String::from("Fetching failed: no appropriate command set.")) |
|
13 |
|
} |
|
14 |
|
} |
|
15 |
|
fetch(dep, command) |
|
16 |
|
} |
|
17 |
|
|
|
18 |
|
pub fn run(dep: PathBuf, mut command: String) -> Result<String, String> { |
|
19 |
|
if command == String::new() { |
|
20 |
|
match set_command(&dep, false) { |
|
21 |
|
Some(cmd) => command = cmd, |
|
22 |
|
None => return Err(String::from("Fetching failed: no appropriate command set.")) |
|
23 |
|
} |
|
24 |
|
} |
|
25 |
|
fetch(dep, command) |
|
26 |
|
} |
|
27 |
|
|
|
28 |
|
fn fetch(dep: PathBuf, command: String) -> Result<String, String> { |
6 |
29 |
let mut args: Vec<&str> = command.split(' ').collect(); |
let mut args: Vec<&str> = command.split(' ').collect(); |
7 |
30 |
let command = args.remove(0); |
let command = args.remove(0); |
8 |
|
let out = Command::new(command).current_dir(dep).args(args).output().expect(""); |
|
|
31 |
|
let out = Command::new(command) |
|
32 |
|
.current_dir(&dep) |
|
33 |
|
.args(args) |
|
34 |
|
.stdin(Stdio::inherit()) |
|
35 |
|
.stdout(Stdio::inherit()) |
|
36 |
|
.output(); |
|
37 |
|
|
|
38 |
|
match out { |
|
39 |
|
Ok(response) => { |
|
40 |
|
match response.status.success() { |
|
41 |
|
true => Ok(String::from_utf8_lossy(&response.stdout).to_string()), |
|
42 |
|
false => Err(String::from_utf8_lossy(&response.stderr).to_string()) |
|
43 |
|
} |
|
44 |
|
}, |
|
45 |
|
Err(e) => { |
|
46 |
|
let mut config_file = dep.clone(); |
|
47 |
|
config_file.push("beheer.json"); |
|
48 |
|
|
|
49 |
|
let mut error = String::from("Fetching failed: command '"); |
|
50 |
|
error.push_str(command); |
|
51 |
|
error.push_str("' invalid. Details: "); |
|
52 |
|
error.push_str(&e.to_string()); |
|
53 |
|
error.push_str("\n\nConsider changing the above command in the '"); |
|
54 |
|
|
|
55 |
|
match config_file.to_str() { |
|
56 |
|
Some(path) => error.push_str(path), |
|
57 |
|
None => error.push_str("beheer.json") |
|
58 |
|
} |
|
59 |
|
|
|
60 |
|
error.push_str("' file."); |
|
61 |
|
Err(error) |
|
62 |
|
} |
|
63 |
|
} |
|
64 |
|
} |
|
65 |
|
|
|
66 |
|
fn set_command(dep: &PathBuf, build_cmd: bool) -> Option<String> { |
|
67 |
|
let mut editor = rustyline::Editor::<()>::new(); |
|
68 |
|
|
|
69 |
|
let msg = match build_cmd { |
|
70 |
|
true => "No build command found. Please enter new command: ", |
|
71 |
|
false => "No run command found. Please enter new command: " |
|
72 |
|
}; |
|
73 |
|
|
|
74 |
|
let cmd = match editor.readline(msg) { |
|
75 |
|
Ok(ref input) if input == &String::new() => return None, |
|
76 |
|
Err(_) => return None, |
|
77 |
|
Ok(input) => input, |
|
78 |
|
}; |
|
79 |
|
|
|
80 |
|
let result = match build_cmd { |
|
81 |
|
true => update_module(dep, String::from("build"), cmd.clone()), |
|
82 |
|
false => update_module(dep, String::from("run"), cmd.clone()) |
|
83 |
|
}; |
|
84 |
|
|
|
85 |
|
match result { |
|
86 |
|
Ok(response) => { |
|
87 |
|
println!("{}", response); |
|
88 |
|
Some(cmd) |
|
89 |
|
}, |
|
90 |
|
Err(e) => { |
|
91 |
|
println!("{}", e); |
|
92 |
|
None |
|
93 |
|
} |
|
94 |
|
} |
|
95 |
|
} |
|
96 |
|
|
|
97 |
|
fn update_module(root: &PathBuf, key: String, value: String) -> Result<String, String> { |
|
98 |
|
let config: serde_json::Value; |
|
99 |
|
|
|
100 |
|
match super::config::get_json(root.clone()) { |
|
101 |
|
Ok(mut json) => { |
|
102 |
|
json[key.clone()]["linux"] = json!(value); |
|
103 |
|
json[key.clone()]["os-x"] = json!(value); |
|
104 |
|
json[key]["windows"] = json!(value); |
|
105 |
|
config = json.clone(); |
|
106 |
|
}, |
|
107 |
|
Err(e) => return Err(e) |
|
108 |
|
} |
9 |
109 |
|
|
10 |
|
match out.status.success() { |
|
11 |
|
true => Ok(String::from_utf8_lossy(&out.stdout).to_string()), |
|
12 |
|
false => Err(String::from_utf8_lossy(&out.stderr).to_string()) |
|
|
110 |
|
match super::config::update(root.clone(), config) { |
|
111 |
|
Ok(_) => Ok(String::from("Command updated!")), |
|
112 |
|
Err(e) => Err(e) |
13 |
113 |
} |
} |
14 |
114 |
} |
} |
File src/backend/project.rs changed (mode: 100644) (index c13406c..be666ca) |
... |
... |
pub fn exe<I>(args: &mut I) -> Result<String, String> where I: Iterator<Item=Str |
67 |
67 |
} |
} |
68 |
68 |
|
|
69 |
69 |
println!("Running project.."); |
println!("Running project.."); |
70 |
|
|
|
71 |
|
let mut arguments: Vec<&str> = args.split(' ').collect(); |
|
72 |
|
let command = arguments.remove(0); |
|
73 |
|
let out = Command::new(command) |
|
74 |
|
.args(arguments) |
|
75 |
|
.stdin(Stdio::inherit()) |
|
76 |
|
.stdout(Stdio::inherit()) |
|
77 |
|
.output() |
|
78 |
|
.expect(""); |
|
79 |
|
|
|
80 |
|
match out.status.success() { |
|
81 |
|
true => Ok(String::from_utf8_lossy(&out.stdout).to_string()), |
|
82 |
|
false => Err(String::from_utf8_lossy(&out.stderr).to_string()) |
|
83 |
|
} |
|
|
70 |
|
super::fetch::run(env::current_dir().unwrap(), args) |
84 |
71 |
} |
} |
85 |
72 |
|
|
86 |
73 |
pub fn run<I>(args: &mut I) -> Result<String, String> where I: Iterator<Item=String> { |
pub fn run<I>(args: &mut I) -> Result<String, String> where I: Iterator<Item=String> { |