-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from LukasKalbertodt/restructure
- Loading branch information
Showing
9 changed files
with
356 additions
and
215 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.