mirror of
https://github.com/FliegendeWurst/ripgrep-all.git
synced 2024-11-24 12:24:56 +00:00
add lots of debug logs, perf timings
This commit is contained in:
parent
5410da1133
commit
7b70188d77
@ -1,3 +1,11 @@
|
|||||||
|
# 0.9.7 (unreleased)
|
||||||
|
|
||||||
|
- auto generate parts of the readme
|
||||||
|
- add loads of debug logs and performance timings when `--debug` is used
|
||||||
|
- better error messages via `anyhow`
|
||||||
|
- add cross-platform rga-fzf binary
|
||||||
|
- add a config file including schema
|
||||||
|
|
||||||
# 0.9.6 (2020-05-19)
|
# 0.9.6 (2020-05-19)
|
||||||
|
|
||||||
- Fix windows builds
|
- Fix windows builds
|
||||||
|
@ -1,16 +1,8 @@
|
|||||||
use super::{spawning::SpawningFileAdapter, AdapterMeta, GetMetadata};
|
use super::{spawning::SpawningFileAdapter, AdapterMeta, GetMetadata};
|
||||||
use crate::{
|
use crate::matching::{FastMatcher, SlowMatcher};
|
||||||
matching::{FastMatcher, SlowMatcher},
|
|
||||||
project_dirs,
|
|
||||||
};
|
|
||||||
use anyhow::*;
|
|
||||||
use derive_more::FromStr;
|
|
||||||
use log::*;
|
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::ffi::OsString;
|
|
||||||
use std::{fs::File, io::Write, iter::IntoIterator, str::FromStr};
|
|
||||||
use structopt::StructOpt;
|
|
||||||
|
|
||||||
// mostly the same as AdapterMeta + SpawningFileAdapter
|
// mostly the same as AdapterMeta + SpawningFileAdapter
|
||||||
#[derive(Debug, Deserialize, Serialize, JsonSchema, Default, PartialEq, Clone)]
|
#[derive(Debug, Deserialize, Serialize, JsonSchema, Default, PartialEq, Clone)]
|
||||||
@ -51,7 +43,7 @@ impl SpawningFileAdapter for CustomSpawningFileAdapter {
|
|||||||
}
|
}
|
||||||
fn command(
|
fn command(
|
||||||
&self,
|
&self,
|
||||||
filepath_hint: &std::path::Path,
|
_filepath_hint: &std::path::Path,
|
||||||
mut command: std::process::Command,
|
mut command: std::process::Command,
|
||||||
) -> std::process::Command {
|
) -> std::process::Command {
|
||||||
command.args(&self.args);
|
command.args(&self.args);
|
||||||
|
103
src/args.rs
103
src/args.rs
@ -205,9 +205,6 @@ pub struct RgaConfig {
|
|||||||
#[structopt(long, help = "Show version of ripgrep itself")]
|
#[structopt(long, help = "Show version of ripgrep itself")]
|
||||||
pub rg_version: bool,
|
pub rg_version: bool,
|
||||||
}
|
}
|
||||||
fn default_schema_path() -> String {
|
|
||||||
"./config.schema.json".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
static RGA_CONFIG: &str = "RGA_CONFIG";
|
static RGA_CONFIG: &str = "RGA_CONFIG";
|
||||||
|
|
||||||
@ -225,42 +222,29 @@ fn json_merge(a: &mut Value, b: &Value) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: this function is pretty inefficient. loads of json / copying stuff
|
fn read_config_file() -> Result<(String, Value)> {
|
||||||
pub fn parse_args<I>(args: I) -> Result<RgaConfig>
|
|
||||||
where
|
|
||||||
I: IntoIterator,
|
|
||||||
I::Item: Into<OsString> + Clone,
|
|
||||||
{
|
|
||||||
let proj = project_dirs()?;
|
let proj = project_dirs()?;
|
||||||
let config_dir = proj.config_dir();
|
let config_dir = proj.config_dir();
|
||||||
let config_filename = config_dir.join("config.json");
|
let config_filename = config_dir.join("config.json");
|
||||||
// TODO: don't read config file in rga-preproc for performance (called for every file)
|
let config_filename_str = config_filename.to_string_lossy().into_owned();
|
||||||
let config_file_config = {
|
|
||||||
if config_filename.exists() {
|
if config_filename.exists() {
|
||||||
let config_file_contents =
|
let config_file_contents = std::fs::read_to_string(config_filename)
|
||||||
std::fs::read_to_string(&config_filename).with_context(|| {
|
.with_context(|| format!("Could not read config file json {}", config_filename_str))?;
|
||||||
format!(
|
|
||||||
"Could not read config file json {}",
|
|
||||||
config_filename.to_string_lossy()
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
{
|
{
|
||||||
// just for error messages
|
// just for error messages
|
||||||
let config_json: RgaConfig = serde_json::from_str(&config_file_contents)
|
serde_json::from_str(&config_file_contents)
|
||||||
.with_context(|| format!("Error in config file: {}", config_file_contents))?;
|
.with_context(|| format!("Error in config file: {}", config_file_contents))?;
|
||||||
}
|
}
|
||||||
let config_json: serde_json::Value = serde_json::from_str(&config_file_contents)
|
let config_json: serde_json::Value =
|
||||||
.context("Could not parse config json")?;
|
serde_json::from_str(&config_file_contents).context("Could not parse config json")?;
|
||||||
log::debug!("Config JSON: {}", config_json.to_string());
|
Ok((config_filename_str, config_json))
|
||||||
config_json
|
|
||||||
} else {
|
} else {
|
||||||
// write default config
|
// write default config
|
||||||
std::fs::create_dir_all(config_dir)?;
|
std::fs::create_dir_all(config_dir)?;
|
||||||
let mut schemafile = File::create(config_dir.join("config.schema.json"))?;
|
let mut schemafile = File::create(config_dir.join("config.schema.json"))?;
|
||||||
|
|
||||||
schemafile.write(
|
schemafile
|
||||||
serde_json::to_string_pretty(&schemars::schema_for!(RgaConfig))?.as_bytes(),
|
.write(serde_json::to_string_pretty(&schemars::schema_for!(RgaConfig))?.as_bytes())?;
|
||||||
)?;
|
|
||||||
|
|
||||||
let mut config_json = serde_json::to_value(&RgaConfig::default())?;
|
let mut config_json = serde_json::to_value(&RgaConfig::default())?;
|
||||||
match &mut config_json {
|
match &mut config_json {
|
||||||
@ -274,43 +258,56 @@ where
|
|||||||
}
|
}
|
||||||
let mut configfile = File::create(config_dir.join("config.json"))?;
|
let mut configfile = File::create(config_dir.join("config.json"))?;
|
||||||
configfile.write(serde_json::to_string_pretty(&config_json)?.as_bytes())?;
|
configfile.write(serde_json::to_string_pretty(&config_json)?.as_bytes())?;
|
||||||
config_json
|
Ok((config_filename_str, config_json))
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
let env_var_config = {
|
fn read_config_env() -> Result<Value> {
|
||||||
let val = std::env::var(RGA_CONFIG).ok();
|
let val = std::env::var(RGA_CONFIG).ok();
|
||||||
if let Some(val) = val {
|
if let Some(val) = val {
|
||||||
serde_json::from_str(&val).context("could not parse config from env RGA_CONFIG")?
|
serde_json::from_str(&val).context("could not parse config from env RGA_CONFIG")
|
||||||
} else {
|
} else {
|
||||||
serde_json::to_value(&RgaConfig::default())?
|
serde_json::to_value(&RgaConfig::default()).context("could not create default config")
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
pub fn parse_args<I>(args: I, is_rga_preproc: bool) -> Result<RgaConfig>
|
||||||
|
where
|
||||||
|
I: IntoIterator,
|
||||||
|
I::Item: Into<OsString> + Clone,
|
||||||
|
{
|
||||||
|
// TODO: don't read config file in rga-preproc for performance (called for every file)
|
||||||
|
|
||||||
let arg_matches = RgaConfig::from_iter(args);
|
let arg_matches = RgaConfig::from_iter(args);
|
||||||
let args_config = {
|
let args_config = serde_json::to_value(&arg_matches)?;
|
||||||
let serialized_config = serde_json::to_value(&arg_matches)?;
|
|
||||||
|
|
||||||
serialized_config
|
let merged_config = {
|
||||||
};
|
if is_rga_preproc {
|
||||||
|
// only read from env and args
|
||||||
log::debug!(
|
let mut merged_config = read_config_env()?;
|
||||||
"Configs:\n{}: {}\n{}: {}\nArgs: {}",
|
json_merge(&mut merged_config, &args_config);
|
||||||
config_filename.to_string_lossy(),
|
log::debug!("Config: {}", serde_json::to_string(&merged_config)?);
|
||||||
serde_json::to_string_pretty(&config_file_config)?,
|
merged_config
|
||||||
RGA_CONFIG,
|
} else {
|
||||||
serde_json::to_string_pretty(&env_var_config)?,
|
// read from config file, env and args
|
||||||
serde_json::to_string_pretty(&args_config)?
|
let env_var_config = read_config_env()?;
|
||||||
);
|
let (config_filename, config_file_config) = read_config_file()?;
|
||||||
let mut merged_config = config_file_config.clone();
|
let mut merged_config = config_file_config.clone();
|
||||||
json_merge(&mut merged_config, &env_var_config);
|
json_merge(&mut merged_config, &env_var_config);
|
||||||
json_merge(&mut merged_config, &args_config);
|
json_merge(&mut merged_config, &args_config);
|
||||||
|
|
||||||
// pass to child processes
|
|
||||||
std::env::set_var(RGA_CONFIG, &merged_config.to_string());
|
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"Merged config: {}",
|
"Configs:\n{}: {}\n{}: {}\nArgs: {}\nMerged: {}",
|
||||||
|
config_filename,
|
||||||
|
serde_json::to_string_pretty(&config_file_config)?,
|
||||||
|
RGA_CONFIG,
|
||||||
|
serde_json::to_string_pretty(&env_var_config)?,
|
||||||
|
serde_json::to_string_pretty(&args_config)?,
|
||||||
serde_json::to_string_pretty(&merged_config)?
|
serde_json::to_string_pretty(&merged_config)?
|
||||||
);
|
);
|
||||||
|
// pass to child processes
|
||||||
|
std::env::set_var(RGA_CONFIG, &merged_config.to_string());
|
||||||
|
merged_config
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let mut res: RgaConfig = serde_json::from_value(merged_config.clone())
|
let mut res: RgaConfig = serde_json::from_value(merged_config.clone())
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
@ -334,7 +331,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Split arguments into the ones we care about and the ones rg cares about
|
/// Split arguments into the ones we care about and the ones rg cares about
|
||||||
pub fn split_args() -> Result<(RgaConfig, Vec<OsString>)> {
|
pub fn split_args(is_rga_preproc: bool) -> Result<(RgaConfig, Vec<OsString>)> {
|
||||||
let mut app = RgaConfig::clap();
|
let mut app = RgaConfig::clap();
|
||||||
|
|
||||||
app.p.create_help_and_version();
|
app.p.create_help_and_version();
|
||||||
@ -359,14 +356,14 @@ pub fn split_args() -> Result<(RgaConfig, Vec<OsString>)> {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
debug!("our_args: {:?}", our_args);
|
debug!("rga (our) args: {:?}", our_args);
|
||||||
let matches = parse_args(our_args).context("Could not parse args")?;
|
let matches = parse_args(our_args, is_rga_preproc).context("Could not parse args")?;
|
||||||
if matches.rg_help {
|
if matches.rg_help {
|
||||||
passthrough_args.insert(0, "--help".into());
|
passthrough_args.insert(0, "--help".into());
|
||||||
}
|
}
|
||||||
if matches.rg_version {
|
if matches.rg_version {
|
||||||
passthrough_args.insert(0, "--version".into());
|
passthrough_args.insert(0, "--version".into());
|
||||||
}
|
}
|
||||||
debug!("passthrough_args: {:?}", passthrough_args);
|
debug!("rga (passthrough) args: {:?}", passthrough_args);
|
||||||
Ok((matches, passthrough_args))
|
Ok((matches, passthrough_args))
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use rga::adapters::spawning::map_exe_error;
|
|
||||||
use ripgrep_all as rga;
|
|
||||||
|
|
||||||
use std::process::{Command, Stdio};
|
use std::process::Command;
|
||||||
|
|
||||||
// TODO: add --rg-params=..., --rg-preview-params=... and --fzf-params=... params
|
// TODO: add --rg-params=..., --rg-preview-params=... and --fzf-params=... params
|
||||||
// TODO: remove passthrough_args
|
// TODO: remove passthrough_args
|
||||||
|
@ -9,7 +9,7 @@ fn main() -> anyhow::Result<()> {
|
|||||||
env_logger::init();
|
env_logger::init();
|
||||||
let mut arg_arr: Vec<std::ffi::OsString> = std::env::args_os().collect();
|
let mut arg_arr: Vec<std::ffi::OsString> = std::env::args_os().collect();
|
||||||
let last = arg_arr.pop().expect("No filename specified");
|
let last = arg_arr.pop().expect("No filename specified");
|
||||||
let args = rga::args::parse_args(arg_arr)?;
|
let args = rga::args::parse_args(arg_arr, true)?;
|
||||||
//clap::App::new("rga-preproc").arg(Arg::from_usage())
|
//clap::App::new("rga-preproc").arg(Arg::from_usage())
|
||||||
let path = {
|
let path = {
|
||||||
let filepath = last;
|
let filepath = last;
|
||||||
|
@ -9,7 +9,7 @@ use structopt::StructOpt;
|
|||||||
|
|
||||||
use schemars::schema_for;
|
use schemars::schema_for;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::Instant;
|
||||||
|
|
||||||
fn list_adapters(args: RgaConfig) -> Result<()> {
|
fn list_adapters(args: RgaConfig) -> Result<()> {
|
||||||
let (enabled_adapters, disabled_adapters) = get_all_adapters(args.custom_adapters.clone());
|
let (enabled_adapters, disabled_adapters) = get_all_adapters(args.custom_adapters.clone());
|
||||||
@ -60,9 +60,14 @@ fn list_adapters(args: RgaConfig) -> Result<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
|
// set debugging as early as possible
|
||||||
|
if std::env::args().position(|e| e == "--debug").is_some() {
|
||||||
|
std::env::set_var("RUST_LOG", "debug");
|
||||||
|
}
|
||||||
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let (args, mut passthrough_args) = split_args()?;
|
let (args, mut passthrough_args) = split_args(false)?;
|
||||||
|
|
||||||
if args.print_config_schema {
|
if args.print_config_schema {
|
||||||
println!("{}", serde_json::to_string_pretty(&schema_for!(RgaConfig))?);
|
println!("{}", serde_json::to_string_pretty(&schema_for!(RgaConfig))?);
|
||||||
|
Loading…
Reference in New Issue
Block a user