feat(config): migrate app_config_dir to Tauri Store for independent management (#109)
This commit is contained in:
137
src-tauri/src/app_store.rs
Normal file
137
src-tauri/src/app_store.rs
Normal file
@@ -0,0 +1,137 @@
|
||||
use serde_json::Value;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{OnceLock, RwLock};
|
||||
use tauri_plugin_store::StoreExt;
|
||||
|
||||
/// Store 中的键名
|
||||
const STORE_KEY_APP_CONFIG_DIR: &str = "app_config_dir_override";
|
||||
|
||||
/// 全局缓存的 AppHandle (在应用启动时设置)
|
||||
static APP_HANDLE: OnceLock<RwLock<Option<tauri::AppHandle>>> = OnceLock::new();
|
||||
|
||||
/// 设置全局 AppHandle
|
||||
pub fn set_app_handle(handle: tauri::AppHandle) {
|
||||
let store = APP_HANDLE.get_or_init(|| RwLock::new(None));
|
||||
if let Ok(mut guard) = store.write() {
|
||||
*guard = Some(handle);
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取全局 AppHandle
|
||||
fn get_app_handle() -> Option<tauri::AppHandle> {
|
||||
let store = APP_HANDLE.get()?;
|
||||
let guard = store.read().ok()?;
|
||||
guard.as_ref().cloned()
|
||||
}
|
||||
|
||||
/// 从 Tauri Store 读取 app_config_dir 覆盖配置 (无需 AppHandle 版本)
|
||||
pub fn get_app_config_dir_override() -> Option<PathBuf> {
|
||||
let app = get_app_handle()?;
|
||||
get_app_config_dir_from_store(&app)
|
||||
}
|
||||
|
||||
/// 从 Tauri Store 读取 app_config_dir 覆盖配置(公开函数)
|
||||
pub fn get_app_config_dir_from_store(app: &tauri::AppHandle) -> Option<PathBuf> {
|
||||
let store = app.store_builder("app_paths.json").build();
|
||||
|
||||
if let Err(e) = &store {
|
||||
log::warn!("无法创建 Store: {}", e);
|
||||
return None;
|
||||
}
|
||||
|
||||
let store = store.unwrap();
|
||||
|
||||
match store.get(STORE_KEY_APP_CONFIG_DIR) {
|
||||
Some(Value::String(path_str)) => {
|
||||
let path_str = path_str.trim();
|
||||
if path_str.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let path = resolve_path(path_str);
|
||||
|
||||
// 验证路径是否存在
|
||||
if !path.exists() {
|
||||
log::warn!(
|
||||
"Store 中配置的 app_config_dir 不存在: {:?}\n\
|
||||
将使用默认路径。",
|
||||
path
|
||||
);
|
||||
return None;
|
||||
}
|
||||
|
||||
log::info!("使用 Store 中的 app_config_dir: {:?}", path);
|
||||
Some(path)
|
||||
}
|
||||
Some(_) => {
|
||||
log::warn!("Store 中的 {} 类型不正确,应为字符串", STORE_KEY_APP_CONFIG_DIR);
|
||||
None
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 写入 app_config_dir 到 Tauri Store
|
||||
pub fn set_app_config_dir_to_store(
|
||||
app: &tauri::AppHandle,
|
||||
path: Option<&str>,
|
||||
) -> Result<(), String> {
|
||||
let store = app
|
||||
.store_builder("app_paths.json")
|
||||
.build()
|
||||
.map_err(|e| format!("创建 Store 失败: {}", e))?;
|
||||
|
||||
match path {
|
||||
Some(p) => {
|
||||
let trimmed = p.trim();
|
||||
if !trimmed.is_empty() {
|
||||
store.set(STORE_KEY_APP_CONFIG_DIR, Value::String(trimmed.to_string()));
|
||||
log::info!("已将 app_config_dir 写入 Store: {}", trimmed);
|
||||
} else {
|
||||
// 空字符串 = 删除配置
|
||||
store.delete(STORE_KEY_APP_CONFIG_DIR);
|
||||
log::info!("已从 Store 中删除 app_config_dir 配置");
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// None = 删除配置
|
||||
store.delete(STORE_KEY_APP_CONFIG_DIR);
|
||||
log::info!("已从 Store 中删除 app_config_dir 配置");
|
||||
}
|
||||
}
|
||||
|
||||
store.save().map_err(|e| format!("保存 Store 失败: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 解析路径,支持 ~ 开头的相对路径
|
||||
fn resolve_path(raw: &str) -> PathBuf {
|
||||
if raw == "~" {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home;
|
||||
}
|
||||
} else if let Some(stripped) = raw.strip_prefix("~/") {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home.join(stripped);
|
||||
}
|
||||
} else if let Some(stripped) = raw.strip_prefix("~\\") {
|
||||
if let Some(home) = dirs::home_dir() {
|
||||
return home.join(stripped);
|
||||
}
|
||||
}
|
||||
|
||||
PathBuf::from(raw)
|
||||
}
|
||||
|
||||
/// 从旧的 settings.json 迁移 app_config_dir 到 Store
|
||||
pub fn migrate_app_config_dir_from_settings(app: &tauri::AppHandle) -> Result<(), String> {
|
||||
// app_config_dir 已从 settings.json 移除,此函数保留但不再执行迁移
|
||||
// 如果用户在旧版本设置过 app_config_dir,需要在 Store 中手动配置
|
||||
log::info!("app_config_dir 迁移功能已移除,请在设置中重新配置");
|
||||
|
||||
// 确保 Store 初始化正常
|
||||
let _ = get_app_config_dir_from_store(app);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1222,6 +1222,13 @@ pub async fn save_settings(settings: crate::settings::AppSettings) -> Result<boo
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// 重启应用程序(当 app_config_dir 变更后使用)
|
||||
#[tauri::command]
|
||||
pub async fn restart_app(app: tauri::AppHandle) -> Result<bool, String> {
|
||||
// 使用 tauri-plugin-process 重启应用
|
||||
app.restart();
|
||||
}
|
||||
|
||||
/// 检查更新
|
||||
#[tauri::command]
|
||||
pub async fn check_for_updates(handle: tauri::AppHandle) -> Result<bool, String> {
|
||||
@@ -1469,3 +1476,20 @@ pub async fn update_endpoint_last_used(
|
||||
state.save()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 获取 app_config_dir 覆盖配置 (从 Store)
|
||||
#[tauri::command]
|
||||
pub async fn get_app_config_dir_override(app: tauri::AppHandle) -> Result<Option<String>, String> {
|
||||
Ok(crate::app_store::get_app_config_dir_from_store(&app)
|
||||
.map(|p| p.to_string_lossy().to_string()))
|
||||
}
|
||||
|
||||
/// 设置 app_config_dir 覆盖配置 (到 Store)
|
||||
#[tauri::command]
|
||||
pub async fn set_app_config_dir_override(
|
||||
app: tauri::AppHandle,
|
||||
path: Option<String>,
|
||||
) -> Result<bool, String> {
|
||||
crate::app_store::set_app_config_dir_to_store(&app, path.as_deref())?;
|
||||
Ok(true)
|
||||
}
|
||||
@@ -33,6 +33,10 @@ pub fn get_claude_settings_path() -> PathBuf {
|
||||
|
||||
/// 获取应用配置目录路径 (~/.cc-switch)
|
||||
pub fn get_app_config_dir() -> PathBuf {
|
||||
if let Some(custom) = crate::app_store::get_app_config_dir_override() {
|
||||
return custom;
|
||||
}
|
||||
|
||||
dirs::home_dir()
|
||||
.expect("无法获取用户主目录")
|
||||
.join(".cc-switch")
|
||||
@@ -245,4 +249,4 @@ pub fn get_claude_config_status() -> ConfigStatus {
|
||||
}
|
||||
}
|
||||
|
||||
//(移除未使用的备份/导入函数,避免 dead_code 告警)
|
||||
//(移除未使用的备份/导入函数,避免 dead_code 告警)
|
||||
@@ -1,4 +1,5 @@
|
||||
mod app_config;
|
||||
mod app_store;
|
||||
mod claude_mcp;
|
||||
mod claude_plugin;
|
||||
mod codex_config;
|
||||
@@ -305,7 +306,10 @@ pub fn run() {
|
||||
.plugin(tauri_plugin_process::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_store::Builder::new().build())
|
||||
.setup(|app| {
|
||||
// 设置全局 AppHandle 以供 Store 使用
|
||||
app_store::set_app_handle(app.handle().clone());
|
||||
// 注册 Updater 插件(桌面端)
|
||||
#[cfg(desktop)]
|
||||
{
|
||||
@@ -359,6 +363,11 @@ pub fn run() {
|
||||
// 初始化应用状态(仅创建一次,并在本函数末尾注入 manage)
|
||||
let app_state = AppState::new();
|
||||
|
||||
// 迁移旧的 app_config_dir 配置到 Store
|
||||
if let Err(e) = app_store::migrate_app_config_dir_from_settings(&app.handle()) {
|
||||
log::warn!("迁移 app_config_dir 失败: {}", e);
|
||||
}
|
||||
|
||||
// 首次启动迁移:扫描副本文件,合并到 config.json,并归档副本;旧 config.json 先归档
|
||||
{
|
||||
let mut config_guard = app_state.config.lock().unwrap();
|
||||
@@ -418,6 +427,7 @@ pub fn run() {
|
||||
commands::read_live_provider_settings,
|
||||
commands::get_settings,
|
||||
commands::save_settings,
|
||||
commands::restart_app,
|
||||
commands::check_for_updates,
|
||||
commands::is_portable_mode,
|
||||
commands::get_claude_plugin_status,
|
||||
@@ -447,6 +457,9 @@ pub fn run() {
|
||||
commands::add_custom_endpoint,
|
||||
commands::remove_custom_endpoint,
|
||||
commands::update_endpoint_last_used,
|
||||
// app_config_dir override via Store
|
||||
commands::get_app_config_dir_override,
|
||||
commands::set_app_config_dir_override,
|
||||
// theirs: config import/export and dialogs
|
||||
import_export::export_config_to_file,
|
||||
import_export::import_config_from_file,
|
||||
@@ -480,4 +493,4 @@ pub fn run() {
|
||||
let _ = (app_handle, event);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,12 @@ impl Default for AppSettings {
|
||||
|
||||
impl AppSettings {
|
||||
fn settings_path() -> PathBuf {
|
||||
crate::config::get_app_config_dir().join("settings.json")
|
||||
// settings.json 必须使用固定路径,不能被 app_config_dir 覆盖
|
||||
// 否则会造成循环依赖:读取 settings 需要知道路径,但路径在 settings 中
|
||||
dirs::home_dir()
|
||||
.expect("无法获取用户主目录")
|
||||
.join(".cc-switch")
|
||||
.join("settings.json")
|
||||
}
|
||||
|
||||
fn normalize_paths(&mut self) {
|
||||
@@ -178,4 +183,4 @@ pub fn get_codex_override_dir() -> Option<PathBuf> {
|
||||
.codex_config_dir
|
||||
.as_ref()
|
||||
.map(|p| resolve_override_path(p))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user