mirror of
https://github.com/Noratrieb/cargo-minimize.git
synced 2026-01-14 16:35:01 +01:00
make things cool and good and wow
This commit is contained in:
parent
958383f991
commit
d9f3f347e9
8 changed files with 486 additions and 41 deletions
54
src/build.rs
54
src/build.rs
|
|
@ -1,28 +1,60 @@
|
|||
use anyhow::{Context, Result};
|
||||
use std::path::PathBuf;
|
||||
use std::{fmt::Display, path::PathBuf};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Build {
|
||||
path: PathBuf,
|
||||
cargo: bool,
|
||||
script_path: Option<PathBuf>,
|
||||
input_path: PathBuf,
|
||||
}
|
||||
|
||||
impl Build {
|
||||
pub fn new(path: impl Into<PathBuf>) -> Self {
|
||||
Self { path: path.into() }
|
||||
pub fn new(cargo: bool, script_path: Option<PathBuf>, input_path: PathBuf) -> Self {
|
||||
Self {
|
||||
cargo,
|
||||
script_path,
|
||||
input_path,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build(&self) -> Result<BuildResult> {
|
||||
let mut cmd = std::process::Command::new("cargo");
|
||||
let reproduces_issue = if self.cargo {
|
||||
let mut cmd = std::process::Command::new("cargo");
|
||||
cmd.arg("build");
|
||||
|
||||
cmd.current_dir(&self.path).arg("build");
|
||||
let output =
|
||||
String::from_utf8(cmd.output().context("spawning rustc process")?.stderr).unwrap();
|
||||
|
||||
let output = cmd.output().context("spawning cargo")?;
|
||||
output.contains("internal compiler error")
|
||||
} else if let Some(script_path) = &self.script_path {
|
||||
let mut cmd = std::process::Command::new(script_path);
|
||||
|
||||
Ok(BuildResult {
|
||||
success: output.status.success(),
|
||||
})
|
||||
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 rustc process")?
|
||||
.status
|
||||
.code()
|
||||
== Some(101)
|
||||
};
|
||||
|
||||
Ok(BuildResult { reproduces_issue })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BuildResult {
|
||||
pub success: bool,
|
||||
pub reproduces_issue: bool,
|
||||
}
|
||||
|
||||
impl Display for BuildResult {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self.reproduces_issue {
|
||||
true => f.write_str("yes"),
|
||||
false => f.write_str("no"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
46
src/everybody_loops.rs
Normal file
46
src/everybody_loops.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use syn::{parse_quote, visit_mut::VisitMut};
|
||||
|
||||
use crate::processor::Processor;
|
||||
|
||||
struct Visitor {
|
||||
loop_expr: syn::Block,
|
||||
has_made_change: bool,
|
||||
}
|
||||
|
||||
impl Visitor {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
has_made_change: false,
|
||||
loop_expr: parse_quote! { { loop {} } },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
body: loop_body, ..
|
||||
}))] if loop_body.stmts.is_empty() => {}
|
||||
_ => {
|
||||
*block = self.loop_expr.clone();
|
||||
self.has_made_change = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EverybodyLoops;
|
||||
|
||||
impl Processor for EverybodyLoops {
|
||||
fn process_file(&mut self, krate: &mut syn::File) -> bool {
|
||||
let mut visitor = Visitor::new();
|
||||
visitor.visit_file_mut(krate);
|
||||
visitor.has_made_change
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"everybody-loops"
|
||||
}
|
||||
}
|
||||
45
src/lib.rs
45
src/lib.rs
|
|
@ -1,15 +1,48 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
mod build;
|
||||
mod everybody_loops;
|
||||
mod expand;
|
||||
mod privatize;
|
||||
mod processor;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use processor::Minimizer;
|
||||
|
||||
pub fn minimize(cargo_dir: &Path) -> Result<()> {
|
||||
let file = expand::expand(cargo_dir).context("during expansion")?;
|
||||
use crate::{everybody_loops::EverybodyLoops, privatize::Privarize, processor::Processor};
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
pub struct Options {
|
||||
#[arg(short, long)]
|
||||
verify_error_path: Option<PathBuf>,
|
||||
#[arg(long)]
|
||||
cargo: bool,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn minimize() -> Result<()> {
|
||||
let options = Options::parse();
|
||||
|
||||
let dir = options.path;
|
||||
|
||||
let build = build::Build::new(options.cargo, options.verify_error_path, dir.clone());
|
||||
|
||||
let mut minimizer = Minimizer::new_glob_dir(&dir, build);
|
||||
|
||||
println!("{minimizer:?}");
|
||||
|
||||
minimizer.run_passes([
|
||||
//Box::new(Privarize::default()) as Box<dyn Processor>,
|
||||
Box::new(EverybodyLoops::default()) as Box<dyn Processor>,
|
||||
])?;
|
||||
|
||||
/*
|
||||
let file = expand::expand(&dir).context("during expansion")?;
|
||||
|
||||
//let file = syn::parse_str("extern { pub fn printf(format: *const ::c_char, ...) -> ::c_int; }",).unwrap();
|
||||
let file = prettyplease::unparse(&file);
|
||||
|
||||
println!("// EXPANDED-START\n\n{file}\n\n// EXPANDED-END");
|
||||
|
|
@ -17,7 +50,7 @@ pub fn minimize(cargo_dir: &Path) -> Result<()> {
|
|||
std::fs::write("expanded.rs", file)?;
|
||||
|
||||
println!("wow, expanded");
|
||||
Ok(())
|
||||
*/
|
||||
|
||||
/*
|
||||
let build = Build::new(cargo_dir);
|
||||
|
|
@ -26,4 +59,6 @@ pub fn minimize(cargo_dir: &Path) -> Result<()> {
|
|||
bail!("build must initially fail!");
|
||||
}
|
||||
*/
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -1,13 +1,7 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::Result;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let dir = std::env::args().nth(1).context("expected an argument")?;
|
||||
|
||||
cargo_minimize::minimize(&Path::new(&dir))?;
|
||||
|
||||
println!("Exit");
|
||||
cargo_minimize::minimize()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
41
src/privatize.rs
Normal file
41
src/privatize.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use syn::{parse_quote, visit_mut::VisitMut, Visibility};
|
||||
|
||||
use crate::processor::Processor;
|
||||
|
||||
struct Visitor {
|
||||
pub_crate: Visibility,
|
||||
has_made_change: bool,
|
||||
}
|
||||
|
||||
impl Visitor {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
has_made_change: false,
|
||||
pub_crate: parse_quote! { pub(crate) },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VisitMut for Visitor {
|
||||
fn visit_visibility_mut(&mut self, vis: &mut Visibility) {
|
||||
if let Visibility::Public(_) = vis {
|
||||
self.has_made_change = true;
|
||||
*vis = self.pub_crate.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Privarize {}
|
||||
|
||||
impl Processor for Privarize {
|
||||
fn process_file(&mut self, krate: &mut syn::File) -> bool {
|
||||
let mut visitor = Visitor::new();
|
||||
visitor.visit_file_mut(krate);
|
||||
visitor.has_made_change
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"privatize"
|
||||
}
|
||||
}
|
||||
97
src/processor.rs
Normal file
97
src/processor.rs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
use std::{
|
||||
ffi::OsStr,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use anyhow::{ensure, Context, Result};
|
||||
|
||||
use crate::build::Build;
|
||||
|
||||
pub trait Processor {
|
||||
fn process_file(&mut self, krate: &mut syn::File) -> bool;
|
||||
|
||||
fn name(&self) -> &'static str;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Minimizer {
|
||||
files: Vec<PathBuf>,
|
||||
build: Build,
|
||||
}
|
||||
|
||||
impl Minimizer {
|
||||
pub fn new_glob_dir(path: &Path, build: Build) -> Self {
|
||||
let walk = walkdir::WalkDir::new(path);
|
||||
|
||||
let files = walk
|
||||
.into_iter()
|
||||
.filter_map(|entry| match entry {
|
||||
Ok(entry) => Some(entry),
|
||||
Err(err) => {
|
||||
eprintln!("WARN: Error in walkdir: {err}");
|
||||
None
|
||||
}
|
||||
})
|
||||
.filter(|entry| entry.path().extension() == Some(OsStr::new("rs")))
|
||||
.map(|entry| entry.into_path())
|
||||
.collect();
|
||||
|
||||
Self { files, build }
|
||||
}
|
||||
|
||||
pub fn run_passes<'a>(
|
||||
&mut self,
|
||||
passes: impl IntoIterator<Item = Box<dyn Processor>>,
|
||||
) -> Result<()> {
|
||||
let inital_build = self.build.build()?;
|
||||
println!("Initial build: {}", inital_build);
|
||||
ensure!(
|
||||
inital_build.reproduces_issue,
|
||||
"Initial build must reproduce issue"
|
||||
);
|
||||
|
||||
for mut pass in passes {
|
||||
'pass: loop {
|
||||
println!("Starting a round of {}", pass.name());
|
||||
let mut any_change = false;
|
||||
|
||||
for file in &self.files {
|
||||
let file_display = file.display();
|
||||
|
||||
let before_string = std::fs::read_to_string(file)
|
||||
.with_context(|| format!("opening file {file_display}"))?;
|
||||
|
||||
let mut krate = syn::parse_file(&before_string)
|
||||
.with_context(|| format!("parsing file {file_display}"))?;
|
||||
|
||||
let has_made_change = pass.process_file(&mut krate);
|
||||
|
||||
if has_made_change {
|
||||
let result = prettyplease::unparse(&krate);
|
||||
|
||||
std::fs::write(file, &result)?;
|
||||
|
||||
let after = self.build.build()?;
|
||||
|
||||
println!("{file_display}: After {}: {after}", pass.name());
|
||||
|
||||
if after.reproduces_issue {
|
||||
any_change = true;
|
||||
} else {
|
||||
std::fs::write(file, before_string)?;
|
||||
}
|
||||
} else {
|
||||
println!("{file_display}: After {}: no change", pass.name());
|
||||
}
|
||||
}
|
||||
|
||||
if !any_change {
|
||||
println!("Finished {}", pass.name());
|
||||
break 'pass;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue