-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.rs
More file actions
159 lines (139 loc) · 5.27 KB
/
main.rs
File metadata and controls
159 lines (139 loc) · 5.27 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use anyhow::Result;
use clap::{Parser, ValueEnum};
use clap_verbosity_flag::InfoLevel;
use log::LevelFilter;
use regex::Regex;
use std::io::Write;
use rewatch::{build, cmd, lock, watcher};
#[derive(Debug, Clone, ValueEnum)]
enum Command {
//github.com/ Build using Rewatch
Build,
//github.com/ Build, then start a watcher
Watch,
//github.com/ Clean the build artifacts
Clean,
}
//github.com/ Rewatch is an alternative build system for the Rescript Compiler bsb (which uses Ninja internally). It strives
//github.com/ to deliver consistent and faster builds in monorepo setups with multiple packages, where the
//github.com/ 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>,
//github.com/ The relative path to where the main rescript.json resides. IE - the root of your project.
folder: Option<String>,
//github.com/ Filter allows for a regex to be supplied which will filter the files to be compiled. For
//github.com/ instance, to filter out test files for compilation while doing feature work.
#[arg(short, long)]
filter: Option<String>,
//github.com/ This allows one to pass an additional command to the watcher, which allows it to run when
//github.com/ finished. For instance, to play a sound when done compiling, or to run a test suite.
//github.com/ NOTE - You may need to add '--color=always' to your subcommand in case you want to output
//github.com/ colour as well
#[arg(short, long)]
after_build: Option<String>,
// Disable timing on the output
#[arg(short, long, default_value = "false", num_args = 0..=1)]
no_timing: bool,
//github.com/ Verbosity:
//github.com/ -v -> Debug
//github.com/ -vv -> Trace
//github.com/ -q -> Warn
//github.com/ -qq -> Error
//github.com/ -qqq -> Off.
//github.com/ Default (/ no argument given): 'info'
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity<InfoLevel>,
//github.com/ This creates a source_dirs.json file at the root of the monorepo, which is needed when you
//github.com/ want to use Reanalyze
#[arg(short, long, default_value_t = false, num_args = 0..=1)]
create_sourcedirs: bool,
//github.com/ This prints the compiler arguments. It expects the path to a rescript.json file.
//github.com/ This also requires --bsc-path and --rescript-version to be present
#[arg(long)]
compiler_args: Option<String>,
//github.com/ This is the flag to also compile development dependencies
//github.com/ It's important to know that we currently do not discern between project src, and
//github.com/ dependencies. So enabling this flag will enable building _all_ development dependencies of
//github.com/ _all_ packages
#[arg(long, default_value_t = false, num_args = 0..=1)]
dev: bool,
//github.com/ To be used in conjunction with compiler_args
#[arg(long)]
rescript_version: Option<String>,
//github.com/ A custom path to bsc
#[arg(long)]
bsc_path: Option<String>,
}
fn main() -> Result<()> {
let args = Args::parse();
let log_level_filter = args.verbose.log_level_filter();
env_logger::Builder::new()
.format(|buf, record| writeln!(buf, "{}:\n{}", record.level(), record.args()))
.filter_level(log_level_filter)
.target(env_logger::fmt::Target::Stdout)
.init();
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, args.dev)?
);
std::process::exit(0);
}
}
// The 'normal run' mode will show the 'pretty' formatted progress. But if we turn off the log
// level, we should never show that.
let show_progress = log_level_filter == LevelFilter::Info;
match lock::get(&folder) {
lock::Lock::Error(ref e) => {
println!("Could not start Rewatch: {e}");
std::process::exit(1)
}
lock::Lock::Aquired(_) => match command {
Command::Clean => build::clean::clean(&folder, show_progress, args.bsc_path, args.dev),
Command::Build => {
match build::build(
&filter,
&folder,
show_progress,
args.no_timing,
args.create_sourcedirs,
args.bsc_path,
args.dev,
) {
Err(e) => {
println!("{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,
show_progress,
&folder,
args.after_build,
args.create_sourcedirs,
args.dev,
args.bsc_path,
);
Ok(())
}
},
}
}