Skip to content

Commit

Permalink
* added add_to_info_bin function, this overrides the binaries in the …
Browse files Browse the repository at this point in the history
…PATH variable for module info

* removed some deprecated functions
* fmt
  • Loading branch information
Frederik Delaere committed Mar 12, 2019
1 parent 5f5afc7 commit dc6fc41
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 43 deletions.
5 changes: 5 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 <modulename>` will only
show the binaries that are added with this function, it will ignore the binaries in your PATH variables.

### Example modulefile

Expand Down
2 changes: 1 addition & 1 deletion src/cache.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 11 additions & 11 deletions src/manage.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand 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};

Expand Down Expand Up @@ -374,7 +374,7 @@ fn parse_opt(matches: &Matches, output: &mut Vec<String>, opt: &str, command: &s
pub fn add_description(shell: &str, mut output: &mut Vec<String>, 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));
}
Expand All @@ -384,7 +384,7 @@ pub fn add_description(shell: &str, mut output: &mut Vec<String>, 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);
Expand All @@ -395,15 +395,15 @@ pub fn add_description(shell: &str, mut output: &mut Vec<String>, skip: bool, mo
pub fn add_path(shell: &str, mut output: &mut Vec<String>, 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(
" * Do you want to set the LD_LIBRARY_PATH variable? [Y/n]: ",
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));
}
Expand All @@ -414,10 +414,10 @@ pub fn add_path(shell: &str, mut output: &mut Vec<String>, 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);
Expand All @@ -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(
Expand All @@ -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::<usize>() {
Expand Down Expand Up @@ -522,11 +522,11 @@ pub fn run_create_wizard(shell: &str, mut _output: &mut Vec<String>) -> 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
Expand Down
81 changes: 51 additions & 30 deletions src/script.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ lazy_static! {
static ref INFO_LD_LIBRARY_PATH: Mutex<Vec<String>> = Mutex::new(vec![]);
static ref INFO_PYTHONPATH: Mutex<Vec<String>> = Mutex::new(vec![]);
static ref INFO_PERL5LIB: Mutex<Vec<String>> = Mutex::new(vec![]);
static ref INFO_BIN: Mutex<Vec<String>> = Mutex::new(vec![]);
static ref LOAD: Mutex<Vec<String>> = Mutex::new(vec![]);
}

Expand Down Expand Up @@ -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))]
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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::<String>(path.to_string_lossy().into_owned().as_ref()) {
Expand Down Expand Up @@ -700,44 +713,52 @@ pub fn get_info(shell: &str, module: &str) -> Vec<String> {
}

let mut execs: Vec<String> = 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<String> = INFO_BIN.lock().unwrap().to_vec();
for bin in bins {
execs.push(format!("echo '{}'", bin));
got_output = true;
}
}

if !execs.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/wizard.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit dc6fc41

Please sign in to comment.