Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Big rewrite of the application #30

Merged
merged 16 commits into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ authors = [
"Gabor Nagy <[email protected]>",
"Ivan Jager <[email protected]>",
"Christopher Brickley <[email protected]>",
"Lukas Kalbertodt <[email protected]>",
]

description = "Utility for cargo to compile projects when sources change"
Expand All @@ -27,8 +28,8 @@ 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"
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.

16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// 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; 3] = ["src", "tests", "benches"];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#33 could just be fixed right there, while we're at it.


/// 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"^~",
];
22 changes: 0 additions & 22 deletions src/ignore.rs

This file was deleted.

Loading