Skip to content

Commit 62f2729

Browse files
committed
refactor: cli structure for more clarity
1 parent 33ca14b commit 62f2729

File tree

5 files changed

+207
-124
lines changed

5 files changed

+207
-124
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/build/clean.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,17 @@ pub fn cleanup_after_build(build_state: &BuildState) {
338338
});
339339
}
340340

341-
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>, build_dev_deps: bool) -> Result<()> {
341+
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Result<()> {
342342
let project_root = helpers::get_abs_path(path);
343343
let workspace_root = helpers::get_workspace_root(&project_root);
344344
let packages = packages::make(
345345
&None,
346346
&project_root,
347347
&workspace_root,
348348
show_progress,
349-
// Always clean dev dependencies
350-
build_dev_deps,
349+
// Build the package tree with dev dependencies.
350+
// They should always be cleaned if they are there.
351+
true,
351352
)?;
352353
let root_config_name = packages::read_package_name(&project_root)?;
353354
let bsc_path = match bsc_path {

src/cli.rs

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
/// This prints the compiler arguments. It expects the path to a rescript.json file.
129+
CompilerArgs {
130+
/// Path to a rescript.json file
131+
#[command()]
132+
path: String,
133+
134+
#[arg(long, default_value_t = false, num_args = 0..=1)]
135+
dev: bool,
136+
137+
/// To be used in conjunction with compiler_args
138+
#[arg(long)]
139+
rescript_version: Option<String>,
140+
141+
/// A custom path to bsc
142+
#[arg(long)]
143+
bsc_path: Option<String>,
144+
},
145+
/// Use the legacy build system.
146+
///
147+
/// After this command is encountered, the rest of the arguments are passed to the legacy build system.
148+
#[command(disable_help_flag = true)]
149+
Legacy {
150+
#[arg(allow_hyphen_values = true, num_args = 0..)]
151+
legacy_args: Vec<OsString>,
152+
},
153+
}

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)