Fix the CLI and log files as they are processed (#90)

* fix: use rayon instead of handwritten thread pool

* style: add newline before a new section

* fix: bring back single file mode

* feat: add log

* chore: only print file name
This commit is contained in:
Wenzhuo Liu 2022-07-05 10:03:11 +08:00 committed by GitHub
parent 7c97affa9d
commit 9d5d1bb0f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 43 deletions

View File

@ -8,9 +8,8 @@ edition = "2018"
[dependencies]
minify-html = { path = "../rust/main" }
num_cpus = "1.13.1"
spmc = "0.3.0"
structopt = "0.3"
rayon = "1.5"
[profile.release]
lto = true

View File

@ -1,10 +1,9 @@
use std::fs::File;
use std::io::{stdin, stdout, Read, Write};
use std::path::PathBuf;
use std::process::exit;
use std::sync::Arc;
use std::thread;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use structopt::StructOpt;
use minify_html::{minify, Cfg};
@ -98,12 +97,13 @@ fn main() {
remove_processing_instructions: args.remove_processing_instructions,
});
if args.inputs.len() < 1 {
if args.inputs.len() <= 1 {
// single file mode or stdin mode
let input_name = args
.inputs
.get(0)
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or("stdin".to_string());
.unwrap_or_else(|| "stdin".to_string());
let mut src_file: Box<dyn Read> = match args.inputs.get(0) {
Some(p) => Box::new(io_expect!(
input_name,
@ -133,43 +133,29 @@ fn main() {
"Could not save minified code"
);
} else {
let (mut tx, rx) = spmc::channel::<PathBuf>();
let mut handles = Vec::new();
for _ in 0..num_cpus::get() {
let rx = rx.clone();
let cfg = cfg.clone();
handles.push(thread::spawn(move || {
let input = rx.recv().unwrap();
let input_name = input.to_string_lossy().into_owned();
args.inputs.par_iter().for_each(|input| {
let input_name = input.to_string_lossy().into_owned();
let mut src_file =
io_expect!(input_name, File::open(&input), "Could not open source file");
let mut src_code = Vec::<u8>::new();
io_expect!(
input_name,
src_file.read_to_end(&mut src_code),
"Could not load source code"
);
let out_code = minify(&src_code, &cfg);
let mut out_file = io_expect!(
input_name,
File::create(&input),
"Could not open output file"
);
io_expect!(
input_name,
out_file.write_all(&out_code),
"Could not save minified code"
);
}));
}
for i in args.inputs {
tx.send(i).unwrap();
}
for handle in handles {
handle.join().unwrap();
}
let mut src_file =
io_expect!(input_name, File::open(&input), "Could not open source file");
let mut src_code = Vec::<u8>::new();
io_expect!(
input_name,
src_file.read_to_end(&mut src_code),
"Could not load source code"
);
let out_code = minify(&src_code, &cfg);
let mut out_file = io_expect!(
input_name,
File::create(&input),
"Could not open output file"
);
io_expect!(
input_name,
out_file.write_all(&out_code),
"Could not save minified code"
);
println!("{}", input_name);
});
}
}