From dc6fc418109572e44060a2f1da37baa3bda4fe4c Mon Sep 17 00:00:00 2001 From: Frederik Delaere Date: Tue, 12 Mar 2019 14:30:29 +0100 Subject: [PATCH] * added add_to_info_bin function, this overrides the binaries in the PATH variable for module info * removed some deprecated functions * fmt --- README.md | 5 ++++ src/cache.rs | 2 +- src/manage.rs | 22 +++++++------- src/script.rs | 81 ++++++++++++++++++++++++++++++++------------------- src/wizard.rs | 2 +- 5 files changed, 69 insertions(+), 43 deletions(-) mode change 100644 => 100755 README.md mode change 100644 => 100755 src/cache.rs mode change 100644 => 100755 src/manage.rs mode change 100644 => 100755 src/script.rs mode change 100644 => 100755 src/wizard.rs diff --git a/README.md b/README.md old mode 100644 new mode 100755 index ccad01b..4053c3b --- a/README.md +++ b/README.md @@ -151,6 +151,11 @@ Next to the default rhai syntax, the following functions are available: * ```set_alias("name","value");``` * ```is_loaded("modulename");``` * ```source("shelltype", "/path/to/filename.shell-extension");``` + * ```add_bin_to_info("binary_name");``` + +###Note: +When using add_bin_to_info (you can add it multiple times to your scripts), `module info ` will only +show the binaries that are added with this function, it will ignore the binaries in your PATH variables. ### Example modulefile diff --git a/src/cache.rs b/src/cache.rs old mode 100644 new mode 100755 index 270fa40..aa91078 --- a/src/cache.rs +++ b/src/cache.rs @@ -165,7 +165,7 @@ pub fn update(modulepath: &str, shell: &str) -> bool { // modulename can start with / // also we don't want the .modulesindex or other hidden files if first == '/' && second != "." { - modulename = modulename.trim_left_matches('/'); + modulename = modulename.trim_start_matches('/'); } let modulename_part: Vec<&str> = modulename.split('/').collect(); diff --git a/src/manage.rs b/src/manage.rs old mode 100644 new mode 100755 index 82e7acc..920c912 --- a/src/manage.rs +++ b/src/manage.rs @@ -23,6 +23,7 @@ SOFTWARE. */ use super::bold; use crate::rsmod::{get_module_paths, Rsmodule}; +use crate::wizard::{is_yes, read_input_shell}; use std::env::args; use std::fs; use std::fs::create_dir_all; @@ -33,7 +34,6 @@ use std::io::Write; use std::path::Path; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; -use crate::wizard::{is_yes, read_input_shell}; //use getopts::{Options, Matches}; @@ -374,7 +374,7 @@ fn parse_opt(matches: &Matches, output: &mut Vec, opt: &str, command: &s pub fn add_description(shell: &str, mut output: &mut Vec, skip: bool, modulename: &str) { if !skip { let desc = read_input_shell(&format!(" * Enter a description for the module {}: ", modulename), shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); output.push(format!("description(\"{}\");", desc)); } @@ -384,7 +384,7 @@ pub fn add_description(shell: &str, mut output: &mut Vec, skip: bool, mo shell, )) { let desc = read_input_shell(" Enter your description: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); output.push(format!("description(\"{}\");", desc)); add_description(shell, &mut output, true, modulename); @@ -395,7 +395,7 @@ pub fn add_description(shell: &str, mut output: &mut Vec, skip: bool, mo pub fn add_path(shell: &str, mut output: &mut Vec, skip: bool) { if !skip { let val = read_input_shell(" Enter the path where the executables can be found: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); output.push(format!("prepend_path(\"PATH\",\"{}\");", val)); if is_yes(&read_input_shell( @@ -403,7 +403,7 @@ pub fn add_path(shell: &str, mut output: &mut Vec, skip: bool) { shell, )) { let val = read_input_shell(" Enter the path where the libraries can be found: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); output.push(format!("prepend_path(\"LD_LIBRARY_PATH\",\"{}\");", val)); } @@ -414,10 +414,10 @@ pub fn add_path(shell: &str, mut output: &mut Vec, skip: bool) { shell, )) { let var = read_input_shell(" Enter the name of variable: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); let val = read_input_shell(" Enter the path you want to add: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); output.push(format!("prepend_path(\"{}\",\"{}\");", var, val)); add_path(shell, &mut output, true); @@ -433,7 +433,7 @@ fn select_modulepath(shell: &str) -> String { modulepaths[0].clone() } else if modulepaths.is_empty() { let modulepath = read_input_shell(" * Enter the path where you want to install this module: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); if !Path::new(&modulepath).is_dir() { if is_yes(&read_input_shell( @@ -460,7 +460,7 @@ fn select_modulepath(shell: &str) -> String { counter += 1; } let modulepath_num = read_input_shell("\n * Select the modulepath where you want to install this module: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); let modulepath_num = match modulepath_num.parse::() { @@ -522,11 +522,11 @@ pub fn run_create_wizard(shell: &str, mut _output: &mut Vec) -> String { // todo: tabcompletion // https://github.com/shaleh/rust-readline/blob/master/examples/fileman.rs let folder = read_input_shell(" * Enter the folder where the modulefile will be saved: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); let modulename = read_input_shell(" * Enter the name of the module: ", shell) - .trim_right_matches('\n') + .trim_end_matches('\n') .to_string(); // set root installation dir diff --git a/src/script.rs b/src/script.rs old mode 100644 new mode 100755 index 5a51aa2..7f17f75 --- a/src/script.rs +++ b/src/script.rs @@ -50,6 +50,7 @@ lazy_static! { static ref INFO_LD_LIBRARY_PATH: Mutex> = Mutex::new(vec![]); static ref INFO_PYTHONPATH: Mutex> = Mutex::new(vec![]); static ref INFO_PERL5LIB: Mutex> = Mutex::new(vec![]); + static ref INFO_BIN: Mutex> = Mutex::new(vec![]); static ref LOAD: Mutex> = Mutex::new(vec![]); } @@ -133,6 +134,11 @@ fn source(wanted_shell: String, path: String) { add_to_commands(&format!("source \"{}\"", path)); } } + +fn info_bin(bin: String) { + INFO_BIN.lock().unwrap().push(bin.to_string()); +} + // dummy functions for unloading #[allow(unused_variables)] #[cfg_attr(feature = "cargo-clippy", allow(clippy::needless_pass_by_value))] @@ -164,6 +170,8 @@ fn getenv_dummy(var: String) -> String { fn print_dummy(_msg: String) {} #[cfg_attr(feature = "cargo-clippy", allow(clippy::needless_pass_by_value))] fn source_dummy(_wanted_shell: String, _path: String) {} +#[cfg_attr(feature = "cargo-clippy", allow(clippy::needless_pass_by_value))] +fn info_bin_dummy(_bin: String) {} #[allow(unused_variables)] #[cfg_attr(feature = "cargo-clippy", allow(clippy::needless_pass_by_value))] fn prepend_path_dummy(var: String, val: String) {} @@ -471,6 +479,7 @@ pub fn run(path: &PathBuf, action: &str) { engine.register_fn("is_loaded", is_loaded_dummy); engine.register_fn("print", print_dummy); engine.register_fn("source", source_dummy); + engine.register_fn("add_bin_to_info", info_bin_dummy); } else if action == "load" { engine.register_fn("setenv", setenv); engine.register_fn("unsetenv", unsetenv); @@ -487,6 +496,7 @@ pub fn run(path: &PathBuf, action: &str) { engine.register_fn("is_loaded", is_loaded); engine.register_fn("print", print); engine.register_fn("source", source); + engine.register_fn("add_bin_to_info", info_bin_dummy); } else if action == "info" { engine.register_fn("setenv", setenv_info); engine.register_fn("unsetenv", unsetenv_dummy); @@ -503,6 +513,7 @@ pub fn run(path: &PathBuf, action: &str) { engine.register_fn("is_loaded", is_loaded); engine.register_fn("print", print_dummy); engine.register_fn("source", source_dummy); + engine.register_fn("add_bin_to_info", info_bin); } else if action == "description" { engine.register_fn("setenv", setenv_dummy); engine.register_fn("unsetenv", unsetenv_dummy); @@ -519,6 +530,7 @@ pub fn run(path: &PathBuf, action: &str) { engine.register_fn("is_loaded", is_loaded); engine.register_fn("print", print_dummy); engine.register_fn("source", source_dummy); + engine.register_fn("add_bin_to_info", info_bin_dummy); } else if action == "readme" { engine.register_fn("setenv", setenv_readme); engine.register_fn("unsetenv", unsetenv_dummy); @@ -535,6 +547,7 @@ pub fn run(path: &PathBuf, action: &str) { engine.register_fn("is_loaded", is_loaded); engine.register_fn("print", print_dummy); engine.register_fn("source", source_dummy); + engine.register_fn("add_bin_to_info", info_bin_dummy); } match engine.eval_file::(path.to_string_lossy().into_owned().as_ref()) { @@ -700,44 +713,52 @@ pub fn get_info(shell: &str, module: &str) -> Vec { } let mut execs: Vec = Vec::new(); - for line in INFO_PATH.lock().unwrap().iter() { - if Path::new(line).is_dir() { - // if activate, activate.csh, activate.fish and activate_this.py exist - // then we are in a python virtualenv, we can skip the typical python - // binaries, we don't want to see them when we run 'module info program/arch/version' - - let is_virtual_env = is_virtual_env(PathBuf::from(line)); + if INFO_BIN.lock().unwrap().is_empty() { + for line in INFO_PATH.lock().unwrap().iter() { + if Path::new(line).is_dir() { + // if activate, activate.csh, activate.fish and activate_this.py exist + // then we are in a python virtualenv, we can skip the typical python + // binaries, we don't want to see them when we run 'module info program/arch/version' - let entries = match read_dir(line) { - Ok(entry) => entry, - Err(_) => continue, - }; + let is_virtual_env = is_virtual_env(PathBuf::from(line)); - for entry in entries { - let path = match &entry { - Ok(p) => p.path(), + let entries = match read_dir(line) { + Ok(entry) => entry, Err(_) => continue, }; - let file_name = match &entry { - Ok(p) => p.file_name(), - Err(_) => continue, - }; - - if is_python_binary(file_name) && is_virtual_env { - continue; - } - - if path.is_dir() { - continue; - } - - if path.is_executable() { - execs.push(format!("echo '{}'", strip_dir(path.to_str().unwrap()))); - got_output = true; + for entry in entries { + let path = match &entry { + Ok(p) => p.path(), + Err(_) => continue, + }; + + let file_name = match &entry { + Ok(p) => p.file_name(), + Err(_) => continue, + }; + + if is_python_binary(file_name) && is_virtual_env { + continue; + } + + if path.is_dir() { + continue; + } + + if path.is_executable() { + execs.push(format!("echo '{}'", strip_dir(path.to_str().unwrap()))); + got_output = true; + } } } } + } else { + let bins: Vec = INFO_BIN.lock().unwrap().to_vec(); + for bin in bins { + execs.push(format!("echo '{}'", bin)); + got_output = true; + } } if !execs.is_empty() { diff --git a/src/wizard.rs b/src/wizard.rs old mode 100644 new mode 100755 index 82425cf..2fb8c93 --- a/src/wizard.rs +++ b/src/wizard.rs @@ -461,7 +461,7 @@ pub fn run(recursive: bool) -> bool { line.truncate(len - 1); path = line.as_ref(); */ - path = line.trim_right_matches('\n'); + path = line.trim_end_matches('\n'); } if Path::new(path).is_dir() {