-
Notifications
You must be signed in to change notification settings - Fork 469
Rust implementation of "rescript format" #7603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use anyhow::{bail, Result}; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::Command; | ||
use std::io::{self, Read, Write}; | ||
use std::fs; | ||
use rayon::prelude::*; | ||
use crate::helpers; | ||
use num_cpus; | ||
|
||
pub fn run( | ||
stdin_path: Option<String>, | ||
all: bool, | ||
check: bool, | ||
files: Vec<String>, | ||
bsc_path: Option<PathBuf>, | ||
path: PathBuf, | ||
) -> Result<()> { | ||
let project_root = helpers::get_abs_path(&path); | ||
let workspace_root = helpers::get_workspace_root(&project_root); | ||
|
||
let bsc_exe = match bsc_path { | ||
Some(path) => helpers::get_abs_path(&path), | ||
None => helpers::get_bsc(&project_root, &workspace_root), | ||
}; | ||
|
||
if check && stdin_path.is_some() { | ||
bail!("format --stdin cannot be used with --check flag"); | ||
} | ||
|
||
if all { | ||
if stdin_path.is_some() || !files.is_empty() { | ||
bail!("format --all can not be in use with other flags"); | ||
} | ||
format_all(&bsc_exe, check)?; | ||
} else if stdin_path.is_some() { | ||
format_stdin(&bsc_exe, stdin_path.unwrap())?; | ||
} else { | ||
format_files(&bsc_exe, files, check)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
||
|
||
fn format_all(bsc_exe: &Path, check: bool) -> Result<()> { | ||
let output = Command::new(std::env::current_exe()?) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rewatch doesn't have an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
.arg("info") | ||
.arg("-list-files") | ||
.output()?; | ||
|
||
if !output.status.success() { | ||
io::stderr().write_all(&output.stderr)?; | ||
bail!("Failed to list files"); | ||
} | ||
|
||
let files_str = String::from_utf8_lossy(&output.stdout); | ||
let files: Vec<String> = files_str | ||
.split('\n') | ||
.filter(|s| !s.trim().is_empty()) | ||
.map(|s| s.trim().to_string()) | ||
.collect(); | ||
|
||
format_files(bsc_exe, files, check)?; | ||
Ok(()) | ||
} | ||
|
||
fn format_stdin(bsc_exe: &Path, stdin_path: String) -> Result<()> { | ||
let mut input = String::new(); | ||
io::stdin().read_to_string(&mut input)?; | ||
|
||
let mut cmd = Command::new(bsc_exe); | ||
cmd.arg("-format").arg(&stdin_path); | ||
cmd.stdin(std::process::Stdio::piped()); | ||
cmd.stdout(std::process::Stdio::piped()); | ||
cmd.stderr(std::process::Stdio::piped()); | ||
|
||
let mut child = cmd.spawn()?; | ||
let mut stdin = child.stdin.take().unwrap(); | ||
std::thread::spawn(move || { | ||
stdin.write_all(input.as_bytes()).unwrap(); | ||
}); | ||
|
||
let output = child.wait_with_output()?; | ||
|
||
if output.status.success() { | ||
io::stdout().write_all(&output.stdout)?; | ||
} | ||
else { | ||
io::stderr().write_all(&output.stderr)?; | ||
bail!("bsc exited with an error"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn format_files(bsc_exe: &Path, files: Vec<String>, check: bool) -> Result<()> { | ||
let batch_size = 4 * num_cpus::get(); | ||
let incorrectly_formatted_files = AtomicUsize::new(0); | ||
|
||
files.par_chunks(batch_size).try_for_each(|batch| { | ||
batch.iter().try_for_each(|file| { | ||
let mut cmd = Command::new(bsc_exe); | ||
if check { | ||
cmd.arg("-format").arg(file); | ||
} | ||
else { | ||
cmd.arg("-o").arg(file).arg("-format").arg(file); | ||
} | ||
|
||
let output = cmd.output()?; | ||
|
||
if output.status.success() { | ||
if check { | ||
let original_content = fs::read_to_string(file)?; | ||
let formatted_content = String::from_utf8_lossy(&output.stdout); | ||
if original_content != formatted_content { | ||
eprintln!("[format check] {}", file); | ||
incorrectly_formatted_files.fetch_add(1, Ordering::SeqCst); | ||
} | ||
} | ||
} | ||
else { | ||
io::stderr().write_all(&output.stderr)?; | ||
bail!("bsc exited with an error for file {}", file); | ||
} | ||
Ok(()) as Result<()> | ||
}) | ||
})?; | ||
|
||
let count = incorrectly_formatted_files.load(Ordering::SeqCst); | ||
if count > 0 { | ||
if count == 1 { | ||
eprintln!("The file listed above needs formatting"); | ||
} | ||
else { | ||
eprintln!( | ||
"The {} files listed above need formatting", | ||
count | ||
); | ||
} | ||
bail!("Formatting check failed"); | ||
} | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ pub mod lock; | |
pub mod queue; | ||
pub mod sourcedirs; | ||
pub mod watcher; | ||
pub mod format_cmd; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--all
is actually-all
in bsb.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this will be a breaking changes I would say.