feat: use resources dir to save files
This commit is contained in:
parent
3b1a816b3b
commit
1500162a9c
1
src-tauri/.gitignore
vendored
1
src-tauri/.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
/target/
|
/target/
|
||||||
WixTools
|
WixTools
|
||||||
|
resources/Country.mmdb
|
||||||
|
6
src-tauri/resources/config_tmp.yaml
Normal file
6
src-tauri/resources/config_tmp.yaml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Default Config For Clash Core
|
||||||
|
|
||||||
|
mixed-port: 7890
|
||||||
|
allow-lan: false
|
||||||
|
external-controller: 127.0.0.1:9090
|
||||||
|
secret: ""
|
3
src-tauri/resources/verge_tmp.yaml
Normal file
3
src-tauri/resources/verge_tmp.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Defaulf Config For Clash Verge
|
||||||
|
|
||||||
|
nothing: ohh!
|
@ -10,18 +10,8 @@ use std::fs;
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use tauri::api::path::home_dir;
|
use tauri::api::path::{home_dir, resource_dir};
|
||||||
|
use tauri::PackageInfo;
|
||||||
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!
|
|
||||||
"#;
|
|
||||||
|
|
||||||
/// get the verge app home dir
|
/// get the verge app home dir
|
||||||
pub fn app_home_dir() -> PathBuf {
|
pub fn app_home_dir() -> PathBuf {
|
||||||
@ -31,27 +21,8 @@ pub fn app_home_dir() -> PathBuf {
|
|||||||
.join(Path::new("clash-verge"))
|
.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
|
/// initialize this instance's log file
|
||||||
fn init_log() {
|
fn init_log(log_dir: &PathBuf) {
|
||||||
let log_dir = init_log_dir();
|
|
||||||
let log_time = SystemTime::now()
|
let log_time = SystemTime::now()
|
||||||
.duration_since(UNIX_EPOCH)
|
.duration_since(UNIX_EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@ -80,64 +51,77 @@ fn init_log() {
|
|||||||
log4rs::init_config(config).unwrap();
|
log4rs::init_config(config).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize & Get the clash config
|
/// Initialize the clash config file
|
||||||
fn init_clash_config() -> Mapping {
|
fn init_clash_config(app_dir: &PathBuf, res_dir: &PathBuf) {
|
||||||
let app_dir = app_home_dir();
|
|
||||||
let yaml_path = app_dir.join("config.yaml");
|
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() {
|
if !yaml_path.exists() {
|
||||||
fs::File::create(yaml_path)
|
if yaml_tmpl.exists() {
|
||||||
.unwrap()
|
fs::copy(yaml_tmpl, yaml_path).unwrap();
|
||||||
.write(CLASH_CONFIG.as_bytes())
|
} else {
|
||||||
.unwrap();
|
let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes();
|
||||||
} else {
|
fs::File::create(yaml_path).unwrap().write(content).unwrap();
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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
|
/// Initialize the verge app config file
|
||||||
fn init_verge_config() -> Mapping {
|
fn init_verge_config(app_dir: &PathBuf, res_dir: &PathBuf) {
|
||||||
let app_dir = app_home_dir();
|
|
||||||
let yaml_path = app_dir.join("verge.yaml");
|
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() {
|
if !yaml_path.exists() {
|
||||||
fs::File::create(yaml_path)
|
if yaml_tmpl.exists() {
|
||||||
.unwrap()
|
fs::copy(yaml_tmpl, yaml_path).unwrap();
|
||||||
.write(VERGE_CONFIG.as_bytes())
|
} else {
|
||||||
.unwrap();
|
let content = "".as_bytes();
|
||||||
} else {
|
fs::File::create(yaml_path).unwrap().write(content).unwrap();
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
yaml_obj
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct InitApp {
|
|
||||||
pub clash_config: Mapping,
|
|
||||||
pub verge_config: Mapping,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// initialize app
|
/// initialize app
|
||||||
pub fn init_app() -> InitApp {
|
pub fn init_app(package_info: &PackageInfo) {
|
||||||
init_app_dir();
|
// create app dir
|
||||||
init_log();
|
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 res_dir = resource_dir(package_info).unwrap().join("resources");
|
||||||
let verge_config = init_verge_config();
|
|
||||||
|
|
||||||
InitApp {
|
if !app_dir.exists() {
|
||||||
clash_config,
|
fs::create_dir(&app_dir).unwrap();
|
||||||
verge_config,
|
|
||||||
}
|
}
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
@ -16,12 +16,6 @@ use tauri::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
init::init_app();
|
|
||||||
// clash::run_clash_bin();
|
|
||||||
|
|
||||||
// 通过clash config初始化menu和tray
|
|
||||||
// 通过verge config干点别的
|
|
||||||
|
|
||||||
let sub_menu = SystemTraySubmenu::new(
|
let sub_menu = SystemTraySubmenu::new(
|
||||||
"出站规则",
|
"出站规则",
|
||||||
SystemTrayMenu::new()
|
SystemTrayMenu::new()
|
||||||
@ -66,6 +60,13 @@ fn main() -> std::io::Result<()> {
|
|||||||
.build(tauri::generate_context!())
|
.build(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.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 {
|
app.run(|app_handle, e| match e {
|
||||||
tauri::Event::CloseRequested { label, api, .. } => {
|
tauri::Event::CloseRequested { label, api, .. } => {
|
||||||
let app_handle = app_handle.clone();
|
let app_handle = app_handle.clone();
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
"icons/icon.icns",
|
"icons/icon.icns",
|
||||||
"icons/icon.ico"
|
"icons/icon.ico"
|
||||||
],
|
],
|
||||||
"resources": [],
|
"resources": ["resources"],
|
||||||
"externalBin": ["sidebar/clash"],
|
"externalBin": ["sidebar/clash"],
|
||||||
"copyright": "",
|
"copyright": "",
|
||||||
"category": "DeveloperTool",
|
"category": "DeveloperTool",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user