Skip to content

Commit 33ca14b

Browse files
committed
feat: call rewatch-legacy
1 parent 38e04d7 commit 33ca14b

File tree

3 files changed

+66
-10
lines changed

3 files changed

+66
-10
lines changed

src/build.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::fmt;
2222
use std::fs::File;
2323
use std::io::{stdout, Write};
2424
use std::path::PathBuf;
25+
use std::process::Stdio;
2526
use std::time::{Duration, Instant};
2627

2728
use self::compile::compiler_args;
@@ -506,3 +507,18 @@ pub fn build(
506507
}
507508
}
508509
}
510+
511+
pub fn pass_through_legacy(args: Vec<String>) -> i32 {
512+
let project_root = helpers::get_abs_path(".");
513+
let workspace_root = helpers::get_workspace_root(&project_root);
514+
515+
let bsb_path = helpers::get_rescript_legacy(&project_root, workspace_root);
516+
517+
let status = std::process::Command::new(bsb_path)
518+
.args(args)
519+
.stdout(Stdio::inherit())
520+
.stderr(Stdio::inherit())
521+
.status();
522+
523+
status.map(|s| s.code().unwrap_or(1)).unwrap_or(1)
524+
}

src/helpers.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ pub fn create_path_for_path(path: &Path) {
150150
fs::DirBuilder::new().recursive(true).create(path).unwrap();
151151
}
152152

153+
pub fn get_rescript_legacy(root_path: &str, workspace_root: Option<String>) -> String {
154+
match (
155+
PathBuf::from(format!("{}/node_modules/rescript/rescript-legacy", root_path)).canonicalize(),
156+
workspace_root.map(|workspace_root| {
157+
PathBuf::from(format!(
158+
"{}/node_modules/rescript/rescript-legacy",
159+
workspace_root
160+
))
161+
.canonicalize()
162+
}),
163+
) {
164+
(Ok(path), _) => path,
165+
(_, Some(Ok(path))) => path,
166+
_ => panic!("Could not find rescript"),
167+
}
168+
.to_string_lossy()
169+
.to_string()
170+
}
171+
153172
pub fn get_bsc(root_path: &str, workspace_root: Option<String>) -> String {
154173
let subfolder = match (std::env::consts::OS, std::env::consts::ARCH) {
155174
("macos", "aarch64") => "darwinarm64",

src/main.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ enum Command {
1515
Watch,
1616
/// Clean the build artifacts
1717
Clean,
18+
/// Format the code
19+
Format,
20+
/// Dump
21+
Dump,
1822
}
1923

2024
/// Rewatch is an alternative build system for the Rescript Compiler bsb (which uses Ninja internally). It strives
@@ -23,11 +27,12 @@ enum Command {
2327
#[derive(Parser, Debug)]
2428
#[command(version)]
2529
struct Args {
26-
#[arg(value_enum)]
27-
command: Option<Command>,
30+
#[arg(value_enum, default_value_t = Command::Build)]
31+
command: Command,
2832

2933
/// The relative path to where the main rescript.json resides. IE - the root of your project.
30-
folder: Option<String>,
34+
#[arg(default_value = ".")]
35+
folder: String,
3136

3237
/// Filter allows for a regex to be supplied which will filter the files to be compiled. For
3338
/// instance, to filter out test files for compilation while doing feature work.
@@ -79,10 +84,22 @@ struct Args {
7984
/// A custom path to bsc
8085
#[arg(long)]
8186
bsc_path: Option<String>,
87+
88+
/// Use the legacy build system.
89+
///
90+
/// After this flag is encountered, the rest of the command line arguments are passed to the legacy build system.
91+
#[arg(long, allow_hyphen_values = true, num_args = 0..)]
92+
legacy: Option<Vec<String>>,
8293
}
8394

8495
fn main() -> Result<()> {
8596
let args = Args::parse();
97+
98+
if let Some(legacy_args) = args.legacy {
99+
let code = build::pass_through_legacy(legacy_args);
100+
std::process::exit(code);
101+
}
102+
86103
let log_level_filter = args.verbose.log_level_filter();
87104

88105
env_logger::Builder::new()
@@ -91,8 +108,6 @@ fn main() -> Result<()> {
91108
.target(env_logger::fmt::Target::Stdout)
92109
.init();
93110

94-
let command = args.command.unwrap_or(Command::Build);
95-
let folder = args.folder.unwrap_or(".".to_string());
96111
let filter = args
97112
.filter
98113
.map(|filter| Regex::new(filter.as_ref()).expect("Could not parse regex"));
@@ -112,17 +127,17 @@ fn main() -> Result<()> {
112127
// level, we should never show that.
113128
let show_progress = log_level_filter == LevelFilter::Info;
114129

115-
match lock::get(&folder) {
130+
match lock::get(&args.folder) {
116131
lock::Lock::Error(ref e) => {
117132
println!("Could not start Rewatch: {e}");
118133
std::process::exit(1)
119134
}
120-
lock::Lock::Aquired(_) => match command {
121-
Command::Clean => build::clean::clean(&folder, show_progress, args.bsc_path, args.dev),
135+
lock::Lock::Aquired(_) => match args.command {
136+
Command::Clean => build::clean::clean(&args.folder, show_progress, args.bsc_path, args.dev),
122137
Command::Build => {
123138
match build::build(
124139
&filter,
125-
&folder,
140+
&args.folder,
126141
show_progress,
127142
args.no_timing,
128143
args.create_sourcedirs,
@@ -145,7 +160,7 @@ fn main() -> Result<()> {
145160
watcher::start(
146161
&filter,
147162
show_progress,
148-
&folder,
163+
&args.folder,
149164
args.after_build,
150165
args.create_sourcedirs,
151166
args.dev,
@@ -154,6 +169,12 @@ fn main() -> Result<()> {
154169

155170
Ok(())
156171
}
172+
Command::Format => {
173+
todo!("Format not implemented yet");
174+
}
175+
Command::Dump => {
176+
todo!("Dump not implemented yet");
177+
}
157178
},
158179
}
159180
}

0 commit comments

Comments
 (0)