1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 15:56:28 +00:00

feat(vlang): support vpkg (#2686)

This commit is contained in:
Dario Vladović 2021-05-07 16:22:18 +02:00 committed by GitHub
parent 23cc78237c
commit c4f977c48d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 32 deletions

View File

@ -2846,16 +2846,16 @@ format = "via [⍱ $version](bold white) "
The `vlang` module shows you your currently installed version of V. 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: 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 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 ### Options
| Option | Default | Description | | Option | Default | Description |
| ------------------- | ------------------------------------------------- | ----------------------------------------------- | | ------------------- | -------------------------------------------- | -------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | | `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"V "` | A format string representing the symbol of V | | `symbol` | `"V "` | A format string representing the symbol of V |
| `detect_extensions` | `["v"]` | Which extensions should trigger this module. | | `detect_extensions` | `["v"]` | Which extensions should trigger this module. |
| `detect_files` | `["v.mod"]` | Which filenames 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. | | `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"blue bold"` | The style for the module. | | `style` | `"blue bold"` | The style for the module. |
| `disabled` | `false` | Disables the `vlang` module. | | `disabled` | `false` | Disables the `vlang` module. |

View File

@ -129,7 +129,7 @@ pub struct FullConfig<'a> {
terraform: terraform::TerraformConfig<'a>, terraform: terraform::TerraformConfig<'a>,
time: time::TimeConfig<'a>, time: time::TimeConfig<'a>,
username: username::UsernameConfig<'a>, username: username::UsernameConfig<'a>,
vlang: v::VLangConfig<'a>, vlang: v::VConfig<'a>,
vagrant: vagrant::VagrantConfig<'a>, vagrant: vagrant::VagrantConfig<'a>,
zig: zig::ZigConfig<'a>, zig: zig::ZigConfig<'a>,
custom: IndexMap<String, custom::CustomConfig<'a>>, custom: IndexMap<String, custom::CustomConfig<'a>>,

View File

@ -4,7 +4,7 @@ use serde::Serialize;
use starship_module_config_derive::ModuleConfig; use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)] #[derive(Clone, ModuleConfig, Serialize)]
pub struct VLangConfig<'a> { pub struct VConfig<'a> {
pub format: &'a str, pub format: &'a str,
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
@ -14,15 +14,15 @@ pub struct VLangConfig<'a> {
pub detect_folders: Vec<&'a str>, pub detect_folders: Vec<&'a str>,
} }
impl<'a> Default for VLangConfig<'a> { impl<'a> Default for VConfig<'a> {
fn default() -> Self { fn default() -> Self {
VLangConfig { VConfig {
format: "via [$symbol($version )]($style)", format: "via [$symbol($version )]($style)",
symbol: "V ", symbol: "V ",
style: "blue bold", style: "blue bold",
disabled: false, disabled: false,
detect_extensions: vec!["v"], detect_extensions: vec!["v"],
detect_files: vec!["v.mod"], detect_files: vec!["v.mod", "vpkg.json", ".vpkg-lock.json"],
detect_folders: vec![], detect_folders: vec![],
} }
} }

View File

@ -54,13 +54,6 @@ fn extract_cargo_version(file_contents: &str) -> Option<String> {
Some(formatted_version) 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> { fn extract_package_version(file_contents: &str, display_private: bool) -> Option<String> {
let package_json: json::Value = json::from_str(file_contents).ok()?; 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) 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> { fn get_package_version(base_dir: &Path, config: &PackageConfig) -> Option<String> {
if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) { if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) {
extract_cargo_version(&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")) { } else if let Ok(meson_build) = utils::read_file(base_dir.join("meson.build")) {
extract_meson_version(&meson_build) extract_meson_version(&meson_build)
} else if let Ok(vlang_mod) = utils::read_file(base_dir.join("v.mod")) { } 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 { } else {
None None
} }
@ -828,20 +840,34 @@ end";
} }
#[test] #[test]
fn test_extract_vlang_version() -> io::Result<()> { fn test_extract_vmod_version() -> io::Result<()> {
let config_name = "v.mod"; let config_name = "v.mod";
let config_content = " let config_content = "\
Module { Module {
name: 'starship', name: 'starship',
author: 'matchai', author: 'matchai',
version: '1.2.3' version: '1.2.3'
}"; }";
let project_dir = create_project_dir()?; let project_dir = create_project_dir()?;
fill_config(&project_dir, config_name, Some(&config_content))?; fill_config(&project_dir, config_name, Some(&config_content))?;
expect_output(&project_dir, Some("v1.2.3"), None); expect_output(&project_dir, Some("v1.2.3"), None);
project_dir.close() 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> { fn create_project_dir() -> io::Result<TempDir> {
tempfile::tempdir() tempfile::tempdir()
} }

View File

@ -1,15 +1,17 @@
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use crate::configs::v::VLangConfig; use crate::configs::v::VConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
/// Creates a module with the current V version /// Creates a module with the current V version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("vlang"); 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 let is_v_project = context
.try_begin_scan()? .try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions) .set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match(); .is_match();
if !is_v_project { if !is_v_project {
@ -89,4 +91,34 @@ mod tests {
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() 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()
}
} }