e_utils/system/cmd/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/// 多任务
pub mod tasks;
use crate::AnyRes;

use super::encode::auto_decode;
use serde::{de, Deserialize, Serialize};
use std::{
  collections::HashMap,
  ffi::OsStr,
  path::{Path, PathBuf},
  process::{Command, ExitStatus, Stdio},
};
use strum::*;
type DWORD = u32;

/// 无首选 NUMA 节点
pub const NUMA_NO_PREFERRED_NODE: DWORD = 0x0;
/// Windows 创建无窗口进程的标志
pub const CREATE_NO_WINDOW: DWORD = 0x08000000;
/// Windows 创建新进程组的标志
pub const CREATE_NEW_PROCESS_GROUP: DWORD = 0x00000200;
/// 调试新进程。调试器将接收所有调试事件,包括来自此进程创建的所有子进程的事件
pub const DEBUG_PROCESS: DWORD = 0x00000001;
/// 调试此进程。调试器不会接收此进程创建的任何子进程的调试事件
pub const DEBUG_ONLY_THIS_PROCESS: DWORD = 0x00000002;
/// 进程的主线程以挂起状态创建,直到调用 ResumeThread 函数
pub const CREATE_SUSPENDED: DWORD = 0x00000004;
/// 对于控制台进程,新进程没有访问其父进程控制台的权限
pub const DETACHED_PROCESS: DWORD = 0x00000008;
/// 新进程有一个新的控制台,而不是继承其父进程的控制台
pub const CREATE_NEW_CONSOLE: DWORD = 0x00000010;
/// 进程具有正常优先级类
pub const NORMAL_PRIORITY_CLASS: DWORD = 0x00000020;
/// 进程具有空闲优先级类
pub const IDLE_PRIORITY_CLASS: DWORD = 0x00000040;
/// 进程具有高优先级类
pub const HIGH_PRIORITY_CLASS: DWORD = 0x00000080;
/// 进程具有实时优先级类
pub const REALTIME_PRIORITY_CLASS: DWORD = 0x00000100;
/// 如果在 lpEnvironment 参数中指定了环境块,则它使用 Unicode 字符
pub const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400;
/// 新进程在单独的 Windows VDM 中运行 16 位应用程序
pub const CREATE_SEPARATE_WOW_VDM: DWORD = 0x00000800;
/// 新进程与其他应用程序共享 Windows VDM
pub const CREATE_SHARED_WOW_VDM: DWORD = 0x00001000;
/// 强制在单独的 VDM 中运行
pub const CREATE_FORCEDOS: DWORD = 0x00002000;
/// 进程具有低于正常优先级的优先级类
pub const BELOW_NORMAL_PRIORITY_CLASS: DWORD = 0x00004000;
/// 进程具有高于正常优先级的优先级类
pub const ABOVE_NORMAL_PRIORITY_CLASS: DWORD = 0x00008000;
/// 进程继承其父进程的处理器关联性
pub const INHERIT_PARENT_AFFINITY: DWORD = 0x00010000;
/// 进程继承其调用者的优先级
pub const INHERIT_CALLER_PRIORITY: DWORD = 0x00020000;
/// 进程是受保护的进程
pub const CREATE_PROTECTED_PROCESS: DWORD = 0x00040000;
/// 进程创建时使用扩展的启动信息
pub const EXTENDED_STARTUPINFO_PRESENT: DWORD = 0x00080000;
/// 开始后台模式,这可能会降低进程的内存和 I/O 优先级
pub const PROCESS_MODE_BACKGROUND_BEGIN: DWORD = 0x00100000;
/// 结束后台模式,恢复正常优先级
pub const PROCESS_MODE_BACKGROUND_END: DWORD = 0x00200000;
/// 进程不受其父作业的限制
pub const CREATE_BREAKAWAY_FROM_JOB: DWORD = 0x01000000;
/// 保留进程的代码授权级别
pub const CREATE_PRESERVE_CODE_AUTHZ_LEVEL: DWORD = 0x02000000;
/// 进程不继承其父进程的错误模式
pub const CREATE_DEFAULT_ERROR_MODE: DWORD = 0x04000000;
/// 为用户启用分析
pub const PROFILE_USER: DWORD = 0x10000000;
/// 为内核启用分析
pub const PROFILE_KERNEL: DWORD = 0x20000000;
/// 为服务器启用分析
pub const PROFILE_SERVER: DWORD = 0x40000000;
/// 忽略系统默认优先级和调度量程
pub const CREATE_IGNORE_SYSTEM_DEFAULT: DWORD = 0x80000000;

