Skip to content

Commit 4734f76

Browse files
committed
Rewatch: simplify getting bsc path
1 parent 7c7a004 commit 4734f76

File tree

11 files changed

+41
-165
lines changed

11 files changed

+41
-165
lines changed

cli/rescript.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,14 @@
33
// @ts-check
44

55
import * as child_process from "node:child_process";
6-
import { bsc_exe, rescript_exe } from "./common/bins.js";
6+
import { rescript_exe } from "./common/bins.js";
77

88
const args = process.argv.slice(2);
99

10-
const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-"));
11-
1210
try {
13-
if (firstPositionalArgIndex !== -1) {
14-
const subcommand = args[firstPositionalArgIndex];
15-
const subcommandWithArgs = args.slice(firstPositionalArgIndex);
16-
17-
if (
18-
subcommand === "build" ||
19-
subcommand === "watch" ||
20-
subcommand === "clean"
21-
) {
22-
child_process.execFileSync(
23-
rescript_exe,
24-
[...subcommandWithArgs, "--bsc-path", bsc_exe],
25-
{
26-
stdio: "inherit",
27-
},
28-
);
29-
} else {
30-
child_process.execFileSync(rescript_exe, [...args], {
31-
stdio: "inherit",
32-
});
33-
}
34-
} else {
35-
// no subcommand means build subcommand
36-
child_process.execFileSync(rescript_exe, [...args, "--bsc-path", bsc_exe], {
37-
stdio: "inherit",
38-
});
39-
}
11+
child_process.execFileSync(rescript_exe, args, {
12+
stdio: "inherit",
13+
});
4014
} catch (err) {
4115
if (err.status !== undefined) {
4216
process.exit(err.status); // Pass through the exit code

rewatch/src/build.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ pub struct CompilerArgs {
5656
pub parser_args: Vec<String>,
5757
}
5858

59-
pub fn get_compiler_args(
60-
path: &Path,
61-
build_dev_deps: bool,
62-
) -> Result<String> {
59+
pub fn get_compiler_args(path: &Path, build_dev_deps: bool) -> Result<String> {
6360
let filename = &helpers::get_abs_path(path);
6461
let package_root =
6562
helpers::get_abs_path(&helpers::get_nearest_config(&path).expect("Couldn't find package root"));
@@ -117,16 +114,12 @@ pub fn initialize_build(
117114
filter: &Option<regex::Regex>,
118115
show_progress: bool,
119116
path: &Path,
120-
bsc_path: &Option<PathBuf>,
121117
build_dev_deps: bool,
122118
snapshot_output: bool,
123119
) -> Result<BuildState> {
124120
let project_root = helpers::get_abs_path(path);
125121
let workspace_root = helpers::get_workspace_root(&project_root);
126-
let bsc_path = match bsc_path {
127-
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
128-
None => helpers::get_bsc(&project_root, &workspace_root),
129-
};
122+
let bsc_path = helpers::get_bsc();
130123
let root_config_name = packages::read_package_name(&project_root)?;
131124

132125
if !snapshot_output && show_progress {
@@ -171,13 +164,7 @@ pub fn initialize_build(
171164
let _ = stdout().flush();
172165
}
173166

174-
let mut build_state = BuildState::new(
175-
project_root,
176-
root_config_name,
177-
packages,
178-
workspace_root,
179-
bsc_path,
180-
);
167+
let mut build_state = BuildState::new(project_root, root_config_name, packages, workspace_root, bsc_path);
181168
packages::parse_packages(&mut build_state);
182169
let timing_source_files_elapsed = timing_source_files.elapsed();
183170

@@ -487,7 +474,6 @@ pub fn build(
487474
show_progress: bool,
488475
no_timing: bool,
489476
create_sourcedirs: bool,
490-
bsc_path: Option<PathBuf>,
491477
build_dev_deps: bool,
492478
snapshot_output: bool,
493479
) -> Result<BuildState> {
@@ -502,7 +488,6 @@ pub fn build(
502488
filter,
503489
show_progress,
504490
path,
505-
&bsc_path,
506491
build_dev_deps,
507492
snapshot_output,
508493
)

rewatch/src/build/clean.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
331331
});
332332
}
333333

334-
pub fn clean(
335-
path: &Path,
336-
show_progress: bool,
337-
bsc_path: Option<PathBuf>,
338-
snapshot_output: bool,
339-
) -> Result<()> {
334+
pub fn clean(path: &Path, show_progress: bool, snapshot_output: bool) -> Result<()> {
340335
let project_root = helpers::get_abs_path(path);
341336
let workspace_root = helpers::get_workspace_root(&project_root);
342337
let packages = packages::make(
@@ -349,11 +344,7 @@ pub fn clean(
349344
true,
350345
)?;
351346
let root_config_name = packages::read_package_name(&project_root)?;
352-
let bsc_path = match bsc_path {
353-
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
354-
None => helpers::get_bsc(&project_root, &workspace_root),
355-
};
356-
347+
let bsc_path = helpers::get_bsc();
357348

358349
let timing_clean_compiler_assets = Instant::now();
359350
if !snapshot_output && show_progress {

rewatch/src/cli.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ pub struct DevArg {
7777
pub dev: bool,
7878
}
7979

80-
#[derive(Args, Debug, Clone)]
81-
pub struct BscPathArg {
82-
/// Custom path to bsc
83-
#[arg(long)]
84-
pub bsc_path: Option<String>,
85-
}
86-
8780
#[derive(Args, Debug, Clone, Copy)]
8881
pub struct SnapshotOutputArg {
8982
/// simple output for snapshot testing
@@ -114,9 +107,6 @@ pub struct BuildArgs {
114107

115108
#[command(flatten)]
116109
pub snapshot_output: SnapshotOutputArg,
117-
118-
#[command(flatten)]
119-
pub bsc_path: BscPathArg,
120110
}
121111

122112
#[derive(Args, Clone, Debug)]
@@ -138,9 +128,6 @@ pub struct WatchArgs {
138128

139129
#[command(flatten)]
140130
pub snapshot_output: SnapshotOutputArg,
141-
142-
#[command(flatten)]
143-
pub bsc_path: BscPathArg,
144131
}
145132

146133
#[derive(Subcommand, Clone, Debug)]
@@ -154,9 +141,6 @@ pub enum Command {
154141
#[command(flatten)]
155142
folder: FolderArg,
156143

157-
#[command(flatten)]
158-
bsc_path: BscPathArg,
159-
160144
#[command(flatten)]
161145
snapshot_output: SnapshotOutputArg,
162146
},
@@ -231,14 +215,6 @@ impl Deref for DevArg {
231215
}
232216
}
233217

234-
impl Deref for BscPathArg {
235-
type Target = Option<String>;
236-
237-
fn deref(&self) -> &Self::Target {
238-
&self.bsc_path
239-
}
240-
}
241-
242218
impl Deref for SnapshotOutputArg {
243219
type Target = bool;
244220

rewatch/src/helpers.rs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -180,43 +180,18 @@ pub fn create_path_for_path(path: &Path) {
180180
fs::DirBuilder::new().recursive(true).create(path).unwrap();
181181
}
182182

183-
fn get_bin_dir() -> PathBuf {
184-
let subfolder = match (std::env::consts::OS, std::env::consts::ARCH) {
185-
("macos", "aarch64") => "darwin-arm64",
186-
("macos", _) => "darwin-x64",
187-
("linux", "aarch64") => "linux-arm64",
188-
("linux", _) => "linux-x64",
189-
("windows", "aarch64") => "win-arm64",
190-
("windows", _) => "win32-x64",
191-
_ => panic!("Unsupported architecture"),
192-
};
193-
194-
Path::new("node_modules")
195-
.join("@rescript")
196-
.join(subfolder)
197-
.join("bin")
183+
pub fn get_rescript_bin_dir() -> PathBuf {
184+
let current_exe_path = std::env::current_exe().expect("Could not get current executable path");
185+
current_exe_path
186+
.parent()
187+
.expect("Could not get parent directory of current executable")
188+
.to_path_buf()
198189
}
199190

200-
pub fn get_bsc(root_path: &Path, workspace_root: &Option<PathBuf>) -> PathBuf {
201-
let bin_dir = get_bin_dir();
202-
203-
match (
204-
root_path
205-
.join(&bin_dir)
206-
.join("bsc.exe")
207-
.canonicalize()
208-
.map(StrippedVerbatimPath::to_stripped_verbatim_path),
209-
workspace_root.as_ref().map(|workspace_root| {
210-
workspace_root
211-
.join(&bin_dir)
212-
.join("bsc.exe")
213-
.canonicalize()
214-
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
215-
}),
216-
) {
217-
(Ok(path), _) => path,
218-
(_, Some(Ok(path))) => path,
219-
_ => panic!("Could not find bsc.exe"),
191+
pub fn get_bsc() -> PathBuf {
192+
match std::env::var("RESCRIPT_BSC_EXE") {
193+
Ok(val) => PathBuf::from(val),
194+
Err(_) => get_rescript_bin_dir().join("bsc.exe"),
220195
}
221196
}
222197

rewatch/src/lock.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ pub enum Error {
2121
impl std::fmt::Display for Error {
2222
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
2323
let msg = match self {
24-
Error::Locked(pid) => format!("A ReScript build is already running. The process ID (PID) is {}", pid),
24+
Error::Locked(pid) => format!(
25+
"A ReScript build is already running. The process ID (PID) is {}",
26+
pid
27+
),
2528
Error::ParsingLockfile(e) => format!(
2629
"Could not parse lockfile: \n {} \n (try removing it and running the command again)",
2730
e

rewatch/src/main.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use anyhow::Result;
22
use clap::Parser;
33
use log::LevelFilter;
44
use regex::Regex;
5-
use std::{
6-
io::Write,
7-
path::{Path, PathBuf},
8-
};
5+
use std::{io::Write, path::Path};
96

107
use rewatch::{build, cli, cmd, lock, watcher};
118

@@ -27,17 +24,8 @@ fn main() -> Result<()> {
2724
let show_progress = log_level_filter == LevelFilter::Info;
2825

2926
match command.clone() {
30-
cli::Command::CompilerArgs {
31-
path,
32-
dev,
33-
} => {
34-
println!(
35-
"{}",
36-
build::get_compiler_args(
37-
Path::new(&path),
38-
*dev
39-
)?
40-
);
27+
cli::Command::CompilerArgs { path, dev } => {
28+
println!("{}", build::get_compiler_args(Path::new(&path), *dev)?);
4129
std::process::exit(0);
4230
}
4331
cli::Command::Build(build_args) => {
@@ -54,7 +42,6 @@ fn main() -> Result<()> {
5442
show_progress,
5543
build_args.no_timing,
5644
*build_args.create_sourcedirs,
57-
build_args.bsc_path.as_ref().map(PathBuf::from),
5845
*build_args.dev,
5946
*build_args.snapshot_output,
6047
) {
@@ -84,25 +71,18 @@ fn main() -> Result<()> {
8471
(*watch_args.after_build).clone(),
8572
*watch_args.create_sourcedirs,
8673
*watch_args.dev,
87-
(*watch_args.bsc_path).clone(),
8874
*watch_args.snapshot_output,
8975
);
9076

9177
Ok(())
9278
}
9379
cli::Command::Clean {
9480
folder,
95-
bsc_path,
9681
snapshot_output,
9782
} => {
9883
let _lock = get_lock(&folder);
9984

100-
build::clean::clean(
101-
Path::new(&folder as &str),
102-
show_progress,
103-
bsc_path.as_ref().map(PathBuf::from),
104-
*snapshot_output,
105-
)
85+
build::clean::clean(Path::new(&folder as &str), show_progress, *snapshot_output)
10686
}
10787
cli::Command::Legacy { legacy_args } => {
10888
let code = build::pass_through_legacy(legacy_args);

rewatch/src/watcher.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use crate::cmd;
55
use crate::helpers;
66
use crate::helpers::StrippedVerbatimPath;
77
use crate::helpers::emojis::*;
8+
use crate::lock::LOCKFILE;
89
use crate::queue::FifoQueue;
910
use crate::queue::*;
1011
use futures_timer::Delay;
1112
use notify::event::ModifyKind;
1213
use notify::{Config, Error, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
13-
use crate::lock::LOCKFILE;
14-
use std::path::{Path, PathBuf};
14+
use std::path::Path;
1515
use std::sync::Arc;
1616
use std::sync::Mutex;
1717
use std::time::{Duration, Instant};
@@ -63,19 +63,11 @@ async fn async_watch(
6363
after_build: Option<String>,
6464
create_sourcedirs: bool,
6565
build_dev_deps: bool,
66-
bsc_path: Option<PathBuf>,
6766
snapshot_output: bool,
6867
) -> notify::Result<()> {
69-
let mut build_state = build::initialize_build(
70-
None,
71-
filter,
72-
show_progress,
73-
path,
74-
&bsc_path,
75-
build_dev_deps,
76-
snapshot_output,
77-
)
78-
.expect("Can't initialize build");
68+
let mut build_state =
69+
build::initialize_build(None, filter, show_progress, path, build_dev_deps, snapshot_output)
70+
.expect("Can't initialize build");
7971
let mut needs_compile_type = CompileType::Incremental;
8072
// create a mutex to capture if ctrl-c was pressed
8173
let ctrlc_pressed = Arc::new(Mutex::new(false));
@@ -259,7 +251,6 @@ async fn async_watch(
259251
filter,
260252
show_progress,
261253
path,
262-
&bsc_path,
263254
build_dev_deps,
264255
snapshot_output,
265256
)
@@ -308,7 +299,6 @@ pub fn start(
308299
after_build: Option<String>,
309300
create_sourcedirs: bool,
310301
build_dev_deps: bool,
311-
bsc_path: Option<String>,
312302
snapshot_output: bool,
313303
) {
314304
futures::executor::block_on(async {
@@ -323,7 +313,6 @@ pub fn start(
323313
.expect("Could not start watcher");
324314

325315
let path = Path::new(folder);
326-
let bsc_path_buf = bsc_path.map(PathBuf::from);
327316

328317
if let Err(e) = async_watch(
329318
consumer,
@@ -333,7 +322,6 @@ pub fn start(
333322
after_build,
334323
create_sourcedirs,
335324
build_dev_deps,
336-
bsc_path_buf,
337325
snapshot_output,
338326
)
339327
.await

0 commit comments

Comments
 (0)