2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2019-10-19 01:51:38 +00:00
|
|
|
|
|
|
|
use crate::configs::ruby::RubyConfig;
|
2021-04-08 22:37:54 +00:00
|
|
|
use crate::formatter::{StringFormatter, VersionFormatter};
|
2019-08-13 22:43:29 +00:00
|
|
|
|
|
|
|
/// Creates a module with the current Ruby version
|
|
|
|
///
|
|
|
|
/// Will display the Ruby version if any of the following criteria are met:
|
|
|
|
/// - Current directory contains a `.rb` file
|
2020-04-03 19:02:28 +00:00
|
|
|
/// - Current directory contains a `Gemfile` or `.ruby-version` file
|
2021-11-08 20:21:09 +00:00
|
|
|
/// - The environment variables `RUBY_VERSION` or `RBENV_VERSION` are set
|
2019-08-13 22:43:29 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2021-02-21 17:01:01 +00:00
|
|
|
let mut module = context.new_module("ruby");
|
|
|
|
let config = RubyConfig::try_load(module.config);
|
|
|
|
|
2019-08-13 22:43:29 +00:00
|
|
|
let is_rb_project = context
|
2019-09-14 14:23:53 +00:00
|
|
|
.try_begin_scan()?
|
2021-02-21 17:01:01 +00:00
|
|
|
.set_files(&config.detect_files)
|
|
|
|
.set_extensions(&config.detect_extensions)
|
|
|
|
.set_folders(&config.detect_folders)
|
2019-09-14 14:23:53 +00:00
|
|
|
.is_match();
|
2019-08-13 22:43:29 +00:00
|
|
|
|
2021-11-08 20:21:09 +00:00
|
|
|
let is_rb_env = &config
|
|
|
|
.detect_variables
|
|
|
|
.iter()
|
|
|
|
.any(|variable| context.get_env(variable).is_some());
|
|
|
|
|
|
|
|
if !is_rb_project && !is_rb_env {
|
2019-08-13 22:43:29 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2021-04-08 22:37:54 +00:00
|
|
|
"version" => format_ruby_version(
|
2021-04-10 17:06:25 +00:00
|
|
|
&context.exec_cmd("ruby", &["-v"])?.stdout,
|
|
|
|
config.version_format,
|
2021-04-08 22:37:54 +00:00
|
|
|
)
|
|
|
|
.map(Ok),
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2019-08-13 22:43:29 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `ruby`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-08-13 22:43:29 +00:00
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
Some(module)
|
2019-08-13 22:43:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 17:06:25 +00:00
|
|
|
fn format_ruby_version(ruby_version: &str, version_format: &str) -> Option<String> {
|
2019-08-13 22:43:29 +00:00
|
|
|
let version = ruby_version
|
|
|
|
// split into ["ruby", "2.6.0p0", "linux/amd64"]
|
|
|
|
.split_whitespace()
|
|
|
|
// return "2.6.0p0"
|
|
|
|
.nth(1)?
|
2020-04-10 16:41:14 +00:00
|
|
|
// split into ["2.6.0", "0"]
|
|
|
|
.split('p')
|
|
|
|
// return "2.6.0"
|
|
|
|
.next()?;
|
2019-08-13 22:43:29 +00:00
|
|
|
|
2021-04-10 17:06:25 +00:00
|
|
|
match VersionFormatter::format_version(version, version_format) {
|
|
|
|
Ok(formatted) => Some(formatted),
|
|
|
|
Err(error) => {
|
2021-04-29 21:22:20 +00:00
|
|
|
log::warn!("Error formatting `ruby` version:\n{}", error);
|
2021-04-10 17:06:25 +00:00
|
|
|
Some(format!("v{}", version))
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 22:43:29 +00:00
|
|
|
}
|
2020-02-21 16:52:39 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-08-07 19:13:12 +00:00
|
|
|
use crate::test::ModuleRenderer;
|
2022-09-04 16:44:54 +00:00
|
|
|
use nu_ansi_term::Color;
|
2020-02-21 16:52:39 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_without_ruby_files() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ruby").path(dir.path()).collect();
|
2020-02-21 16:52:39 +00:00
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-02-21 16:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_gemfile() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("Gemfile"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ruby").path(dir.path()).collect();
|
2020-04-03 19:02:28 +00:00
|
|
|
|
2021-01-20 17:46:26 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Red.bold().paint("💎 v2.5.1 ")));
|
2020-04-03 19:02:28 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_ruby_version() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join(".ruby-version"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ruby").path(dir.path()).collect();
|
2020-02-21 16:52:39 +00:00
|
|
|
|
2021-01-20 17:46:26 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Red.bold().paint("💎 v2.5.1 ")));
|
2020-02-21 16:52:39 +00:00
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-02-21 16:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_rb_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.rb"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("ruby").path(dir.path()).collect();
|
2020-02-21 16:52:39 +00:00
|
|
|
|
2021-01-20 17:46:26 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Red.bold().paint("💎 v2.5.1 ")));
|
2020-02-21 16:52:39 +00:00
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-02-21 16:52:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 20:21:09 +00:00
|
|
|
#[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()
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:52:39 +00:00
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
fn test_format_ruby_version() {
|
2021-04-10 17:06:25 +00:00
|
|
|
let config = RubyConfig::default();
|
2020-04-10 16:41:14 +00:00
|
|
|
assert_eq!(
|
2021-04-08 22:37:54 +00:00
|
|
|
format_ruby_version(
|
2021-04-10 17:06:25 +00:00
|
|
|
"ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-darwin19.0]",
|
|
|
|
config.version_format
|
2021-04-08 22:37:54 +00:00
|
|
|
),
|
2020-04-10 16:41:14 +00:00
|
|
|
Some("v2.1.10".to_string())
|
|
|
|
);
|
2020-02-21 16:52:39 +00:00
|
|
|
assert_eq!(
|
2021-04-08 22:37:54 +00:00
|
|
|
format_ruby_version(
|
2021-04-10 17:06:25 +00:00
|
|
|
"ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]",
|
|
|
|
config.version_format
|
2021-04-08 22:37:54 +00:00
|
|
|
),
|
2020-02-21 16:52:39 +00:00
|
|
|
Some("v2.5.1".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
format_ruby_version(
|
2021-04-10 17:06:25 +00:00
|
|
|
"ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-musl]",
|
|
|
|
config.version_format
|
2020-02-21 16:52:39 +00:00
|
|
|
),
|
|
|
|
Some("v2.7.0".to_string())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|