dirs.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. use std::env::temp_dir;
  2. use std::path::PathBuf;
  3. use tauri::{
  4. api::path::{home_dir, resource_dir},
  5. Env, PackageInfo,
  6. };
  7. #[cfg(not(feature = "verge-dev"))]
  8. static APP_DIR: &str = "clash-verge";
  9. #[cfg(feature = "verge-dev")]
  10. static APP_DIR: &str = "clash-verge-dev";
  11. static CLASH_CONFIG: &str = "config.yaml";
  12. static VERGE_CONFIG: &str = "verge.yaml";
  13. static PROFILE_YAML: &str = "profiles.yaml";
  14. static PROFILE_TEMP: &str = "clash-verge-runtime.yaml";
  15. static mut RESOURCE_DIR: Option<PathBuf> = None;
  16. /// portable flag
  17. #[allow(unused)]
  18. static mut PORTABLE_FLAG: bool = false;
  19. /// initialize portable flag
  20. #[allow(unused)]
  21. pub unsafe fn init_portable_flag() {
  22. #[cfg(target_os = "windows")]
  23. {
  24. use tauri::utils::platform::current_exe;
  25. let exe = current_exe().unwrap();
  26. let dir = exe.parent().unwrap();
  27. let dir = PathBuf::from(dir).join(".config/PORTABLE");
  28. if dir.exists() {
  29. PORTABLE_FLAG = true;
  30. }
  31. }
  32. }
  33. /// get the verge app home dir
  34. pub fn app_home_dir() -> PathBuf {
  35. #[cfg(target_os = "windows")]
  36. unsafe {
  37. use tauri::utils::platform::current_exe;
  38. if !PORTABLE_FLAG {
  39. home_dir().unwrap().join(".config").join(APP_DIR)
  40. } else {
  41. let app_exe = current_exe().unwrap();
  42. let app_exe = dunce::canonicalize(app_exe).unwrap();
  43. let app_dir = app_exe.parent().unwrap();
  44. PathBuf::from(app_dir).join(".config").join(APP_DIR)
  45. }
  46. }
  47. #[cfg(not(target_os = "windows"))]
  48. home_dir().unwrap().join(".config").join(APP_DIR)
  49. }
  50. /// get the resources dir
  51. pub fn app_resources_dir(package_info: &PackageInfo) -> PathBuf {
  52. let res_dir = resource_dir(package_info, &Env::default())
  53. .unwrap()
  54. .join("resources");
  55. unsafe {
  56. RESOURCE_DIR = Some(res_dir.clone());
  57. }
  58. res_dir
  59. }
  60. /// profiles dir
  61. pub fn app_profiles_dir() -> PathBuf {
  62. app_home_dir().join("profiles")
  63. }
  64. /// logs dir
  65. pub fn app_logs_dir() -> PathBuf {
  66. app_home_dir().join("logs")
  67. }
  68. pub fn clash_path() -> PathBuf {
  69. app_home_dir().join(CLASH_CONFIG)
  70. }
  71. pub fn verge_path() -> PathBuf {
  72. app_home_dir().join(VERGE_CONFIG)
  73. }
  74. pub fn profiles_path() -> PathBuf {
  75. app_home_dir().join(PROFILE_YAML)
  76. }
  77. pub fn profiles_temp_path() -> PathBuf {
  78. #[cfg(not(feature = "debug-yml"))]
  79. return temp_dir().join(PROFILE_TEMP);
  80. #[cfg(feature = "debug-yml")]
  81. return app_home_dir().join(PROFILE_TEMP);
  82. }
  83. pub fn clash_pid_path() -> PathBuf {
  84. unsafe { RESOURCE_DIR.clone().unwrap().join("clash.pid") }
  85. }
  86. #[cfg(windows)]
  87. static SERVICE_PATH: &str = "clash-verge-service.exe";
  88. #[cfg(windows)]
  89. pub fn service_path() -> PathBuf {
  90. unsafe {
  91. let res_dir = RESOURCE_DIR.clone().unwrap();
  92. res_dir.join(SERVICE_PATH)
  93. }
  94. }
  95. #[cfg(windows)]
  96. pub fn service_log_file() -> PathBuf {
  97. use chrono::Local;
  98. let log_dir = app_logs_dir().join("service");
  99. let local_time = Local::now().format("%Y-%m-%d-%H%M%S").to_string();
  100. let log_file = format!("{}.log", local_time);
  101. let log_file = log_dir.join(log_file);
  102. std::fs::create_dir_all(&log_dir).unwrap();
  103. log_file
  104. }