From 1500162a9c1e46b3c2ba52d07866ec0050857c28 Mon Sep 17 00:00:00 2001 From: GyDi Date: Sun, 12 Dec 2021 17:46:16 +0800 Subject: [PATCH] feat: use resources dir to save files --- src-tauri/.gitignore | 1 + src-tauri/resources/config_tmp.yaml | 6 ++ src-tauri/resources/verge_tmp.yaml | 3 + src-tauri/src/init.rs | 132 ++++++++++++---------------- src-tauri/src/main.rs | 13 +-- src-tauri/tauri.conf.json | 2 +- 6 files changed, 76 insertions(+), 81 deletions(-) create mode 100644 src-tauri/resources/config_tmp.yaml create mode 100644 src-tauri/resources/verge_tmp.yaml diff --git a/src-tauri/.gitignore b/src-tauri/.gitignore index c123704..327f660 100644 --- a/src-tauri/.gitignore +++ b/src-tauri/.gitignore @@ -2,3 +2,4 @@ # will have compiled files and executables /target/ WixTools +resources/Country.mmdb diff --git a/src-tauri/resources/config_tmp.yaml b/src-tauri/resources/config_tmp.yaml new file mode 100644 index 0000000..102aa79 --- /dev/null +++ b/src-tauri/resources/config_tmp.yaml @@ -0,0 +1,6 @@ +# Default Config For Clash Core + +mixed-port: 7890 +allow-lan: false +external-controller: 127.0.0.1:9090 +secret: "" diff --git a/src-tauri/resources/verge_tmp.yaml b/src-tauri/resources/verge_tmp.yaml new file mode 100644 index 0000000..49f3c61 --- /dev/null +++ b/src-tauri/resources/verge_tmp.yaml @@ -0,0 +1,3 @@ +# Defaulf Config For Clash Verge + +nothing: ohh! diff --git a/src-tauri/src/init.rs b/src-tauri/src/init.rs index 7333e47..7e77c08 100644 --- a/src-tauri/src/init.rs +++ b/src-tauri/src/init.rs @@ -10,18 +10,8 @@ use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; -use tauri::api::path::home_dir; - -const CLASH_CONFIG: &str = r#" -mixed-port: 7890 -allow-lan: false -external-controller: 127.0.0.1:9090 -secret: '' -"#; - -const VERGE_CONFIG: &str = r#" -nothing: ohh! -"#; +use tauri::api::path::{home_dir, resource_dir}; +use tauri::PackageInfo; /// get the verge app home dir pub fn app_home_dir() -> PathBuf { @@ -31,27 +21,8 @@ pub fn app_home_dir() -> PathBuf { .join(Path::new("clash-verge")) } -/// initialize the app home dir -fn init_app_dir() -> PathBuf { - let app_dir = app_home_dir(); - if !app_dir.exists() { - fs::create_dir(&app_dir).unwrap(); - } - app_dir -} - -/// initialize the logs dir -fn init_log_dir() -> PathBuf { - let log_dir = app_home_dir().join("logs"); - if !log_dir.exists() { - fs::create_dir(&log_dir).unwrap(); - } - log_dir -} - /// initialize this instance's log file -fn init_log() { - let log_dir = init_log_dir(); +fn init_log(log_dir: &PathBuf) { let log_time = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() @@ -80,64 +51,77 @@ fn init_log() { log4rs::init_config(config).unwrap(); } -/// Initialize & Get the clash config -fn init_clash_config() -> Mapping { - let app_dir = app_home_dir(); +/// Initialize the clash config file +fn init_clash_config(app_dir: &PathBuf, res_dir: &PathBuf) { let yaml_path = app_dir.join("config.yaml"); - let mut yaml_obj = serde_yaml::from_str::(CLASH_CONFIG).unwrap(); + let yaml_tmpl = res_dir.join("config_tmp.yaml"); if !yaml_path.exists() { - fs::File::create(yaml_path) - .unwrap() - .write(CLASH_CONFIG.as_bytes()) - .unwrap(); - } else { - let yaml_str = fs::read_to_string(yaml_path).unwrap(); - let user_obj = serde_yaml::from_str::(&yaml_str).unwrap(); - for (key, value) in user_obj.iter() { - yaml_obj.insert(key.clone(), value.clone()); + if yaml_tmpl.exists() { + fs::copy(yaml_tmpl, yaml_path).unwrap(); + } else { + let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes(); + fs::File::create(yaml_path).unwrap().write(content).unwrap(); } } - yaml_obj + + let mmdb_path = app_dir.join("Country.mmdb"); + let mmdb_tmpl = res_dir.join("Country.mmdb"); + + if !mmdb_path.exists() && mmdb_tmpl.exists() { + fs::copy(mmdb_tmpl, mmdb_path).unwrap(); + } } -/// Initialize & Get the app config -fn init_verge_config() -> Mapping { - let app_dir = app_home_dir(); +/// Initialize the verge app config file +fn init_verge_config(app_dir: &PathBuf, res_dir: &PathBuf) { let yaml_path = app_dir.join("verge.yaml"); - let mut yaml_obj = serde_yaml::from_str::(VERGE_CONFIG).unwrap(); + let yaml_tmpl = res_dir.join("verge_tmp.yaml"); if !yaml_path.exists() { - fs::File::create(yaml_path) - .unwrap() - .write(VERGE_CONFIG.as_bytes()) - .unwrap(); - } else { - let yaml_str = fs::read_to_string(yaml_path).unwrap(); - let user_obj = serde_yaml::from_str::(&yaml_str).unwrap(); - for (key, value) in user_obj.iter() { - yaml_obj.insert(key.clone(), value.clone()); + if yaml_tmpl.exists() { + fs::copy(yaml_tmpl, yaml_path).unwrap(); + } else { + let content = "".as_bytes(); + fs::File::create(yaml_path).unwrap().write(content).unwrap(); } } - yaml_obj -} - -#[derive(Debug)] -pub struct InitApp { - pub clash_config: Mapping, - pub verge_config: Mapping, } /// initialize app -pub fn init_app() -> InitApp { - init_app_dir(); - init_log(); +pub fn init_app(package_info: &PackageInfo) { + // create app dir + let app_dir = app_home_dir(); + let log_dir = app_dir.join("logs"); + let profiles_dir = app_dir.join("profiles"); - let clash_config = init_clash_config(); - let verge_config = init_verge_config(); + let res_dir = resource_dir(package_info).unwrap().join("resources"); - InitApp { - clash_config, - verge_config, + if !app_dir.exists() { + fs::create_dir(&app_dir).unwrap(); } + if !log_dir.exists() { + fs::create_dir(&log_dir).unwrap(); + } + if !profiles_dir.exists() { + fs::create_dir(&profiles_dir).unwrap(); + } + + init_log(&log_dir); + init_clash_config(&app_dir, &res_dir); + init_verge_config(&app_dir, &res_dir); +} + +/// Get the user config of clash core +pub fn read_clash_config() -> Mapping { + let yaml_path = app_home_dir().join("config.yaml"); + let yaml_str = fs::read_to_string(yaml_path).unwrap(); + serde_yaml::from_str::(&yaml_str).unwrap() +} + +/// Get the user config of verge +pub fn read_verge_config() -> Mapping { + let yaml_path = app_home_dir().join("verge.yaml"); + let yaml_str = fs::read_to_string(yaml_path).unwrap(); + serde_yaml::from_str::(&yaml_str).unwrap() } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 5485c09..97da2ab 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -16,12 +16,6 @@ use tauri::{ }; fn main() -> std::io::Result<()> { - init::init_app(); - // clash::run_clash_bin(); - - // 通过clash config初始化menu和tray - // 通过verge config干点别的 - let sub_menu = SystemTraySubmenu::new( "出站规则", SystemTrayMenu::new() @@ -66,6 +60,13 @@ fn main() -> std::io::Result<()> { .build(tauri::generate_context!()) .expect("error while running tauri application"); + // init app config + init::init_app(app.package_info()); + // clash::run_clash_bin(); + + // 通过clash config初始化menu和tray + // 通过verge config干点别的 + app.run(|app_handle, e| match e { tauri::Event::CloseRequested { label, api, .. } => { let app_handle = app_handle.clone(); diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 2871fda..f9e29e4 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -25,7 +25,7 @@ "icons/icon.icns", "icons/icon.ico" ], - "resources": [], + "resources": ["resources"], "externalBin": ["sidebar/clash"], "copyright": "", "category": "DeveloperTool",