/// 表示命令执行结果的结构体
/// 这个结构体包含了命令执行后的标准输出、退出状态和标准错误输出。
#[derive(Debug, Clone)]
pub struct CmdOutput {
  /// 命令的标准输出,已经被解码为字符串
  pub stdout: String,
  /// 命令的退出状态
  pub status: ExitStatus,
  /// 命令的标准错误输出,保持为原始字节
  pub stderr: Vec<u8>,
}

/// 通用命令结果结构体,可序列化和反序列化
/// 这个结构体用于表示一个通用的命令执行结果,包含内容、状态和自定义选项。
/// 它可以被序列化和反序列化,方便在不同的上下文中传递和存储。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CmdResult<T> {
  /// 命令执行的结果内容
  pub content: String,
  /// 命令执行的状态,true 表示成功,false 表示失败
  pub status: bool,
  /// 与命令相关的自定义选项,类型为泛型 T
  pub opts: T,
}

impl<T> CmdResult<T> {
  /// 设置选项
  pub fn set_opts(mut self, opts: T) -> Self {
    self.opts = opts;
    self
  }

  /// 合并另一个 CmdResult
  pub fn merge(mut self, target: Self) -> Self {
    self.content = target.content;
    self.status = target.status;
    self
  }

  /// 设置状态
  pub fn set_status(mut self, state: bool) -> Self {
    self.status = state;
    self
  }

  /// 设置内容
  pub fn set_content(mut self, content: String) -> Self {
    self.content = content;
    self
  }

  /// 获取选项引用
  pub fn opts(&self) -> &T {
    &self.opts
  }
}

impl<'a, T: de::Deserialize<'a>> CmdResult<T> {
  /// 从字符串解析 CmdResult
  pub fn from_str(value: &'a str) -> crate::AnyResult<Self> {
    let s = value.trim().trim_start_matches("R<").trim_end_matches(">R");
    Ok(serde_json::from_str(s)?)
  }
}

impl<T: Serialize> CmdResult<T> {
  /// 将 CmdResult 转换为字符串
  pub fn to_str(&self) -> crate::AnyResult<String> {
    Ok(format!("R<{}>R", serde_json::to_string(self)?))
  }

  /// 将 CmdResult 转换为格式化的字符串
  pub fn to_string_pretty(&self) -> crate::AnyResult<String> {
    Ok(format!("R<{}>R", serde_json::to_string_pretty(self)?))
  }
}

/// 打开文件或目录的 shell 命令
#[cfg(feature = "fs")]
pub fn shell_open(target: impl AsRef<str>) -> crate::AnyResult<()> {
  let pathname = crate::fs::convert_path(target.as_ref());
  #[cfg(target_os = "macos")]
  Cmd::new("open").args(&["-R", &pathname]).spawn()?;
  #[cfg(target_os = "windows")]
  Cmd::new("explorer.exe").arg(pathname).spawn()?;
  #[cfg(target_os = "linux")]
  Cmd::new("xdg-open").arg(pathname).spawn()?;
  Ok(())
}

/// 异步打开文件或目录的 shell 命令
#[cfg(all(feature = "fs", feature = "tokio"))]
pub async fn a_shell_open(target: impl AsRef<str>) -> crate::AnyResult<()> {
  let pathname = crate::fs::convert_path(target.as_ref());
  #[cfg(target_os = "macos")]
  Cmd::new("open")
    .args(&["-R", &pathname])
    .a_spawn()?
    .wait()
    .await?;
  #[cfg(target_os = "windows")]
  Cmd::new("explorer.exe")
    .arg(pathname)
    .a_spawn()?
    .wait()
    .await?;
  #[cfg(target_os = "linux")]
  Cmd::new("xdg-open").arg(pathname).a_spawn()?.wait().await?;
  Ok(())
}

