sweet_cli/commands/
cmd_cargo.rs1use anyhow::Result;
2use beet_utils::prelude::*;
3use clap::Parser;
4
5#[derive(Debug, Parser)]
6#[command(name = "test")]
7pub struct CargoTest {
8 #[command(flatten)]
9 cargo_runner: CargoCmdExtra,
10}
11impl CargoTest {
12 pub async fn run(mut self) -> Result<()> {
13 self.cargo_runner.build_cmd.cmd = "test".to_string();
14 self.cargo_runner.run().await?;
15 Ok(())
16 }
17}
18
19#[derive(Debug, Parser)]
20#[command(name = "run")]
21pub struct CargoRun {
22 #[command(flatten)]
23 cargo_runner: CargoCmdExtra,
24}
25impl CargoRun {
26 pub async fn run(mut self) -> Result<()> {
27 self.cargo_runner.build_cmd.cmd = "run".to_string();
28 self.cargo_runner.run().await?;
29 Ok(())
30 }
31}
32
33
34#[derive(Debug, Parser)]
36pub struct CargoCmdExtra {
37 #[command(flatten)]
41 pub build_cmd: CargoBuildCmd,
42 #[arg(short, long)]
43 pub watch: bool,
44 #[arg(short, long)]
45 pub no_default_filters: bool,
46 #[command(flatten)]
47 pub filter: GlobFilter,
48}
49
50
51impl CargoCmdExtra {
52 pub async fn run(mut self) -> Result<()> {
53 self.append_args();
54 self.run_binary()?;
55 if self.watch {
56 self.watch().await?;
57 }
58 Ok(())
59 }
60
61 fn append_args(&mut self) {
62 if self.no_default_filters == false {
63 self.filter
64 .include("**/*.rs")
65 .exclude("{.git,target,html}/**")
66 .exclude("*/codegen/*");
67 }
68 let is_upstream = self
69 .build_cmd
70 .package
71 .as_ref()
72 .map(|p| ["beet_utils"].contains(&p.as_str()))
74 .unwrap_or(false);
75 if self.watch && self.build_cmd.lib && !is_upstream {
76 self.build_cmd.trailing_args.push("--watch".to_string());
78 }
79 }
80 async fn watch(self) -> Result<()> {
85 let mut rx = FsWatcher {
86 filter: self.filter.clone(),
87 ..Default::default()
88 }
89 .watch()?;
90
91 while let Some(ev) = rx.recv().await? {
92 if !ev.has_mutate() {
93 continue;
94 }
95 self.run_binary()?;
96 }
97
98 Ok(())
99 }
100
101 fn run_binary(&self) -> Result<()> {
105 if self.watch {
106 terminal::clear()?;
107 println!("\nš¤ sweet as š¤\n");
108 }
109 let result = self.build_cmd.spawn();
110 if !self.watch {
113 result?;
114 }
115 Ok(())
116 }
117}