From c4f977c48d29790f27d332c32d97eff40fd258f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Vladovi=C4=87?= Date: Fri, 7 May 2021 16:22:18 +0200 Subject: [PATCH] feat(vlang): support `vpkg` (#2686) --- docs/config/README.md | 20 +++++++-------- src/configs/mod.rs | 2 +- src/configs/v.rs | 8 +++--- src/modules/package.rs | 56 +++++++++++++++++++++++++++++++----------- src/modules/vlang.rs | 36 +++++++++++++++++++++++++-- 5 files changed, 90 insertions(+), 32 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 86e20616..cec7bcbd 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 08fbbfa0..e307060d 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>, diff --git a/src/configs/v.rs b/src/configs/v.rs index dc8624c6..d49bad7f 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 1f95a7e0..718ef21e 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -54,13 +54,6 @@ fn extract_cargo_version(file_contents: &str) -> Option { Some(formatted_version) } -fn extract_vlang_version(file_contents: &str) -> Option { - let re = Regex::new(r"(?m)^\s*version\s*:\s*'(?P[^']+)'").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 { let package_json: json::Value = json::from_str(file_contents).ok()?; @@ -184,6 +177,23 @@ fn extract_meson_version(file_contents: &str) -> Option { Some(formatted_version) } +fn extract_vmod_version(file_contents: &str) -> Option { + let re = Regex::new(r"(?m)^\s*version\s*:\s*'(?P[^']+)'").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 { + 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 { 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 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 { tempfile::tempdir() } diff --git a/src/modules/vlang.rs b/src/modules/vlang.rs index 0d7d0e7b..f516c711 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> { 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() + } }