Skip to content

Commit

Permalink
Use .gitignore to clean old files
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg committed Apr 2, 2018
1 parent 9555e19 commit 04b153c
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions protobuf-test-common/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
pub use protobuf_codegen::Customize;

use std::io::Write;
use std::io::BufRead;
use std::io::BufReader;
use std::fs;
use std::fmt;
use std::path::Path;
Expand All @@ -24,16 +26,62 @@ pub fn glob_simple(pattern: &str) -> Vec<String> {
}


pub fn clean_old_files() {
// TODO: use .gitignore
for f in glob_simple("src/**/*_pb.rs") {
fs::remove_file(f).expect("rm");
fn read_gitignore(dir: &Path) -> Vec<String> {
let mut patterns = Vec::new();

let gitignore = format!("{}/.gitignore", dir.display());
let gitignore = &Path::new(&gitignore);
if gitignore.exists() {
let gitignore = fs::File::open(gitignore).expect("open gitignore");
for line in BufReader::new(gitignore).lines() {
let line = line.expect("read_line");
if line.is_empty() || line.starts_with("#") {
continue;
}
patterns.push(line);
}
}
for f in glob_simple("src/**/*_pb_proto3.rs") {
fs::remove_file(f).expect("rm");

patterns
}


fn clean_recursively(dir: &Path, patterns: &[&str]) {
assert!(dir.is_dir());

let gitignore_patterns = read_gitignore(dir);

let mut patterns = patterns.to_vec();
patterns.extend(gitignore_patterns.iter().map(String::as_str));

let patterns_compiled: Vec<_> = patterns.iter()
.map(|&p| glob::Pattern::new(p).expect("failed to compile pattern"))
.collect();

for entry in fs::read_dir(dir).expect("read_dir") {
let entry = entry.expect("entry");
let entry_path = entry.path();
let file_name = entry_path.as_path().file_name().unwrap().to_str().unwrap();
if entry.metadata().expect("metadata").is_dir() {
clean_recursively(&entry_path, &patterns);
} else if file_name == ".gitignore" {
// keep it
} else {
for pattern in &patterns_compiled {
if pattern.matches(file_name) {
fs::remove_file(&entry_path).expect("remove_file");
break;
}
}
}
}
}


pub fn clean_old_files() {
clean_recursively(&Path::new("src"), &["*_pb.rs", "*_pb_proto3.rs"]);
}

#[derive(Default)]
pub struct GenInDirArgs<'a> {
pub out_dir: &'a str,
Expand Down

0 comments on commit 04b153c

Please sign in to comment.