fix(rlang): module not working on MacOs (#2880)

Fix rlang module and refactor other modules to use new method.
This commit is contained in:
filip 2021-08-02 19:13:29 +02:00 committed by GitHub
parent 749593e614
commit b1db771baa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 39 deletions

View File

@ -1,5 +1,6 @@
use crate::configs::java::JavaConfig;
use crate::formatter::{StringFormatter, VersionFormatter};
use crate::utils::get_command_string_output;
use std::path::PathBuf;
use super::{Context, Module, RootModuleConfig};
@ -64,12 +65,8 @@ fn get_java_version(context: &Context, config: &JavaConfig) -> Option<String> {
})
.unwrap_or_else(|| String::from("java"));
let output = context.exec_cmd(&java_command, &["-Xinternalversion"])?;
let java_version = if output.stdout.is_empty() {
output.stderr
} else {
output.stdout
};
let command = context.exec_cmd(&java_command, &["-Xinternalversion"])?;
let java_version = get_command_string_output(command);
format_java_version(&java_version, config.version_format)
}

View File

@ -3,6 +3,7 @@ use super::{Context, Module, RootModuleConfig};
use crate::configs::kotlin::KotlinConfig;
use crate::formatter::StringFormatter;
use crate::formatter::VersionFormatter;
use crate::utils::get_command_string_output;
use regex::Regex;
const KOTLIN_VERSION_PATTERN: &str = "(?P<version>[\\d\\.]+[\\d\\.]+[\\d\\.]+)";
@ -60,17 +61,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
fn get_kotlin_version(context: &Context, kotlin_binary: &str) -> Option<String> {
match context.exec_cmd(kotlin_binary, &["-version"]) {
Some(output) => {
let kotlin_output = if output.stdout.is_empty() {
output.stderr
} else {
output.stdout
};
parse_kotlin_version(&kotlin_output)
}
None => None,
}
let command = context.exec_cmd(kotlin_binary, &["-version"])?;
let kotlin_version = get_command_string_output(command);
parse_kotlin_version(&kotlin_version)
}
fn parse_kotlin_version(kotlin_stdout: &str) -> Option<String> {

View File

@ -3,6 +3,7 @@ use super::{Context, Module, RootModuleConfig};
use crate::configs::lua::LuaConfig;
use crate::formatter::StringFormatter;
use crate::formatter::VersionFormatter;
use crate::utils::get_command_string_output;
/// Creates a module with the current Lua version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -57,12 +58,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
fn get_lua_version(context: &Context, lua_binary: &str) -> Option<String> {
let output = context.exec_cmd(lua_binary, &["-v"])?;
let lua_version = if output.stdout.is_empty() {
output.stderr
} else {
output.stdout
};
let command = context.exec_cmd(lua_binary, &["-v"])?;
let lua_version = get_command_string_output(command);
parse_lua_version(&lua_version)
}

View File

@ -5,6 +5,7 @@ use super::{Context, Module, RootModuleConfig};
use crate::configs::python::PythonConfig;
use crate::formatter::StringFormatter;
use crate::formatter::VersionFormatter;
use crate::utils::get_command_string_output;
/// Creates a module with the current Python version and, if active, virtual environment.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -73,13 +74,7 @@ fn get_python_version(context: &Context, config: &PythonConfig) -> Option<String
.0
.iter()
.find_map(|binary| context.exec_cmd(binary, &["--version"]))
.map(|output| {
if output.stdout.is_empty() {
output.stderr
} else {
output.stdout
}
})?;
.map(get_command_string_output)?;
format_python_version(&version, config.version_format)
}

View File

@ -3,6 +3,7 @@ use crate::formatter::VersionFormatter;
use crate::configs::rlang::RLangConfig;
use crate::formatter::StringFormatter;
use crate::utils::get_command_string_output;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("rlang");
@ -55,7 +56,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
fn get_r_version(context: &Context) -> Option<String> {
let r_version = context.exec_cmd("R", &["--version"])?.stderr;
let r_version = get_command_string_output(context.exec_cmd("R", &["--version"])?);
parse_version(&r_version)
}

View File

@ -3,6 +3,7 @@ use crate::formatter::StringFormatter;
use super::{Context, Module, RootModuleConfig};
use crate::formatter::VersionFormatter;
use crate::utils::get_command_string_output;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("scala");
@ -56,13 +57,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
fn get_scala_version(context: &Context) -> Option<String> {
let output = context.exec_cmd("scalac", &["-version"])?;
let scala_version = if output.stdout.is_empty() {
output.stderr
} else {
output.stdout
};
let command = context.exec_cmd("scalac", &["-version"])?;
let scala_version = get_command_string_output(command);
parse_scala_version(&scala_version)
}

View File

@ -18,12 +18,21 @@ pub fn read_file<P: AsRef<Path> + Debug>(file_name: P) -> Result<String> {
if result.is_err() {
log::debug!("Error reading file: {:?}", result);
} else {
log::trace!("File read sucessfully");
log::trace!("File read successfully");
};
result
}
/// Reads command output from stderr or stdout depending on to which stream program streamed it's output
pub fn get_command_string_output(command: CommandOutput) -> String {
if command.stdout.is_empty() {
command.stderr
} else {
command.stdout
}
}
/// Attempt to resolve `binary_name` from and creates a new `Command` pointing at it
/// This allows executing cmd files on Windows and prevents running executable from cwd on Windows
/// This function also initialises std{err,out,in} to protect against processes changing the console mode
@ -631,4 +640,17 @@ mod tests {
test
);
}
#[test]
fn test_get_command_string_output() {
let case1 = CommandOutput {
stdout: String::from("stdout"),
stderr: String::from("stderr"),
};
assert_eq!(get_command_string_output(case1), "stdout");
let case2 = CommandOutput {
stdout: String::from(""),
stderr: String::from("stderr"),
};
assert_eq!(get_command_string_output(case2), "stderr");
}
}