dsh模块、快捷键调整
This commit is contained in:
1 parent
ff1d5cd517
commit
7e65825d3e
54 files changed
+7082
-109
No files matched your search
@@ -0,0 +1,232 @@
|
||||
//! dsh 模块的 Tauri 命令层。
|
||||
//!
|
||||
//! 前端统一经 `@/lib/bindings` 的 `commands.dsh*` 调用。
|
||||
//! 进度事件 `dsh-install-progress`、状态事件 `dsh-server-state` 见
|
||||
//! `crate::constants::events`(与 `src/lib/constants.ts` 的 `EVENTS` 对应)。
|
||||
|
||||
use tauri::Manager;
|
||||
use tauri::{AppHandle, State};
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
|
||||
use super::types::{
|
||||
DshAiPreview, DshEnvStatus, DshInstallStatus, DshNpmRegistryInfo, DshServerStatus, DshSettings,
|
||||
DshUpdateInfo, NodeStatus,
|
||||
};
|
||||
use super::DshManager;
|
||||
|
||||
/// 环境总览:Node / dsh / 服务状态 + 模块设置(模块首屏一次拿全)。
|
||||
///
|
||||
/// 声明为 `async`:Tauri 的**同步**命令在主线程执行,而本命令要跑
|
||||
/// `node --version` 子进程探测,放主线程会造成界面卡顿。
|
||||
/// (async 命令带 `State<'_>` 引用参数时必须返回 `Result`,故这里包一层 Ok。)
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_env_status(state: State<'_, DshManager>) -> Result<DshEnvStatus, String> {
|
||||
Ok(DshEnvStatus {
|
||||
node: state.node_status(),
|
||||
dsh: state.install_status(),
|
||||
server: state.server_status(),
|
||||
settings: state.load_settings(),
|
||||
})
|
||||
}
|
||||
|
||||
/// 重新探测 Node(系统 PATH + 托管目录)。同样因执行 `node --version` 而用 async。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_node_detect(state: State<'_, DshManager>) -> Result<NodeStatus, String> {
|
||||
Ok(state.node_status())
|
||||
}
|
||||
|
||||
/// 下载并安装便携 Node(`version` 为空 = 自动取满足要求的最新版)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_node_install(
|
||||
app: AppHandle,
|
||||
state: State<'_, DshManager>,
|
||||
version: Option<String>,
|
||||
) -> Result<NodeStatus, String> {
|
||||
state.node_install(&app, version).await
|
||||
}
|
||||
|
||||
/// 安装 dsh(`version` 为空 = latest)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_install(
|
||||
app: AppHandle,
|
||||
state: State<'_, DshManager>,
|
||||
version: Option<String>,
|
||||
) -> Result<DshInstallStatus, String> {
|
||||
state.install(&app, version).await
|
||||
}
|
||||
|
||||
/// 更新 dsh 到 latest(更新前停止服务,避免文件被占用)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_update(
|
||||
app: AppHandle,
|
||||
state: State<'_, DshManager>,
|
||||
) -> Result<DshInstallStatus, String> {
|
||||
state.stop_server(&app);
|
||||
state.install(&app, None).await
|
||||
}
|
||||
|
||||
/// 卸载 dsh(停止服务并删除包目录;保留设置与 $DSH_HOME)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_uninstall(app: AppHandle, state: State<'_, DshManager>) -> Result<(), String> {
|
||||
state.uninstall(&app)
|
||||
}
|
||||
|
||||
/// 取消进行中的下载 / 安装。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_cancel_install(state: State<'_, DshManager>) {
|
||||
state.request_cancel();
|
||||
}
|
||||
|
||||
/// 查询 npm 上的可用版本。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_check_update(state: State<'_, DshManager>) -> Result<DshUpdateInfo, String> {
|
||||
state.check_update().await
|
||||
}
|
||||
|
||||
/// 读取「本次安装实际使用的 npm 源」及其来源。
|
||||
///
|
||||
/// 设置为空时 npm 会用自己配置链里的源(含系统 Node 的 `~/.npmrc`),
|
||||
/// 这里把解析结果回给前端展示,让用户能看出「到底走的是哪个源」。
|
||||
/// async:内部要跑 `node <npm-cli.js> config get registry` 子进程。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_npm_registry_info(
|
||||
state: State<'_, DshManager>,
|
||||
) -> Result<DshNpmRegistryInfo, String> {
|
||||
Ok(state.npm_registry_info())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_get_settings(state: State<'_, DshManager>) -> DshSettings {
|
||||
state.load_settings()
|
||||
}
|
||||
|
||||
/// 保存设置(即时生效;端口等改动在下次启动服务时生效)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_save_settings(
|
||||
state: State<'_, DshManager>,
|
||||
settings: DshSettings,
|
||||
) -> Result<DshSettings, String> {
|
||||
state.save_settings(settings)
|
||||
}
|
||||
|
||||
/// 启动 dsh Web 服务。async:内部要 spawn 子进程 + 读凭据 + 写 settings.yaml,
|
||||
/// 同步命令会在主线程执行从而卡住界面。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_start(app: AppHandle, state: State<'_, DshManager>) -> Result<DshServerStatus, String> {
|
||||
state.start_server(&app)
|
||||
}
|
||||
|
||||
/// 停止 dsh Web 服务。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_stop(app: AppHandle, state: State<'_, DshManager>) -> Result<DshServerStatus, String> {
|
||||
state.stop_server(&app);
|
||||
Ok(state.server_status())
|
||||
}
|
||||
|
||||
/// 重启 dsh Web 服务。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_restart(app: AppHandle, state: State<'_, DshManager>) -> Result<DshServerStatus, String> {
|
||||
state.stop_server(&app);
|
||||
// Windows 上端口释放有延迟,稍等再起,避免 bind 预检误判占用
|
||||
// (用异步 sleep:同步命令在主线程执行,阻塞 500ms 会冻界面)
|
||||
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
||||
state.start_server(&app)
|
||||
}
|
||||
|
||||
/// 预览将写入 `$DSH_HOME/settings.yaml` 的内容(不含明文密钥)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_ai_preview(state: State<'_, DshManager>) -> DshAiPreview {
|
||||
state.ai_preview()
|
||||
}
|
||||
|
||||
/// 执行 AI 配置同步(合并写入 settings.yaml,写前备份)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_ai_sync(app: AppHandle, state: State<'_, DshManager>) -> Result<DshAiPreview, String> {
|
||||
state.ai_sync(&app)?;
|
||||
Ok(state.ai_preview())
|
||||
}
|
||||
|
||||
/// 读取服务日志尾部。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_log_tail(state: State<'_, DshManager>, lines: u32) -> String {
|
||||
state.log_tail(lines as usize)
|
||||
}
|
||||
|
||||
/// 读取 dsh 安装输出(npm 的 stdout/stderr 尾部)。
|
||||
///
|
||||
/// 只存在内存里(每次安装开始时清空),供「安装引导 → dsh 本体」页临时展示,
|
||||
/// **不写应用日志、不落盘**。前端在安装进行中轮询这个命令。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_install_log(state: State<'_, DshManager>) -> String {
|
||||
state.install_log()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_log_clear(state: State<'_, DshManager>) {
|
||||
state.clear_log();
|
||||
}
|
||||
|
||||
/// 在资源管理器中打开 dsh 模块目录。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn dsh_open_dir(app: AppHandle, state: State<'_, DshManager>) -> Result<(), String> {
|
||||
app.opener()
|
||||
.open_path(state.root().to_string_lossy().to_string(), None::<&str>)
|
||||
.map_err(|e| format!("打开目录失败: {e}"))
|
||||
}
|
||||
|
||||
/// 注册(或切换)「打开 dsh 独立窗口」全局快捷键;传空字符串则注销。
|
||||
///
|
||||
/// 按下时只 emit `dsh-open-window` 事件,**窗口由前端创建**:前端才持有带令牌的
|
||||
/// 启动地址,也能用 label 判重复用已开窗口(与截图模块「快捷键 → 事件 → 前端开窗」同构)。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_register_shortcut(app: AppHandle, shortcut: String) -> Result<(), String> {
|
||||
crate::shortcut::register_shortcut(&app, "dsh", &shortcut, |a| {
|
||||
use tauri::Emitter;
|
||||
let _ = a.emit(crate::constants::events::DSH_OPEN_WINDOW, ());
|
||||
})
|
||||
}
|
||||
|
||||
/// 注销「打开 dsh 独立窗口」全局快捷键。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn dsh_unregister_shortcut(app: AppHandle) -> Result<(), String> {
|
||||
crate::shortcut::unregister_shortcut(&app, "dsh");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 供 `setup.rs` 在应用启动时按设置自启服务。
|
||||
pub fn autostart_if_enabled(app: &AppHandle) {
|
||||
let manager = app.state::<DshManager>();
|
||||
let settings = manager.load_settings();
|
||||
if !settings.auto_start {
|
||||
return;
|
||||
}
|
||||
if !manager.install_status().installed {
|
||||
crate::logger::log_warn("dsh", "设置了自动启动,但 dsh 尚未安装,跳过");
|
||||
return;
|
||||
}
|
||||
if let Err(e) = manager.start_server(app) {
|
||||
crate::logger::log_warn("dsh", &format!("自动启动 dsh 服务失败: {e}"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user