-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathformat.rs
More file actions
110 lines (93 loc) · 3.1 KB
/
Copy pathformat.rs
File metadata and controls
110 lines (93 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::{path::Path, process::Stdio};
use clap::Args;
use color_eyre::{
Result,
eyre::{Context, bail},
};
use repository::Repository;
use tokio::process::Command;
use crate::progress_indicator::ProgressIndicator;
#[derive(Args)]
pub struct Arguments {
/// Run only in check mode and do not make changes in the filesystem
#[arg(long, visible_alias = "prüf")]
check: bool,
}
async fn rustfmt(root: impl AsRef<Path>, check: bool) -> Result<()> {
let check_flag = if check { "--check" } else { "" };
let output = Command::new("sh")
.stderr(Stdio::piped())
.arg("-c")
.arg(format!(
"cd {} && cargo fmt {check_flag}",
root.as_ref().display()
))
.output()
.await
.wrap_err("rustfmt failed")?;
if output.status.success() {
return Ok(());
}
bail!(String::from_utf8(output.stderr).expect("stderr was not utf8"))
}
async fn taplo_fmt(root: impl AsRef<Path>, check: bool) -> Result<()> {
let format_or_check = if check { "check" } else { "format" };
let output = Command::new("sh")
.stderr(Stdio::piped())
.arg("-c")
.arg(format!(
"cd {} && git ls-files -z '*.toml' | xargs -0 taplo {format_or_check}",
root.as_ref().display()
))
.output()
.await
.wrap_err("taplo fmt failed")?;
if output.status.success() {
return Ok(());
}
bail!(String::from_utf8(output.stderr).expect("stderr was not utf8"))
}
async fn ruff_fmt(root: impl AsRef<Path>, check: bool) -> Result<()> {
let check_flag = if check { "--check" } else { "" };
let output = Command::new("sh")
.stderr(Stdio::piped())
.arg("-c")
.arg(format!(
"cd {} && git ls-files -z '*.py' '*.pyi' | xargs -0 uvx ruff format {check_flag}",
root.as_ref().display()
))
.output()
.await
.wrap_err("taplo fmt failed")?;
if output.status.success() {
return Ok(());
}
bail!(String::from_utf8(output.stderr).expect("stderr was not utf8"))
}
pub async fn format(arguments: Arguments, repository: &Repository) -> Result<()> {
let progress_indicator = ProgressIndicator::new();
let (rustfmt_result, taplo_result, ruff_result) = tokio::join!(
async {
let task = progress_indicator.task("rustfmt", true);
let result = rustfmt(&repository.root, arguments.check).await;
task.finish_with(result.as_ref());
result
},
async {
let task = progress_indicator.task("taplo", true);
let result = taplo_fmt(&repository.root, arguments.check).await;
task.finish_with(result.as_ref());
result
},
async {
let task = progress_indicator.task("ruff", true);
let result = ruff_fmt(&repository.root, arguments.check).await;
task.finish_with(result.as_ref());
result
}
);
if rustfmt_result.is_err() || taplo_result.is_err() || ruff_result.is_err() {
bail!("formatting failed");
}
Ok(())
}