-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
133 lines (97 loc) · 2.77 KB
/
main.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
use std::env;
use std::path::PathBuf;
use anyhow::anyhow;
use clap::{Parser, Subcommand};
use xshell::{cmd, Shell};
fn get_project_root() -> anyhow::Result<PathBuf> {
let cargo_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let path = cargo_dir
.parent()
.ok_or(anyhow!("Cannot get project root"))?
.to_path_buf();
Ok(path)
}
fn build(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "docker-compose build").run()?;
Ok(())
}
fn run_server(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "cargo run --package server").run()?;
Ok(())
}
fn run_client(sh: &Shell) -> anyhow::Result<()> {
let project_root = get_project_root()?;
sh.change_dir(project_root.join("client"));
cmd!(sh, "pnpm run dev").run()?;
Ok(())
}
fn watch_server(sh: &Shell) -> anyhow::Result<()> {
let subcommand = "run --package server";
cmd!(sh, "cargo watch -x {subcommand}").run()?;
Ok(())
}
fn watch_client(sh: &Shell) -> anyhow::Result<()> {
let project_root = get_project_root()?;
sh.change_dir(project_root.join("client"));
cmd!(sh, "pnpm run watch").run()?;
Ok(())
}
fn test(sh: &Shell) -> anyhow::Result<()> {
cmd!(sh, "cargo test").run()?;
Ok(())
}
fn lint(sh: &Shell) -> anyhow::Result<()> {
let package_root = get_project_root()?;
sh.change_dir(&package_root);
cmd!(sh, "cargo clippy").run()?;
cmd!(sh, "cargo fmt -- --check").run()?;
sh.change_dir(package_root.join("client"));
cmd!(sh, "pnpm run lint").run()?;
Ok(())
}
fn clean(sh: &Shell) -> anyhow::Result<()> {
let project_root = get_project_root()?;
cmd!(sh, "cargo clean").run()?;
sh.change_dir(project_root.join("client"));
cmd!(sh, "pnpm run clean").run()?;
Ok(())
}
#[derive(Subcommand, Debug)]
enum Action {
/// Build docker image with docker-compose
Build,
/// Run actix server
RunServer,
/// Run vite dev server
RunClient,
/// Run actix server with file watcher (requires cargo-watch)
WatchServer,
/// Run vite client with file watcher (requires pnpm)
WatchClient,
/// Run tests for server and client
Test,
/// Run linters for server and client
Lint,
/// Clean build artifacts
Clean,
}
#[derive(Parser, Debug)]
struct Args {
#[clap(subcommand)]
action: Action,
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
let sh = Shell::new()?;
match args.action {
Action::Build => build(&sh)?,
Action::RunServer => run_server(&sh)?,
Action::RunClient => run_client(&sh)?,
Action::WatchServer => watch_server(&sh)?,
Action::WatchClient => watch_client(&sh)?,
Action::Test => test(&sh)?,
Action::Clean => clean(&sh)?,
Action::Lint => lint(&sh)?,
}
Ok(())
}