#![allow(dead_code)]
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
pub const CCGO_BUILD_DIR: &str = "ccgo_build";
pub fn sanitize_for_path(s: &str) -> String {
s.chars()
.map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '_' })
.collect()
}
pub fn clean_ccgo_build_platform(build_root: &Path, platform: &str) -> Result<()> {
if !build_root.exists() {
return Ok(());
}
for entry in std::fs::read_dir(build_root)? {
let entry = entry?;
if !entry.file_type()?.is_dir() {
continue;
}
let plat_dir = entry.path().join(platform);
if plat_dir.exists() {
std::fs::remove_dir_all(&plat_dir)
.with_context(|| format!("Failed to clean {}", plat_dir.display()))?;
}
}
Ok(())
}
pub fn find_project_root() -> Result<PathBuf> {
let current_dir = std::env::current_dir().context("Failed to get current directory")?;
find_project_root_from(¤t_dir)
}
pub fn find_project_root_from(start: &Path) -> Result<PathBuf> {
let mut dir = start;
loop {
if dir.join("CCGO.toml").exists() {
return Ok(dir.to_path_buf());
}
match dir.parent() {
Some(parent) => dir = parent,
None => {
anyhow::bail!("Could not find CCGO.toml in current directory or any parent")
}
}
}
}
pub fn get_target_dir(project_root: &Path) -> PathBuf {
project_root.join("target")
}
pub fn get_ccgo_build_dir(project_root: &Path) -> PathBuf {
project_root.join(CCGO_BUILD_DIR)
}
pub fn get_ccgo_build_dir_with_mode(project_root: &Path, release: bool) -> PathBuf {
let subdir = if release { "release" } else { "debug" };
project_root.join(CCGO_BUILD_DIR).join(subdir)
}
pub fn get_ccgo_build_dir_for_platform(
project_root: &Path,
platform: &str,
release: bool,
) -> PathBuf {
let subdir = if release { "release" } else { "debug" };
project_root
.join(CCGO_BUILD_DIR)
.join(subdir)
.join(platform.to_lowercase())
}
pub fn ensure_dir(path: &Path) -> Result<()> {
if !path.exists() {
std::fs::create_dir_all(path)
.with_context(|| format!("Failed to create directory: {}", path.display()))?;
}
Ok(())
}