use std::path::Path;
use super::{Context, Module, RootModuleConfig};
use crate::configs::package::PackageConfig;
use crate::formatter::StringFormatter;
use crate::utils;
use quick_xml::events::Event as QXEvent;
use quick_xml::Reader as QXReader;
use regex::Regex;
use serde_json as json;
/// Creates a module with the current package version
///
/// Will display if a version is defined for your Node.js or Rust project (if one exists)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("package");
let config: PackageConfig = PackageConfig::try_load(module.config);
let module_version = get_package_version(&context.current_dir, &config)?;
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" => Some(Ok(&module_version)),
_ => None,
})
.parse(None)
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `package`:\n{}", error);
return None;
}
});
Some(module)
}
fn extract_cargo_version(file_contents: &str) -> Option<String> {
let cargo_toml: toml::Value = toml::from_str(file_contents).ok()?;
let raw_version = cargo_toml.get("package")?.get("version")?.as_str()?;
let formatted_version = format_version(raw_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> {
let package_json: json::Value = json::from_str(file_contents).ok()?;
if !display_private && package_json.get("private").and_then(json::Value::as_bool) == Some(true)
{
return None;
}
let raw_version = package_json.get("version")?.as_str()?;
if raw_version == "null" {
return None;
};
let formatted_version = format_version(raw_version);
if formatted_version == "v0.0.0-development" || formatted_version.starts_with("v0.0.0-semantic")
{
return Some("semantic".to_string());
};
Some(formatted_version)
}
fn extract_poetry_version(file_contents: &str) -> Option<String> {
let poetry_toml: toml::Value = toml::from_str(file_contents).ok()?;
let raw_version = poetry_toml
.get("tool")?
.get("poetry")?
.get("version")?
.as_str()?;
let formatted_version = format_version(raw_version);
Some(formatted_version)
}
fn extract_gradle_version(file_contents: &str) -> Option<String> {
let re = Regex::new(r#"(?m)^version ['"](?P<version>[^'"]+)['"]$"#).unwrap();
let caps = re.captures(file_contents)?;
let formatted_version = format_version(&caps["version"]);
Some(formatted_version)
}
fn extract_composer_version(file_contents: &str) -> Option<String> {
let composer_json: json::Value = json::from_str(file_contents).ok()?;
let raw_version = composer_json.get("version")?.as_str()?;
if raw_version == "null" {
return None;
};
let formatted_version = format_version(raw_version);
Some(formatted_version)
}
fn extract_project_version(file_contents: &str) -> Option<String> {
let project_toml: toml::Value = toml::from_str(file_contents).ok()?;
let raw_version = project_toml.get("version")?.as_str()?;
let formatted_version = format_version(raw_version);
Some(formatted_version)
}
fn extract_helm_package_version(file_contents: &str) -> Option<String>