Skip to content

Commit d6eda54

Browse files
committed
feat: upstream changes of rescript-lang/rewatch#162
1 parent 085763a commit d6eda54

File tree

8 files changed

+315
-129
lines changed

8 files changed

+315
-129
lines changed

rewatch/src/build.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ 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};
2425
use std::path::{Path, PathBuf};
26+
use std::process::Stdio;
2527
use std::time::{Duration, Instant};
2628

2729
use self::compile::compiler_args;
@@ -551,3 +553,18 @@ pub fn build(
551553
}
552554
}
553555
}
556+
557+
pub fn pass_through_legacy(args: Vec<OsString>) -> i32 {
558+
let project_root = helpers::get_abs_path(Path::new("."));
559+
let workspace_root = helpers::get_workspace_root(&project_root);
560+
561+
let bsb_path = helpers::get_rescript_legacy(&project_root, workspace_root);
562+
563+
let status = std::process::Command::new(bsb_path)
564+
.args(args)
565+
.stdout(Stdio::inherit())
566+
.stderr(Stdio::inherit())
567+
.status();
568+
569+
status.map(|s| s.code().unwrap_or(1)).unwrap_or(1)
570+
}

rewatch/src/build/clean.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ pub fn clean(
335335
path: &Path,
336336
show_progress: bool,
337337
bsc_path: &Option<PathBuf>,
338-
build_dev_deps: bool,
339338
snapshot_output: bool,
340339
) -> Result<()> {
341340
let project_root = helpers::get_abs_path(path);
@@ -345,8 +344,9 @@ pub fn clean(
345344
&project_root,
346345
&workspace_root,
347346
show_progress,
348-
// Always clean dev dependencies
349-
build_dev_deps,
347+
// Build the package tree with dev dependencies.
348+
// They should always be cleaned if they are there.
349+
true,
350350
)?;
351351
let root_config_name = packages::read_package_name(&project_root)?;
352352
let bsc_path = match bsc_path {

rewatch/src/cli.rs

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

rewatch/src/helpers.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,41 @@ pub fn get_bsc(root_path: &Path, workspace_root: &Option<PathBuf>) -> PathBuf {
218218
}
219219
}
220220

221+
pub fn get_rescript_legacy(root_path: &Path, workspace_root: Option<PathBuf>) -> PathBuf {
222+
let subfolder = match (std::env::consts::OS, std::env::consts::ARCH) {
223+
("macos", "aarch64") => "darwin-arm64",
224+
("macos", _) => "darwin-x64",
225+
("linux", "aarch64") => "linux-arm64",
226+
("linux", _) => "linux-x64",
227+
("windows", "aarch64") => "win-arm64",
228+
("windows", _) => "win-x64",
229+
_ => panic!("Unsupported architecture"),
230+
};
231+
232+
let legacy_path_fragment = Path::new("node_modules")
233+
.join("@rescript")
234+
.join(subfolder)
235+
.join("bin")
236+
.join("rescript-legacy");
237+
238+
match (
239+
root_path
240+
.join(&legacy_path_fragment)
241+
.canonicalize()
242+
.map(StrippedVerbatimPath::to_stripped_verbatim_path),
243+
workspace_root.map(|workspace_root| {
244+
workspace_root
245+
.join(&legacy_path_fragment)
246+
.canonicalize()
247+
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
248+
}),
249+
) {
250+
(Ok(path), _) => path,
251+
(_, Some(Ok(path))) => path,
252+
_ => panic!("Could not find rescript-legacy"),
253+
}
254+
}
255+
221256
pub fn string_ends_with_any(s: &Path, suffixes: &[&str]) -> bool {
222257
suffixes
223258
.iter()

rewatch/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)