Skip to content

Commit 6c3607f

Browse files
committed
refactor: cli structure for more clarity
1 parent 33ca14b commit 6c3607f

File tree

4 files changed

+208
-121
lines changed

4 files changed

+208
-121
lines changed

src/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use console::style;
1818
use indicatif::{ProgressBar, ProgressStyle};
1919
use log::log_enabled;
2020
use serde::Serialize;
21+
use std::ffi::OsString;
2122
use std::fmt;
2223
use std::fs::File;
2324
use std::io::{stdout, Write};
@@ -508,7 +509,7 @@ pub fn build(
508509
}
509510
}
510511

511-
pub fn pass_through_legacy(args: Vec<String>) -> i32 {
512+
pub fn pass_through_legacy(args: Vec<OsString>) -> i32 {
512513
let project_root = helpers::get_abs_path(".");
513514
let workspace_root = helpers::get_workspace_root(&project_root);
514515

src/cli.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
use std::ffi::OsString;
2+
3+
use clap::{Args, Parser, Subcommand};
4+
use clap_verbosity_flag::InfoLevel;
5+
6+
/// Rewatch is an alternative build system for the Rescript Compiler bsb (which uses Ninja internally). It strives
7+
/// to deliver consistent and faster builds in monorepo setups with multiple packages, where the
8+
/// default build system fails to pick up changed interfaces across multiple packages.
9+
#[derive(Parser, Debug)]
10+
#[command(version)]
11+
pub struct Cli {
12+
/// Verbosity:
13+
/// -v -> Debug
14+
/// -vv -> Trace
15+
/// -q -> Warn
16+
/// -qq -> Error
17+
/// -qqq -> Off.
18+
/// Default (/ no argument given): 'info'
19+
#[command(flatten)]
20+
pub verbose: clap_verbosity_flag::Verbosity<InfoLevel>,
21+
22+
/// The command to run. If not provided it will default to build.
23+
#[command(subcommand)]
24+
pub command: Option<Command>,
25+
26+
/// The relative path to where the main rescript.json resides. IE - the root of your project.
27+
#[arg(default_value = ".")]
28+
pub folder: String,
29+
30+
#[command(flatten)]
31+
pub build_args: BuildArgs,
32+
}
33+
34+
#[derive(Args, Debug)]
35+
pub struct BuildArgs {
36+
/// Filter files by regex
37+
///
38+
/// Filter allows for a regex to be supplied which will filter the files to be compiled. For
39+
/// instance, to filter out test files for compilation while doing feature work.
40+
#[arg(short, long)]
41+
pub filter: Option<String>,
42+
43+
/// Action after build
44+
///
45+
/// This allows one to pass an additional command to the watcher, which allows it to run when
46+
/// finished. For instance, to play a sound when done compiling, or to run a test suite.
47+
/// NOTE - You may need to add '--color=always' to your subcommand in case you want to output
48+
/// colour as well
49+
#[arg(short, long)]
50+
pub after_build: Option<String>,
51+
52+
/// Create source_dirs.json
53+
///
54+
/// This creates a source_dirs.json file at the root of the monorepo, which is needed when you
55+
/// want to use Reanalyze
56+
#[arg(short, long, default_value_t = false, num_args = 0..=1)]
57+
pub create_sourcedirs: bool,
58+
59+
/// Build development dependencies
60+
///
61+
/// This is the flag to also compile development dependencies
62+
/// It's important to know that we currently do not discern between project src, and
63+
/// dependencies. So enabling this flag will enable building _all_ development dependencies of
64+
/// _all_ packages
65+
#[arg(long, default_value_t = false, num_args = 0..=1)]
66+
pub dev: bool,
67+
68+
/// Disable timing on the output
69+
#[arg(short, long, default_value = "false", num_args = 0..=1)]
70+
pub no_timing: bool,
71+
72+
/// Path to bsc
73+
#[arg(long)]
74+
pub bsc_path: Option<String>,
75+
}
76+
77+
#[derive(Args, Debug)]
78+
pub struct WatchArgs {
79+
/// Filter files by regex
80+
///
81+
/// Filter allows for a regex to be supplied which will filter the files to be compiled. For
82+
/// instance, to filter out test files for compilation while doing feature work.
83+
#[arg(short, long)]
84+
pub filter: Option<String>,
85+
86+
/// Action after build
87+
///
88+
/// This allows one to pass an additional command to the watcher, which allows it to run when
89+
/// finished. For instance, to play a sound when done compiling, or to run a test suite.
90+
/// NOTE - You may need to add '--color=always' to your subcommand in case you want to output
91+
/// colour as well
92+
#[arg(short, long)]
93+
pub after_build: Option<String>,
94+
95+
/// Create source_dirs.json
96+
///
97+
/// This creates a source_dirs.json file at the root of the monorepo, which is needed when you
98+
/// want to use Reanalyze
99+
#[arg(short, long, default_value_t = false, num_args = 0..=1)]
100+
pub create_sourcedirs: bool,
101+
102+
/// Build development dependencies
103+
///
104+
/// This is the flag to also compile development dependencies
105+
/// It's important to know that we currently do not discern between project src, and
106+
/// dependencies. So enabling this flag will enable building _all_ development dependencies of
107+
/// _all_ packages
108+
#[arg(long, default_value_t = false, num_args = 0..=1)]
109+
pub dev: bool,
110+
111+
/// Path to bsc
112+
#[arg(long)]
113+
pub bsc_path: Option<String>,
114+
}
115+
116+
#[derive(Subcommand, Debug)]
117+
pub enum Command {
118+
/// Build using Rewatch
119+
Build(BuildArgs),
120+
/// Build, then start a watcher
121+
Watch(WatchArgs),
122+
/// Clean the build artifacts
123+
Clean {
124+
/// Path to bsc
125+
#[arg(long)]
126+
bsc_path: Option<String>,
127+
128+
#[arg(long, default_value_t = false, num_args = 0..=1)]
129+
dev: bool,
130+
},
131+
/// This prints the compiler arguments. It expects the path to a rescript.json file.
132+
CompilerArgs {
133+
/// Path to a rescript.json file
134+
#[command()]
135+
path: String,
136+
137+
#[arg(long, default_value_t = false, num_args = 0..=1)]
138+
dev: bool,
139+
140+
/// To be used in conjunction with compiler_args
141+
#[arg(long)]
142+
rescript_version: Option<String>,
143+
144+
/// A custom path to bsc
145+
#[arg(long)]
146+
bsc_path: Option<String>,
147+
},
148+
/// Use the legacy build system.
149+
///
150+
/// After this command is encountered, the rest of the arguments are passed to the legacy build system.
151+
#[command(disable_help_flag = true)]
152+
Legacy {
153+
#[arg(allow_hyphen_values = true, num_args = 0..)]
154+
legacy_args: Vec<OsString>,
155+
},
156+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod build;
2+
pub mod cli;
23
pub mod cmd;
34
pub mod config;
45
pub mod helpers;

0 commit comments

Comments
 (0)