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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Watch files in a Cargo project and compile it when they change
#![forbid(unsafe_code)]
#![cfg_attr(feature = "cargo-clippy", deny(clippy_pedantic))]
#![cfg_attr(feature = "cargo-clippy", allow(non_ascii_literal))]
#![cfg_attr(feature = "cargo-clippy", allow(cast_sign_loss))]
#![cfg_attr(feature = "cargo-clippy", allow(cast_possible_truncation))]

#[macro_use]
extern crate clap;
extern crate watchexec;

use clap::{ArgMatches, Error, ErrorKind};
use std::{
    env::set_current_dir,
    path::{PathBuf, MAIN_SEPARATOR},
};
use watchexec::cli::Args;

pub mod args;
pub mod cargo;
pub mod watch;

pub fn change_dir() {
    cargo::root()
        .and_then(|p| set_current_dir(p).ok())
        .unwrap_or_else(|| {
            Error::with_description("Not a Cargo project, aborting.", ErrorKind::Io).exit();
        });
}

pub fn get_commands(debug: bool, matches: &ArgMatches) -> Vec<String> {
    let mut commands: Vec<String> = Vec::new();

    // Cargo commands are in front of the rest
    if matches.is_present("cmd:cargo") {
        for cargo in values_t!(matches, "cmd:cargo", String).unwrap_or_else(|e| e.exit()) {
            let mut cmd: String = "cargo ".into();
            cmd.push_str(&cargo);
            commands.push(cmd);
        }
    }

    // Shell/raw commands go last
    if matches.is_present("cmd:shell") {
        for shell in values_t!(matches, "cmd:shell", String).unwrap_or_else(|e| e.exit()) {
            commands.push(shell);
        }
    }

    // Default to `cargo check`
    if commands.is_empty() {
        commands.push("cargo check".into());
    }

    if debug {
        println!(">>> Commands: {:?}", commands);
    }

    if matches.is_present("quiet") {
        commands.push("quiet".into());
    }

    commands
}

pub fn get_ignores(debug: bool, matches: &ArgMatches) -> (bool, Vec<String>) {
    let mut opts = Vec::new();

    if matches.is_present("ignore-nothing") {
        if debug {
            println!(">>> Ignoring nothing");
        }

        return (true, Vec::new());
    }

    let novcs = matches.is_present("no-gitignore");
    if debug {
        println!(">>> Load Git/VCS ignores: {:?}", !novcs);
    }

    // Mac
    opts.push(format!("*{}.DS_Store", MAIN_SEPARATOR));

    // Vim
    opts.push("*.sw?".into());
    opts.push("*.sw?x".into());

    // Emacs
    opts.push("#*#".into());
    opts.push(".#*".into());

    // VCS
    opts.push(format!("*{s}.hg{s}**", s = MAIN_SEPARATOR));
    opts.push(format!("*{s}.git{s}**", s = MAIN_SEPARATOR));
    opts.push(format!("*{s}.svn{s}**", s = MAIN_SEPARATOR));

    // SQLite
    opts.push("*.db".into());
    opts.push("*.db-*".into());
    opts.push(format!("*{s}*.db-journal{s}**", s = MAIN_SEPARATOR));

    // Rust
    opts.push(format!("*{s}target{s}**", s = MAIN_SEPARATOR));

    if debug {
        println!(">>> Default ignores: {:?}", opts);
    }

    if matches.is_present("ignore") {
        for ignore in values_t!(matches, "ignore", String).unwrap_or_else(|e| e.exit()) {
            #[cfg(windows)]
            let ignore = ignore.replace("/", &MAIN_SEPARATOR.to_string());
            opts.push(ignore);
        }
    }

    if debug {
        println!(">>> All ignores: {:?}", opts);
    }

    (novcs, opts)
}

pub fn get_debounce(debug: bool, matches: &ArgMatches) -> u32 {
    if matches.is_present("delay") {
        let debounce = value_t!(matches, "delay", f32).unwrap_or_else(|e| e.exit());
        if debug {
            println!(">>> File updates debounce: {} seconds", debounce);
        }
        (debounce * 1000.0) as u32
    } else {
        500
    }
}

pub fn get_watches(debug: bool, matches: &ArgMatches) -> Vec<PathBuf> {
    let mut opts = Vec::new();
    if matches.is_present("watch") {
        for watch in values_t!(matches, "watch", String).unwrap_or_else(|e| e.exit()) {
            opts.push(watch.into());
        }
    }

    if debug {
        println!(">>> Watches: {:?}", opts);
    }

    opts
}

pub fn get_options(debug: bool, matches: &ArgMatches) -> Args {
    let (novcs, ignores) = get_ignores(debug, &matches);
    let debounce = get_debounce(debug, &matches);

    let arglist = Args {
        filters: vec![],
        no_shell: false,
        once: matches.is_present("once"),
        signal: None,
        restart: !matches.is_present("no-restart"),

        poll: matches.is_present("poll"),
        poll_interval: debounce,
        debounce: u64::from(debounce),

        ignores,
        no_vcs_ignore: novcs,

        clear_screen: matches.is_present("clear"),
        debug,
        run_initially: !matches.is_present("postpone"),

        cmd: get_commands(debug, &matches),
        paths: get_watches(debug, &matches),
    };

    if debug {
        println!(">>> Watchexec arguments: {:?}", arglist);
    }

    arglist
}