refactor: adjust all path methods and reduce unwrap

This commit is contained in:
GyDi
2022-11-18 09:35:05 +08:00
parent a786023160
commit ce2d4498e1
11 changed files with 204 additions and 181 deletions

View File

@@ -1,5 +1,5 @@
use anyhow::Result;
use std::path::PathBuf;
use std::{env::temp_dir, path::PathBuf};
use tauri::{
api::path::{home_dir, resource_dir},
Env, PackageInfo,
@@ -13,7 +13,6 @@ static APP_DIR: &str = "clash-verge-dev";
static CLASH_CONFIG: &str = "config.yaml";
static VERGE_CONFIG: &str = "verge.yaml";
static PROFILE_YAML: &str = "profiles.yaml";
static CLASH_RUNTIME_YAML: &str = "clash-verge-runtime.yaml";
static mut RESOURCE_DIR: Option<PathBuf> = None;
@@ -21,7 +20,7 @@ static mut RESOURCE_DIR: Option<PathBuf> = None;
#[allow(unused)]
static mut PORTABLE_FLAG: bool = false;
pub static mut APP_VERSION: &str = "v1.1.1";
pub static mut APP_VERSION: &str = "v1.1.2";
/// initialize portable flag
#[cfg(target_os = "windows")]
@@ -42,29 +41,37 @@ pub unsafe fn init_portable_flag() -> Result<()> {
}
/// get the verge app home dir
pub fn app_home_dir() -> PathBuf {
pub fn app_home_dir() -> Result<PathBuf> {
#[cfg(target_os = "windows")]
unsafe {
use tauri::utils::platform::current_exe;
if !PORTABLE_FLAG {
home_dir().unwrap().join(".config").join(APP_DIR)
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get app home dir"))?
.join(".config")
.join(APP_DIR))
} else {
let app_exe = current_exe().unwrap();
let app_exe = dunce::canonicalize(app_exe).unwrap();
let app_dir = app_exe.parent().unwrap();
PathBuf::from(app_dir).join(".config").join(APP_DIR)
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"))?;
Ok(PathBuf::from(app_dir).join(".config").join(APP_DIR))
}
}
#[cfg(not(target_os = "windows"))]
home_dir().unwrap().join(".config").join(APP_DIR)
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get the app home dir"))?
.join(".config")
.join(APP_DIR))
}
/// get the resources dir
pub fn app_resources_dir(package_info: &PackageInfo) -> PathBuf {
pub fn app_resources_dir(package_info: &PackageInfo) -> Result<PathBuf> {
let res_dir = resource_dir(package_info, &Env::default())
.unwrap()
.ok_or(anyhow::anyhow!("failed to get the resource dir"))?
.join("resources");
unsafe {
@@ -75,37 +82,49 @@ pub fn app_resources_dir(package_info: &PackageInfo) -> PathBuf {
APP_VERSION = Box::leak(Box::new(ver_str));
}
res_dir
Ok(res_dir)
}
/// profiles dir
pub fn app_profiles_dir() -> PathBuf {
app_home_dir().join("profiles")
pub fn app_profiles_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("profiles"))
}
/// logs dir
pub fn app_logs_dir() -> PathBuf {
app_home_dir().join("logs")
pub fn app_logs_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("logs"))
}
pub fn clash_path() -> PathBuf {
app_home_dir().join(CLASH_CONFIG)
pub fn clash_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join(CLASH_CONFIG))
}
pub fn verge_path() -> PathBuf {
app_home_dir().join(VERGE_CONFIG)
pub fn verge_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join(VERGE_CONFIG))
}
pub fn profiles_path() -> PathBuf {
app_home_dir().join(PROFILE_YAML)
pub fn profiles_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join(PROFILE_YAML))
}
pub fn clash_runtime_yaml() -> PathBuf {
app_home_dir().join(CLASH_RUNTIME_YAML)
pub fn clash_runtime_yaml() -> Result<PathBuf> {
Ok(app_home_dir()?.join("clash-verge-runtime.yaml"))
}
pub fn clash_pid_path() -> PathBuf {
unsafe { RESOURCE_DIR.clone().unwrap().join("clash.pid") }
pub fn clash_check_yaml() -> Result<PathBuf> {
Ok(temp_dir().join("clash-verge-check.yaml"))
}
pub fn app_res_dir() -> Result<PathBuf> {
unsafe {
Ok(RESOURCE_DIR
.clone()
.ok_or(anyhow::anyhow!("failed to get the resource dir"))?)
}
}
pub fn clash_pid_path() -> Result<PathBuf> {
unsafe { Ok(RESOURCE_DIR.clone().unwrap().join("clash.pid")) }
}
#[cfg(windows)]