|
| 1 | +use crate::helpers; |
| 2 | +use anyhow::{Result, bail}; |
| 3 | +use num_cpus; |
| 4 | +use rayon::prelude::*; |
| 5 | +use std::fs; |
| 6 | +use std::io::{self, Write}; |
| 7 | +use std::path::Path; |
| 8 | +use std::process::Command; |
| 9 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 10 | + |
| 11 | +use crate::build::packages; |
| 12 | +use crate::cli::FileExtension; |
| 13 | +use clap::ValueEnum; |
| 14 | + |
| 15 | +pub fn format( |
| 16 | + stdin_extension: Option<FileExtension>, |
| 17 | + all: bool, |
| 18 | + check: bool, |
| 19 | + files: Vec<String>, |
| 20 | + project_root: String, |
| 21 | +) -> Result<()> { |
| 22 | + let bsc_path = helpers::get_bsc(); |
| 23 | + |
| 24 | + match stdin_extension { |
| 25 | + Some(extension) => { |
| 26 | + format_stdin(&bsc_path, extension)?; |
| 27 | + } |
| 28 | + None => { |
| 29 | + let files = if all { get_all_files(&project_root)? } else { files }; |
| 30 | + format_files(&bsc_path, files, check)?; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + Ok(()) |
| 35 | +} |
| 36 | + |
| 37 | +fn get_all_files(project_root: &str) -> Result<Vec<String>> { |
| 38 | + let project_root = Path::new(project_root); |
| 39 | + let project_root_path = helpers::get_abs_path(project_root); |
| 40 | + let workspace_root_option = helpers::get_workspace_root(&project_root_path); |
| 41 | + |
| 42 | + let build_state = packages::make(&None, &project_root_path, &workspace_root_option, false, false)?; |
| 43 | + let mut files: Vec<String> = Vec::new(); |
| 44 | + |
| 45 | + for (_package_name, package) in build_state { |
| 46 | + if let Some(source_files) = package.source_files { |
| 47 | + for (path, _metadata) in source_files { |
| 48 | + if let Some(extension) = path.extension() { |
| 49 | + if extension == "res" || extension == "resi" { |
| 50 | + files.push(package.path.join(path).to_string_lossy().into_owned()); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + Ok(files) |
| 57 | +} |
| 58 | + |
| 59 | +fn format_stdin(bsc_exe: &Path, extension: FileExtension) -> Result<()> { |
| 60 | + let extension = extension.to_possible_value().unwrap().get_name().to_string(); |
| 61 | + let mut temp_file = tempfile::Builder::new().suffix(&extension).tempfile()?; |
| 62 | + io::copy(&mut io::stdin(), &mut temp_file)?; |
| 63 | + let temp_path = temp_file.path(); |
| 64 | + |
| 65 | + let mut cmd = Command::new(bsc_exe); |
| 66 | + cmd.arg("-format").arg(temp_path); |
| 67 | + |
| 68 | + let output = cmd.output()?; |
| 69 | + |
| 70 | + if output.status.success() { |
| 71 | + io::stdout().write_all(&output.stdout)?; |
| 72 | + } else { |
| 73 | + io::stderr().write_all(&output.stderr)?; |
| 74 | + bail!("bsc exited with an error"); |
| 75 | + } |
| 76 | + |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +fn format_files(bsc_exe: &Path, files: Vec<String>, check: bool) -> Result<()> { |
| 81 | + let batch_size = 4 * num_cpus::get(); |
| 82 | + let incorrectly_formatted_files = AtomicUsize::new(0); |
| 83 | + |
| 84 | + files.par_chunks(batch_size).try_for_each(|batch| { |
| 85 | + batch.iter().try_for_each(|file| { |
| 86 | + let mut cmd = Command::new(bsc_exe); |
| 87 | + if check { |
| 88 | + cmd.arg("-format").arg(file); |
| 89 | + } else { |
| 90 | + cmd.arg("-o").arg(file).arg("-format").arg(file); |
| 91 | + } |
| 92 | + |
| 93 | + let output = cmd.output()?; |
| 94 | + |
| 95 | + if output.status.success() { |
| 96 | + if check { |
| 97 | + let original_content = fs::read_to_string(file)?; |
| 98 | + let formatted_content = String::from_utf8_lossy(&output.stdout); |
| 99 | + if original_content != formatted_content { |
| 100 | + eprintln!("[format check] {}", file); |
| 101 | + incorrectly_formatted_files.fetch_add(1, Ordering::SeqCst); |
| 102 | + } |
| 103 | + } |
| 104 | + } else { |
| 105 | + io::stderr().write_all(&output.stderr)?; |
| 106 | + bail!("bsc exited with an error for file {}", file); |
| 107 | + } |
| 108 | + Ok(()) |
| 109 | + }) |
| 110 | + })?; |
| 111 | + |
| 112 | + let count = incorrectly_formatted_files.load(Ordering::SeqCst); |
| 113 | + if count > 0 { |
| 114 | + if count == 1 { |
| 115 | + eprintln!("The file listed above needs formatting"); |
| 116 | + } else { |
| 117 | + eprintln!("The {} files listed above need formatting", count); |
| 118 | + } |
| 119 | + bail!("Formatting check failed"); |
| 120 | + } |
| 121 | + |
| 122 | + Ok(()) |
| 123 | +} |
0 commit comments