summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDario Vladović <d.vladimyr@gmail.com>2021-05-07 16:22:18 +0200
committerGitHub <noreply@github.com>2021-05-07 10:22:18 -0400
commitc4f977c48d29790f27d332c32d97eff40fd258f1 (patch)
treec9104c2a0411b2cd30acad0799e98878c54cd7dc
parent23cc78237c5775c31fc3acd0f17a3840ad91a997 (diff)
feat(vlang): support `vpkg` (#2686)
-rw-r--r--docs/config/README.md20
-rw-r--r--src/configs/mod.rs2
-rw-r--r--src/configs/v.rs8
-rw-r--r--src/modules/package.rs56
-rw-r--r--src/modules/vlang.rs36
5 files changed, 90 insertions, 32 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index 86e206160..cec7bcbda 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -2846,19 +2846,19 @@ format = "via [⍱ $version](bold white) "
The `vlang` module shows you your currently installed version of V.
By default the module will be shown if any of the following conditions are met:
- The current directory contains a file with `.v` extension
-- The current directory contains a `v.mod` file
+- The current directory contains a `v.mod`, `vpkg.json` or `.vpkg-lock.json` file
### Options
-| Option | Default | Description |
-| ------------------- | ------------------------------------------------- | ----------------------------------------------- |
-| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
-| `symbol` | `"V "` | A format string representing the symbol of V |
-| `detect_extensions` | `["v"]` | Which extensions should trigger this module. |
-| `detect_files` | `["v.mod"]` | Which filenames should trigger this module. |
-| `detect_folders` | `[]` | Which folders should trigger this module. |
-| `style` | `"blue bold"` | The style for the module. |
-| `disabled` | `false` | Disables the `vlang` module. |
+| Option | Default | Description |
+| ------------------- | -------------------------------------------- | -------------------------------------------- |
+| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
+| `symbol` | `"V "` | A format string representing the symbol of V |
+| `detect_extensions` | `["v"]` | Which extensions should trigger this module. |
+| `detect_files` | `["v.mod", "vpkg.json", ".vpkg-lock.json" ]` | Which filenames should trigger this module. |
+| `detect_folders` | `[]` | Which folders should trigger this module. |
+| `style` | `"blue bold"` | The style for the module. |
+| `disabled` | `false` | Disables the `vlang` module. |
### Variables
diff --git a/src/configs/mod.rs b/src/configs/mod.rs
index 08fbbfa07..e307060da 100644
--- a/src/configs/mod.rs
+++ b/src/configs/mod.rs
@@ -129,7 +129,7 @@ pub struct FullConfig<'a> {
terraform: terraform::TerraformConfig<'a>,
time: time::TimeConfig<'a>,
username: username::UsernameConfig<'a>,
- vlang: v::VLangConfig<'a>,
+ vlang: v::VConfig<'a>,
vagrant: vagrant::VagrantConfig<'a>,
zig: zig::ZigConfig<'a>,
custom: IndexMap<String, custom::CustomConfig<'a>>,
diff --git a/src/configs/v.rs b/src/configs/v.rs
index dc8624c69..d49bad7fd 100644
--- a/src/configs/v.rs
+++ b/src/configs/v.rs
@@ -4,7 +4,7 @@ use serde::Serialize;
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)]
-pub struct VLangConfig<'a> {
+pub struct VConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
@@ -14,15 +14,15 @@ pub struct VLangConfig<'a> {
pub detect_folders: Vec<&'a str>,
}
-impl<'a> Default for VLangConfig<'a> {
+impl<'a> Default for VConfig<'a> {
fn default() -> Self {
- VLangConfig {
+ VConfig {
format: "via [$symbol($version )]($style)",
symbol: "V ",
style: "blue bold",
disabled: false,
detect_extensions: vec!["v"],
- detect_files: vec!["v.mod"],
+ detect_files: vec!["v.mod", "vpkg.json", ".vpkg-lock.json"],
detect_folders: vec![],
}
}
diff --git a/src/modules/package.rs b/src/modules/package.rs
index 1f95a7e0a..718ef21ea 100644
--- a/src/modules/package.rs
+++ b/src/modules/package.rs
@@ -54,13 +54,6 @@ fn extract_cargo_version(file_contents: &str) -> Option<String> {
Some(formatted_version)
}
-fn extract_vlang_version(file_contents: &str) -> Option<String> {
- let re = Regex::new(r"(?m)^\s*version\s*:\s*'(?P<version>[^']+)'").unwrap();
- let caps = re.captures(file_contents)?;
- let formatted_version = format_version(&caps["version"]);
- Some(formatted_version)
-}
-
fn extract_package_version(file_contents: &str, display_private: bool) -> Option<String> {
let package_json: json::Value = json::from_str(file_contents).ok()?;
@@ -184,6 +177,23 @@ fn extract_meson_version(file_contents: &str) -> Option<String> {
Some(formatted_version)
}
+fn extract_vmod_version(file_contents: &str) -> Option<String> {
+ let re = Regex::new(r"(?m)^\s*version\s*:\s*'(?P<version>[^']+)'").unwrap();
+ let caps = re.captures(file_contents)?;
+ let formatted_version = format_version(&caps["version"]);
+ Some(formatted_version)
+}
+
+fn extract_vpkg_version(file_contents: &str) -> Option<String> {
+ let vpkg_json: json::Value = json::from_str(file_contents).ok()?;
+ let version = vpkg_json.get("version")?.as_str()?;
+ if version == "null" {
+ return None;
+ }
+ let formatted_version = format_version(&version);
+ Some(formatted_version)
+}
+
fn get_package_version(base_dir: &Path, config: &PackageConfig) -> Option<String> {
if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) {
extract_cargo_version(&cargo_toml)
@@ -206,7 +216,9 @@ fn get_package_version(base_dir: &Path, config: &PackageConfig) -> Option<String
} else if let Ok(meson_build) = utils::read_file(base_dir.join("meson.build")) {
extract_meson_version(&meson_build)
} else if let Ok(vlang_mod) = utils::read_file(base_dir.join("v.mod")) {
- extract_vlang_version(&vlang_mod)
+ extract_vmod_version(&vlang_mod)
+ } else if let Ok(vlang_vpkg) = utils::read_file(base_dir.join("vpkg.json")) {
+ extract_vpkg_version(&vlang_vpkg)
} else {
None
}
@@ -828,20 +840,34 @@ end";
}
#[test]
- fn test_extract_vlang_version() -> io::Result<()> {
+ fn test_extract_vmod_version() -> io::Result<()> {
let config_name = "v.mod";
- let config_content = "
- Module {
- name: 'starship',
- author: 'matchai',
- version: '1.2.3'
- }";
+ let config_content = "\
+Module {
+ name: 'starship',
+ author: 'matchai',
+ version: '1.2.3'
+}";
let project_dir = create_project_dir()?;
fill_config(&project_dir, config_name, Some(&config_content))?;
expect_output(&project_dir, Some("v1.2.3"), None);
project_dir.close()
}
+ #[test]
+ fn test_extract_vpkg_version() -> io::Result<()> {
+ let config_name = "vpkg.json";
+ let config_content = json::json!({
+ "name": "starship",
+ "version": "0.1.0"
+ })
+ .to_string();
+ let project_dir = create_project_dir()?;
+ fill_config(&project_dir, config_name, Some(&config_content))?;
+ expect_output(&project_dir, Some("v0.1.0"), None);
+ project_dir.close()
+ }
+
fn create_project_dir() -> io::Result<TempDir> {
tempfile::tempdir()
}
diff --git a/src/modules/vlang.rs b/src/modules/vlang.rs
index 0d7d0e7b0..f516c7115 100644
--- a/src/modules/vlang.rs
+++ b/src/modules/vlang.rs
@@ -1,15 +1,17 @@
use super::{Context, Module, RootModuleConfig};
-use crate::configs::v::VLangConfig;
+use crate::configs::v::VConfig;
use crate::formatter::StringFormatter;
/// Creates a module with the current V version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("vlang");
- let config = VLangConfig::try_load(module.config);
+ let config = VConfig::try_load(module.config);
let is_v_project = context
.try_begin_scan()?
+ .set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
+ .set_folders(&config.detect_folders)
.is_match();
if !is_v_project {
@@ -89,4 +91,34 @@ mod tests {
assert_eq!(expected, actual);
dir.close()
}
+
+ #[test]
+ fn folder_with_vmod_file() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join("v.mod"))?.sync_all()?;
+ let actual = ModuleRenderer::new("vlang").path(dir.path()).collect();
+ let expected = Some(format!("via {}", Color::Blue.bold().paint("V v0.2 ")));
+ assert_eq!(expected, actual);
+ dir.close()
+ }
+
+ #[test]
+ fn folder_with_vpkg_file() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join("vpkg.json"))?.sync_all()?;
+ let actual = ModuleRenderer::new("vlang").path(dir.path()).collect();
+ let expected = Some(format!("via {}", Color::Blue.bold().paint("V v0.2 ")));
+ assert_eq!(expected, actual);
+ dir.close()
+ }
+
+ #[test]
+ fn folder_with_vpkg_lockfile() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join(".vpkg-lock.json"))?.sync_all()?;
+ let actual = ModuleRenderer::new("vlang").path(dir.path()).collect();
+ let expected = Some(format!("via {}", Color::Blue.bold().paint("V v0.2 ")));
+ assert_eq!(expected, actual);
+ dir.close()
+ }
}