win-rar 0.1.0

A Windows archive manager supporting ZIP, 7z, RAR, TAR with encryption, shell integration, and drag-and-drop
// Copyright (c) 北京锋通科技有限公司 — 郭玉峰、吴琼
// SPDX-License-Identifier: MIT

use std::sync::Mutex;
use tauri::Manager;

mod archive;
mod async_ops;
mod commands;
mod shell;

/// 存储启动时的命令行参数,供前端就绪后查询
struct CliArgs(Mutex<Option<serde_json::Value>>);

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_dialog::init())
        .invoke_handler(tauri::generate_handler![
            commands::archive::list_archive,
            commands::archive::extract_archive,
            commands::archive::compress_files,
            commands::archive::detect_format,
            commands::archive::check_rar_support,
            commands::system::open_file_dialog,
            commands::system::open_files_dialog,
            commands::system::open_folder_dialog,
            commands::shell::register_shell,
            commands::shell::unregister_shell,
            commands::shell::check_shell_integration,
            commands::system::get_cli_args,
            commands::system::check_file_exists,
        ])
        .setup(|app| {
            // 创建 Tokio 异步运行时
            let rt = tokio::runtime::Runtime::new()
                .expect("无法创建 Tokio 运行时");
            let handle = rt.handle().clone();
            app.manage(async_ops::task::TaskManager::new(handle));
            // 保持 runtime 存活
            app.manage(rt);

            // 解析命令行参数并存储
            let args: Vec<String> = std::env::args().collect();
            let cli_event = if args.len() > 1 {
                let arg = &args[1];
                if arg == "--register-shell" {
                    let exe_path = std::env::current_exe()
                        .map(|p| p.to_string_lossy().to_string())
                        .unwrap_or_default();
                    match shell::register_shell_integration(&exe_path) {
                        Ok(()) => println!("壳集成注册成功"),
                        Err(e) => eprintln!("壳集成注册失败: {}", e),
                    }
                    std::process::exit(0);
                } else if arg == "--unregister-shell" {
                    match shell::unregister_shell_integration() {
                        Ok(()) => println!("壳集成注销成功"),
                        Err(e) => eprintln!("壳集成注销失败: {}", e),
                    }
                    std::process::exit(0);
                } else if arg == "--extract-here" && args.len() > 2 {
                    let file_path = &args[2];
                    let dir = std::path::Path::new(file_path)
                        .parent()
                        .map(|p| p.to_string_lossy().to_string())
                        .unwrap_or_else(|| ".".to_string());
                    Some(serde_json::json!({
                        "type": "extract-here",
                        "path": file_path,
                        "output_dir": dir,
                    }))
                } else if arg == "--compress" && args.len() > 2 {
                    Some(serde_json::json!({
                        "type": "compress",
                        "target": &args[2],
                    }))
                } else if !arg.starts_with('-') {
                    Some(serde_json::json!({
                        "type": "open-file",
                        "path": arg,
                    }))
                } else {
                    None
                }
            } else {
                None
            };

            app.manage(CliArgs(Mutex::new(cli_event)));

            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}