|
| 1 | +use crate::cli::GlobalOptions; |
| 2 | +use crate::cli::utils::{ |
| 3 | + maybe_plural, |
| 4 | + write_file, |
| 5 | + FileProcessing, |
| 6 | +}; |
| 7 | + |
| 8 | +use darklua_core::{LuaGenerator, ToLua, Parser, ParsingError}; |
| 9 | +use std::fmt::{self, Display}; |
| 10 | +use std::path::PathBuf; |
| 11 | +use std::fs; |
| 12 | +use std::process; |
| 13 | +use structopt::StructOpt; |
| 14 | + |
| 15 | +#[derive(Debug, StructOpt)] |
| 16 | +pub struct Options { |
| 17 | + /// Path to the lua file to minify. |
| 18 | + #[structopt(parse(from_os_str))] |
| 19 | + pub input_path: PathBuf, |
| 20 | + /// Where to output the result. |
| 21 | + #[structopt(parse(from_os_str))] |
| 22 | + pub output_path: PathBuf, |
| 23 | + /// Amount of characters before the code is wrapped into a new line. |
| 24 | + #[structopt(long, short, default_value = "80")] |
| 25 | + pub column_span: usize, |
| 26 | +} |
| 27 | + |
| 28 | +pub enum Error { |
| 29 | + Parser(PathBuf, ParsingError), |
| 30 | + InputFile(String), |
| 31 | + InputFileNotFound(PathBuf), |
| 32 | + OutputFile(PathBuf, String), |
| 33 | +} |
| 34 | + |
| 35 | +impl Display for Error { |
| 36 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 37 | + match self { |
| 38 | + Self::Parser(path, error) => { |
| 39 | + write!(f, "could not parse input at <{}>: {:?}", path.to_string_lossy(), error) |
| 40 | + } |
| 41 | + Self::InputFile(error) => { |
| 42 | + write!(f, "error while reading input file: {}", error) |
| 43 | + } |
| 44 | + Self::InputFileNotFound(path) => { |
| 45 | + write!(f, "input file not found: {}", path.to_string_lossy()) |
| 46 | + } |
| 47 | + Self::OutputFile(path, error) => { |
| 48 | + write!(f, "error with output file <{}>: {}", path.to_string_lossy(), error) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +type MinifyResult = Result<(), Error>; |
| 55 | + |
| 56 | +fn process(file: &FileProcessing, options: &Options, global: &GlobalOptions) -> MinifyResult { |
| 57 | + let source = &file.source; |
| 58 | + let output = &file.output; |
| 59 | + |
| 60 | + if !source.exists() { |
| 61 | + return Err(Error::InputFileNotFound(source.clone())) |
| 62 | + } |
| 63 | + |
| 64 | + let input = fs::read_to_string(source) |
| 65 | + .map_err(|io_error| Error::InputFile(format!("{}", io_error)))?; |
| 66 | + |
| 67 | + let parser = Parser::default(); |
| 68 | + |
| 69 | + let block = parser.parse(&input) |
| 70 | + .map_err(|parser_error| Error::Parser(source.clone(), parser_error))?; |
| 71 | + |
| 72 | + let mut generator = LuaGenerator::new(options.column_span); |
| 73 | + block.to_lua(&mut generator); |
| 74 | + let minified = generator.into_string(); |
| 75 | + |
| 76 | + write_file(&output, &minified) |
| 77 | + .map_err(|io_error| Error::OutputFile(output.clone(), format!("{}", io_error)))?; |
| 78 | + |
| 79 | + if global.verbose > 0 { |
| 80 | + println!("Successfully processed <{}>", source.to_string_lossy()); |
| 81 | + }; |
| 82 | + |
| 83 | + Ok(()) |
| 84 | +} |
| 85 | + |
| 86 | +pub fn run(options: &Options, global: &GlobalOptions) { |
| 87 | + let file = FileProcessing::find(&options.input_path, &options.output_path, global); |
| 88 | + |
| 89 | + let results: Vec<Result<(), Error>> = file.iter() |
| 90 | + .map(|file_processing| process(file_processing, &options, global)) |
| 91 | + .collect(); |
| 92 | + |
| 93 | + let total_files = results.len(); |
| 94 | + |
| 95 | + let errors: Vec<Error> = results.into_iter() |
| 96 | + .filter_map(|result| match result { |
| 97 | + Ok(()) => { |
| 98 | + None |
| 99 | + } |
| 100 | + Err(error) => Some(error), |
| 101 | + }) |
| 102 | + .collect(); |
| 103 | + |
| 104 | + let error_count = errors.len(); |
| 105 | + |
| 106 | + if error_count == 0 { |
| 107 | + println!("Successfully minified {} file{}", total_files, maybe_plural(total_files)); |
| 108 | + |
| 109 | + } else { |
| 110 | + let success_count = total_files - error_count; |
| 111 | + |
| 112 | + if success_count > 0 { |
| 113 | + eprintln!("Successfully minified {} file{}.", success_count, maybe_plural(success_count)); |
| 114 | + } |
| 115 | + |
| 116 | + eprintln!("But {} error{} happened:", error_count, maybe_plural(error_count)); |
| 117 | + |
| 118 | + errors.iter().for_each(|error| eprintln!("-> {}", error)); |
| 119 | + process::exit(1); |
| 120 | + } |
| 121 | +} |
0 commit comments