-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostprocessing.rs
164 lines (159 loc) · 5.99 KB
/
postprocessing.rs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{cell::RefCell, collections::BTreeSet, io, process::Command, sync::Mutex};
use camino::{Utf8Path, Utf8PathBuf};
#[derive(Debug, Clone)]
pub(crate) struct Postprocessor {
files_to_process: RefCell<Vec<Utf8PathBuf>>,
postprocessor_lang: PostprocessorLanguage,
output_dir: Utf8PathBuf,
}
impl Postprocessor {
fn new(postprocessor_lang: PostprocessorLanguage, output_dir: Utf8PathBuf) -> Self {
Self {
files_to_process: RefCell::new(Vec::new()),
postprocessor_lang,
output_dir,
}
}
pub(crate) fn from_ext(ext: &str, output_dir: &Utf8Path) -> Self {
let lang = match ext {
"py" => PostprocessorLanguage::Python,
"rs" => PostprocessorLanguage::Rust,
"go" => PostprocessorLanguage::Go,
"kt" => PostprocessorLanguage::Kotlin,
"cs" => PostprocessorLanguage::CSharp,
"java" => PostprocessorLanguage::Java,
"ts" => PostprocessorLanguage::TypeScript,
"rb" => PostprocessorLanguage::Ruby,
_ => {
tracing::warn!("no known postprocessing command(s) for {ext} files");
PostprocessorLanguage::Unknown
}
};
Self::new(lang, output_dir.to_path_buf())
}
pub(crate) fn run_postprocessor(&self) {
match self.postprocessor_lang {
// pass each file to postprocessor at once
PostprocessorLanguage::Java | PostprocessorLanguage::Rust => {
let commands = self.postprocessor_lang.postprocessing_commands();
for (command, args) in commands {
execute_command(command, args, &self.files_to_process.borrow());
}
}
// pass output dir to postprocessor
PostprocessorLanguage::Ruby
| PostprocessorLanguage::Python
| PostprocessorLanguage::Go
| PostprocessorLanguage::Kotlin
| PostprocessorLanguage::CSharp
| PostprocessorLanguage::TypeScript => {
let commands = self.postprocessor_lang.postprocessing_commands();
for (command, args) in commands {
execute_command(command, args, &vec![self.output_dir.clone()]);
}
}
PostprocessorLanguage::Unknown => (),
}
}
pub(crate) fn add_path(&self, path: &Utf8Path) {
let mut v = self.files_to_process.borrow_mut();
v.push(path.to_path_buf());
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum PostprocessorLanguage {
Python,
Rust,
Go,
Kotlin,
CSharp,
Java,
TypeScript,
Ruby,
Unknown,
}
impl PostprocessorLanguage {
fn postprocessing_commands(&self) -> &[(&'static str, &[&str])] {
match self {
Self::Unknown => &[],
// https://github.com/astral-sh/ruff
Self::Python => &[
("ruff", &["check", "--no-respect-gitignore", "--fix"]), // First lint and remove unused imports
(
"ruff", // Then sort imports
&["check", "--no-respect-gitignore", "--select", "I", "--fix"],
),
("ruff", &["format", "--no-respect-gitignore"]), // Then format the file
],
Self::Rust => &[(
"rustfmt",
&[
"+nightly-2025-02-27",
"--unstable-features",
"--skip-children",
"--edition",
"2021",
],
)],
// https://pkg.go.dev/golang.org/x/tools/cmd/goimports
Self::Go => &[("goimports", &["-w"]), ("gofmt", &["-w"])],
// https://github.com/facebook/ktfmt
Self::Kotlin => &[("ktfmt", &["--kotlinlang-style"])],
// https://github.com/belav/csharpier
Self::CSharp => &[("dotnet", &["csharpier", "--fast", "--no-msbuild-check"])],
// https://github.com/google/google-java-format
Self::Java => &[("google-java-format", &["-i", "-a"])],
// https://github.com/biomejs/biome
Self::TypeScript => &[
(
"biome",
&["lint", "--only=correctness/noUnusedImports", "--write"],
),
(
"biome",
&[
"check",
"--formatter-enabled=false",
"--linter-enabled=false",
"--organize-imports-enabled=true",
"--write",
],
),
(
"biome",
&[
"format",
"--trailing-commas=es5",
"--indent-style=space",
"--line-width=90",
"--write",
],
),
],
// https://github.com/fables-tales/rubyfmt
Self::Ruby => &[("rubyfmt", &["-i", "--include-gitignored", "--fail-fast"])],
}
}
}
fn execute_command(command: &'static str, args: &[&str], paths: &Vec<Utf8PathBuf>) {
let result = Command::new(command).args(args).args(paths).status();
match result {
Ok(exit_status) if exit_status.success() => {}
Ok(exit_status) => {
tracing::warn!(exit_status = exit_status.code(), "`{command}` failed");
}
Err(e) if e.kind() == io::ErrorKind::NotFound => {
// only print one error per command that's not found
static NOT_FOUND_LOGGED_FOR: Mutex<BTreeSet<&str>> = Mutex::new(BTreeSet::new());
if NOT_FOUND_LOGGED_FOR.lock().unwrap().insert(command) {
tracing::warn!("`{command}` not found");
}
}
Err(e) => {
tracing::warn!(
error = &e as &dyn std::error::Error,
"running `{command}` failed"
);
}
}
}