Files
AeroPkg/src/utils/parser/env.rs
2026-07-22 04:29:56 +03:00

92 lines
4.3 KiB
Rust

use std::io;
use std::env;
use std::path::Path;
use std::collections::HashMap;
use super::*;
pub fn get_install_env(repo: &String, pkgname: &String, pkg_md_path: &Path, stage: &str) -> HashMap<String, String> {
let mut full_env = HashMap::new();
let pkgpath = crate::commands::get_aeropkg_base().join(repo).join(pkgname);
full_env.insert("repo".to_string(), repo.clone());
full_env.insert("pkgname".to_string(), pkgname.clone());
full_env.insert("pkgpath".to_string(), pkgpath.to_string_lossy().into_owned());
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
if let Some(pkg_env) = get_pkg_env(pkg_md_path).ok() { full_env.extend(pkg_env) }
if let Some(repo_env) = get_repo_env(repo).ok() { full_env.extend(repo_env) }
if let Some(stage_env) = get_stage_env(stage).ok() { full_env.extend(stage_env) }
if let Some(repo_stage_env) = get_repo_and_stage_env(repo, stage).ok() { full_env.extend(repo_stage_env) }
full_env.extend(env::vars().map(|(k, v)| (k, v)));
return full_env
}
pub fn get_custom_env(repo: &String, pkgname: &String, pkg_md_path: &Path) -> HashMap<String, String> {
let mut full_env = HashMap::new();
let pkgpath = crate::commands::get_aeropkg_base().join(repo).join(pkgname);
full_env.insert("repo".to_string(), repo.clone());
full_env.insert("pkgname".to_string(), pkgname.clone());
full_env.insert("pkgpath".to_string(), pkgpath.to_string_lossy().into_owned());
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
if let Some(pkg_env) = get_pkg_env(pkg_md_path).ok() { full_env.extend(pkg_env) }
if let Some(repo_env) = get_repo_env(repo).ok() { full_env.extend(repo_env) }
full_env.extend(env::vars().map(|(k, v)| (k, v)));
return full_env
}
pub fn get_global_env() -> HashMap<String, String> {
let mut full_env = HashMap::new();
full_env.insert("aeropkg_home".to_string(), crate::commands::get_aeropkg_home().to_string_lossy().into_owned().clone());
full_env.insert("aeropkg_base".to_string(), crate::commands::get_aeropkg_base().to_string_lossy().into_owned().clone());
full_env.insert("pkgbase".to_string(), crate::commands::get_aeropkg_base().to_string_lossy().into_owned().clone());
full_env.insert("aeropkg_etc".to_string(), crate::commands::get_aeropkg_etc().to_string_lossy().into_owned().clone());
full_env.insert("aeropkg_var".to_string(), crate::commands::get_aeropkg_var().to_string_lossy().into_owned().clone());
if let Some(global_env) = get_global_env_string().ok() { full_env.extend(global_env) }
return full_env
}
fn get_global_env_string() -> io::Result<HashMap<String, String>> {
let cfg_path = crate::commands::get_aeropkg_etc().join("aeropkg.md");
let content = extract_block(&cfg_path, &format!("``` env *** env ***"), "```")?;
Ok(parse_env_vars(&content))
}
fn parse_env_vars(content: &str) -> HashMap<String, String> {
content
.lines()
.filter_map(|line| {
let line = line.trim();
if line.is_empty() || line.starts_with('#') { return None }
let parts: Vec<&str> = line.splitn(2, '=').collect();
if parts.len() != 2 { return None }
Some((parts[0].to_string(), parts[1].to_string()))
})
.collect()
}
pub fn get_pkg_env(file_path: &Path) -> io::Result<HashMap<String, String>> {
let content = extract_block(file_path, "``` env *** env ***", "```")?;
Ok(parse_env_vars(&content))
}
pub fn get_repo_env(repo: &str) -> io::Result<HashMap<String, String>> {
let cfg_path = crate::commands::get_aeropkg_etc().join("aeropkg.md");
let content = extract_block(&cfg_path, &format!("``` env *** env {} ***", repo), "```")?;
Ok(parse_env_vars(&content))
}
pub fn get_stage_env(stage: &str) -> io::Result<HashMap<String, String>> {
let cfg_path = crate::commands::get_aeropkg_etc().join("aeropkg.md");
let content = extract_block(&cfg_path, &format!("``` env *** env {} ***", stage), "```")?;
Ok(parse_env_vars(&content))
}
pub fn get_repo_and_stage_env(repo: &str, stage: &str) -> io::Result<HashMap<String, String>> {
let cfg_path = crate::commands::get_aeropkg_etc().join("aeropkg.md");
let content = extract_block(&cfg_path, &format!("``` env *** env {} {} ***", repo, stage), "```")?;
Ok(parse_env_vars(&content))
}