mirror of
https://github.com/Noratrieb/cargo-minimize.git
synced 2026-01-14 16:35:01 +01:00
wow
This commit is contained in:
parent
d9f3f347e9
commit
002bad34ae
5 changed files with 96 additions and 40 deletions
67
src/build.rs
67
src/build.rs
|
|
@ -1,45 +1,64 @@
|
|||
use anyhow::{Context, Result};
|
||||
use std::{fmt::Display, path::PathBuf};
|
||||
|
||||
use crate::Options;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Build {
|
||||
cargo: bool,
|
||||
script_path: Option<PathBuf>,
|
||||
mode: BuildMode,
|
||||
input_path: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum BuildMode {
|
||||
Cargo,
|
||||
Script(PathBuf),
|
||||
Rustc,
|
||||
}
|
||||
|
||||
impl Build {
|
||||
pub fn new(cargo: bool, script_path: Option<PathBuf>, input_path: PathBuf) -> Self {
|
||||
pub fn new(options: &Options) -> Self {
|
||||
let mode = if options.cargo {
|
||||
BuildMode::Cargo
|
||||
} else if let Some(script) = &options.verify_error_path {
|
||||
BuildMode::Script(script.clone())
|
||||
} else {
|
||||
BuildMode::Rustc
|
||||
};
|
||||
Self {
|
||||
cargo,
|
||||
script_path,
|
||||
input_path,
|
||||
mode,
|
||||
input_path: options.path.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(&self) -> Result<BuildResult> {
|
||||
let reproduces_issue = if self.cargo {
|
||||
let mut cmd = std::process::Command::new("cargo");
|
||||
cmd.arg("build");
|
||||
let reproduces_issue = match &self.mode {
|
||||
BuildMode::Cargo => {
|
||||
let mut cmd = std::process::Command::new("cargo");
|
||||
cmd.arg("build");
|
||||
|
||||
let output =
|
||||
String::from_utf8(cmd.output().context("spawning rustc process")?.stderr).unwrap();
|
||||
let output =
|
||||
String::from_utf8(cmd.output().context("spawning rustc process")?.stderr)
|
||||
.unwrap();
|
||||
|
||||
output.contains("internal compiler error")
|
||||
} else if let Some(script_path) = &self.script_path {
|
||||
let mut cmd = std::process::Command::new(script_path);
|
||||
output.contains("internal compiler error")
|
||||
}
|
||||
BuildMode::Script(script_path) => {
|
||||
let mut cmd = std::process::Command::new(script_path);
|
||||
|
||||
cmd.output().context("spawning script")?.status.success()
|
||||
} else {
|
||||
let mut cmd = std::process::Command::new("rustc");
|
||||
cmd.args(["--edition", "2018"]);
|
||||
cmd.arg(&self.input_path);
|
||||
cmd.output().context("spawning script")?.status.success()
|
||||
}
|
||||
BuildMode::Rustc => {
|
||||
let mut cmd = std::process::Command::new("rustc");
|
||||
cmd.args(["--edition", "2018"]);
|
||||
cmd.arg(&self.input_path);
|
||||
|
||||
cmd.output()
|
||||
.context("spawning rustc process")?
|
||||
.status
|
||||
.code()
|
||||
== Some(101)
|
||||
cmd.output()
|
||||
.context("spawning rustc process")?
|
||||
.status
|
||||
.code()
|
||||
== Some(101)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(BuildResult { reproduces_issue })
|
||||
|
|
|
|||
|
|
@ -1,22 +1,28 @@
|
|||
use quote::ToTokens;
|
||||
use syn::{parse_quote, visit_mut::VisitMut};
|
||||
|
||||
use crate::processor::Processor;
|
||||
use crate::processor::{ProcessChecker, Processor};
|
||||
|
||||
struct Visitor<'a> {
|
||||
current_path: Vec<String>,
|
||||
checker: &'a mut ProcessChecker,
|
||||
|
||||
struct Visitor {
|
||||
loop_expr: syn::Block,
|
||||
has_made_change: bool,
|
||||
}
|
||||
|
||||
impl Visitor {
|
||||
fn new() -> Self {
|
||||
impl<'a> Visitor<'a> {
|
||||
fn new(checker: &'a mut ProcessChecker) -> Self {
|
||||
Self {
|
||||
current_path: Vec::new(),
|
||||
checker,
|
||||
has_made_change: false,
|
||||
loop_expr: parse_quote! { { loop {} } },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitMut for Visitor {
|
||||
impl VisitMut for Visitor<'_> {
|
||||
fn visit_block_mut(&mut self, block: &mut syn::Block) {
|
||||
match block.stmts.as_slice() {
|
||||
[syn::Stmt::Expr(syn::Expr::Loop(syn::ExprLoop {
|
||||
|
|
@ -28,14 +34,39 @@ impl VisitMut for Visitor {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_item_fn_mut(&mut self, func: &mut syn::ItemFn) {
|
||||
self.current_path.push(func.sig.ident.to_string());
|
||||
syn::visit_mut::visit_item_fn_mut(self, func);
|
||||
self.current_path.pop();
|
||||
}
|
||||
|
||||
fn visit_impl_item_method_mut(&mut self, method: &mut syn::ImplItemMethod) {
|
||||
self.current_path.push(method.sig.ident.to_string());
|
||||
syn::visit_mut::visit_impl_item_method_mut(self, method);
|
||||
self.current_path.pop();
|
||||
}
|
||||
|
||||
fn visit_item_impl_mut(&mut self, item: &mut syn::ItemImpl) {
|
||||
self.current_path
|
||||
.push(item.self_ty.clone().into_token_stream().to_string());
|
||||
syn::visit_mut::visit_item_impl_mut(self, item);
|
||||
self.current_path.pop();
|
||||
}
|
||||
|
||||
fn visit_item_mod_mut(&mut self, module: &mut syn::ItemMod) {
|
||||
self.current_path.push(module.ident.to_string());
|
||||
syn::visit_mut::visit_item_mod_mut(self, module);
|
||||
self.current_path.pop();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EverybodyLoops;
|
||||
|
||||
impl Processor for EverybodyLoops {
|
||||
fn process_file(&mut self, krate: &mut syn::File) -> bool {
|
||||
let mut visitor = Visitor::new();
|
||||
fn process_file(&mut self, krate: &mut syn::File, checker: &mut ProcessChecker) -> bool {
|
||||
let mut visitor = Visitor::new(checker);
|
||||
visitor.visit_file_mut(krate);
|
||||
visitor.has_made_change
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ use anyhow::Result;
|
|||
use clap::Parser;
|
||||
use processor::Minimizer;
|
||||
|
||||
use crate::{everybody_loops::EverybodyLoops, privatize::Privarize, processor::Processor};
|
||||
use crate::{everybody_loops::EverybodyLoops, processor::Processor};
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
pub struct Options {
|
||||
|
|
@ -26,11 +26,9 @@ pub struct Options {
|
|||
pub fn minimize() -> Result<()> {
|
||||
let options = Options::parse();
|
||||
|
||||
let dir = options.path;
|
||||
let build = build::Build::new(&options);
|
||||
|
||||
let build = build::Build::new(options.cargo, options.verify_error_path, dir.clone());
|
||||
|
||||
let mut minimizer = Minimizer::new_glob_dir(&dir, build);
|
||||
let mut minimizer = Minimizer::new_glob_dir(&options.path, build);
|
||||
|
||||
println!("{minimizer:?}");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use syn::{parse_quote, visit_mut::VisitMut, Visibility};
|
||||
|
||||
use crate::processor::Processor;
|
||||
use crate::processor::{ProcessChecker, Processor};
|
||||
|
||||
struct Visitor {
|
||||
pub_crate: Visibility,
|
||||
|
|
@ -29,7 +29,7 @@ impl VisitMut for Visitor {
|
|||
pub struct Privarize {}
|
||||
|
||||
impl Processor for Privarize {
|
||||
fn process_file(&mut self, krate: &mut syn::File) -> bool {
|
||||
fn process_file(&mut self, krate: &mut syn::File, _: &mut ProcessChecker) -> bool {
|
||||
let mut visitor = Visitor::new();
|
||||
visitor.visit_file_mut(krate);
|
||||
visitor.has_made_change
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use anyhow::{ensure, Context, Result};
|
|||
use crate::build::Build;
|
||||
|
||||
pub trait Processor {
|
||||
fn process_file(&mut self, krate: &mut syn::File) -> bool;
|
||||
fn process_file(&mut self, krate: &mut syn::File, checker: &mut ProcessChecker) -> bool;
|
||||
|
||||
fn name(&self) -> &'static str;
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ impl Minimizer {
|
|||
let mut krate = syn::parse_file(&before_string)
|
||||
.with_context(|| format!("parsing file {file_display}"))?;
|
||||
|
||||
let has_made_change = pass.process_file(&mut krate);
|
||||
let has_made_change = pass.process_file(&mut krate, &mut ProcessChecker {});
|
||||
|
||||
if has_made_change {
|
||||
let result = prettyplease::unparse(&krate);
|
||||
|
|
@ -95,3 +95,11 @@ impl Minimizer {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProcessChecker {}
|
||||
|
||||
impl ProcessChecker {
|
||||
pub fn can_process(&mut self, _: &[String]) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue