From a8b11abec8177f93af83aab91d10ee0ae47a6d57 Mon Sep 17 00:00:00 2001 From: MystiPanda Date: Wed, 17 Jan 2024 15:06:16 +0800 Subject: [PATCH] feat: Support Startup Script --- src-tauri/Cargo.toml | 2 +- src-tauri/src/config/verge.rs | 4 ++ src-tauri/src/utils/init.rs | 43 ++++++++++++++++ src-tauri/src/utils/resolve.rs | 2 +- src-tauri/tauri.conf.json | 4 ++ src/components/setting/setting-verge.tsx | 62 ++++++++++++++++++++++-- src/locales/en.json | 2 + src/locales/ru.json | 2 + src/locales/zh.json | 2 + src/services/types.d.ts | 1 + 10 files changed, 119 insertions(+), 5 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a084977..801fa89 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -39,7 +39,7 @@ tokio = { version = "1", features = ["full"] } serde = { version = "1.0", features = ["derive"] } reqwest = { version = "0.11", features = ["json", "rustls-tls"] } sysproxy = { git="https://github.com/clash-verge-rev/sysproxy-rs", branch = "main" } -tauri = { version = "1.5", features = [ "notification-all", "icon-png", "clipboard-all", "global-shortcut-all", "process-all", "shell-all", "system-tray", "updater", "window-all"] } +tauri = { version = "1.5", features = [ "dialog-open", "notification-all", "icon-png", "clipboard-all", "global-shortcut-all", "process-all", "shell-all", "system-tray", "updater", "window-all"] } [target.'cfg(windows)'.dependencies] runas = "=1.0.0" # 高版本会返回错误 Status diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs index 9582793..16422a9 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/verge.rs @@ -25,6 +25,9 @@ pub struct IVerge { /// copy env type pub env_type: Option, + /// startup script path + pub startup_script: Option, + /// enable traffic graph default is true pub traffic_graph: Option, @@ -189,6 +192,7 @@ impl IVerge { patch!(theme_mode); patch!(tray_event); patch!(env_type); + patch!(startup_script); patch!(traffic_graph); patch!(enable_memory_usage); diff --git a/src-tauri/src/utils/init.rs b/src-tauri/src/utils/init.rs index 595f389..5ce1488 100644 --- a/src-tauri/src/utils/init.rs +++ b/src-tauri/src/utils/init.rs @@ -8,7 +8,9 @@ use log4rs::append::file::FileAppender; use log4rs::config::{Appender, Logger, Root}; use log4rs::encode::pattern::PatternEncoder; use std::fs::{self, DirEntry}; +use std::path::PathBuf; use std::str::FromStr; +use tauri::api::process::Command; /// initialize this instance's log file fn init_log() -> Result<()> { @@ -340,3 +342,44 @@ pub fn init_scheme() -> Result<()> { pub fn init_scheme() -> Result<()> { Ok(()) } + +pub fn startup_script() -> Result<()> { + let path = { + let verge = Config::verge(); + let verge = verge.latest(); + verge.startup_script.clone().unwrap_or("".to_string()) + }; + + if !path.is_empty() { + let mut shell = ""; + if path.ends_with(".sh") { + shell = "bash"; + } + if path.ends_with(".ps1") { + shell = "powershell"; + } + if path.ends_with(".bat") { + shell = "cmd"; + } + if shell.is_empty() { + return Err(anyhow::anyhow!("unsupported script: {path}")); + } + let current_dir = PathBuf::from(path.clone()); + if !current_dir.exists() { + return Err(anyhow::anyhow!("script not found: {path}")); + } + let current_dir = current_dir.parent(); + match current_dir { + Some(dir) => { + let _ = Command::new(shell) + .current_dir(dir.to_path_buf()) + .args(&[path]) + .output()?; + } + None => { + let _ = Command::new(shell).args(&[path]).output()?; + } + } + } + Ok(()) +} diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index 3cd4867..47b434c 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -44,7 +44,7 @@ pub fn resolve_setup(app: &mut App) { #[cfg(target_os = "windows")] log_err!(init::init_service()); log_err!(init::init_scheme()); - + log_err!(init::startup_script()); // 处理随机端口 let enable_random_port = Config::verge().latest().enable_random_port.unwrap_or(false); diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 6c8ba90..93d1813 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -54,6 +54,10 @@ }, "notification": { "all": true + }, + "dialog": { + "all": false, + "open": true } }, "windows": [], diff --git a/src/components/setting/setting-verge.tsx b/src/components/setting/setting-verge.tsx index 210aca3..165ba2a 100644 --- a/src/components/setting/setting-verge.tsx +++ b/src/components/setting/setting-verge.tsx @@ -1,7 +1,15 @@ import { useRef } from "react"; import { useLockFn } from "ahooks"; import { useTranslation } from "react-i18next"; -import { IconButton, MenuItem, Select, Typography } from "@mui/material"; +import { open } from "@tauri-apps/api/dialog"; +import { + Button, + IconButton, + MenuItem, + Select, + Input, + Typography, +} from "@mui/material"; import { openAppDir, openCoreDir, openLogsDir } from "@/services/cmds"; import { ArrowForward } from "@mui/icons-material"; import { checkUpdate } from "@tauri-apps/api/updater"; @@ -30,7 +38,8 @@ const SettingVerge = ({ onError }: Props) => { const { t } = useTranslation(); const { verge, patchVerge, mutateVerge } = useVerge(); - const { theme_mode, language, tray_event, env_type } = verge ?? {}; + const { theme_mode, language, tray_event, env_type, startup_script } = + verge ?? {}; const configRef = useRef(null); const hotkeyRef = useRef(null); const miscRef = useRef(null); @@ -125,7 +134,54 @@ const SettingVerge = ({ onError }: Props) => { - + + e.target.value} + onChange={(e) => onChangeData({ startup_script: e })} + onGuard={(e) => patchVerge({ startup_script: e })} + > + + + {startup_script && ( + + )} + + } + > + +