/// 命令结构体,用于构建和执行系统命令
/// ```rust
/// use e_utils::{shell_open, Cmd};
/// fn test_cmd() {
///   let output = Cmd::new("echo Hello from cmd").output().unwrap();
///   assert_eq!(output.stdout, "Hello from cmd");
///   assert!(Cmd::new("echo Hello from cmd")
///     .output()
///     .is_err());
/// }
/// fn test_shell_open_windows() {
///   assert!(shell_open("C:\\").is_ok());
/// }
/// fn main() {
///   test_cmd();
///   test_shell_open_windows();
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cmd {
  exe: String,
  args: Vec<String>,
  cwd: Option<PathBuf>,
  flags: DWORD,
  env: Option<HashMap<String, String>>,
  exe_type: ExeType,
}
impl Cmd {
  /// 获取自动识别的exe路径
  pub fn get_exe_path(&self) -> PathBuf {
    let cwd = self
      .cwd
      .clone()
      .unwrap_or(std::env::current_dir().unwrap_or_default());
    cwd.join(&self.exe)
  }
  /// 检查exe路径是否存在
  pub fn check_exe_path(&self) -> crate::Result<PathBuf> {
    let path = self.get_exe_path();
    if !path.exists() {
      return Err(format!("File not found: {}", path.display()).into());
    }
    Ok(path)
  }
  /// Args处理
  pub fn split_args(args: &str, key: char) -> Vec<String> {
    let mut result = Vec::with_capacity(args.split_whitespace().count());
    let mut start = 0;
    let mut in_quotes = None;
    let chars: Vec<_> = args.chars().collect();
    for (i, &c) in chars.iter().enumerate() {
      match c {
        '"' | '\'' => {
          if let Some(quote) = in_quotes {
            if quote == c {
              in_quotes = None;
              if start < i {
                result.push(chars[start..i].iter().collect());
                start = i + 1;
              }
            }
          } else {
            in_quotes = Some(c);
            start = i + 1;
          }
        }
        _ if c == key && in_quotes.is_none() => {
          if start < i {
            result.push(chars[start..i].iter().collect());
          }
          start = i + 1;
        }
        _ if i == chars.len() - 1 => {
          if start <= i {
            result.push(chars[start..=i].iter().collect());
          }
        }
        _ => {}
      }
    }

    result
  }
}
impl Cmd {
  /// 命令结构体,用于构建和执行系统命令
  /// ```rust
  /// use e_utils::{shell_open, Cmd};
  /// fn test_cmd() {
  ///   let output = Cmd::new("echo Hello from cmd").output().unwrap();
  ///   assert_eq!(output.stdout, "Hello from cmd");
  ///   assert!(Cmd::new("echo Hello from cmd")
  ///     .output()
  ///     .is_err());
  /// }
  /// fn test_shell_open_windows() {
  ///   assert!(shell_open("C:\\").is_ok());
  /// }
  /// fn main() {
  ///   test_cmd();
  ///   test_shell_open_windows();
  /// }
  /// ```
  pub fn new<S: AsRef<OsStr>>(exe: S) -> Self {
    Self {
      exe: exe.as_ref().to_string_lossy().into_owned(),
      args: Vec::new(),
      cwd: None,
      flags: CREATE_UNICODE_ENVIRONMENT |    // Unicode 环境支持
        CREATE_NO_WINDOW |           // 没控制台
        CREATE_NEW_PROCESS_GROUP |     // 新进程组
        NORMAL_PRIORITY_CLASS |        // 正常优先级
        INHERIT_PARENT_AFFINITY |      // 继承父进程关联性
        INHERIT_CALLER_PRIORITY| // 继承调用者优先级,
        // CREATE_PROTECTED_PROCESS |      // 受保护进程
        CREATE_PRESERVE_CODE_AUTHZ_LEVEL| // 保留代码授权级别
        CREATE_DEFAULT_ERROR_MODE, // 默认错误模式
      env: None,
      exe_type: ExeType::default(),
    }
  }
  /// 添加Path
  pub fn cwd(mut self, env_path: impl AsRef<std::path::Path>) -> Self {
    let path = env_path.as_ref().to_path_buf();
    let f = |myenv: &mut HashMap<String, String>, new_path: PathBuf| {
      if let Some(p) = myenv.get_mut("Path") {
        let mut paths = std::env::split_paths(&p).collect::<Vec<_>>();
        paths.push(new_path);
        if let Some(new_path_str) = std::env::join_paths(paths)
          .ok()
          .and_then(|x| x.into_string().ok())
        {
          *p = new_path_str;
        }
      }
    };
    self.cwd = Some(path.clone());
    if let Some(ref mut myenv) = self.env {
      f(myenv, path)
    } else {
      let mut myenv: HashMap<String, String> = std::env::vars().collect();
      f(&mut myenv, path);
      self.env = Some(myenv);
    }
    self
  }
  /// 添加单个参数
  pub fn arg(mut self, arg: impl Into<String>) -> Self {
    let arg = arg.into();
    if !arg.is_empty() {
      self.args.push(arg);
    }
    self
  }
  /// 配置flags
  pub fn flags(mut self, flags: DWORD) -> Self {
    self.flags = flags;
    self
  }
  /// 添加多个参数
  pub fn args<I, S>(mut self, args: I) -> Self
  where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
  {
    self.args.extend(
      args
        .into_iter()
        .map(|s| s.as_ref().to_string_lossy().into_owned()),
    );
    self
  }
  /// 添加多个参数
  pub fn set_args(&mut self, args: Vec<String>) -> &mut Self {
    if !args.is_empty() {
      self.args = args;
    }
    self
  }

