11use anyhow:: Result ;
2- use clap:: { Parser , ValueEnum } ;
2+ use clap:: { arg , Arg , ArgAction , Args , FromArgMatches , Parser , ValueEnum } ;
33use clap_verbosity_flag:: InfoLevel ;
44use log:: LevelFilter ;
55use regex:: Regex ;
@@ -15,19 +15,24 @@ 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
2125/// to deliver consistent and faster builds in monorepo setups with multiple packages, where the
2226/// default build system fails to pick up changed interfaces across multiple packages.
2327#[ derive( Parser , Debug ) ]
2428#[ command( version) ]
25- struct Args {
26- #[ arg( value_enum) ]
27- command : Option < Command > ,
29+ struct DerivedArgs {
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.
@@ -82,7 +87,33 @@ struct Args {
8287}
8388
8489fn main ( ) -> Result < ( ) > {
85- let args = Args :: parse ( ) ;
90+ let legacy = clap:: Command :: new ( "rewatch" )
91+ . disable_help_flag ( true )
92+ . disable_version_flag ( true )
93+ . arg (
94+ arg ! ( -l - -legacy ... "Use the legacy build system" )
95+ . global ( true )
96+ . action ( ArgAction :: SetTrue ) ,
97+ )
98+ . defer ( |cmd| {
99+ cmd. arg ( arg ! ( [ cmd] ... "commands and args for the legacy build system" ) . trailing_var_arg ( true ) )
100+ } ) ;
101+
102+ let legacy_matches = legacy. get_matches ( ) ;
103+
104+ if legacy_matches. get_flag ( "legacy" ) {
105+ let s = std:: env:: args ( ) . collect :: < Vec < String > > ( ) . join ( " " ) ;
106+
107+ println ! ( "Using legacy build system" ) ;
108+ println ! ( "Running: {s}" ) ;
109+ std:: process:: exit ( 0 ) ;
110+ }
111+
112+ let matches = DerivedArgs :: augment_args ( clap:: Command :: new ( "rewatch" ) ) . get_matches ( ) ;
113+ let args = DerivedArgs :: from_arg_matches ( & matches)
114+ . map_err ( |err| err. exit ( ) )
115+ . unwrap ( ) ;
116+
86117 let log_level_filter = args. verbose . log_level_filter ( ) ;
87118
88119 env_logger:: Builder :: new ( )
@@ -91,8 +122,6 @@ fn main() -> Result<()> {
91122 . target ( env_logger:: fmt:: Target :: Stdout )
92123 . init ( ) ;
93124
94- let command = args. command . unwrap_or ( Command :: Build ) ;
95- let folder = args. folder . unwrap_or ( "." . to_string ( ) ) ;
96125 let filter = args
97126 . filter
98127 . map ( |filter| Regex :: new ( filter. as_ref ( ) ) . expect ( "Could not parse regex" ) ) ;
@@ -112,17 +141,17 @@ fn main() -> Result<()> {
112141 // level, we should never show that.
113142 let show_progress = log_level_filter == LevelFilter :: Info ;
114143
115- match lock:: get ( & folder) {
144+ match lock:: get ( & args . folder ) {
116145 lock:: Lock :: Error ( ref e) => {
117146 println ! ( "Could not start Rewatch: {e}" ) ;
118147 std:: process:: exit ( 1 )
119148 }
120- lock:: Lock :: Aquired ( _) => match command {
121- Command :: Clean => build:: clean:: clean ( & folder, show_progress, args. bsc_path ) ,
149+ lock:: Lock :: Aquired ( _) => match args . command {
150+ Command :: Clean => build:: clean:: clean ( & args . folder , show_progress, args. bsc_path ) ,
122151 Command :: Build => {
123152 match build:: build (
124153 & filter,
125- & folder,
154+ & args . folder ,
126155 show_progress,
127156 args. no_timing ,
128157 args. create_sourcedirs ,
@@ -145,14 +174,20 @@ fn main() -> Result<()> {
145174 watcher:: start (
146175 & filter,
147176 show_progress,
148- & folder,
177+ & args . folder ,
149178 args. after_build ,
150179 args. create_sourcedirs ,
151180 args. dev ,
152181 ) ;
153182
154183 Ok ( ( ) )
155184 }
185+ Command :: Format => {
186+ todo ! ( "Format not implemented yet" ) ;
187+ }
188+ Command :: Dump => {
189+ todo ! ( "Dump not implemented yet" ) ;
190+ }
156191 } ,
157192 }
158193}
0 commit comments