Skip to content

Commit

Permalink
feat(config): add migration for old config dir (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
4o3F committed Feb 12, 2024
1 parent 58ccf81 commit 4688334
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
18 changes: 18 additions & 0 deletions backend/tauri/src/utils/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Barrier};
use tauri::api::dialog::{MessageDialogBuilder, MessageDialogButtons, MessageDialogKind};

Expand All @@ -16,3 +17,20 @@ pub fn panic_dialog(msg: &str) {
});
barrier.wait();
}

pub fn migrate_dialog() -> bool {
let msg = "Old version config file detected\nMigrate to new version or not?\n WARNING: This will override your current config if exists".to_string();
let barrier = Arc::new(Barrier::new(2));
let barrier_ref = barrier.clone();
let migrate = Arc::new(AtomicBool::new(false));
let migrate_ref = migrate.clone();
MessageDialogBuilder::new("Migration", msg)
.kind(MessageDialogKind::Warning)
.buttons(MessageDialogButtons::YesNo)
.show(move |_migrate| {
migrate_ref.store(_migrate, Ordering::Relaxed);
barrier_ref.wait();
});
barrier.wait();
migrate.load(Ordering::Relaxed)
}
36 changes: 34 additions & 2 deletions backend/tauri/src/utils/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ use tauri::{
};

#[cfg(not(feature = "verge-dev"))]
static APP_DIR: &str = "clash-verge";
static OLD_APP_DIR: &str = "clash-verge";
#[cfg(feature = "verge-dev")]
static APP_DIR: &str = "clash-verge-dev";
static OLD_APP_DIR: &str = "clash-verge-dev";
#[cfg(not(feature = "verge-dev"))]
static APP_DIR: &str = "clash-nyanpasu";
#[cfg(feature = "verge-dev")]
static APP_DIR: &str = "clash-nyanpasu-dev";

static CLASH_CONFIG: &str = "config.yaml";
static VERGE_CONFIG: &str = "verge.yaml";
Expand Down Expand Up @@ -51,6 +55,33 @@ pub fn init_portable_flag() -> Result<()> {
Ok(())
}

pub fn old_app_home_dir() -> Result<PathBuf> {
#[cfg(target_os = "windows")]
{
use tauri::utils::platform::current_exe;

if !PORTABLE_FLAG.get().unwrap_or(&false) {
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to check old app home dir"))?
.join(".config")
.join(OLD_APP_DIR))
} else {
let app_exe = current_exe()?;
let app_exe = dunce::canonicalize(app_exe)?;
let app_dir = app_exe
.parent()
.ok_or(anyhow::anyhow!("failed to check the old portable app dir"))?;
Ok(PathBuf::from(app_dir).join(".config").join(OLD_APP_DIR))
}
}

#[cfg(not(target_os = "windows"))]
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get the app home dir"))?
.join(".config")
.join(OLD_APP_DIR))
}

/// get the verge app home dir
pub fn app_home_dir() -> Result<PathBuf> {
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -91,6 +122,7 @@ pub fn app_resources_dir() -> Result<PathBuf> {
};
Err(anyhow::anyhow!("failed to get the resource dir"))
}

/// profiles dir
pub fn app_profiles_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("profiles"))
Expand Down
16 changes: 16 additions & 0 deletions backend/tauri/src/utils/init.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::dialog::migrate_dialog;
use crate::{
config::*,
utils::{dirs, help},
Expand All @@ -11,6 +12,7 @@ use log4rs::{
encode::pattern::PatternEncoder,
};
use std::fs;
use std::path::PathBuf;

/// initialize this instance's log file
fn init_log() -> Result<()> {
Expand Down Expand Up @@ -77,9 +79,23 @@ pub fn init_config() -> Result<()> {
#[cfg(target_os = "windows")]
let _ = dirs::init_portable_flag();

// Check if old config dir exist
let mut old_app_dir: Option<PathBuf> = None;
crate::log_err!(dirs::old_app_home_dir().map(|_old_app_dir| {
if _old_app_dir.exists() && migrate_dialog() {
old_app_dir = Some(_old_app_dir);
}
}));

let _ = init_log();

crate::log_err!(dirs::app_home_dir().map(|app_dir| {
// Do migrate
if let Some(_old_app_dir) = old_app_dir {
let _ = fs::remove_dir_all(&app_dir);
let _ = fs::rename(_old_app_dir, &app_dir);
}

if !app_dir.exists() {
let _ = fs::create_dir_all(&app_dir);
}
Expand Down

0 comments on commit 4688334

Please sign in to comment.