  /// 设置单个环境变量
  pub fn env<K, V>(mut self, key: K, val: V) -> Self
  where
    K: Into<String>,
    V: Into<String>,
  {
    self
      .env
      .get_or_insert_with(HashMap::new)
      .insert(key.into(), val.into());
    self
  }

  /// 设置多个环境变量
  pub fn envs<I, K, V>(mut self, vars: I) -> Self
  where
    I: IntoIterator<Item = (K, V)>,
    K: Into<String>,
    V: Into<String>,
  {
    let env = self.env.get_or_insert_with(HashMap::new);
    for (key, val) in vars {
      env.insert(key.into(), val.into());
    }
    self
  }
  /// 设置是否使用 cmd.exe 或 sh
  pub fn set_type(mut self, exe_type: ExeType) -> Self {
    self.exe_type = exe_type;
    self
  }

  /// 准备标准 Command
  fn prepare_command(&self) -> crate::Result<Command> {
    self.prepare_generic_command(|cmd| Box::new(Command::new(cmd)))
  }

  /// 准备 Tokio Command
  #[cfg(feature = "tokio")]
  fn prepare_tokio_command(&self) -> crate::Result<tokio::process::Command> {
    self.prepare_generic_command(|cmd| Box::new(tokio::process::Command::new(cmd)))
  }

  /// 通用命令准备函数
  fn prepare_generic_command<C>(&self, new_command: impl Fn(&str) -> Box<C>) -> crate::Result<C>
  where
    C: CommandTrait,
  {
    let exe_type = match self.exe_type {
      ExeType::AutoShell => match ExeType::from_target(&self.exe) {
        ExeType::Unknown => {
          if cfg!(target_os = "windows") {
            ExeType::PowerShell
          } else {
            ExeType::Shell
          }
        }
        v => v,
      },
      other => other,
    };
    let mut cmd = match exe_type {
      ExeType::AutoShell => return Err("AutoShell 无法执行".into()),
      ExeType::PowerShell => new_command("powershell").args(["-NoProfile", "-Command", &self.exe]),
      ExeType::Shell => new_command("sh").args(["-c", &self.exe]),
      ExeType::Cmd => new_command("cmd.exe").args(["/C", &self.exe]),
      ExeType::Ps1Script => {
        new_command("powershell.exe").args(["-ExecutionPolicy", "Bypass", "-File", &self.exe])
      }
      ExeType::Vbs => new_command("cscript.exe").args(["/Nologo"]).arg(&self.exe),
      ExeType::PythonScript => new_command("python").arg(&self.exe),
      ExeType::MacOSApp => new_command("open").arg(&self.get_exe_path()),
      ExeType::AndroidApk => new_command("adb").args(["shell", "am", "start", "-n", &self.exe]),
      ExeType::IosApp => new_command("xcrun").args(["simctl", "launch", "booted", &self.exe]),
      _ => *new_command(&self.check_exe_path()?.to_string_lossy()),
    };
    // 如果参数不为空,则将exe作为参数
    if !self.args.is_empty() {
      cmd = cmd.args(&self.args);
    }
    // 设置环境变量
    if let Some(ref env) = self.env {
      cmd = cmd.envs(env);
    } else {
      cmd = cmd.envs(std::env::vars());
    }

    // 设置工作目录
    if let Some(ref cwd) = self.cwd {
      cmd = cmd.current_dir(cwd);
    }

    cmd = cmd.creation_flags(self.flags);
    // 配置标准输入输出
    cmd = cmd
      .stdin(Stdio::piped())
      .stdout(Stdio::piped())
      .stderr(Stdio::piped());
    Ok(cmd)
  }

