feat: use resources dir to save files

This commit is contained in:
GyDi 2021-12-12 17:46:16 +08:00
parent 3b1a816b3b
commit 1500162a9c
6 changed files with 76 additions and 81 deletions

View File

@ -2,3 +2,4 @@
# will have compiled files and executables
/target/
WixTools
resources/Country.mmdb

View File

@ -0,0 +1,6 @@
# Default Config For Clash Core
mixed-port: 7890
allow-lan: false
external-controller: 127.0.0.1:9090
secret: ""

View File

@ -0,0 +1,3 @@
# Defaulf Config For Clash Verge
nothing: ohh!

View File

@ -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::<Mapping>(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::<Mapping>(&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::<Mapping>(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::<Mapping>(&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::<Mapping>(&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::<Mapping>(&yaml_str).unwrap()
}

View File

@ -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();

View File

@ -25,7 +25,7 @@
"icons/icon.icns",
"icons/icon.ico"
],
"resources": [],
"resources": ["resources"],
"externalBin": ["sidebar/clash"],
"copyright": "",
"category": "DeveloperTool",