forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
120 lines (105 loc) · 3.87 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use clap::{Parser, ValueEnum};
use regex::Regex;
use rewatch::{build, cmd, lock, watcher};
#[derive(Debug, Clone, ValueEnum)]
enum Command {
/// Build using Rewatch
Build,
/// Build, then start a watcher
Watch,
/// Clean the build artifacts
Clean,
}
/// Rewatch is an alternative build system for the Rescript Compiler bsb (which uses Ninja internally). It strives
/// to deliver consistent and faster builds in monorepo setups with multiple packages, where the
/// default build system fails to pick up changed interfaces across multiple packages.
#[derive(Parser, Debug)]
#[command(version)]
struct Args {
#[arg(value_enum)]
command: Option<Command>,
/// The relative path to where the main bsconfig.json resides. IE - the root of your project.
folder: Option<String>,
/// Filter allows for a regex to be supplied which will filter the files to be compiled. For
/// instance, to filter out test files for compilation while doing feature work.
#[arg(short, long)]
filter: Option<String>,
/// This allows one to pass an additional command to the watcher, which allows it to run when
/// finished. For instance, to play a sound when done compiling, or to run a test suite.
/// NOTE - You may need to add '--color=always' to your subcommand in case you want to output
/// colour as well
#[arg(short, long)]
after_build: Option<String>,
#[arg(short, long)]
no_timing: Option<bool>,
/// This creates a source_dirs.json file at the root of the monorepo, which is needed when you
/// want to use Reanalyze
#[arg(short, long)]
create_sourcedirs: Option<bool>,
/// This prints the compiler arguments. It expects the path to a rescript.json file.
/// This also requires --bsc-path and --rescript-version to be present
#[arg(long)]
compiler_args: Option<String>,
/// To be used in conjunction with compiler_args
#[arg(long)]
rescript_version: Option<String>,
/// A custom path to bsc
#[arg(long)]
bsc_path: Option<String>,
}
fn main() {
env_logger::init();
let args = Args::parse();
let command = args.command.unwrap_or(Command::Build);
let folder = args.folder.unwrap_or(".".to_string());
let filter = args
.filter
.map(|filter| Regex::new(filter.as_ref()).expect("Could not parse regex"));
match args.compiler_args {
None => (),
Some(path) => {
println!(
"{}",
build::get_compiler_args(&path, args.rescript_version, args.bsc_path)
);
std::process::exit(0);
}
}
match lock::get(&folder) {
lock::Lock::Error(ref e) => {
eprintln!("Error while trying to get lock: {e}");
std::process::exit(1)
}
lock::Lock::Aquired(_) => match command {
Command::Clean => build::clean::clean(&folder, args.bsc_path),
Command::Build => {
match build::build(
&filter,
&folder,
args.no_timing.unwrap_or(false),
args.create_sourcedirs.unwrap_or(false),
args.bsc_path,
) {
Err(e) => {
eprintln!("Error Building: {e}");
std::process::exit(1)
}
Ok(_) => {
if let Some(args_after_build) = args.after_build {
cmd::run(args_after_build)
}
std::process::exit(0)
}
};
}
Command::Watch => {
watcher::start(
&filter,
&folder,
args.after_build,
args.create_sourcedirs.unwrap_or(false),
);
}
},
}
}