  /// 执行命令并等待结果
  pub fn output(&self) -> crate::Result<CmdOutput> {
    let output = self.prepare_command()?.output().any()?;
    let stdout = auto_decode(&output.stdout)
      .unwrap_or_else(|_| String::from_utf8_lossy(&output.stdout).to_string());

    Ok(CmdOutput {
      stdout,
      status: output.status,
      stderr: output.stderr,
    })
  }

  /// 异步执行命令并等待结果
  #[cfg(feature = "tokio")]
  pub async fn a_output(&self) -> crate::Result<CmdOutput> {
    let output = self.prepare_tokio_command()?.output().await.any()?;
    let stdout = auto_decode(&output.stdout)
      .unwrap_or_else(|_| String::from_utf8_lossy(&output.stdout).to_string());

    Ok(CmdOutput {
      stdout,
      status: output.status,
      stderr: output.stderr,
    })
  }

  /// 启动命令但不等待结果
  pub fn spawn(&self) -> crate::Result<std::process::Child> {
    self.prepare_command()?.spawn().any()
  }

  /// 异步启动命令但不等待结果
  #[cfg(feature = "tokio")]
  pub fn a_spawn(&self) -> crate::Result<tokio::process::Child> {
    self.prepare_tokio_command()?.spawn().any()
  }
}

/// 统一 Command 和 tokio::process::Command 接口的 trait
/// 这个 trait 定义了一组通用的命令配置方法,使得 std::process::Command 和 tokio::process::Command
/// 可以使用相同的接口进行操作。
pub trait CommandTrait<Target = Command> {
  /// 添加单个命令行参数
  fn arg<S: AsRef<OsStr>>(self, arg: S) -> Self;

  /// 添加多个命令行参数
  fn args<I, S>(self, args: I) -> Self
  where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>;

  /// 设置环境变量
  fn envs<I, K, V>(self, vars: I) -> Self
  where
    I: IntoIterator<Item = (K, V)>,
    K: AsRef<OsStr>,
    V: AsRef<OsStr>;
  /// 设置命令的工作目录
  fn current_dir<P: AsRef<std::path::Path>>(self, dir: P) -> Self;

  /// 配置命令的标准输入
  fn stdin<T: Into<Stdio>>(self, cfg: T) -> Self;

  /// 配置命令的标准输出
  fn stdout<T: Into<Stdio>>(self, cfg: T) -> Self;

  /// 配置命令的标准错误输出
  fn stderr<T: Into<Stdio>>(self, cfg: T) -> Self;

  /// 设置进程创建标志(主要用于 Windows 系统)
  fn creation_flags(self, flags: u32) -> Self;
}

// 为 std::process::Command 实现 CommandTrait
impl CommandTrait for Command {
  fn arg<S: AsRef<OsStr>>(mut self, arg: S) -> Self {
    Command::arg(&mut self, arg);
    self
  }
  fn args<I, S>(mut self, args: I) -> Self
  where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
  {
    Command::args(&mut self, args);
    self
  }
  fn envs<I, K, V>(mut self, vars: I) -> Self
  where
    I: IntoIterator<Item = (K, V)>,
    K: AsRef<OsStr>,
    V: AsRef<OsStr>,
  {
    Command::env_clear(&mut self).envs(vars);
    self
  }
  fn current_dir<P: AsRef<std::path::Path>>(mut self, dir: P) -> Self {
    Command::current_dir(&mut self, dir);
    self
  }
  fn stdin<T: Into<Stdio>>(mut self, cfg: T) -> Self {
    Command::stdin(&mut self, cfg);
    self
  }
  fn stdout<T: Into<Stdio>>(mut self, cfg: T) -> Self {
    Command::stdout(&mut self, cfg);
    self
  }
  fn stderr<T: Into<Stdio>>(mut self, cfg: T) -> Self {
    Command::stderr(&mut self, cfg);
    self
  }

