2016-12-07 17:32:24 +02:00
|
|
|
use std::env;
|
2018-02-06 01:45:45 +02:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::{self, Read, Write};
|
|
|
|
use std::path::Path;
|
2018-02-03 17:41:36 +02:00
|
|
|
use std::process;
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
|
|
|
|
use clap::Shell;
|
|
|
|
|
2018-02-06 01:45:45 +02:00
|
|
|
use app::{RGArg, RGArgKind};
|
|
|
|
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[path = "src/app.rs"]
|
|
|
|
mod app;
|
|
|
|
|
|
|
|
fn main() {
|
2018-02-03 17:41:36 +02:00
|
|
|
// OUT_DIR is set by Cargo and it's where any additional build artifacts
|
|
|
|
// are written.
|
2016-12-07 17:32:24 +02:00
|
|
|
let outdir = match env::var_os("OUT_DIR") {
|
|
|
|
Some(outdir) => outdir,
|
2018-02-03 17:41:36 +02:00
|
|
|
None => {
|
|
|
|
eprintln!(
|
|
|
|
"OUT_DIR environment variable not defined. \
|
|
|
|
Please file a bug: \
|
|
|
|
https://github.com/BurntSushi/ripgrep/issues/new");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2016-12-07 17:32:24 +02:00
|
|
|
};
|
|
|
|
fs::create_dir_all(&outdir).unwrap();
|
2018-02-10 17:30:35 +02:00
|
|
|
|
|
|
|
let stamp_path = Path::new(&outdir).join("ripgrep-stamp");
|
|
|
|
if let Err(err) = File::create(&stamp_path) {
|
|
|
|
panic!("failed to write {}: {}", stamp_path.display(), err);
|
|
|
|
}
|
2018-02-06 01:45:45 +02:00
|
|
|
if let Err(err) = generate_man_page(&outdir) {
|
|
|
|
eprintln!("failed to generate man page: {}", err);
|
|
|
|
}
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
|
2018-02-03 17:41:36 +02:00
|
|
|
// Use clap to build completion files.
|
2017-04-05 07:14:55 +02:00
|
|
|
let mut app = app::app();
|
2016-12-07 17:32:24 +02:00
|
|
|
app.gen_completions("rg", Shell::Bash, &outdir);
|
|
|
|
app.gen_completions("rg", Shell::Fish, &outdir);
|
|
|
|
app.gen_completions("rg", Shell::PowerShell, &outdir);
|
2018-02-03 17:41:36 +02:00
|
|
|
// Note that we do not use clap's support for zsh. Instead, zsh completions
|
|
|
|
// are manually maintained in `complete/_rg`.
|
|
|
|
|
|
|
|
// Make the current git hash available to the build.
|
2018-02-06 01:45:45 +02:00
|
|
|
if let Some(rev) = git_revision_hash() {
|
|
|
|
println!("cargo:rustc-env=RIPGREP_BUILD_GIT_HASH={}", rev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn git_revision_hash() -> Option<String> {
|
2018-02-03 17:41:36 +02:00
|
|
|
let result = process::Command::new("git")
|
|
|
|
.args(&["rev-parse", "--short=10", "HEAD"])
|
|
|
|
.output();
|
2018-02-21 02:46:47 +02:00
|
|
|
result.ok().and_then(|output| {
|
|
|
|
let v = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
|
|
|
if v.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(v)
|
|
|
|
}
|
2018-02-06 01:45:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_man_page<P: AsRef<Path>>(outdir: P) -> io::Result<()> {
|
|
|
|
// If asciidoc isn't installed, then don't do anything.
|
|
|
|
if let Err(err) = process::Command::new("a2x").output() {
|
|
|
|
eprintln!("Could not run 'a2x' binary, skipping man page generation.");
|
|
|
|
eprintln!("Error from running 'a2x': {}", err);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-02-10 17:30:35 +02:00
|
|
|
// 1. Read asciidoc template.
|
|
|
|
// 2. Interpolate template with auto-generated docs.
|
|
|
|
// 3. Save interpolation to disk.
|
|
|
|
// 4. Use a2x (part of asciidoc) to convert to man page.
|
2018-02-06 01:45:45 +02:00
|
|
|
let outdir = outdir.as_ref();
|
|
|
|
let cwd = env::current_dir()?;
|
|
|
|
let tpl_path = cwd.join("doc").join("rg.1.txt.tpl");
|
|
|
|
let txt_path = outdir.join("rg.1.txt");
|
|
|
|
|
|
|
|
let mut tpl = String::new();
|
|
|
|
File::open(&tpl_path)?.read_to_string(&mut tpl)?;
|
|
|
|
tpl = tpl.replace("{OPTIONS}", &formatted_options()?);
|
|
|
|
|
|
|
|
let githash = git_revision_hash();
|
|
|
|
let githash = githash.as_ref().map(|x| &**x);
|
|
|
|
tpl = tpl.replace("{VERSION}", &app::long_version(githash));
|
|
|
|
|
|
|
|
File::create(&txt_path)?.write_all(tpl.as_bytes())?;
|
|
|
|
let result = process::Command::new("a2x")
|
|
|
|
.arg("--no-xmllint")
|
|
|
|
.arg("--doctype").arg("manpage")
|
|
|
|
.arg("--format").arg("manpage")
|
|
|
|
.arg(&txt_path)
|
|
|
|
.spawn()?
|
|
|
|
.wait()?;
|
|
|
|
if !result.success() {
|
|
|
|
let msg = format!("'a2x' failed with exit code {:?}", result.code());
|
|
|
|
return Err(ioerr(msg));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn formatted_options() -> io::Result<String> {
|
|
|
|
let mut args = app::all_args_and_flags();
|
|
|
|
args.sort_by(|x1, x2| x1.name.cmp(&x2.name));
|
|
|
|
|
|
|
|
let mut formatted = vec![];
|
|
|
|
for arg in args {
|
2018-02-07 01:26:23 +02:00
|
|
|
if arg.hidden {
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-06 01:45:45 +02:00
|
|
|
// ripgrep only has two positional arguments, and probably will only
|
|
|
|
// ever have two positional arguments, so we just hardcode them into
|
|
|
|
// the template.
|
|
|
|
if let app::RGArgKind::Positional{..} = arg.kind {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
formatted.push(formatted_arg(&arg)?);
|
2018-02-03 17:41:36 +02:00
|
|
|
}
|
2018-02-06 01:45:45 +02:00
|
|
|
Ok(formatted.join("\n\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn formatted_arg(arg: &RGArg) -> io::Result<String> {
|
|
|
|
match arg.kind {
|
|
|
|
RGArgKind::Positional{..} => panic!("unexpected positional argument"),
|
|
|
|
RGArgKind::Switch { long, short, multiple } => {
|
|
|
|
let mut out = vec![];
|
|
|
|
|
|
|
|
let mut header = format!("--{}", long);
|
|
|
|
if let Some(short) = short {
|
|
|
|
header = format!("-{}, {}", short, header);
|
|
|
|
}
|
|
|
|
if multiple {
|
|
|
|
header = format!("*{}* ...::", header);
|
|
|
|
} else {
|
|
|
|
header = format!("*{}*::", header);
|
|
|
|
}
|
|
|
|
writeln!(out, "{}", header)?;
|
|
|
|
writeln!(out, "{}", formatted_doc_txt(arg)?)?;
|
|
|
|
|
|
|
|
Ok(String::from_utf8(out).unwrap())
|
|
|
|
}
|
|
|
|
RGArgKind::Flag { long, short, value_name, multiple, .. } => {
|
|
|
|
let mut out = vec![];
|
|
|
|
|
|
|
|
let mut header = format!("--{}", long);
|
|
|
|
if let Some(short) = short {
|
|
|
|
header = format!("-{}, {}", short, header);
|
|
|
|
}
|
|
|
|
if multiple {
|
|
|
|
header = format!("*{}* _{}_ ...::", header, value_name);
|
|
|
|
} else {
|
|
|
|
header = format!("*{}* _{}_::", header, value_name);
|
|
|
|
}
|
|
|
|
writeln!(out, "{}", header)?;
|
|
|
|
writeln!(out, "{}", formatted_doc_txt(arg)?)?;
|
|
|
|
|
|
|
|
Ok(String::from_utf8(out).unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn formatted_doc_txt(arg: &RGArg) -> io::Result<String> {
|
2018-11-06 13:57:16 +02:00
|
|
|
let paragraphs: Vec<String> = arg.doc_long
|
|
|
|
.replace("{", "{")
|
|
|
|
.replace("}", r"}")
|
|
|
|
.split("\n\n")
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect();
|
2018-02-06 01:45:45 +02:00
|
|
|
if paragraphs.is_empty() {
|
|
|
|
return Err(ioerr(format!("missing docs for --{}", arg.name)));
|
|
|
|
}
|
|
|
|
let first = format!(" {}", paragraphs[0].replace("\n", "\n "));
|
|
|
|
if paragraphs.len() == 1 {
|
|
|
|
return Ok(first);
|
|
|
|
}
|
|
|
|
Ok(format!("{}\n+\n{}", first, paragraphs[1..].join("\n+\n")))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ioerr(msg: String) -> io::Error {
|
|
|
|
io::Error::new(io::ErrorKind::Other, msg)
|
Switch from Docopt to Clap.
There were two important reasons for the switch:
1. Performance. Docopt does poorly when the argv becomes large, which is
a reasonable common use case for search tools. (e.g., use with xargs)
2. Better failure modes. Clap knows a lot more about how a particular
argv might be invalid, and can therefore provide much clearer error
messages.
While both were important, (1) made it urgent.
Note that since Clap requires at least Rust 1.11, this will in turn
increase the minimum Rust version supported by ripgrep from Rust 1.9 to
Rust 1.11. It is therefore a breaking change, so the soonest release of
ripgrep with Clap will have to be 0.3.
There is also at least one subtle breaking change in real usage.
Previous to this commit, this used to work:
rg -e -foo
Where this would cause ripgrep to search for the string `-foo`. Clap
currently has problems supporting this use case
(see: https://github.com/kbknapp/clap-rs/issues/742),
but it can be worked around by using this instead:
rg -e [-]foo
or even
rg [-]foo
and this still works:
rg -- -foo
This commit also adds Bash, Fish and PowerShell completion files to the
release, fixes a bug that prevented ripgrep from working on file
paths containing invalid UTF-8 and shows short descriptions in the
output of `-h` but longer descriptions in the output of `--help`.
Fixes #136, Fixes #189, Fixes #210, Fixes #230
2016-11-13 04:48:11 +02:00
|
|
|
}
|