Skip to content

Commit

Permalink
fix(rlang): module not working on MacOs (starship#2880)
Browse files Browse the repository at this point in the history
Fix rlang module and refactor other modules to use new method.
  • Loading branch information
cymruu authored Aug 2, 2021
1 parent 749593e commit b1db771
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 39 deletions.
9 changes: 3 additions & 6 deletions src/modules/java.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 5 additions & 11 deletions src/modules/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\\.]+)";
Expand Down Expand Up @@ -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> {
Expand Down
9 changes: 3 additions & 6 deletions src/modules/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 2 additions & 7 deletions src/modules/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/rlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 3 additions & 7 deletions src/modules/scala.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)
}

Expand Down
24 changes: 23 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
}

0 comments on commit b1db771

Please sign in to comment.