framework_startup/
config.rs1use std::env;
2use std::path::PathBuf;
3
4use anyhow::{Context, Result};
5
6#[derive(Debug, Clone)]
8pub struct StartupConfig {
9 pub name: String,
11 pub description: String,
13 pub program: PathBuf,
15 pub args: Vec<String>,
17 pub working_directory: Option<PathBuf>,
19 pub ignore_not_installed: bool,
22 #[cfg(target_os = "windows")]
28 pub highest: bool,
29}
30
31impl StartupConfig {
32 pub fn new(name: impl Into<String>) -> Self {
34 Self {
35 name: name.into(),
36 description: String::new(),
37 program: PathBuf::new(),
38 args: Vec::new(),
39 working_directory: None,
40 ignore_not_installed: true,
41 #[cfg(target_os = "windows")]
42 highest: false,
43 }
44 }
45
46 pub(crate) fn resolve(mut self) -> Result<Self> {
48 if self.program.as_os_str().is_empty() {
49 self.program = env::current_exe().context("获取当前可执行文件路径失败")?;
50 }
51
52 Ok(self)
53 }
54
55 #[cfg(any(target_os = "windows", target_os = "linux"))]
58 pub(crate) fn command_line(&self) -> String {
59 let mut line = quote(&self.program.to_string_lossy());
60 for arg in &self.args {
61 line.push(' ');
62 line.push_str("e(arg));
63 }
64
65 line
66 }
67}
68
69#[cfg(any(target_os = "windows", target_os = "linux"))]
71fn quote(value: &str) -> String {
72 if value.contains('"') || !value.contains(' ') {
73 return value.to_string();
74 }
75
76 format!("\"{value}\"")
77}