use include_dir::{include_dir, Dir};
use std::path::PathBuf;
use std::sync::OnceLock;
static STDLIB_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/stdlib");
static STDLIB_PATH: OnceLock<PathBuf> = OnceLock::new();
pub fn stdlib_path() -> &'static str {
STDLIB_PATH
.get_or_init(|| {
let path = get_stdlib_install_path();
ensure_stdlib_extracted(&path);
path
})
.to_str()
.unwrap_or(".")
}
fn get_stdlib_install_path() -> PathBuf {
dirs::data_dir()
.or_else(|| {
dirs::home_dir()
})
.unwrap_or_else(|| PathBuf::from("."))
.join("vibelang")
.join("stdlib")
}
fn ensure_stdlib_extracted(target_path: &PathBuf) {
if target_path.exists() && target_path.join("index.vibe").exists() {
let version_file = target_path.join(".version");
let current_version = env!("CARGO_PKG_VERSION");
if let Ok(installed_version) = std::fs::read_to_string(&version_file) {
if installed_version.trim() == current_version {
return;
}
}
eprintln!(
"Updating stdlib to version {} in {}",
current_version,
target_path.display()
);
} else {
eprintln!("Extracting stdlib to {}", target_path.display());
}
if let Err(e) = extract_stdlib(target_path) {
eprintln!("Warning: Failed to extract stdlib: {}", e);
eprintln!("You may need to manually specify stdlib path with -I flag");
}
}
fn extract_stdlib(target_path: &PathBuf) -> std::io::Result<()> {
std::fs::create_dir_all(target_path)?;
extract_dir(&STDLIB_DIR, target_path)?;
let version_file = target_path.join(".version");
std::fs::write(version_file, env!("CARGO_PKG_VERSION"))?;
eprintln!("Stdlib extracted successfully");
Ok(())
}
fn extract_dir(dir: &Dir, target_path: &PathBuf) -> std::io::Result<()> {
for file in dir.files() {
let file_path = target_path.join(file.path());
if let Some(parent) = file_path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(&file_path, file.contents())?;
}
for subdir in dir.dirs() {
extract_dir(subdir, target_path)?;
}
Ok(())
}
pub const CATEGORIES: &[&str] = &[
"drums",
"bass",
"leads",
"pads",
"textures",
"effects",
"fx",
"theory",
"modulators",
];
pub fn embedded_stdlib() -> &'static Dir<'static> {
&STDLIB_DIR
}
pub fn get_stdlib_files() -> std::collections::HashMap<String, String> {
let mut files = std::collections::HashMap::new();
collect_files_recursive(&STDLIB_DIR, &mut files);
files
}
fn collect_files_recursive(dir: &Dir, files: &mut std::collections::HashMap<String, String>) {
for file in dir.files() {
if let Some(path) = file.path().to_str() {
if path.ends_with(".vibe") {
if let Some(content) = file.contents_utf8() {
files.insert(path.to_string(), content.to_string());
}
}
}
}
for subdir in dir.dirs() {
collect_files_recursive(subdir, files);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stdlib_path_returns_string() {
let path = stdlib_path();
assert!(!path.is_empty());
}
#[test]
fn test_embedded_stdlib_has_content() {
let dir = embedded_stdlib();
assert!(dir.files().count() > 0 || dir.dirs().count() > 0);
}
#[test]
fn test_get_stdlib_files_paths() {
let files = get_stdlib_files();
println!("Found {} stdlib files:", files.len());
for path in files.keys().take(20) {
println!(" - {}", path);
}
assert!(!files.is_empty(), "Should have stdlib files");
}
}