use std::io::Write;
use std::collections::HashSet;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MyComponent {
pub name: String,
pub registry_dependencies: Vec<String>,
pub cargo_dependencies: Vec<String>,
#[serde(rename = "type")]
pub component_type: String,
#[serde(rename = "parent_dir")]
pub parent_dir: String,
}
#[derive(Debug)]
pub struct ResolvedComponent {
pub component: MyComponent,
pub resolved_registry_dependencies: HashSet<String>, pub resolved_cargo_dependencies: HashSet<String>, }
pub struct Components {}
impl Components {
pub fn create_components_mod_if_not_exists_with_pub_mods(user_config_path: String, parent_dirs: Vec<String>) {
let components_mod_path = format!("{user_config_path}/mod.rs");
let dir = std::path::Path::new(&components_mod_path)
.parent()
.expect("Failed to get parent directory");
std::fs::create_dir_all(dir).expect("Failed to create directories");
let mut mod_content = String::new();
if std::path::Path::new(&components_mod_path).exists() {
mod_content = std::fs::read_to_string(&components_mod_path).expect("Failed to read mod.rs");
}
let mut mod_rs_file = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(components_mod_path)
.expect("Failed to open mod.rs");
for parent_dir in parent_dirs {
if !mod_content.contains(&format!("pub mod {parent_dir};")) {
writeln!(mod_rs_file, "pub mod {parent_dir};").expect("🔸 Failed to write to mod.rs");
}
}
}
pub fn register_components_in_application_entry(entry_file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
let file_content = std::fs::read_to_string(entry_file_path)?;
const MOD_COMPONENTS: &str = "mod components;";
if file_content.contains(MOD_COMPONENTS) {
return Ok(());
}
let mod_components_import = format!("{MOD_COMPONENTS}\n{file_content}");
std::fs::write(entry_file_path, mod_components_import.as_bytes())?;
Ok(())
}
}