From bd016b6ce2648d66c0d1db7aeed158e769f03383 Mon Sep 17 00:00:00 2001 From: Shu Kutsuzawa Date: Sat, 4 Apr 2020 04:02:28 +0900 Subject: [PATCH] feat: Enable to display language version when `.-version` file exists. (#1028) * adjust https://github.com/sonnym/elmenv * adjust https://github.com/syndbg/goenv * adjust https://github.com/jenv/jenv * adjust https://github.com/nodenv/nodenv * adjust https://github.com/phpenv/phpenv * adjust https://github.com/rbenv/rbenv * add description * golang.rs is executed rustfmt * add testcases --- docs/config/README.md | 7 ++++++- src/modules/elm.rs | 13 ++++++++++++- src/modules/golang.rs | 20 +++++++++++++++++++- src/modules/java.rs | 4 ++-- src/modules/nodejs.rs | 15 +++++++++++++-- src/modules/php.rs | 19 +++++++++++++++++-- src/modules/ruby.rs | 16 ++++++++++++++-- 7 files changed, 83 insertions(+), 11 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 3042691d8..3de40b5ee 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -470,6 +470,7 @@ The module will be shown if any of the following conditions are met: - The current directory contains a `elm.json` file - The current directory contains a `elm-package.json` file +- The current directory contains a `.elm-version` file - The current directory contains a `elm-stuff` folder - The current directory contains a `*.elm` files @@ -672,6 +673,7 @@ The module will be shown if any of the following conditions are met: - The current directory contains a `glide.yaml` file - The current directory contains a `Gopkg.yml` file - The current directory contains a `Gopkg.lock` file +- The current directory contains a `.go-version` file - The current directory contains a `Godeps` directory - The current directory contains a file with the `.go` extension @@ -749,7 +751,7 @@ disabled = false The `java` module shows the currently installed version of Java. The module will be shown if any of the following conditions are met: -- The current directory contains a `pom.xml`, `build.gradle.kts` or `build.sbt` file +- The current directory contains a `pom.xml`, `build.gradle.kts`, `build.sbt` or `.java-version` file - The current directory contains a file with the `.java`, `.class`, `.gradle` or `.jar` extension ### Options @@ -973,6 +975,7 @@ The `nodejs` 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 `package.json` file +- The current directory contains a `.node-version` file - The current directory contains a `node_modules` directory - The current directory contains a file with the `.js` extension @@ -1036,6 +1039,7 @@ The `php` module shows the currently installed version of PHP. The module will be shown if any of the following conditions are met: - The current directory contains a `composer.json` file +- The current directory contains a `.php-version` file - The current directory contains a `.php` file ### Options @@ -1102,6 +1106,7 @@ The `ruby` module shows the currently installed version of Ruby. 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 `.ruby-version` file - The current directory contains a `.rb` file ### Options diff --git a/src/modules/elm.rs b/src/modules/elm.rs index 8cfadfaa8..b6c1da4f9 100644 --- a/src/modules/elm.rs +++ b/src/modules/elm.rs @@ -8,12 +8,13 @@ use crate::utils; /// Will display the Elm version if any of the following criteria are met: /// - The current directory contains a `elm.json` file /// - The current directory contains a `elm-package.json` file +/// - The current directory contains a `.elm-version` file /// - The current directory contains a `elm-stuff` folder /// - The current directory contains a `*.elm` files pub fn module<'a>(context: &'a Context) -> Option> { let is_elm_project = context .try_begin_scan()? - .set_files(&["elm.json", "elm-package.json"]) + .set_files(&["elm.json", "elm-package.json", ".elm-version"]) .set_extensions(&["elm"]) .set_folders(&["elm-stuff"]) .is_match(); @@ -72,6 +73,16 @@ mod tests { dir.close() } + #[test] + fn folder_with_elm_version() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join(".elm-version"))?.sync_all()?; + let actual = render_module("elm", dir.path()); + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🌳 v0.19.1"))); + assert_eq!(expected, actual); + dir.close() + } + #[test] fn folder_with_elm_stuff_directory() -> io::Result<()> { let dir = tempfile::tempdir()?; diff --git a/src/modules/golang.rs b/src/modules/golang.rs index 4bfc17c67..809b9b1c9 100644 --- a/src/modules/golang.rs +++ b/src/modules/golang.rs @@ -11,12 +11,20 @@ use crate::utils; /// - Current directory contains a `glide.yaml` file /// - Current directory contains a `Gopkg.yml` file /// - Current directory contains a `Gopkg.lock` file +/// - Current directory contains a `.go-version` file /// - Current directory contains a `Godeps` directory /// - Current directory contains a file with the `.go` extension pub fn module<'a>(context: &'a Context) -> Option> { let is_go_project = context .try_begin_scan()? - .set_files(&["go.mod", "go.sum", "glide.yaml", "Gopkg.yml", "Gopkg.lock"]) + .set_files(&[ + "go.mod", + "go.sum", + "glide.yaml", + "Gopkg.yml", + "Gopkg.lock", + ".go-version", + ]) .set_extensions(&["go"]) .set_folders(&["Godeps"]) .is_match(); @@ -157,6 +165,16 @@ mod tests { assert_eq!(expected, actual); dir.close() } + #[test] + fn folder_with_go_version() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join(".go-version"))?.sync_all()?; + + let actual = render_module("golang", dir.path()); + let expected = Some(format!("via {} ", Color::Cyan.bold().paint("🐹 v1.12.1"))); + assert_eq!(expected, actual); + dir.close() + } #[test] fn test_format_go_version() { diff --git a/src/modules/java.rs b/src/modules/java.rs index 1b9e709b1..177e95451 100644 --- a/src/modules/java.rs +++ b/src/modules/java.rs @@ -9,11 +9,11 @@ use crate::utils; /// /// Will display the Java version if any of the following criteria are met: /// - Current directory contains a file with a `.java`, `.class`, `.gradle` or `.jar` extension -/// - Current directory contains a `pom.xml`, `build.gradle.kts` or `build.sbt` file +/// - Current directory contains a `pom.xml`, `build.gradle.kts`, `build.sbt` or `.java-version` file pub fn module<'a>(context: &'a Context) -> Option> { let is_java_project = context .try_begin_scan()? - .set_files(&["pom.xml", "build.gradle.kts", "build.sbt"]) + .set_files(&["pom.xml", "build.gradle.kts", "build.sbt", ".java-version"]) .set_extensions(&["java", "class", "jar", "gradle"]) .is_match(); diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index d78ea5236..bc8c2c36f 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -7,12 +7,12 @@ use crate::utils; /// /// Will display the Node.js version if any of the following criteria are met: /// - Current directory contains a `.js` file -/// - Current directory contains a `package.json` file +/// - Current directory contains a `package.json` or `.node-version` file /// - Current directory contains a `node_modules` directory pub fn module<'a>(context: &'a Context) -> Option> { let is_js_project = context .try_begin_scan()? - .set_files(&["package.json"]) + .set_files(&["package.json", ".node-version"]) .set_extensions(&["js"]) .set_folders(&["node_modules"]) .is_match(); @@ -63,6 +63,17 @@ mod tests { dir.close() } + #[test] + fn folder_with_node_version() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join(".node-version"))?.sync_all()?; + + let actual = render_module("nodejs", dir.path()); + let expected = Some(format!("via {} ", Color::Green.bold().paint("⬢ v12.0.0"))); + assert_eq!(expected, actual); + dir.close() + } + #[test] fn folder_with_js_file() -> io::Result<()> { let dir = tempfile::tempdir()?; diff --git a/src/modules/php.rs b/src/modules/php.rs index 878cdb2ae..59ee4c26e 100644 --- a/src/modules/php.rs +++ b/src/modules/php.rs @@ -7,11 +7,11 @@ use crate::utils; /// /// Will display the PHP version if any of the following criteria are met: /// - Current directory contains a `.php` file -/// - Current directory contains a `composer.json` file +/// - Current directory contains a `composer.json` or `.php-version` file pub fn module<'a>(context: &'a Context) -> Option> { let is_php_project = context .try_begin_scan()? - .set_files(&["composer.json"]) + .set_files(&["composer.json", ".php-version"]) .set_extensions(&["php"]) .is_match(); @@ -92,6 +92,21 @@ mod tests { dir.close() } + #[test] + fn folder_with_php_version() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join(".php-version"))?.sync_all()?; + + let actual = render_module("php", dir.path()); + + let expected = Some(format!( + "via {} ", + Color::Fixed(147).bold().paint("🐘 v7.3.8") + )); + assert_eq!(expected, actual); + dir.close() + } + #[test] fn folder_with_php_file() -> io::Result<()> { let dir = tempfile::tempdir()?; diff --git a/src/modules/ruby.rs b/src/modules/ruby.rs index f50e80256..152a72f0d 100644 --- a/src/modules/ruby.rs +++ b/src/modules/ruby.rs @@ -7,11 +7,11 @@ use crate::utils; /// /// 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 +/// - Current directory contains a `Gemfile` or `.ruby-version` file pub fn module<'a>(context: &'a Context) -> Option> { let is_rb_project = context .try_begin_scan()? - .set_files(&["Gemfile"]) + .set_files(&["Gemfile", ".ruby-version"]) .set_extensions(&["rb"]) .is_match(); @@ -78,6 +78,18 @@ mod tests { dir.close() } + #[test] + fn folder_with_ruby_version() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join(".ruby-version"))?.sync_all()?; + + let actual = render_module("ruby", dir.path()); + + let expected = Some(format!("via {} ", Color::Red.bold().paint("💎 v2.5.1"))); + assert_eq!(expected, actual); + dir.close() + } + #[test] fn folder_with_rb_file() -> io::Result<()> { let dir = tempfile::tempdir()?;