use std::path::Path;
use cargo_toml::Manifest;
use crate::shared::cli_error::{CliError, CliResult};
pub fn detect_workspace() -> CliResult<bool> {
let cargo_toml_path = Path::new("Cargo.toml");
let manifest = load_cargo_manifest(cargo_toml_path)?;
Ok(manifest.map_or(false, |m| m.workspace.is_some()))
}
pub fn get_component_base_path(is_workspace: bool) -> String {
if is_workspace {
"src/components".to_string()
} else {
"src/components".to_string()
}
}
pub fn check_leptos_dependency() -> CliResult<bool> {
check_leptos_dependency_in_path(".")
}
fn check_leptos_dependency_in_path(dir_path: &str) -> CliResult<bool> {
let cargo_toml_path = Path::new(dir_path).join("Cargo.toml");
let manifest = load_cargo_manifest(&cargo_toml_path)?;
let Some(manifest) = manifest else {
return Err(CliError::file_operation("Cargo.toml not found in current directory"));
};
if manifest.dependencies.contains_key("leptos") {
return Ok(true);
}
if let Some(workspace) = manifest.workspace {
if workspace.dependencies.contains_key("leptos") {
return Ok(true);
}
}
Ok(false)
}
fn load_cargo_manifest(cargo_toml_path: &Path) -> CliResult<Option<Manifest>> {
if !cargo_toml_path.exists() {
return Ok(None);
}
match Manifest::from_path(cargo_toml_path) {
Ok(manifest) => Ok(Some(manifest)),
Err(_) => {
let contents = std::fs::read_to_string(cargo_toml_path)?;
let manifest = Manifest::from_slice(contents.as_bytes())?;
Ok(Some(manifest))
}
}
}