summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrΓ© Zanellato <30451287+AZanellato@users.noreply.github.com>2019-08-13 19:43:29 -0300
committerMatan Kushner <hello@matchai.me>2019-08-13 18:43:29 -0400
commitb06249d61c98813bf56c5c9fdc7d974a35b61dad (patch)
treebf13e98ea264925b7a99e762e4be36ae67cc5cff
parentf10bfe4616794967e9014c978c3819e91634e4d6 (diff)
feat: implement the ruby module (#131)
-rw-r--r--README.md1
-rw-r--r--ci/setup-test-env.yml7
-rw-r--r--docs/config/README.md23
-rw-r--r--src/modules/mod.rs2
-rw-r--r--src/modules/ruby.rs59
-rw-r--r--tests/Dockerfile10
-rw-r--r--tests/testsuite/main.rs1
-rw-r--r--tests/testsuite/ruby.rs52
8 files changed, 155 insertions, 0 deletions
diff --git a/README.md b/README.md
index 14fac3607..2d376bc5d 100644
--- a/README.md
+++ b/README.md
@@ -30,6 +30,7 @@ The prompt shows information you need while you're working, while staying sleek
- Current username if not the same as the logged-in user.
- Current Node.js version(`β¬’`).
- Current Rust version (`πŸ¦€`).
+- Current Ruby version (`πŸ’Ž`).
- Current Python version (`🐍`).
- Current Go version (`🐹`).
- Package version of package in current directory (`πŸ“¦`).
diff --git a/ci/setup-test-env.yml b/ci/setup-test-env.yml
index e36bca304..e91e9453d 100644
--- a/ci/setup-test-env.yml
+++ b/ci/setup-test-env.yml
@@ -4,12 +4,19 @@ steps:
inputs:
versionSpec: "12.0.0"
displayName: "Install a fixed version of Node"
+
# Install Go
- task: GoTool@0
inputs:
versionSpec: "1.10"
displayName: "Install a fixed version of Go"
+ # Install Ruby
+ - task: UseRubyVersion@0
+ inputs:
+ versionSpec: "2.5.5"
+ displayName: "Install a fixed version of Ruby"
+
# We are using pyenv to install Python for integration tests
# Install Python
- script: |
diff --git a/docs/config/README.md b/docs/config/README.md
index 1f32e2350..0c92e9361 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -292,6 +292,29 @@ The `line_break` module separates the prompt into two lines.
disabled = true
```
+## Ruby
+
+The `ruby` module shows the currently installed version of NodeJS.
+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 `.rb` file
+
+### Options
+
+| Variable | Default | Description |
+| ---------- | ------- | -------------------------------------------------------- |
+| `disabled` | `false` | Disables the `ruby` module. |
+
+### Example
+
+```toml
+# ~/.config/starship.toml
+
+[ruby]
+disabled = false
+```
+
## NodeJS
The `nodejs` module shows the currently installed version of NodeJS.
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 35bbe2d31..3ab7d8b3f 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -10,6 +10,7 @@ mod line_break;
mod nodejs;
mod package;
mod python;
+mod ruby;
mod rust;
mod username;
@@ -23,6 +24,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"nodejs" => nodejs::module(context),
"rust" => rust::module(context),
"python" => python::module(context),
+ "ruby" => ruby::module(context),
"golang" => golang::module(context),
"line_break" => line_break::module(context),
"package" => package::module(context),
diff --git a/src/modules/ruby.rs b/src/modules/ruby.rs
new file mode 100644
index 000000000..ff3945360
--- /dev/null
+++ b/src/modules/ruby.rs
@@ -0,0 +1,59 @@
+use ansi_term::Color;
+use std::process::Command;
+
+use super::{Context, Module};
+
+/// 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
+/// - Current directory contains a `Gemfile` file
+pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
+ let is_rb_project = context
+ .new_scan_dir()
+ .set_files(&["Gemfile"])
+ .set_extensions(&["rb"])
+ .scan();
+
+ if !is_rb_project {
+ return None;
+ }
+
+ match get_ruby_version() {
+ Some(ruby_version) => {
+ const RUBY_CHAR: &str = "πŸ’Ž ";
+ let module_color = Color::Red.bold();
+
+ let mut module = context.new_module("ruby")?;
+ module.set_style(module_color);
+
+ let formatted_version = format_ruby_version(&ruby_version)?;
+ module.new_segment("symbol", RUBY_CHAR);
+ module.new_segment("version", &formatted_version);
+
+ Some(module)
+ }
+ None => None,
+ }
+}
+
+fn get_ruby_version() -> Option<String> {
+ match Command::new("ruby").arg("-v").output() {
+ Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
+ Err(_) => None,
+ }
+}
+
+fn format_ruby_version(ruby_version: &str) -> Option<String> {
+ let version = ruby_version
+ // split into ["ruby", "2.6.0p0", "linux/amd64"]
+ .split_whitespace()
+ // return "2.6.0p0"
+ .nth(1)?
+ .get(0..5)?;
+
+ let mut formatted_version = String::with_capacity(version.len() + 1);
+ formatted_version.push('v');
+ formatted_version.push_str(version);
+ Some(formatted_version)
+}
diff --git a/tests/Dockerfile b/tests/Dockerfile
index 03c230303..8a9dfae00 100644
--- a/tests/Dockerfile
+++ b/tests/Dockerfile
@@ -27,6 +27,16 @@ RUN git clone https://github.com/syndbg/goenv.git $GOENV_ROOT \
# Check that Go was correctly installed
RUN go version
+# Install Ruby
+ENV RUBY_VERSION 2.5.5
+ENV RBENV_ROOT /home/nonroot/.rbenv
+ENV PATH $RBENV_ROOT/bin:$RBENV_ROOT/shims:$PATH
+RUN curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash \
+ && rbenv install $RUBY_VERSION \
+ && rbenv global $RUBY_VERSION
+# Check that Ruby was correctly installed
+RUN ruby --version
+
# Install Python
ENV PYTHON_VERSION 3.6.9
ENV PYENV_ROOT /home/nonroot/.pyenv
diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs
index a2d47ac25..cbb1d9b69 100644
--- a/tests/testsuite/main.rs
+++ b/tests/testsuite/main.rs
@@ -8,4 +8,5 @@ mod jobs;
mod line_break;
mod nodejs;
mod python;
+mod ruby;
mod username;
diff --git a/tests/testsuite/ruby.rs b/tests/testsuite/ruby.rs
new file mode 100644
index 000000000..f8a4356a1
--- /dev/null
+++ b/tests/testsuite/ruby.rs
@@ -0,0 +1,52 @@
+use ansi_term::Color;
+use std::fs::{self, File};
+use std::io;
+
+use crate::common;
+
+#[test]
+fn folder_without_ruby_files() -> io::Result<()> {
+ let dir = common::new_tempdir()?;
+
+ let output = common::render_module("ruby")
+ .arg("--path")
+ .arg(dir.path())
+ .output()?;
+ let actual = String::from_utf8(output.stdout).unwrap();
+
+ let expected = "";
+ assert_eq!(expected, actual);
+ Ok(())
+}
+
+#[test]
+fn folder_with_gemfile() -> io::Result<()> {
+ let dir = common::new_tempdir()?;
+ File::create(dir.path().join("Gemfile"))?;
+
+ let output = common::render_module("ruby")
+ .arg("--path")
+ .arg(dir.path())
+ .output()?;
+ let actual = String::from_utf8(output.stdout).unwrap();
+
+ let expected = format!("via {} ", Color::Red.bold().paint("πŸ’Ž v2.5.5"));
+ assert_eq!(expected, actual);
+ Ok(())
+}
+
+#[test]
+fn folder_with_rb_file() -> io::Result<()> {
+ let dir = common::new_tempdir()?;
+ File::create(dir.path().join("any.rb"))?;
+
+ let output = common::render_module("ruby")
+ .arg("--path")
+ .arg(dir.path())
+ .output()?;
+ let actual = String::from_utf8(output.stdout).unwrap();
+
+ let expected = format!("via {} ", Color::Red.bold().paint("πŸ’Ž v2.5.5"));
+ assert_eq!(expected, actual);
+ Ok(())
+}