|
| 1 | +use std::{ |
| 2 | + fs, |
| 3 | + io::{self, Read, Write}, |
| 4 | + path::PathBuf, |
| 5 | + process::exit, |
| 6 | +}; |
| 7 | + |
| 8 | +use clap::Parser; |
| 9 | +use emmylua_code_style::{LuaCodeStyle, cmd_args, reformat_lua_code}; |
| 10 | + |
| 11 | +#[global_allocator] |
| 12 | +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; |
| 13 | + |
| 14 | +fn read_stdin_to_string() -> io::Result<String> { |
| 15 | + let mut s = String::new(); |
| 16 | + io::stdin().read_to_string(&mut s)?; |
| 17 | + Ok(s) |
| 18 | +} |
| 19 | + |
| 20 | +fn format_content(content: &str, style: &LuaCodeStyle) -> String { |
| 21 | + reformat_lua_code(content, style) |
| 22 | +} |
| 23 | + |
| 24 | +#[allow(unused)] |
| 25 | +fn process_file( |
| 26 | + path: &PathBuf, |
| 27 | + style: &LuaCodeStyle, |
| 28 | + write: bool, |
| 29 | + list_diff: bool, |
| 30 | +) -> io::Result<(bool, Option<String>)> { |
| 31 | + let original = fs::read_to_string(path)?; |
| 32 | + let formatted = format_content(&original, style); |
| 33 | + let changed = formatted != original; |
| 34 | + |
| 35 | + if write && changed { |
| 36 | + fs::write(path, formatted)?; |
| 37 | + return Ok((true, None)); |
| 38 | + } |
| 39 | + |
| 40 | + if list_diff && changed { |
| 41 | + return Ok((true, Some(path.to_string_lossy().to_string()))); |
| 42 | + } |
| 43 | + |
| 44 | + Ok((changed, None)) |
| 45 | +} |
| 46 | + |
| 47 | +fn main() { |
| 48 | + let args = cmd_args::CliArgs::parse(); |
| 49 | + |
| 50 | + let mut exit_code = 0; |
| 51 | + |
| 52 | + let style = match cmd_args::resolve_style(&args) { |
| 53 | + Ok(s) => s, |
| 54 | + Err(e) => { |
| 55 | + eprintln!("Error: {e}"); |
| 56 | + exit(2); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + let is_stdin = args.stdin || args.paths.is_empty(); |
| 61 | + |
| 62 | + if is_stdin { |
| 63 | + let content = match read_stdin_to_string() { |
| 64 | + Ok(s) => s, |
| 65 | + Err(e) => { |
| 66 | + eprintln!("Failed to read stdin: {e}"); |
| 67 | + exit(2); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + let formatted = format_content(&content, &style); |
| 72 | + let changed = formatted != content; |
| 73 | + |
| 74 | + if args.check || args.list_different { |
| 75 | + if changed { |
| 76 | + exit_code = 1; |
| 77 | + } |
| 78 | + } else if let Some(out) = &args.output { |
| 79 | + if let Err(e) = fs::write(out, formatted) { |
| 80 | + eprintln!("Failed to write output to {out:?}: {e}"); |
| 81 | + exit(2); |
| 82 | + } |
| 83 | + } else if args.write { |
| 84 | + eprintln!("--write with stdin requires --output <FILE>"); |
| 85 | + exit(2); |
| 86 | + } else { |
| 87 | + let mut stdout = io::stdout(); |
| 88 | + if let Err(e) = stdout.write_all(formatted.as_bytes()) { |
| 89 | + eprintln!("Failed to write to stdout: {e}"); |
| 90 | + exit(2); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + exit(exit_code); |
| 95 | + } |
| 96 | + |
| 97 | + if args.paths.len() > 1 && args.output.is_some() { |
| 98 | + eprintln!("--output can only be used with a single input or stdin"); |
| 99 | + exit(2); |
| 100 | + } |
| 101 | + |
| 102 | + if args.paths.len() > 1 && !(args.write || args.check || args.list_different) { |
| 103 | + eprintln!("Multiple inputs require --write or --check"); |
| 104 | + exit(2); |
| 105 | + } |
| 106 | + |
| 107 | + let mut different_paths: Vec<String> = Vec::new(); |
| 108 | + |
| 109 | + for path in &args.paths { |
| 110 | + match fs::metadata(path) { |
| 111 | + Ok(meta) => { |
| 112 | + if !meta.is_file() { |
| 113 | + eprintln!("Skipping non-file path: {}", path.to_string_lossy()); |
| 114 | + continue; |
| 115 | + } |
| 116 | + } |
| 117 | + Err(e) => { |
| 118 | + eprintln!("Cannot access {}: {e}", path.to_string_lossy()); |
| 119 | + exit_code = 2; |
| 120 | + continue; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + match fs::read_to_string(path) { |
| 125 | + Ok(original) => { |
| 126 | + let formatted = format_content(&original, &style); |
| 127 | + let changed = formatted != original; |
| 128 | + |
| 129 | + if args.check || args.list_different { |
| 130 | + if changed { |
| 131 | + exit_code = 1; |
| 132 | + if args.list_different { |
| 133 | + different_paths.push(path.to_string_lossy().to_string()); |
| 134 | + } |
| 135 | + } |
| 136 | + } else if args.write { |
| 137 | + if changed { |
| 138 | + if let Err(e) = fs::write(path, formatted) { |
| 139 | + eprintln!("Failed to write {}: {e}", path.to_string_lossy()); |
| 140 | + exit_code = 2; |
| 141 | + } |
| 142 | + } |
| 143 | + } else if let Some(out) = &args.output { |
| 144 | + if let Err(e) = fs::write(out, formatted) { |
| 145 | + eprintln!("Failed to write output to {out:?}: {e}"); |
| 146 | + exit(2); |
| 147 | + } |
| 148 | + } else { |
| 149 | + // Single file without write/check: print to stdout |
| 150 | + let mut stdout = io::stdout(); |
| 151 | + if let Err(e) = stdout.write_all(formatted.as_bytes()) { |
| 152 | + eprintln!("Failed to write to stdout: {e}"); |
| 153 | + exit(2); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + Err(e) => { |
| 158 | + eprintln!("Failed to read {}: {e}", path.to_string_lossy()); |
| 159 | + exit_code = 2; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if args.list_different && !different_paths.is_empty() { |
| 165 | + for p in different_paths { |
| 166 | + println!("{p}"); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + exit(exit_code); |
| 171 | +} |
0 commit comments