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