|
| 1 | +use getopts::Options; |
| 2 | +use regex::RegexBuilder; |
| 3 | +use std::{ |
| 4 | + env, |
| 5 | + fs::{read_to_string, write}, |
| 6 | + io::{stdout, Write}, |
| 7 | + path::Path, |
| 8 | + process::{self, Command, Stdio}, |
| 9 | +}; |
| 10 | + |
| 11 | +enum PubKind { |
| 12 | + Pub, |
| 13 | + Crate, |
| 14 | + Super, |
| 15 | + Private, |
| 16 | +} |
| 17 | + |
| 18 | +fn process(oracle_cmd: &str, p: &Path) -> u64 { |
| 19 | + let pub_regex = RegexBuilder::new("pub(?:\\s*\\(\\s*(.*?)\\s*\\))?") |
| 20 | + .multi_line(true) |
| 21 | + .build() |
| 22 | + .unwrap(); |
| 23 | + let mut cur_txt = read_to_string(p).unwrap(); |
| 24 | + let mut i = 0; |
| 25 | + let mut cl = pub_regex.capture_locations(); |
| 26 | + let mut num_changed = 0; |
| 27 | + while i < cur_txt.len() { |
| 28 | + print!("."); |
| 29 | + stdout().flush().ok(); |
| 30 | + let old_txt = cur_txt.clone(); |
| 31 | + let m = match pub_regex.captures_read_at(&mut cl, &old_txt, i) { |
| 32 | + Some(m) => m, |
| 33 | + None => break, |
| 34 | + }; |
| 35 | + let mut kind = if let Some((start, end)) = cl.get(1) { |
| 36 | + match &cur_txt[start..end] { |
| 37 | + "crate" => PubKind::Crate, |
| 38 | + "super" => PubKind::Super, |
| 39 | + _ => { |
| 40 | + // FIXME: this captures things we don't need to deal with (e.g. `pub(self)`), |
| 41 | + // things we could deal with (e.g. `pub(in ...)`) and random strings we've |
| 42 | + // accidentally picked up (e.g. `a pub(The Frog and Cow)`). We should probably |
| 43 | + // do something better with the middle class of thing than simply ignoring it. |
| 44 | + i = m.end(); |
| 45 | + continue; |
| 46 | + } |
| 47 | + } |
| 48 | + } else { |
| 49 | + PubKind::Pub |
| 50 | + }; |
| 51 | + let mut next_txt = cur_txt.clone(); |
| 52 | + let mut depubed = false; |
| 53 | + loop { |
| 54 | + let next_kind = match kind { |
| 55 | + PubKind::Pub => PubKind::Crate, |
| 56 | + PubKind::Crate => PubKind::Super, |
| 57 | + PubKind::Super => PubKind::Private, |
| 58 | + PubKind::Private => break, |
| 59 | + }; |
| 60 | + let mut try_txt = cur_txt[..m.start()].to_string(); |
| 61 | + let pub_txt = match next_kind { |
| 62 | + PubKind::Crate => "pub(crate) ", |
| 63 | + PubKind::Super => "pub(super) ", |
| 64 | + PubKind::Private => "", |
| 65 | + _ => unreachable!(), |
| 66 | + }; |
| 67 | + try_txt.push_str(&pub_txt); |
| 68 | + try_txt.push_str(&cur_txt[m.end()..]); |
| 69 | + write(p, &try_txt).unwrap(); |
| 70 | + match Command::new("sh") |
| 71 | + .arg("-c") |
| 72 | + .arg(oracle_cmd) |
| 73 | + .stderr(Stdio::null()) |
| 74 | + .stdout(Stdio::null()) |
| 75 | + .status() |
| 76 | + { |
| 77 | + Ok(s) if s.success() => { |
| 78 | + if !depubed { |
| 79 | + num_changed += 1; |
| 80 | + depubed = true; |
| 81 | + } |
| 82 | + next_txt = try_txt; |
| 83 | + } |
| 84 | + _ => break, |
| 85 | + } |
| 86 | + kind = next_kind; |
| 87 | + } |
| 88 | + cur_txt = next_txt; |
| 89 | + if cur_txt.len() >= old_txt.len() { |
| 90 | + i = m.end() + (cur_txt.len() - old_txt.len()); |
| 91 | + } else { |
| 92 | + i = m.end() - (old_txt.len() - cur_txt.len()); |
| 93 | + } |
| 94 | + } |
| 95 | + write(p, cur_txt).unwrap(); |
| 96 | + num_changed |
| 97 | +} |
| 98 | + |
| 99 | +fn progname() -> String { |
| 100 | + match env::current_exe() { |
| 101 | + Ok(p) => p |
| 102 | + .file_name() |
| 103 | + .map(|x| x.to_str().unwrap_or("depub")) |
| 104 | + .unwrap_or("depub") |
| 105 | + .to_owned(), |
| 106 | + Err(_) => "depub".to_owned(), |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/// Print out program usage then exit. This function must not be called after daemonisation. |
| 111 | +fn usage() -> ! { |
| 112 | + eprintln!( |
| 113 | + "Usage: {} -c <command> file_1 [... file_n]", |
| 114 | + progname = progname() |
| 115 | + ); |
| 116 | + process::exit(1) |
| 117 | +} |
| 118 | + |
| 119 | +fn main() { |
| 120 | + let matches = Options::new() |
| 121 | + .reqopt("c", "command", "Command to execute.", "string") |
| 122 | + .optflag("h", "help", "") |
| 123 | + .parse(env::args().skip(1)) |
| 124 | + .unwrap_or_else(|_| usage()); |
| 125 | + if matches.opt_present("h") || matches.free.is_empty() { |
| 126 | + usage(); |
| 127 | + } |
| 128 | + |
| 129 | + let oracle_cmd = matches.opt_str("c").unwrap(); |
| 130 | + let mut round = 1; |
| 131 | + loop { |
| 132 | + println!("===> Round {}", round); |
| 133 | + let mut changed = false; |
| 134 | + for p in &matches.free { |
| 135 | + print!("{}: ", p); |
| 136 | + stdout().flush().ok(); |
| 137 | + let num_changed = process(oracle_cmd.as_str(), &Path::new(&p)); |
| 138 | + if num_changed > 0 { |
| 139 | + print!(" ({} items depub'ed)", num_changed); |
| 140 | + changed = true; |
| 141 | + } |
| 142 | + println!(""); |
| 143 | + } |
| 144 | + if !changed { |
| 145 | + break; |
| 146 | + } |
| 147 | + round += 1; |
| 148 | + } |
| 149 | +} |
0 commit comments