  fn creation_flags(mut self, flags: u32) -> Self {
    #[cfg(target_os = "windows")]
    {
      // 在非 Windows 系统上不执行任何操作
      std::os::windows::process::CommandExt::creation_flags(&mut self, flags);
    }
    self
  }
}

// 为 tokio::process::Command 实现 CommandTrait
#[cfg(feature = "tokio")]
impl CommandTrait for tokio::process::Command {
  fn arg<S: AsRef<OsStr>>(mut self, arg: S) -> Self {
    tokio::process::Command::arg(&mut self, arg);
    self
  }
  fn args<I, S>(mut self, args: I) -> Self
  where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
  {
    tokio::process::Command::args(&mut self, args);
    self
  }
  fn envs<I, K, V>(mut self, vars: I) -> Self
  where
    I: IntoIterator<Item = (K, V)>,
    K: AsRef<OsStr>,
    V: AsRef<OsStr>,
  {
    tokio::process::Command::env_clear(&mut self).envs(vars);
    self
  }
  fn current_dir<P: AsRef<std::path::Path>>(mut self, dir: P) -> Self {
    tokio::process::Command::current_dir(&mut self, dir);
    self
  }
  fn stdin<T: Into<Stdio>>(mut self, cfg: T) -> Self {
    tokio::process::Command::stdin(&mut self, cfg);
    self
  }
  fn stdout<T: Into<Stdio>>(mut self, cfg: T) -> Self {
    tokio::process::Command::stdout(&mut self, cfg);
    self
  }
  fn stderr<T: Into<Stdio>>(mut self, cfg: T) -> Self {
    tokio::process::Command::stderr(&mut self, cfg);
    self
  }
  fn creation_flags(mut self, flags: u32) -> Self {
    #[cfg(target_os = "windows")]
    tokio::process::Command::creation_flags(&mut self, flags);
    self
  }
}

/// 应用程序类型枚举
#[allow(missing_docs)]
#[derive(
  Default, Clone, Copy, Debug, Display, PartialEq, EnumString, VariantArray, Deserialize, Serialize,
)]
#[repr(i32)]
pub enum ExeType {
  #[default]
  #[strum(to_string = "Auto")]
  AutoShell,
  #[strum(to_string = "PS1")]
  PowerShell,
  #[strum(to_string = "SH")]
  Shell,
  #[strum(to_string = "CMD")]
  Cmd,
  #[strum(to_string = ".exe")]
  WindowsExe,
  #[strum(to_string = ".sh")]
  ShellScript,
  #[strum(to_string = ".ps1")]
  Ps1Script,
  #[strum(to_string = ".bat")]
  Bat,
  #[strum(to_string = ".vbs")]
  Vbs,
  #[strum(to_string = ".py")]
  PythonScript,
  #[strum(to_string = ".cmd")]
  CmdScript,
  #[strum(to_string = ".app")]
  MacOSApp,
  #[strum(to_string = ".LinuxEXE")]
  LinuxExe,
  #[strum(to_string = ".apk")]
  AndroidApk,
  #[strum(to_string = ".ipa")]
  IosApp,
  #[strum(to_string = ".so")]
  So,
  #[strum(to_string = ".dll")]
  Dll,
  #[strum(to_string = "Unknown")]
  Unknown,
}
impl ExeType {
  /// 从目标路径推断可执行文件类型
  pub fn from_target(p: impl AsRef<Path>) -> Self {
    match p
      .as_ref()
      .extension()
      .and_then(|x| x.to_str())
      .unwrap_or_default()
      .to_lowercase()
      .as_str()
    {
      "exe" => ExeType::WindowsExe,
      "bat" => ExeType::Bat,
      "cmd" => ExeType::CmdScript,
      "vbs" => ExeType::Vbs,
      "ps1" => ExeType::Ps1Script,
      "sh" => ExeType::ShellScript,
      "app" => ExeType::MacOSApp,
      "apk" => ExeType::AndroidApk,
      "ipa" => ExeType::IosApp,
      "py" => ExeType::PythonScript,
      "so" => ExeType::So,
      "dll" => ExeType::Dll,
      _ => ExeType::Unknown,
    }
  }
  /// 获取可执行文件类型的扩展名
  pub fn to_extension(&self) -> &'static str {
    match self {
      ExeType::WindowsExe => "exe",
      ExeType::Bat => "bat",
      ExeType::CmdScript => "cmd",
      ExeType::Vbs => "vbs",
      ExeType::Ps1Script => "ps1",
      ExeType::ShellScript => "sh",
      ExeType::MacOSApp => "app",
      ExeType::AndroidApk => "apk",
      ExeType::IosApp => "ipa",
      ExeType::PythonScript => "py",
      ExeType::So => "so",
      ExeType::Dll => "dll",
      _ => "",
    }
  }
}

