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:
parent
7c97affa9d
commit
9d5d1bb0f3
|
@ -8,9 +8,8 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
minify-html = { path = "../rust/main" }
|
minify-html = { path = "../rust/main" }
|
||||||
num_cpus = "1.13.1"
|
|
||||||
spmc = "0.3.0"
|
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
|
rayon = "1.5"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{stdin, stdout, Read, Write};
|
use std::io::{stdin, stdout, Read, Write};
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::thread;
|
|
||||||
|
|
||||||
|
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
use minify_html::{minify, Cfg};
|
use minify_html::{minify, Cfg};
|
||||||
|
@ -98,12 +97,13 @@ fn main() {
|
||||||
remove_processing_instructions: args.remove_processing_instructions,
|
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
|
let input_name = args
|
||||||
.inputs
|
.inputs
|
||||||
.get(0)
|
.get(0)
|
||||||
.map(|p| p.to_string_lossy().into_owned())
|
.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) {
|
let mut src_file: Box<dyn Read> = match args.inputs.get(0) {
|
||||||
Some(p) => Box::new(io_expect!(
|
Some(p) => Box::new(io_expect!(
|
||||||
input_name,
|
input_name,
|
||||||
|
@ -133,43 +133,29 @@ fn main() {
|
||||||
"Could not save minified code"
|
"Could not save minified code"
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let (mut tx, rx) = spmc::channel::<PathBuf>();
|
args.inputs.par_iter().for_each(|input| {
|
||||||
let mut handles = Vec::new();
|
let input_name = input.to_string_lossy().into_owned();
|
||||||
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();
|
|
||||||
|
|
||||||
let mut src_file =
|
let mut src_file =
|
||||||
io_expect!(input_name, File::open(&input), "Could not open source file");
|
io_expect!(input_name, File::open(&input), "Could not open source file");
|
||||||
let mut src_code = Vec::<u8>::new();
|
let mut src_code = Vec::<u8>::new();
|
||||||
io_expect!(
|
io_expect!(
|
||||||
input_name,
|
input_name,
|
||||||
src_file.read_to_end(&mut src_code),
|
src_file.read_to_end(&mut src_code),
|
||||||
"Could not load source code"
|
"Could not load source code"
|
||||||
);
|
);
|
||||||
let out_code = minify(&src_code, &cfg);
|
let out_code = minify(&src_code, &cfg);
|
||||||
let mut out_file = io_expect!(
|
let mut out_file = io_expect!(
|
||||||
input_name,
|
input_name,
|
||||||
File::create(&input),
|
File::create(&input),
|
||||||
"Could not open output file"
|
"Could not open output file"
|
||||||
);
|
);
|
||||||
io_expect!(
|
io_expect!(
|
||||||
input_name,
|
input_name,
|
||||||
out_file.write_all(&out_code),
|
out_file.write_all(&out_code),
|
||||||
"Could not save minified code"
|
"Could not save minified code"
|
||||||
);
|
);
|
||||||
}));
|
println!("{}", input_name);
|
||||||
}
|
});
|
||||||
|
|
||||||
for i in args.inputs {
|
|
||||||
tx.send(i).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
for handle in handles {
|
|
||||||
handle.join().unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue