-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: draft patch to resolve custom app config migration
- Loading branch information
1 parent
593a2ed
commit 8cc9e8c
Showing
10 changed files
with
355 additions
and
109 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use anyhow::Ok; | ||
use clap::{Parser, Subcommand}; | ||
|
||
#[derive(Parser)] | ||
#[command(version = None, about = None, long_about = None)] | ||
pub struct Cli { | ||
#[command(subcommand)] | ||
command: Option<Commands>, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
enum Commands { | ||
MigrateHomeDir { | ||
#[arg(short = 't', long)] | ||
target_path: String, | ||
}, | ||
} | ||
|
||
pub fn parse() -> anyhow::Result<()> { | ||
let cli = Cli::parse(); | ||
if let Some(commands) = &cli.command { | ||
match commands { | ||
Commands::MigrateHomeDir { target_path } => { | ||
self::handler::migrate_home_dir_handler(target_path).unwrap(); | ||
} | ||
} | ||
std::process::exit(0); | ||
} | ||
Ok(()) // bypass | ||
} | ||
|
||
mod handler { | ||
#[cfg(target_os = "windows")] | ||
pub fn migrate_home_dir_handler(target_path: &str) -> anyhow::Result<()> { | ||
use crate::utils::{self, dirs}; | ||
use anyhow::Context; | ||
use deelevate::{PrivilegeLevel, Token}; | ||
use std::{path::PathBuf, process::Command, str::FromStr, thread, time::Duration}; | ||
use sysinfo::System; | ||
use tauri::utils::platform::current_exe; | ||
|
||
let token = Token::with_current_process()?; | ||
if let PrivilegeLevel::NotPrivileged = token.privilege_level()? { | ||
eprintln!("Please run this command as admin to prevent authority issue."); | ||
std::process::exit(1); | ||
} | ||
|
||
let current_home_dir = dirs::app_home_dir()?; | ||
let target_home_dir = PathBuf::from_str(target_path)?; | ||
|
||
// 1. waiting for app exited | ||
println!("waiting for app exited."); | ||
let placeholder = dirs::get_single_instance_placeholder(); | ||
let mut single_instance: single_instance::SingleInstance; | ||
loop { | ||
single_instance = single_instance::SingleInstance::new(&placeholder) | ||
.context("failed to create single instance")?; | ||
if single_instance.is_single() { | ||
break; | ||
} | ||
thread::sleep(Duration::from_secs(1)); | ||
} | ||
|
||
// 2. kill all related processes. | ||
let related_names = [ | ||
"clash-verge-service", | ||
"clash-nyanpasu-service", // for upcoming v1.6.x | ||
"clash-rs", | ||
"mihomo", | ||
"mihomo-alpha", | ||
"clash", | ||
]; | ||
let sys = System::new_all(); | ||
'outer: for process in sys.processes().values() { | ||
let mut process_name = process.name(); | ||
if process_name.ends_with(".exe") { | ||
process_name = &process_name[..process_name.len() - 4]; // remove .exe | ||
} | ||
for name in related_names.iter() { | ||
if process_name.ends_with(name) { | ||
println!( | ||
"Process found: {} should be killed. killing...", | ||
process_name | ||
); | ||
if !process.kill() { | ||
eprintln!("failed to kill {}.", process_name) | ||
} | ||
continue 'outer; | ||
} | ||
} | ||
} | ||
|
||
// 3. do config migrate and update the registry. | ||
utils::init::do_config_migration(¤t_home_dir, &target_home_dir)?; | ||
utils::winreg::set_app_dir(target_home_dir.as_path())?; | ||
println!("migration finished. starting application..."); | ||
drop(single_instance); // release single instance lock | ||
|
||
let app_path = current_exe()?; | ||
thread::spawn(move || { | ||
Command::new(app_path).spawn().unwrap(); | ||
}); | ||
thread::sleep(Duration::from_secs(5)); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
pub fn migrate_home_dir_handler(target_path: &str) -> anyhow::Result<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod clash; | ||
pub mod commands; | ||
pub mod handle; | ||
pub mod hotkey; | ||
pub mod logger; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters