feat(ruby): Add environment variable checks to ruby module (#3206)
* feat(ruby): Add environment variable checks to ruby module * docs(ruby): describe version detection method
This commit is contained in:
parent
2014bd3a12
commit
d1ce352528
|
@ -2642,6 +2642,9 @@ The module will be shown if any of the following conditions are met:
|
||||||
- The current directory contains a `Gemfile` file
|
- The current directory contains a `Gemfile` file
|
||||||
- The current directory contains a `.ruby-version` file
|
- The current directory contains a `.ruby-version` file
|
||||||
- The current directory contains a `.rb` file
|
- The current directory contains a `.rb` file
|
||||||
|
- The environment variables `RUBY_VERSION` or `RBENV_VERSION` are set
|
||||||
|
|
||||||
|
Starship gets the current Ruby version by running `ruby -v`.
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
|
@ -2653,6 +2656,7 @@ The module will be shown if any of the following conditions are met:
|
||||||
| `detect_extensions` | `["rb"]` | Which extensions should trigger this module. |
|
| `detect_extensions` | `["rb"]` | Which extensions should trigger this module. |
|
||||||
| `detect_files` | `["Gemfile", ".ruby-version"]` | Which filenames should trigger this module. |
|
| `detect_files` | `["Gemfile", ".ruby-version"]` | Which filenames should trigger this module. |
|
||||||
| `detect_folders` | `[]` | Which folders should trigger this module. |
|
| `detect_folders` | `[]` | Which folders should trigger this module. |
|
||||||
|
| `detect_variables` | `["RUBY_VERSION", "RBENV_VERSION"]` | Which environment variables should trigger this module. |
|
||||||
| `style` | `"bold red"` | The style for the module. |
|
| `style` | `"bold red"` | The style for the module. |
|
||||||
| `disabled` | `false` | Disables the `ruby` module. |
|
| `disabled` | `false` | Disables the `ruby` module. |
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ pub struct RubyConfig<'a> {
|
||||||
pub detect_extensions: Vec<&'a str>,
|
pub detect_extensions: Vec<&'a str>,
|
||||||
pub detect_files: Vec<&'a str>,
|
pub detect_files: Vec<&'a str>,
|
||||||
pub detect_folders: Vec<&'a str>,
|
pub detect_folders: Vec<&'a str>,
|
||||||
|
pub detect_variables: Vec<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Default for RubyConfig<'a> {
|
impl<'a> Default for RubyConfig<'a> {
|
||||||
|
@ -26,6 +27,7 @@ impl<'a> Default for RubyConfig<'a> {
|
||||||
detect_extensions: vec!["rb"],
|
detect_extensions: vec!["rb"],
|
||||||
detect_files: vec!["Gemfile", ".ruby-version"],
|
detect_files: vec!["Gemfile", ".ruby-version"],
|
||||||
detect_folders: vec![],
|
detect_folders: vec![],
|
||||||
|
detect_variables: vec!["RUBY_VERSION", "RBENV_VERSION"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ use crate::formatter::{StringFormatter, VersionFormatter};
|
||||||
/// Will display the Ruby version if any of the following criteria are met:
|
/// Will display the Ruby version if any of the following criteria are met:
|
||||||
/// - Current directory contains a `.rb` file
|
/// - Current directory contains a `.rb` file
|
||||||
/// - Current directory contains a `Gemfile` or `.ruby-version` file
|
/// - Current directory contains a `Gemfile` or `.ruby-version` file
|
||||||
|
/// - The environment variables `RUBY_VERSION` or `RBENV_VERSION` are set
|
||||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
let mut module = context.new_module("ruby");
|
let mut module = context.new_module("ruby");
|
||||||
let config = RubyConfig::try_load(module.config);
|
let config = RubyConfig::try_load(module.config);
|
||||||
|
@ -19,7 +20,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
.set_folders(&config.detect_folders)
|
.set_folders(&config.detect_folders)
|
||||||
.is_match();
|
.is_match();
|
||||||
|
|
||||||
if !is_rb_project {
|
let is_rb_env = &config
|
||||||
|
.detect_variables
|
||||||
|
.iter()
|
||||||
|
.any(|variable| context.get_env(variable).is_some());
|
||||||
|
|
||||||
|
if !is_rb_project && !is_rb_env {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,6 +136,33 @@ mod tests {
|
||||||
dir.close()
|
dir.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn with_ruby_version_env() -> io::Result<()> {
|
||||||
|
let dir = tempfile::tempdir()?;
|
||||||
|
let actual = ModuleRenderer::new("ruby")
|
||||||
|
.path(dir.path())
|
||||||
|
.env("RUBY_VERSION", "2.5.1")
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let expected = Some(format!("via {}", Color::Red.bold().paint("💎 v2.5.1 ")));
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
dir.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn with_rbenv_version_env() -> io::Result<()> {
|
||||||
|
let dir = tempfile::tempdir()?;
|
||||||
|
let actual = ModuleRenderer::new("ruby")
|
||||||
|
.path(dir.path())
|
||||||
|
.env("RBENV_VERSION", "2.6.8")
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// rbenv variable is only detected; its value is not used
|
||||||
|
let expected = Some(format!("via {}", Color::Red.bold().paint("💎 v2.5.1 ")));
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
dir.close()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_ruby_version() {
|
fn test_format_ruby_version() {
|
||||||
let config = RubyConfig::default();
|
let config = RubyConfig::default();
|
||||||
|
|
Loading…
Reference in New Issue