From 6f2c9fb397fcfb22829c043a23372afe1f877499 Mon Sep 17 00:00:00 2001 From: Andrew Prokhorenkov Date: Sat, 25 Jan 2020 00:48:39 -0600 Subject: [PATCH] feat: add Haskell Stack support (#546) Add a Haskell Stack module when a stack.yaml file is detected --- .github/workflows/workflow.yml | 17 ++++++++ README.md | 5 ++- docs/config/README.md | 25 ++++++++++++ src/configs/haskell.rs | 23 +++++++++++ src/configs/mod.rs | 1 + src/module.rs | 1 + src/modules/haskell.rs | 40 +++++++++++++++++++ src/modules/mod.rs | 2 + tests/testsuite/haskell.rs | 71 ++++++++++++++++++++++++++++++++++ tests/testsuite/main.rs | 1 + 10 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/configs/haskell.rs create mode 100644 src/modules/haskell.rs create mode 100644 tests/testsuite/haskell.rs diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 202e7bf5..c4900c6b 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -116,6 +116,23 @@ jobs: toolchain: stable override: true + # Install Stack at a fixed version (Linux & macOS version) + - name: Install Stack [-nix] + if: matrix.os != 'windows-latest' + uses: mstksg/setup-stack@v1 + + # Install Stack at a fixed version (Windows version), use Chocolatey + - name: Install Stack [-windows] + if: matrix.os == 'windows-latest' + uses: crazy-max/ghaction-chocolatey@v1 + with: + args: install haskell-stack -y + + - name: Install GHC version + env: + ARGS: --resolver nightly-2019-09-21 + run: stack $ARGS ghc -- --numeric-version --no-install-ghc + # Install Node.js at a fixed version - uses: actions/setup-node@v1 with: diff --git a/README.md b/README.md index 094befe9..2e612c89 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,9 @@ The prompt shows information you need while you're working, while staying sleek - Prompt character turns red if the last command exits with non-zero code - Current Go version (`馃惞`) -- Current Java version (`鈽昤) -- Current Node.js version (`猬) +- Current Haskell version (`位`) +- Current Java version(`鈽昤) +- Current Node.js version(`猬) - Current PHP version (`馃悩`) - Current Python version (`馃悕`) - Current Ruby version (`馃拵`) diff --git a/docs/config/README.md b/docs/config/README.md index 4de9ae7a..ebcaa600 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -99,6 +99,7 @@ prompt_order = [ "git_commit", "git_state", "git_status", + "haskell", "hg_branch", "package", "dotnet", @@ -610,6 +611,30 @@ The module will be shown if any of the following conditions are met: [golang] symbol = "馃弾馃挩 " ``` +## Haskell + +The `haskell` module shows the currently installed version of Haskell Stack version. +The module will be shown if any of the following conditions are met: + +- The current directory contains a `stack.yaml` file + +### Options + +| Variable | Default | Description | +| ---------- | ------------- | --------------------------------------------------------- | +| `symbol` | `"位 "` | The symbol used before displaying the version of Haskell. | +| `style` | `"bold red"` | The style for the module. | +| `disabled` | `false` | Disables the `haskell` module. | + + +### Example + +```toml +# ~/.config/starship.toml + +[haskell] +symbol = "位x.x " +``` ## Mercurial Branch diff --git a/src/configs/haskell.rs b/src/configs/haskell.rs new file mode 100644 index 00000000..b7e29e49 --- /dev/null +++ b/src/configs/haskell.rs @@ -0,0 +1,23 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct HaskellConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub version: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for HaskellConfig<'a> { + fn new() -> Self { + HaskellConfig { + symbol: SegmentConfig::new("位 "), + version: SegmentConfig::default(), + style: Color::Red.bold(), + disabled: false, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index dcf0337f..02ee00ef 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -11,6 +11,7 @@ pub mod git_commit; pub mod git_state; pub mod git_status; pub mod go; +pub mod haskell; pub mod hg_branch; pub mod hostname; pub mod java; diff --git a/src/module.rs b/src/module.rs index 3de985d6..34f3a652 100644 --- a/src/module.rs +++ b/src/module.rs @@ -22,6 +22,7 @@ pub const ALL_MODULES: &[&str] = &[ "git_state", "git_status", "golang", + "haskell", "hg_branch", "hostname", "java", diff --git a/src/modules/haskell.rs b/src/modules/haskell.rs new file mode 100644 index 00000000..ce0918cc --- /dev/null +++ b/src/modules/haskell.rs @@ -0,0 +1,40 @@ +use super::{Context, Module, RootModuleConfig, SegmentConfig}; + +use crate::configs::haskell::HaskellConfig; +use crate::utils; + +/// Creates a module with the current Haskell Stack version +/// +/// Will display the Haskell version if any of the following criteria are met: +/// - Current directory contains a `stack.yaml` file +pub fn module<'a>(context: &'a Context) -> Option> { + let is_haskell_project = context + .try_begin_scan()? + .set_files(&["stack.yaml"]) + .is_match(); + + if !is_haskell_project { + return None; + } + + let haskell_version = utils::exec_cmd( + "stack", + &["ghc", "--", "--numeric-version", "--no-install-ghc"], + )? + .stdout; + let formatted_version = format_haskell_version(&haskell_version)?; + + let mut module = context.new_module("haskell"); + let config: HaskellConfig = HaskellConfig::try_load(module.config); + module.set_style(config.style); + + module.create_segment("symbol", &config.symbol); + module.create_segment("version", &SegmentConfig::new(&formatted_version)); + + Some(module) +} + +fn format_haskell_version(haskell_version: &str) -> Option { + let formatted_version = format!("v{}", haskell_version.trim()); + Some(formatted_version) +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 0d206a8d..9c9f403c 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -11,6 +11,7 @@ mod git_commit; mod git_state; mod git_status; mod golang; +mod haskell; mod hg_branch; mod hostname; mod java; @@ -55,6 +56,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "git_state" => git_state::module(context), "git_status" => git_status::module(context), "golang" => golang::module(context), + "haskell" => haskell::module(context), "hg_branch" => hg_branch::module(context), "hostname" => hostname::module(context), "java" => java::module(context), diff --git a/tests/testsuite/haskell.rs b/tests/testsuite/haskell.rs new file mode 100644 index 00000000..11e90238 --- /dev/null +++ b/tests/testsuite/haskell.rs @@ -0,0 +1,71 @@ +use ansi_term::Color; +use dirs::home_dir; +use std::fs::{File, OpenOptions}; +use std::io::{self, Write}; +use tempfile::{self, TempDir}; + +use crate::common; + +#[test] +fn folder_without_stack_yaml() -> io::Result<()> { + let dir = tempfile::tempdir()?; + + let output = common::render_module("haskell") + .arg("--path") + .arg(dir.path()) + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + + let expected = ""; + assert_eq!(expected, actual); + Ok(()) +} + +#[test] +#[ignore] +#[cfg(not(windows))] +fn folder_with_stack_yaml() -> io::Result<()> { + let dir = tempfile::tempdir()?; + create_dummy_haskell_project(&dir, Some("nightly-2019-09-21 # Last GHC 8.6.5"))?; + + let output = if cfg!(windows) { + let mut app_data = home_dir().unwrap(); + app_data.push("AppData"); + app_data.push("Local"); + eprintln!("{}", app_data.to_str().unwrap()); + common::render_module("haskell") + .env("HOME", home_dir().unwrap()) + .env("LOCALAPPDATA", app_data) + .env("STACK_ROOT", r"C:\sr") + .arg("--path") + .arg(dir.path()) + .output()? + } else { + common::render_module("haskell") + .env("HOME", home_dir().unwrap()) + .arg("--path") + .arg(dir.path()) + .output()? + }; + + let actual = String::from_utf8(output.stdout).unwrap(); + + let expected = format!("via {} ", Color::Red.bold().paint("位 v8.6.5")); + assert_eq!(expected, actual); + Ok(()) +} + +fn create_dummy_haskell_project(folder: &TempDir, contents: Option<&str>) -> io::Result<()> { + let cabal_path = folder.path().join("test.cabal"); + File::create(cabal_path)?.sync_all()?; + + let stack_yaml_path = folder.path().join("stack.yaml"); + + let mut stack_yaml_file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&stack_yaml_path)?; + write!(stack_yaml_file, "resolver: {}", contents.unwrap_or(""))?; + stack_yaml_file.sync_data() +} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index f8192f41..b7cce943 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -12,6 +12,7 @@ mod git_commit; mod git_state; mod git_status; mod golang; +mod haskell; mod hg_branch; mod hostname; mod jobs;