Skip to content

Commit

Permalink
Merge pull request #30 from LukasKalbertodt/restructure
Browse files Browse the repository at this point in the history
Refactor into more idiomatic and cleaner code

Fix #2
Fix #33
Do most of the work towards #25
  • Loading branch information
passcod committed Jan 31, 2016
2 parents a8fa319 + aceace5 commit b71a889
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 215 deletions.
77 changes: 52 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ authors = [
"Gabor Nagy <mail@aigeruth.hu>",
"Ivan Jager <aij+git@mrph.org>",
"Christopher Brickley <brickley@gmail.com>",
"Lukas Kalbertodt <lukas.kalbertodt@gmail.com>",
]

description = "Utility for cargo to compile projects when sources change"
Expand All @@ -27,8 +28,9 @@ keywords = [
[dependencies]
docopt = "^0.6.78"
env_logger = "^0.3.2"
lazy_static = "0.1.15"
log = "^0.3.4"
notify = "^2.5.4"
regex = "^0.1.44"
rustc-serialize = "^0.3.16"
time = "^0.1.34"
wait-timeout = "0.1"
81 changes: 37 additions & 44 deletions src/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,50 @@
//! Utilities for working with cargo,
//! Utilities for working with cargo and rust files
use config;
use regex::Regex;
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;

macro_rules! Sl(($v:expr) => (String::from_utf8_lossy($v.as_slice())));

/// Returns the closest ancestor Path containing a Cargo.toml.
/// Returns the closest ancestor path containing a `Cargo.toml`.
///
/// Returns None if no ancestor Path contains a Cargo.toml, or if
/// the limit of 10 ancestors has been run through.
/// Returns `None` if no ancestor path contains a `Cargo.toml`, or if
/// the limit of MAX_ANCESTORS ancestors has been reached.
pub fn root() -> Option<PathBuf> {
let mut wd = match env::current_dir() {
Err(_) => { return None; },
Ok(w) => w
};

fn contains_manifest(path: &mut PathBuf) -> bool {
match fs::read_dir(path) {
Ok(mut entries) =>
entries.any(|ent| match ent {
Err(_) => false,
Ok(ref ent) => {
ent.path().file_name() == Some(OsStr::new("Cargo.toml"))
}
}),
Err(_) => false
/// Checks if the directory contains `Cargo.toml`
fn contains_manifest(path: &PathBuf) -> bool {
fs::read_dir(path).map(|entries| {
entries.filter_map(|res| res.ok())
.any(|ent| &ent.file_name() == "Cargo.toml")
}).unwrap_or(false)
}
}

for _ in 0..11 {
if contains_manifest(&mut wd) {
return Some(wd)
}
if !wd.pop() { break }
}
// From the current directory we work our way up, looking for `Cargo.toml`
env::current_dir().ok().and_then(|mut wd| {
for _ in 0..config::MAX_ANCESTORS {
if contains_manifest(&mut wd) {
return Some(wd);
}
if !wd.pop() {
break;
}
}

None
})
}

None
lazy_static! {
static ref IGNORED_FILES: Vec<Regex> = {
config::IGNORED_FILES.iter().map(|s| {
// FIXME: This should use the compile-time `regex!` macros, when
// syntax extensions become stabilized (see #32)
Regex::new(s).expect("Couldn't parse regex")
}).collect()
};
}

/// Runs one or more cargo commands and displays the output.
pub fn run(cmds: &str) {
let cmds_vec: Vec<&str> = cmds.split_whitespace().collect();
println!("\n$ cargo {}", cmds);
match Command::new("cargo")
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.args(&cmds_vec)
.output() {
Ok(o) => println!("-> {}", o.status),
Err(e) => println!("Failed to execute 'cargo {}': {}", cmds, e)
};
/// Checks if the given filename should be ignored
pub fn is_ignored_file(f: &str) -> bool {
IGNORED_FILES.iter().any(|fr| fr.is_match(f))
}
53 changes: 0 additions & 53 deletions src/compile.rs

This file was deleted.

25 changes: 25 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// These commands are executed when no arguments are given to `cargo watch`
pub const DEFAULT_COMMANDS: [&'static str; 1] = ["test"];

/// How many parent folders are searched for a `Cargo.toml`
pub const MAX_ANCESTORS: u32 = 10;

/// Which subdirectories are being watched for changes
pub const WATCH_DIRS: [&'static str; 4] = [
"src",
"tests",
"benches",
"examples",
];

/// Changes on files whose names match one of these regexes are ignored
pub const IGNORED_FILES: [&'static str; 3] = [
// FIXME: It should be possible to trigger on non-.rs changes (see #31)
r"[^.][^r][^s]$",
r"^\.",
r"^~",
];

/// The timeout for waiting for another process. Shorter means more CPU usage,
/// longer means slower response to file changes. Value in ms.
pub const PROCESS_WAIT_TIMEOUT: u32 = 100;
Loading

0 comments on commit b71a889

Please sign in to comment.