a
This commit is contained in:
73
src/main.rs
73
src/main.rs
@@ -1,8 +1,8 @@
|
||||
#[macro_use]
|
||||
extern crate rust_util;
|
||||
|
||||
use std::process::Command;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
fn main() {
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_|
|
||||
@@ -10,41 +10,54 @@ fn main() {
|
||||
);
|
||||
let rust_script = PathBuf::from(format!("{}/.cargo/bin/rust-script", home));
|
||||
if !rust_script.exists() {
|
||||
warning!("rust-script not found, install it...");
|
||||
let mut cargo_install_rust_script = Command::new("cargo");
|
||||
cargo_install_rust_script.args(&["install", "rust-script"]);
|
||||
debugging!("Run command: {:?}", cargo_install_rust_script);
|
||||
let run_result = rust_util::util_cmd::run_command_and_wait(&mut cargo_install_rust_script);
|
||||
if let Err(e) = run_result {
|
||||
failure_and_exit!("Install rust-script failed: {}", e);
|
||||
}
|
||||
// warning!("rust-script not found, install it...");
|
||||
// let mut cargo_install_rust_script = Command::new("cargo");
|
||||
// cargo_install_rust_script.args(&["install", "rust-script"]);
|
||||
// debugging!("Run command: {:?}", cargo_install_rust_script);
|
||||
// let run_result = rust_util::util_cmd::run_command_and_wait(&mut cargo_install_rust_script);
|
||||
// if let Err(e) = run_result {
|
||||
// failure_and_exit!("Install rust-script failed: {}", e);
|
||||
// }
|
||||
failure_and_exit!("Need install rust-script tool, https://git.hatter.ink/hatter/runrs/src/branch/master/external/rust-script");
|
||||
}
|
||||
|
||||
let mut copied_args = vec![];
|
||||
std::env::args().skip(1).for_each(|arg| {
|
||||
copied_args.push(arg);
|
||||
});
|
||||
debugging!("Arguments: {:?}", copied_args);
|
||||
let args = std::env::args().skip(1).collect::<Vec<_>>();
|
||||
|
||||
if copied_args.is_empty() {
|
||||
failure_and_exit!("Run `runrs --help` for help.");
|
||||
if args.is_empty() {
|
||||
failure_and_exit!("runrs need arguments, e.g.\n\nrunrs <scirpt.ers> [arguments]\n");
|
||||
}
|
||||
|
||||
if let Some(arg1) = copied_args.get(0) {
|
||||
match arg1.as_str() {
|
||||
":::" => {
|
||||
// TODO
|
||||
}
|
||||
_ => {} // IGNORE
|
||||
}
|
||||
}
|
||||
let script_file = match args.get(0) {
|
||||
None => failure_and_exit!("Must assign file name"),
|
||||
Some(f) => f,
|
||||
};
|
||||
let script_content = match std::fs::read_to_string(script_file) {
|
||||
Err(e) => failure_and_exit!("Read file: {}, failed: {}", script_file, e),
|
||||
Ok(c) => c,
|
||||
};
|
||||
let script_sha256 = sha256::digest(script_content);
|
||||
debugging!("File {} -> sha256: {}", script_file, script_sha256);
|
||||
|
||||
let mut run_rs = Command::new(rust_script);
|
||||
for arg in &copied_args {
|
||||
run_rs.arg(arg);
|
||||
let cache_script_bin_name = format!("{}/Library/Caches/rust-script/binaries/release/{}",
|
||||
home,
|
||||
script_sha256);
|
||||
|
||||
let cache_script_bin_name_exists = std::fs::metadata(&cache_script_bin_name).is_ok();
|
||||
debugging!("Bin name: {} {} exists", cache_script_bin_name, iff!(cache_script_bin_name_exists, "", "not"));
|
||||
|
||||
let mut run_script_cmd = if cache_script_bin_name_exists {
|
||||
Command::new(cache_script_bin_name)
|
||||
} else {
|
||||
let mut cmd = Command::new(rust_script);
|
||||
cmd.args(&["--bin-name", &script_sha256, script_file]);
|
||||
cmd
|
||||
};
|
||||
for arg in args.iter().skip(1) {
|
||||
run_script_cmd.arg(arg);
|
||||
}
|
||||
debugging!("Run command: {:?}", run_rs);
|
||||
if let Err(e) = rust_util::util_cmd::run_command_and_wait(&mut run_rs) {
|
||||
failure_and_exit!("Run rust-script failed: {}", e);
|
||||
debugging!("Run command: {:?}", run_script_cmd);
|
||||
match rust_util::util_cmd::run_command_and_wait(&mut run_script_cmd) {
|
||||
Err(e) => failure_and_exit!("Run rust-script failed: {}", e),
|
||||
Ok(exit_status) => std::process::exit(exit_status.code().unwrap_or_else(|| 0)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user