use crate::core::handle; use anyhow::Result; use once_cell::sync::OnceCell; use std::path::PathBuf; use tauri::Manager; #[cfg(not(feature = "verge-dev"))] pub static APP_ID: &str = "io.github.clash-verge-rev.clash-verge-rev"; #[cfg(feature = "verge-dev")] pub static APP_ID: &str = "io.github.clash-verge-rev.clash-verge-rev.dev"; pub static PORTABLE_FLAG: OnceCell = OnceCell::new(); static CLASH_CONFIG: &str = "config.yaml"; static VERGE_CONFIG: &str = "verge.yaml"; static PROFILE_YAML: &str = "profiles.yaml"; /// init portable flag pub fn init_portable_flag() -> Result<()> { use tauri::utils::platform::current_exe; let app_exe = current_exe()?; if let Some(dir) = app_exe.parent() { let dir = PathBuf::from(dir).join(".config/PORTABLE"); if dir.exists() { PORTABLE_FLAG.get_or_init(|| true); } } PORTABLE_FLAG.get_or_init(|| false); Ok(()) } /// get the verge app home dir pub fn app_home_dir() -> Result { use tauri::utils::platform::current_exe; let flag = PORTABLE_FLAG.get().unwrap_or(&false); if *flag { let app_exe = current_exe()?; let app_exe = dunce::canonicalize(app_exe)?; let app_dir = app_exe .parent() .ok_or(anyhow::anyhow!("failed to get the portable app dir"))?; return Ok(PathBuf::from(app_dir).join(".config").join(APP_ID)); } let app_handle = handle::Handle::global().app_handle().unwrap(); match app_handle.path().data_dir() { Ok(dir) => Ok(dir.join(APP_ID)), Err(e) => { log::error!(target:"app", "Failed to get the app home directory: {}", e); Err(anyhow::anyhow!("Failed to get the app homedirectory")) } } } /// get the resources dir pub fn app_resources_dir() -> Result { let app_handle = handle::Handle::global().app_handle().unwrap(); match app_handle.path().resource_dir() { Ok(dir) => Ok(dir.join("resources")), Err(e) => { log::error!(target:"app", "Failed to get the resource directory: {}", e); Err(anyhow::anyhow!("Failed to get the resource directory")) } } } /// profiles dir pub fn app_profiles_dir() -> Result { Ok(app_home_dir()?.join("profiles")) } /// logs dir pub fn app_logs_dir() -> Result { Ok(app_home_dir()?.join("logs")) } pub fn clash_path() -> Result { Ok(app_home_dir()?.join(CLASH_CONFIG)) } pub fn verge_path() -> Result { Ok(app_home_dir()?.join(VERGE_CONFIG)) } pub fn profiles_path() -> Result { Ok(app_home_dir()?.join(PROFILE_YAML)) } #[cfg(not(target_os = "windows"))] pub fn service_path() -> Result { Ok(app_resources_dir()?.join("clash-verge-service")) } #[cfg(windows)] pub fn service_path() -> Result { Ok(app_resources_dir()?.join("clash-verge-service.exe")) } pub fn service_log_file() -> Result { use chrono::Local; let log_dir = app_logs_dir()?.join("service"); let local_time = Local::now().format("%Y-%m-%d-%H%M").to_string(); let log_file = format!("{}.log", local_time); let log_file = log_dir.join(log_file); let _ = std::fs::create_dir_all(&log_dir); Ok(log_file) } pub fn path_to_str(path: &PathBuf) -> Result<&str> { let path_str = path .as_os_str() .to_str() .ok_or(anyhow::anyhow!("failed to get path from {:?}", path))?; Ok(path_str) }