2019-06-07 13:56:04 +00:00
|
|
|
use failure::Fallible;
|
2019-06-06 10:31:18 +00:00
|
|
|
use log::*;
|
2019-06-07 13:56:04 +00:00
|
|
|
use rga::adapters::spawning::map_exe_error;
|
2019-06-05 19:28:35 +00:00
|
|
|
use rga::adapters::*;
|
2019-06-07 19:46:03 +00:00
|
|
|
use rga::args::*;
|
2019-06-07 19:46:17 +00:00
|
|
|
|
2019-06-06 10:31:18 +00:00
|
|
|
use std::ffi::OsString;
|
2019-06-04 18:08:26 +00:00
|
|
|
use std::process::Command;
|
2019-06-07 19:46:03 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2019-06-07 21:04:18 +00:00
|
|
|
fn split_args() -> Fallible<(RgaArgs, Vec<OsString>)> {
|
|
|
|
let mut app = RgaArgs::clap();
|
2019-06-04 18:08:26 +00:00
|
|
|
|
2019-06-06 10:31:18 +00:00
|
|
|
app.p.create_help_and_version();
|
|
|
|
let mut firstarg = true;
|
2019-06-07 19:46:03 +00:00
|
|
|
// debug!("{:#?}", app.p.flags);
|
2019-06-06 10:31:18 +00:00
|
|
|
let (our_args, mut passthrough_args): (Vec<OsString>, Vec<OsString>) = std::env::args_os()
|
|
|
|
.partition(|os_arg| {
|
|
|
|
if firstarg {
|
|
|
|
// hacky, but .enumerate() would be ugly because partition is too simplistic
|
|
|
|
firstarg = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if let Some(arg) = os_arg.to_str() {
|
|
|
|
for flag in app.p.flags() {
|
|
|
|
if let Some(s) = flag.s.short {
|
|
|
|
if arg == format!("-{}", s) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(l) = flag.s.long {
|
|
|
|
if arg == format!("--{}", l) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// println!("{}", flag.s.long);
|
|
|
|
}
|
|
|
|
for opt in app.p.opts() {
|
|
|
|
// only parse --x=... for now
|
|
|
|
if let Some(l) = opt.s.long {
|
2019-06-07 22:04:48 +00:00
|
|
|
if arg.starts_with(&format!("--{}", l)) {
|
2019-06-06 10:31:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
});
|
|
|
|
debug!("our_args: {:?}", our_args);
|
2019-06-07 19:46:03 +00:00
|
|
|
let matches = parse_args(our_args)?;
|
|
|
|
if matches.rg_help {
|
2019-06-06 10:31:18 +00:00
|
|
|
passthrough_args.insert(0, "--help".into());
|
|
|
|
}
|
2019-06-07 19:46:03 +00:00
|
|
|
if matches.rg_version {
|
2019-06-06 10:31:18 +00:00
|
|
|
passthrough_args.insert(0, "--version".into());
|
|
|
|
}
|
|
|
|
debug!("passthrough_args: {:?}", passthrough_args);
|
2019-06-07 19:46:03 +00:00
|
|
|
Ok((matches, passthrough_args))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Fallible<()> {
|
|
|
|
env_logger::init();
|
2019-06-06 10:31:18 +00:00
|
|
|
|
2019-06-07 19:46:03 +00:00
|
|
|
let (args, passthrough_args) = split_args()?;
|
2019-06-07 22:04:48 +00:00
|
|
|
let adapters = get_adapters_filtered(&args.rga_adapters)?;
|
2019-06-05 19:28:35 +00:00
|
|
|
|
2019-06-07 21:04:18 +00:00
|
|
|
if args.rga_list_adapters {
|
2019-06-07 22:04:48 +00:00
|
|
|
println!("Adapters:\n");
|
2019-06-06 10:31:18 +00:00
|
|
|
for adapter in adapters {
|
|
|
|
let meta = adapter.metadata();
|
2019-06-06 12:55:27 +00:00
|
|
|
let matchers = meta
|
|
|
|
.matchers
|
|
|
|
.iter()
|
|
|
|
.map(|m| match m {
|
|
|
|
Matcher::FileExtension(ext) => format!(".{}", ext),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ");
|
2019-06-07 22:04:48 +00:00
|
|
|
print!(
|
|
|
|
" - {}\n {}\n Extensions: {}\n",
|
|
|
|
meta.name, meta.description, matchers
|
|
|
|
);
|
2019-06-06 12:55:27 +00:00
|
|
|
println!("");
|
2019-06-06 10:31:18 +00:00
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-06-05 19:28:35 +00:00
|
|
|
let extensions = adapters
|
|
|
|
.iter()
|
|
|
|
.flat_map(|a| &a.metadata().matchers)
|
|
|
|
.filter_map(|m| match m {
|
|
|
|
Matcher::FileExtension(ext) => Some(ext as &str),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(",");
|
|
|
|
|
2019-06-04 18:08:26 +00:00
|
|
|
let exe = std::env::current_exe().expect("Could not get executable location");
|
|
|
|
let preproc_exe = exe.with_file_name("rga-preproc");
|
|
|
|
let mut child = Command::new("rg")
|
2019-06-05 19:28:35 +00:00
|
|
|
.arg("--no-line-number")
|
2019-06-04 18:08:26 +00:00
|
|
|
.arg("--pre")
|
|
|
|
.arg(preproc_exe)
|
2019-06-05 19:28:35 +00:00
|
|
|
.arg("--pre-glob")
|
|
|
|
.arg(format!("*.{{{}}}", extensions))
|
2019-06-06 10:31:18 +00:00
|
|
|
.args(passthrough_args)
|
2019-06-07 13:56:04 +00:00
|
|
|
.spawn()
|
|
|
|
.map_err(|e| map_exe_error(e, "rg", "Please make sure you have ripgrep installed."))?;
|
2019-06-04 18:08:26 +00:00
|
|
|
|
|
|
|
child.wait()?;
|
|
|
|
Ok(())
|
|
|
|
}
|