|  | 
|  | 1 | +use std::ffi::OsString; | 
|  | 2 | + | 
|  | 3 | +use clap::{Args, Parser, Subcommand}; | 
|  | 4 | +use clap_verbosity_flag::InfoLevel; | 
|  | 5 | + | 
|  | 6 | +/// Rewatch is an alternative build system for the Rescript Compiler bsb (which uses Ninja internally). It strives | 
|  | 7 | +/// to deliver consistent and faster builds in monorepo setups with multiple packages, where the | 
|  | 8 | +/// default build system fails to pick up changed interfaces across multiple packages. | 
|  | 9 | +#[derive(Parser, Debug)] | 
|  | 10 | +#[command(version)] | 
|  | 11 | +#[command(args_conflicts_with_subcommands = true)] | 
|  | 12 | +pub struct Cli { | 
|  | 13 | +    /// Verbosity: | 
|  | 14 | +    /// -v -> Debug | 
|  | 15 | +    /// -vv -> Trace | 
|  | 16 | +    /// -q -> Warn | 
|  | 17 | +    /// -qq -> Error | 
|  | 18 | +    /// -qqq -> Off. | 
|  | 19 | +    /// Default (/ no argument given): 'info' | 
|  | 20 | +    #[command(flatten)] | 
|  | 21 | +    pub verbose: clap_verbosity_flag::Verbosity<InfoLevel>, | 
|  | 22 | + | 
|  | 23 | +    /// The command to run. If not provided it will default to build. | 
|  | 24 | +    #[command(subcommand)] | 
|  | 25 | +    pub command: Option<Command>, | 
|  | 26 | + | 
|  | 27 | +    /// The relative path to where the main rescript.json resides. IE - the root of your project. | 
|  | 28 | +    #[arg(default_value = ".")] | 
|  | 29 | +    pub folder: String, | 
|  | 30 | + | 
|  | 31 | +    #[command(flatten)] | 
|  | 32 | +    pub build_args: BuildArgs, | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +#[derive(Args, Debug, Clone)] | 
|  | 36 | +pub struct BuildArgs { | 
|  | 37 | +    /// Filter files by regex | 
|  | 38 | +    /// | 
|  | 39 | +    /// Filter allows for a regex to be supplied which will filter the files to be compiled. For | 
|  | 40 | +    /// instance, to filter out test files for compilation while doing feature work. | 
|  | 41 | +    #[arg(short, long)] | 
|  | 42 | +    pub filter: Option<String>, | 
|  | 43 | + | 
|  | 44 | +    /// Action after build | 
|  | 45 | +    /// | 
|  | 46 | +    /// This allows one to pass an additional command to the watcher, which allows it to run when | 
|  | 47 | +    /// finished. For instance, to play a sound when done compiling, or to run a test suite. | 
|  | 48 | +    /// NOTE - You may need to add '--color=always' to your subcommand in case you want to output | 
|  | 49 | +    /// color as well | 
|  | 50 | +    #[arg(short, long)] | 
|  | 51 | +    pub after_build: Option<String>, | 
|  | 52 | + | 
|  | 53 | +    /// Create source_dirs.json | 
|  | 54 | +    /// | 
|  | 55 | +    /// This creates a source_dirs.json file at the root of the monorepo, which is needed when you | 
|  | 56 | +    /// want to use Reanalyze | 
|  | 57 | +    #[arg(short, long, default_value_t = false, num_args = 0..=1)] | 
|  | 58 | +    pub create_sourcedirs: bool, | 
|  | 59 | + | 
|  | 60 | +    /// Build development dependencies | 
|  | 61 | +    /// | 
|  | 62 | +    /// This is the flag to also compile development dependencies | 
|  | 63 | +    /// It's important to know that we currently do not discern between project src, and | 
|  | 64 | +    /// dependencies. So enabling this flag will enable building _all_ development dependencies of | 
|  | 65 | +    /// _all_ packages | 
|  | 66 | +    #[arg(long, default_value_t = false, num_args = 0..=1)] | 
|  | 67 | +    pub dev: bool, | 
|  | 68 | + | 
|  | 69 | +    /// Disable timing on the output | 
|  | 70 | +    #[arg(short, long, default_value_t = false, num_args = 0..=1)] | 
|  | 71 | +    pub no_timing: bool, | 
|  | 72 | + | 
|  | 73 | +    /// simple output for snapshot testing | 
|  | 74 | +    #[arg(short, long, default_value = "false", num_args = 0..=1)] | 
|  | 75 | +    pub snapshot_output: bool, | 
|  | 76 | + | 
|  | 77 | +    /// Path to bsc | 
|  | 78 | +    #[arg(long)] | 
|  | 79 | +    pub bsc_path: Option<String>, | 
|  | 80 | +} | 
|  | 81 | + | 
|  | 82 | +#[derive(Args, Clone, Debug)] | 
|  | 83 | +pub struct WatchArgs { | 
|  | 84 | +    /// Filter files by regex | 
|  | 85 | +    /// | 
|  | 86 | +    /// Filter allows for a regex to be supplied which will filter the files to be compiled. For | 
|  | 87 | +    /// instance, to filter out test files for compilation while doing feature work. | 
|  | 88 | +    #[arg(short, long)] | 
|  | 89 | +    pub filter: Option<String>, | 
|  | 90 | + | 
|  | 91 | +    /// Action after build | 
|  | 92 | +    /// | 
|  | 93 | +    /// This allows one to pass an additional command to the watcher, which allows it to run when | 
|  | 94 | +    /// finished. For instance, to play a sound when done compiling, or to run a test suite. | 
|  | 95 | +    /// NOTE - You may need to add '--color=always' to your subcommand in case you want to output | 
|  | 96 | +    /// color as well | 
|  | 97 | +    #[arg(short, long)] | 
|  | 98 | +    pub after_build: Option<String>, | 
|  | 99 | + | 
|  | 100 | +    /// Create source_dirs.json | 
|  | 101 | +    /// | 
|  | 102 | +    /// This creates a source_dirs.json file at the root of the monorepo, which is needed when you | 
|  | 103 | +    /// want to use Reanalyze | 
|  | 104 | +    #[arg(short, long, default_value_t = false, num_args = 0..=1)] | 
|  | 105 | +    pub create_sourcedirs: bool, | 
|  | 106 | + | 
|  | 107 | +    /// Build development dependencies | 
|  | 108 | +    /// | 
|  | 109 | +    /// This is the flag to also compile development dependencies | 
|  | 110 | +    /// It's important to know that we currently do not discern between project src, and | 
|  | 111 | +    /// dependencies. So enabling this flag will enable building _all_ development dependencies of | 
|  | 112 | +    /// _all_ packages | 
|  | 113 | +    #[arg(long, default_value_t = false, num_args = 0..=1)] | 
|  | 114 | +    pub dev: bool, | 
|  | 115 | + | 
|  | 116 | +    /// simple output for snapshot testing | 
|  | 117 | +    #[arg(short, long, default_value = "false", num_args = 0..=1)] | 
|  | 118 | +    pub snapshot_output: bool, | 
|  | 119 | + | 
|  | 120 | +    /// Path to bsc | 
|  | 121 | +    #[arg(long)] | 
|  | 122 | +    pub bsc_path: Option<String>, | 
|  | 123 | +} | 
|  | 124 | + | 
|  | 125 | +#[derive(Subcommand, Clone, Debug)] | 
|  | 126 | +pub enum Command { | 
|  | 127 | +    /// Build using Rewatch | 
|  | 128 | +    Build(BuildArgs), | 
|  | 129 | +    /// Build, then start a watcher | 
|  | 130 | +    Watch(WatchArgs), | 
|  | 131 | +    /// Clean the build artifacts | 
|  | 132 | +    Clean { | 
|  | 133 | +        /// Path to bsc | 
|  | 134 | +        #[arg(long)] | 
|  | 135 | +        bsc_path: Option<String>, | 
|  | 136 | + | 
|  | 137 | +        /// simple output for snapshot testing | 
|  | 138 | +        #[arg(short, long, default_value = "false", num_args = 0..=1)] | 
|  | 139 | +        snapshot_output: bool, | 
|  | 140 | +    }, | 
|  | 141 | +    /// Alias to `legacy format`. | 
|  | 142 | +    #[command(disable_help_flag = true)] | 
|  | 143 | +    Format { | 
|  | 144 | +        #[arg(allow_hyphen_values = true, num_args = 0..)] | 
|  | 145 | +        format_args: Vec<OsString>, | 
|  | 146 | +    }, | 
|  | 147 | +    /// Alias to `legacy dump`. | 
|  | 148 | +    #[command(disable_help_flag = true)] | 
|  | 149 | +    Dump { | 
|  | 150 | +        #[arg(allow_hyphen_values = true, num_args = 0..)] | 
|  | 151 | +        dump_args: Vec<OsString>, | 
|  | 152 | +    }, | 
|  | 153 | +    /// This prints the compiler arguments. It expects the path to a rescript.json file. | 
|  | 154 | +    CompilerArgs { | 
|  | 155 | +        /// Path to a rescript.json file | 
|  | 156 | +        #[command()] | 
|  | 157 | +        path: String, | 
|  | 158 | + | 
|  | 159 | +        #[arg(long, default_value_t = false, num_args = 0..=1)] | 
|  | 160 | +        dev: bool, | 
|  | 161 | + | 
|  | 162 | +        /// To be used in conjunction with compiler_args | 
|  | 163 | +        #[arg(long)] | 
|  | 164 | +        rescript_version: Option<String>, | 
|  | 165 | + | 
|  | 166 | +        /// A custom path to bsc | 
|  | 167 | +        #[arg(long)] | 
|  | 168 | +        bsc_path: Option<String>, | 
|  | 169 | +    }, | 
|  | 170 | +    /// Use the legacy build system. | 
|  | 171 | +    /// | 
|  | 172 | +    /// After this command is encountered, the rest of the arguments are passed to the legacy build system. | 
|  | 173 | +    #[command(disable_help_flag = true)] | 
|  | 174 | +    Legacy { | 
|  | 175 | +        #[arg(allow_hyphen_values = true, num_args = 0..)] | 
|  | 176 | +        legacy_args: Vec<OsString>, | 
|  | 177 | +    }, | 
|  | 178 | +} | 
0 commit comments