| 1 | # Reflex |
| 2 | |
| 3 | Reflex is a small tool to watch a directory and rerun a command when certain |
| 4 | files change. It's great for automatically running compile/lint/test tasks and |
| 5 | for reloading your application when the code changes. |
| 6 | |
| 7 | ## A simple example |
| 8 | |
| 9 | # Rerun make whenever a .c file changes |
| 10 | reflex -r '\.c$' make |
| 11 | |
| 12 | ## Installation |
| 13 | |
| 14 | You can download binaries from the |
| 15 | [Releases page](https://github.com/cespare/reflex/releases). |
| 16 | |
| 17 | To compile from source, you'll need Go 1.13+ installed. |
| 18 | |
| 19 | If you have Go 1.16 or later, you can download and install the latest module |
| 20 | version directly with |
| 21 | |
| 22 | go install github.com/cespare/reflex@latest |
| 23 | |
| 24 | Reflex is only tested on Linux and macOS. |
| 25 | |
| 26 | ## Usage |
| 27 | |
| 28 | The following is given by running `reflex -h`: |
| 29 | |
| 30 | ``` |
| 31 | Usage: reflex [OPTIONS] [COMMAND] |
| 32 | |
| 33 | COMMAND is any command you'd like to run. Any instance of {} will be replaced |
| 34 | with the filename of the changed file. (The symbol may be changed with the |
| 35 | --substitute flag.) |
| 36 | |
| 37 | OPTIONS are given below: |
| 38 | --all=false: |
| 39 | Include normally ignored files (VCS and editor special files). |
| 40 | -c, --config="": |
| 41 | A configuration file that describes how to run reflex |
| 42 | (or '-' to read the configuration from stdin). |
| 43 | -d, --decoration="plain": |
| 44 | How to decorate command output. Choices: none, plain, fancy. |
| 45 | -g, --glob=[]: |
| 46 | A shell glob expression to match filenames. (May be repeated.) |
| 47 | -G, --inverse-glob=[]: |
| 48 | A shell glob expression to exclude matching filenames. |
| 49 | (May be repeated.) |
| 50 | -R, --inverse-regex=[]: |
| 51 | A regular expression to exclude matching filenames. |
| 52 | (May be repeated.) |
| 53 | --only-dirs=false: |
| 54 | Only match directories (not files). |
| 55 | --only-files=false: |
| 56 | Only match files (not directories). |
| 57 | -r, --regex=[]: |
| 58 | A regular expression to match filenames. (May be repeated.) |
| 59 | -e, --sequential=false: |
| 60 | Don't run multiple commands at the same time. |
| 61 | -t, --shutdown-timeout=500ms: |
| 62 | Allow services this long to shut down. |
| 63 | -s, --start-service=false: |
| 64 | Indicates that the command is a long-running process to be |
| 65 | restarted on matching changes. |
| 66 | --substitute="{}": |
| 67 | The substitution symbol that is replaced with the filename |
| 68 | in a command. |
| 69 | -v, --verbose=false: |
| 70 | Verbose mode: print out more information about what reflex is doing. |
| 71 | |
| 72 | Examples: |
| 73 | |
| 74 | # Print each .txt file if it changes |
| 75 | $ reflex -r '\.txt$' echo {} |
| 76 | |
| 77 | # Run 'make' if any of the .c files in this directory change: |
| 78 | $ reflex -g '*.c' make |
| 79 | |
| 80 | # Build and run a server; rebuild and restart when .java files change: |
| 81 | $ reflex -r '\.java$' -s -- sh -c 'make && java bin/Server' |
| 82 | ``` |
| 83 | |
| 84 | ### Overview |
| 85 | |
| 86 | Reflex watches file changes in the current working directory and re-runs the |
| 87 | command that you specify. The flags change what changes cause the command to be |
| 88 | rerun and other behavior. |
| 89 | |
| 90 | ### Patterns |
| 91 | |
| 92 | You can specify files to match using either shell glob patterns (`-g`) or |
| 93 | regular expressions (`-r`). If you don't specify either, reflex will run your |
| 94 | command after any file changes. (Reflex ignores some common editor and version |
| 95 | control files; see Ignored files, below.) |
| 96 | |
| 97 | You can specify inverse matches by using the `--inverse-glob` (`-G`) and |
| 98 | `--inverse-regex` (`-R`) flags. |
| 99 | |
| 100 | If you specify multiple globs/regexes (e.g. `-r foo -r bar -R baz -G x/*/y`), |
| 101 | only files that match all patterns and none of the inverse patterns are |
| 102 | selected. |
| 103 | |
| 104 | The shell glob syntax is described |
| 105 | [here](http://golang.org/pkg/path/filepath/#Match), while the regular expression |
| 106 | syntax is described [here](https://code.google.com/p/re2/wiki/Syntax). |
| 107 | |
| 108 | The path that is matched against the glob or regular expression does not have a |
| 109 | leading `./`. For example, if there is a file `./foobar.txt` that changes, then |
| 110 | it will be matched by the regular expression `^foobar`. If the path is a |
| 111 | directory, it has a trailing `/`. |
| 112 | |
| 113 | ### --start-service |
| 114 | |
| 115 | The `--start-service` flag (short version: `-s`) inverts the behavior of command |
| 116 | running: it runs the command when reflex starts and kills/restarts it each time |
| 117 | files change. This is expected to be used with an indefinitely-running command, |
| 118 | such as a server. You can use this flag to relaunch the server when the code is |
| 119 | changed. |
| 120 | |
| 121 | ### Substitution |
| 122 | |
| 123 | Reflex provides a way for you to determine, inside your command, what file |
| 124 | changed. This is via a substitution symbol. The default is `{}`. Every instance |
| 125 | of the substitution symbol inside your command is replaced by the filename. |
| 126 | |
| 127 | As a simple example, suppose you're writing Coffeescript and you wish to compile |
| 128 | the CS files to Javascript when they change. You can do this with: |
| 129 | |
| 130 | reflex -r '\.coffee$' -- coffee -c {} |
| 131 | |
| 132 | In case you need to use `{}` for something else in your command, you can change |
| 133 | the substitution symbol with the `--substitute` flag. |
| 134 | |
| 135 | ### Configuration file |
| 136 | |
| 137 | What if you want to run many watches at once? For example, when writing web |
| 138 | applications I often want to rebuild/rerun the server when my code changes, but |
| 139 | also build SCSS and Coffeescript when those change as well. Instead of running |
| 140 | multiple reflex instances, which is cumbersome (and inefficient), you can give |
| 141 | reflex a configuration file. |
| 142 | |
| 143 | The configuration file syntax is simple: each line is a command, and each |
| 144 | command is composed of flags and arguments -- just like calling reflex but |
| 145 | without the initial `reflex`. Lines that start with `#` are ignored. Commands |
| 146 | can span multiple lines if they're \\-continued, or include multi-line strings. |
| 147 | Here's an example: |
| 148 | |
| 149 | # Rebuild SCSS when it changes |
| 150 | -r '\.scss$' -- \ |
| 151 | sh -c 'sass {} `basename {} .scss`.css' |
| 152 | |
| 153 | # Restart server when ruby code changes |
| 154 | -sr '\.rb$' -- \ |
| 155 | ./bin/run_server.sh |
| 156 | |
| 157 | If you want to change the configuration file and have reflex reload it on the |
| 158 | fly, you can run reflex inside reflex: |
| 159 | |
| 160 | reflex -s -g reflex.conf -- reflex -c reflex.conf |
| 161 | |
| 162 | This tells reflex to run another reflex process as a service that's restarted |
| 163 | whenever `reflex.conf` changes. |
| 164 | |
| 165 | ### --sequential |
| 166 | |
| 167 | When using a config file to run multiple simultaneous commands, reflex will run |
| 168 | them at the same time (if appropriate). That is, a particular command can only |
| 169 | be run once a previous run of that command finishes, but two different commands |
| 170 | may run at the same time. This is usually what you want (for speed). |
| 171 | |
| 172 | As a concrete example, consider this config file: |
| 173 | |
| 174 | -- sh -c 'for i in `seq 1 5`; do sleep 0.1; echo first; done' |
| 175 | -- sh -c 'for i in `seq 1 5`; do sleep 0.1; echo second; done' |
| 176 | |
| 177 | When this runs, you'll see something like this: |
| 178 | |
| 179 | [01] second |
| 180 | [00] first |
| 181 | [01] second |
| 182 | [00] first |
| 183 | [00] first |
| 184 | [01] second |
| 185 | [01] second |
| 186 | [00] first |
| 187 | [01] second |
| 188 | [00] first |
| 189 | |
| 190 | Note that the output is interleaved. (Reflex does ensure that each line of |
| 191 | output is not interleaved with a different line.) If, for some reason, you need |
| 192 | to ensure that your commands don't run at the same time, you can do this with |
| 193 | the `--sequential` (`-e`) flag. Then the output would look like (for example): |
| 194 | |
| 195 | [01] second |
| 196 | [01] second |
| 197 | [01] second |
| 198 | [01] second |
| 199 | [01] second |
| 200 | [00] first |
| 201 | [00] first |
| 202 | [00] first |
| 203 | [00] first |
| 204 | [00] first |
| 205 | |
| 206 | ### Decoration |
| 207 | |
| 208 | By default, each line of output from your command is prefixed with something |
| 209 | like `[00]`, which is simply an id that reflex assigns to each command. You can |
| 210 | use `--decoration` (`-d`) to change this output: `--decoration=none` will print |
| 211 | the output as is; `--decoration=fancy` will color each line differently |
| 212 | depending on which command it is, making it easier to distinguish the output. |
| 213 | |
| 214 | ### Ignored files |
| 215 | |
| 216 | Reflex ignores a variety of version control and editor metadata files by |
| 217 | default. If you wish for these to be included, you can provide reflex with the |
| 218 | `--all` flag. |
| 219 | |
| 220 | You can see a list of regular expressions that match the files that reflex |
| 221 | ignores by default |
| 222 | [here](https://github.com/cespare/reflex/blob/master/defaultexclude.go#L5). |
| 223 | |
| 224 | ## Notes and Tips |
| 225 | |
| 226 | If you don't use `-r` or `-g`, reflex will match every file. |
| 227 | |
| 228 | Reflex only considers file creation and modification changes. It does not report |
| 229 | attribute changes nor deletions. |
| 230 | |
| 231 | For ignoring directories, it's easiest to use a regular expression: `-R '^dir/'`. |
| 232 | |
| 233 | Many regex and glob characters are interpreted specially by various shells. |
| 234 | You'll generally want to minimize this effect by putting the regex and glob |
| 235 | patterns in single quotes. |
| 236 | |
| 237 | If your command has options, you'll probably need to use `--` to separate the |
| 238 | reflex flags from your command flags. For example: `reflex -r '.*\.txt' -- ls |
| 239 | -l`. |
| 240 | |
| 241 | If you're going to use shell things, you need to invoke a shell as a parent |
| 242 | process: |
| 243 | |
| 244 | reflex -- sh -c 'sleep 1 && echo {}' |
| 245 | |
| 246 | If your command is running with sudo, you'll need a passwordless sudo, because |
| 247 | you cannot enter your password in through reflex. |
| 248 | |
| 249 | It's not difficult to accidentally make an infinite loop with certain commands. |
| 250 | For example, consider this command: `reflex -r '\.txt' cp {} {}.bak`. If |
| 251 | `foo.txt` changes, then this will create `foo.txt.bak`, `foo.txt.bak.bak`, and |
| 252 | so forth, because the regex `\.txt` matches each file. Reflex doesn't have any |
| 253 | kind of infinite loop detection, so be careful with commands like `cp`. |
| 254 | |
| 255 | The restart behavior works as follows: if your program is still running, reflex |
| 256 | sends it SIGINT; after 1 second if it's still alive, it gets SIGKILL. The new |
| 257 | process won't be started up until the old process is dead. |
| 258 | |
| 259 | ### Batching |
| 260 | |
| 261 | Part of what reflex does is apply some heuristics to batch together file |
| 262 | changes. There are many reasons that files change on disk, and these changes |
| 263 | frequently come in large bursts. For instance, when you save a file in your |
| 264 | editor, it probably makes a tempfile and then copies it over the target, leading |
| 265 | to several different changes. Reflex hides this from you by batching some |
| 266 | changes together. |
| 267 | |
| 268 | One thing to note, though, is that the the batching is a little different |
| 269 | depending on whether or not you have a substitution symbol in your command. If |
| 270 | you do not, then updates for different files that all match your pattern can be |
| 271 | batched together in a single update that only causes your command to be run |
| 272 | once. |
| 273 | |
| 274 | If you are using a substitution symbol, however, each unique matching file will |
| 275 | be batched separately. |
| 276 | |
| 277 | ### Argument list splitting |
| 278 | |
| 279 | When you give reflex a command from the commandline (i.e., not in a config |
| 280 | file), that command is split into pieces by whatever shell you happen to be |
| 281 | using. When reflex parses the config file, however, it must do that splitting |
| 282 | itself. For this purpose, it uses [this library](https://github.com/kballard/go-shellquote) |
| 283 | which attempts to match `sh`'s argument splitting rules. |
| 284 | |
| 285 | This difference can lead to slightly different behavior when running commands |
| 286 | from a config file. If you're confused, it can help to use `--verbose` (`-v`) |
| 287 | which will print out each command as interpreted by reflex. |
| 288 | |
| 289 | ### Open file limits |
| 290 | |
| 291 | Reflex currently must hold an open file descriptor for every directory it's |
| 292 | watching, recursively. If you run reflex at the top of a big directory tree, you |
| 293 | can easily run into file descriptor limits. You might see an error like this: |
| 294 | |
| 295 | open some/path: too many open files |
| 296 | |
| 297 | There are several things you can do to get around this problem. |
| 298 | |
| 299 | 1. Run reflex in the most specific directory possible. Don't run |
| 300 | `reflex -g path/to/project/*.c ...` from `$HOME`; instead run reflex in |
| 301 | `path/to/project`. |
| 302 | 2. Ignore large subdirectories. Reflex already ignores, for instance, `.git/`. |
| 303 | If you have other large subdirectories, you can ignore those yourself: |
| 304 | `reflex -R '^third_party/' ...` ignores everything under `third_party/` in |
| 305 | your project directory. |
| 306 | 3. Raise the fd limit using `ulimit` or some other tool. On some systems, this |
| 307 | might default to a restrictively small value like 256. |
| 308 | |
| 309 | See [issue #6](https://github.com/cespare/reflex/issues/6) for some more |
| 310 | background on this issue. |
| 311 | |
| 312 | ## The competition |
| 313 | |
| 314 | * https://github.com/guard/guard |
| 315 | * https://github.com/alexch/rerun |
| 316 | * https://github.com/mynyml/watchr |
| 317 | * https://github.com/eaburns/Watch |
| 318 | * https://github.com/alloy/kicker |
| 319 | * https://github.com/eradman/entr |
| 320 | |
| 321 | ### Why you should use reflex instead |
| 322 | |
| 323 | * Reflex has no dependencies. No need to install Ruby or anything like that. |
| 324 | * Reflex uses an appropriate file watching mechanism to watch for changes |
| 325 | efficiently on your platform. |
| 326 | * Reflex gives your command the name of the file that changed. |
| 327 | * No DSL to learn -- just give it a shell command. |
| 328 | * No plugins. |
| 329 | * Not tied to any language, framework, workflow, or editor. |
| 330 | |
| 331 | ## Authors |
| 332 | |
| 333 | * Benedikt Böhm ([hollow](https://github.com/hollow)) |
| 334 | * Caleb Spare ([cespare](https://github.com/cespare)) |
| 335 | * Jim Kalafut ([kalafut](https://github.com/kalafut)) |
| 336 | * PJ Eby ([pjeby](https://github.com/pjeby)) |
| 337 | * Rich Liebling ([rliebling](https://github.com/rliebling)) |
| 338 | * Seth W. Klein ([sethwklein](https://github.com/sethwklein)) |
| 339 | * Vincent Vanackere ([vanackere](https://github.com/vanackere)) |