Skip to content

Commit

Permalink
feat: Add support for Daml (starship#4004)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanobaghino authored May 26, 2022
1 parent 7702b0a commit 3fe6cc0
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 1 deletion.
67 changes: 67 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,25 @@
}
]
},
"daml": {
"default": {
"detect_extensions": [],
"detect_files": [
"daml.yaml"
],
"detect_folders": [],
"disabled": false,
"format": "via [$symbol($version )]($style)",
"style": "bold cyan",
"symbol": "Λ ",
"version_format": "v${raw}"
},
"allOf": [
{
"$ref": "#/definitions/DamlConfig"
}
]
},
"dart": {
"default": {
"detect_extensions": [
Expand Down Expand Up @@ -2015,6 +2034,54 @@
}
}
},
"DamlConfig": {
"type": "object",
"properties": {
"symbol": {
"default": "Λ ",
"type": "string"
},
"format": {
"default": "via [$symbol($version )]($style)",
"type": "string"
},
"version_format": {
"default": "v${raw}",
"type": "string"
},
"style": {
"default": "bold cyan",
"type": "string"
},
"disabled": {
"default": false,
"type": "boolean"
},
"detect_extensions": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"detect_files": {
"default": [
"daml.yaml"
],
"type": "array",
"items": {
"type": "string"
}
},
"detect_folders": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
}
},
"DartConfig": {
"type": "object",
"properties": {
Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/bracketed-segments.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ format = '\[[$symbol$environment]($style)\]'
[crystal]
format = '\[[$symbol($version)]($style)\]'

[daml]
format = '\[[$symbol($version)]($style)\]'

[dart]
format = '\[[$symbol($version)]($style)\]'

Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/no-runtime-versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ format = "via [$symbol]($style)"
[crystal]
format = "via [$symbol]($style)"

[daml]
format = "via [$symbol]($style)"

[dart]
format = "via [$symbol]($style)"

Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/plain-text-symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ symbol = "cr "
[cmake]
symbol = "cmake "

[daml]
symbol = "daml "

[dart]
symbol = "dart "

Expand Down
46 changes: 45 additions & 1 deletion docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ $c\
$cmake\
$cobol\
$container\
$daml\
$dart\
$deno\
$dotnet\
Expand Down Expand Up @@ -845,6 +846,48 @@ By default the module will be shown if any of the following conditions are met:
format = "via [✨ $version](bold blue) "
```

## Daml

The `daml` module shows the currently used [Daml](https://www.digitalasset.com/developers)
SDK version when you are in the root directory of your Daml project. The `sdk-version` in
the `daml.yaml` file will be used, unless it's overridden by the `DAML_SDK_VERSION`
environment variable.
By default the module will be shown if any of the following conditions are met:

- The current directory contains a `daml.yaml` file

### Options

| Option | Default | Description |
| ------------------- | ---------------------------------- | ------------------------------------------------------------------------- |
| `format` | `via [$symbol($version )]($style)` | The format for the module. |
| `version_format` | `v${raw}` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `symbol` | `"Λ "` | A format string representing the symbol of Daml |
| `style` | `"bold cyan"` | The style for the module. |
| `detect_extensions` | `[]` | Which extensions should trigger this module. |
| `detect_files` | `["daml.yaml"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `disabled` | `false` | Disables the `daml` module. |

### Variables

| Variable | Example | Description |
| -------- | -------- | ------------------------------------ |
| version | `v2.2.0` | The version of `daml` |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |

*: This variable can only be used as a part of a style string

### Example

```toml
# ~/.config/starship.toml

[daml]
format = "via [D $version](bold bright-green) "
```

## Dart

The `dart` module shows the currently installed version of [Dart](https://dart.dev/).
Expand Down Expand Up @@ -2506,7 +2549,7 @@ symbol = "☁️ "

The `package` module is shown when the current directory is the repository for a
package, and shows its current version. The module currently supports `npm`, `nimble`, `cargo`,
`poetry`, `python`, `composer`, `gradle`, `julia`, `mix`, `helm`, `shards` and `dart` packages.
`poetry`, `python`, `composer`, `gradle`, `julia`, `mix`, `helm`, `shards`, `daml` and `dart` packages.

- [**npm**](https://docs.npmjs.com/cli/commands/npm) – The `npm` package version is extracted from the `package.json` present
in the current directory
Expand All @@ -2526,6 +2569,7 @@ package, and shows its current version. The module currently supports `npm`, `ni
- [**Shards**](https://crystal-lang.org/reference/the_shards_command/index.html) - The `shards` package version is extracted from the `shard.yml` present in the current directory
- [**V**](https://vlang.io) - The `vlang` package version is extracted from the `v.mod` present in the current directory
- [**SBT**](https://scala-sbt.org) - The `sbt` package version is extracted from the `build.sbt` present in the current directory
- [**Daml**](https://www.digitalasset.com/developers) - The `daml` package version is extracted from the `daml.yaml` present in the current directory
- [**Dart**](https://pub.dev/) - The `dart` package version is extracted from the `pubspec.yaml` present in the current directory

> ⚠️ The version being shown is that of the package whose source code is in your
Expand Down
30 changes: 30 additions & 0 deletions src/configs/daml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "config-schema", derive(schemars::JsonSchema))]
#[serde(default)]
pub struct DamlConfig<'a> {
pub symbol: &'a str,
pub format: &'a str,
pub version_format: &'a str,
pub style: &'a str,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
}

impl<'a> Default for DamlConfig<'a> {
fn default() -> Self {
DamlConfig {
symbol: "Λ ",
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
style: "bold cyan",
disabled: false,
detect_extensions: vec![],
detect_files: vec!["daml.yaml"],
detect_folders: vec![],
}
}
}
3 changes: 3 additions & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod conda;
pub mod container;
pub mod crystal;
pub mod custom;
pub mod daml;
pub mod dart;
pub mod deno;
pub mod directory;
Expand Down Expand Up @@ -114,6 +115,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)]
crystal: crystal::CrystalConfig<'a>,
#[serde(borrow)]
daml: daml::DamlConfig<'a>,
#[serde(borrow)]
dart: dart::DartConfig<'a>,
#[serde(borrow)]
deno: deno::DenoConfig<'a>,
Expand Down
1 change: 1 addition & 0 deletions src/configs/starship_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"c",
"cmake",
"cobol",
"daml",
"dart",
"deno",
"dotnet",
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub const ALL_MODULES: &[&str] = &[
"conda",
"container",
"crystal",
"daml",
"dart",
"deno",
"directory",
Expand Down
135 changes: 135 additions & 0 deletions src/modules/daml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use super::{Context, Module, ModuleConfig};
use crate::configs::daml::DamlConfig;
use crate::formatter::{StringFormatter, VersionFormatter};

const DAML_SDK_VERSION: &str = "sdk-version";
const DAML_SDK_VERSION_ENV: &str = "DAML_SDK_VERSION";
const DAML_YAML: &str = "daml.yaml";

/// Creates a module with the current Daml version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("daml");
let config: DamlConfig = DamlConfig::try_load(module.config);

let is_daml_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();

if !is_daml_project {
return None;
}

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 {
"version" => {
let daml_sdk_version = get_daml_sdk_version(context)?;
VersionFormatter::format_module_version(
module.get_name(),
&daml_sdk_version,
config.version_format,
)
.map(Ok)
}
_ => None,
})
.parse(None, Some(context))
});

module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `daml`:\n{}", error);
return None;
}
});

Some(module)
}

fn get_daml_sdk_version(context: &Context) -> Option<String> {
context
.get_env(DAML_SDK_VERSION_ENV)
.or_else(|| read_sdk_version_from_daml_yaml(context))
}

fn read_sdk_version_from_daml_yaml(context: &Context) -> Option<String> {
let file_contents = context.read_file_from_pwd(DAML_YAML)?;
let daml_yaml = yaml_rust::YamlLoader::load_from_str(&file_contents).ok()?;
let sdk_version = daml_yaml.first()?[DAML_SDK_VERSION].as_str()?;
Some(sdk_version.to_string())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test::ModuleRenderer;
use ansi_term::Color;
use std::fs::File;
use std::io::{Result, Write};

#[test]
fn folder_without_daml_yaml() -> Result<()> {
let dir = tempfile::tempdir()?;
let actual = ModuleRenderer::new("daml").path(dir.path()).collect();
let expected = None;
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_daml_yaml() -> Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(DAML_YAML))?.write_all(b"sdk-version: 2.2.0\n")?;
let actual = ModuleRenderer::new("daml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Cyan.bold().paint("Λ v2.2.0 ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_daml_yaml_without_sdk_version_entry() -> Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(DAML_YAML))?.write_all(b"version: 1.0.0\n")?;
let actual = ModuleRenderer::new("daml").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Cyan.bold().paint("Λ ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_daml_yaml_and_daml_sdk_version() -> Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(DAML_YAML))?.write_all(b"sdk-version: 2.2.0\n")?;
let actual = ModuleRenderer::new("daml")
.env(DAML_SDK_VERSION_ENV, "2.0.0")
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Cyan.bold().paint("Λ v2.0.0 ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_without_daml_yaml_with_daml_sdk_version() -> Result<()> {
let dir = tempfile::tempdir()?;
let actual = ModuleRenderer::new("daml")
.env(DAML_SDK_VERSION_ENV, "2.0.0")
.path(dir.path())
.collect();
let expected = None;
assert_eq!(expected, actual);
dir.close()
}
}
Loading

0 comments on commit 3fe6cc0

Please sign in to comment.