#[cfg(test)]
mod tests {
  #[cfg(feature = "tokio")]
  mod a_async {
    use crate::cmd::{Cmd, ExeType};
    #[tokio::test]
    #[cfg(not(target_os = "windows"))]
    fn test_shell_open_unix() {
      assert!(a_shell_open("/").await.is_ok());
    }
    #[tokio::test]
    #[cfg(target_os = "windows")]
    async fn test_shell_open_windows() {
      use crate::cmd::a_shell_open;

      assert!(a_shell_open("C:\\").await.is_ok());
    }
    #[tokio::test]
    async fn test_cmd_bat() {
      let cwd = std::env::current_dir().unwrap().join("examples");
      let output = Cmd::new("test.bat").cwd(cwd).a_output().await.unwrap();
      assert!(output.stdout.contains("test"));
      assert!(!Cmd::new("test.bat")
        .a_output()
        .await
        .unwrap()
        .status
        .success());
    }

    #[tokio::test]
    async fn test_cmd_type() {
      assert!(Cmd::new("echo Hello from cmd")
        .set_type(ExeType::Cmd)
        .output()
        .is_ok());
      assert!(Cmd::new("echo Hello from cmd")
        .set_type(ExeType::AutoShell)
        .output()
        .is_ok());
      assert!(Cmd::new("echo.exe")
        .args(["Hello", "from", "cmd"])
        .set_type(ExeType::IosApp)
        .output()
        .is_err());
      assert!(Cmd::new("echo Hello from cmd")
        .set_type(ExeType::WindowsExe)
        .output()
        .is_err());
    }
    #[tokio::test]
    async fn test_cmd_zh() {
      let output = Cmd::new("echo 你好Rust").a_output().await.unwrap();
      assert_eq!(output.stdout, "你好Rust");
    }
  }
  mod sync {
    use crate::cmd::{shell_open, Cmd, CmdResult};
    use serde::{Deserialize, Serialize};
    #[test]
    #[cfg(target_os = "windows")]
    fn test_shell_open_windows() {
      assert!(shell_open("C:\\").is_ok());
    }
    #[test]
    #[cfg(not(target_os = "windows"))]
    fn test_shell_open_unix() {
      assert!(shell_open("/").is_ok());
    }
    #[test]
    fn test_cmd() {
      let output = Cmd::new("echo Hello from cmd").output().unwrap();
      assert_eq!(output.stdout, "Hello from cmd");
      assert!(Cmd::new("echo Hello from cmd").output().is_err());
    }

    #[test]
    fn test_cmd_result_serialization() {
      #[derive(Debug, Serialize, Deserialize)]
      struct TestOpts {
        value: String,
      }

      let result = CmdResult {
        content: "Test content".to_string(),
        status: true,
        opts: TestOpts {
          value: "test".to_string(),
        },
      };

      let serialized = result.to_str().unwrap();
      assert!(serialized.starts_with("R<") && serialized.ends_with(">R"));

      let deserialized: CmdResult<TestOpts> = CmdResult::from_str(&serialized).unwrap();
      assert_eq!(deserialized.content, "Test content");
      assert_eq!(deserialized.status, true);
      assert_eq!(deserialized.opts.value, "test");
    }
    #[test]
    fn test_cmd_bat() {
      let cwd = std::env::current_dir().unwrap().join("examples");
      let output = Cmd::new("test.bat").cwd(cwd).output().unwrap();
      assert!(output.stdout.contains("test"));
      assert!(!Cmd::new("test.bat").output().unwrap().status.success());
    }
  }
}