feat: add Haskell Stack support (#546)

Add a Haskell Stack module when a stack.yaml file is detected
This commit is contained in:
Andrew Prokhorenkov 2020-01-25 00:48:39 -06:00 committed by Kevin Song
parent f4c095de92
commit 6f2c9fb397
10 changed files with 184 additions and 2 deletions

View File

@ -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:

View File

@ -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 (`💎`)

View File

@ -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

23
src/configs/haskell.rs Normal file
View File

@ -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,
}
}
}

View File

@ -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;

View File

@ -22,6 +22,7 @@ pub const ALL_MODULES: &[&str] = &[
"git_state",
"git_status",
"golang",
"haskell",
"hg_branch",
"hostname",
"java",

40
src/modules/haskell.rs Normal file
View File

@ -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<Module<'a>> {
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<String> {
let formatted_version = format!("v{}", haskell_version.trim());
Some(formatted_version)
}

View File

@ -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<Module<'a>> {
"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),

View File

@ -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()
}

View File

@ -12,6 +12,7 @@ mod git_commit;
mod git_state;
mod git_status;
mod golang;
mod haskell;
mod hg_branch;
mod hostname;
mod jobs;