Skip to content

Commit 3b0f4b5

Browse files
committed
New feature rustfmt enables Rustfmt (custom formatter is default)
1 parent 59cc347 commit 3b0f4b5

File tree

7 files changed

+55
-12
lines changed

7 files changed

+55
-12
lines changed

godot-codegen/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ repository = "https://github.com/godot-rust/gdext"
1111
homepage = "https://godot-rust.github.io"
1212

1313
[features]
14-
default = ["codegen-fmt"]
15-
codegen-fmt = []
14+
default = []
1615
codegen-full = []
1716
codegen-lazy-fptrs = []
17+
codegen-rustfmt = []
1818
double-precision = []
1919
api-custom = ["godot-bindings/api-custom"]
2020
experimental-godot-api = []

godot-codegen/src/formatter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8-
#![cfg(feature = "codegen-fmt")]
8+
#![cfg(not(feature = "codegen-rustfmt"))]
99

1010
use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree};
1111

@@ -37,7 +37,7 @@ use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree};
3737
/// - anonymous function literals are a bit weird because the `|` don't have
3838
/// joint spacing when they come out of a `TokenStream` and identifiers don't
3939
/// have any spacing information attached, so it looks like `| arg |`.
40-
/// Also to know whether it's a chained bitwise or a closure would need more
40+
/// Also, to know whether it's a chained bitwise or, a closure would need more
4141
/// information. `move | arg | x`
4242
/// - Because we don't keep track of what's a type and what's an expression,
4343
/// generic parameters look a bit spaced out. `T < A, B >`

godot-codegen/src/lib.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,47 @@ fn write_file(path: &Path, contents: String) {
5151
.unwrap_or_else(|e| panic!("failed to write code file to {};\n\t{}", path.display(), e));
5252
}
5353

54-
#[cfg(feature = "codegen-fmt")]
54+
#[cfg(not(feature = "codegen-rustfmt"))]
5555
fn submit_fn(path: PathBuf, tokens: TokenStream) {
5656
write_file(&path, formatter::format_tokens(tokens));
5757
}
5858

59-
#[cfg(not(feature = "codegen-fmt"))]
60-
fn submit_fn(path: PathBuf, tokens: TokenStream) {
61-
write_file(&path, tokens.to_string());
59+
#[cfg(feature = "codegen-rustfmt")]
60+
mod rustfmt {
61+
use super::*;
62+
use std::process::Command;
63+
use std::sync::Mutex;
64+
65+
pub fn submit_fn(path: PathBuf, tokens: TokenStream) {
66+
write_file(&path, tokens.to_string());
67+
FILES_TO_RUSTFMT.lock().unwrap().push(path);
68+
}
69+
70+
pub fn rustfmt_files() {
71+
let out_files = FILES_TO_RUSTFMT.lock().unwrap();
72+
println!("Format {} generated files...", out_files.len());
73+
74+
for files in out_files.chunks(20) {
75+
let mut command = Command::new("rustfmt");
76+
for file in files {
77+
command.arg(file);
78+
}
79+
80+
let status = command.status().expect("failed to invoke rustfmt");
81+
if !status.success() {
82+
panic!("rustfmt failed on {:?}", command);
83+
}
84+
}
85+
86+
println!("Rustfmt completed successfully");
87+
}
88+
89+
static FILES_TO_RUSTFMT: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());
6290
}
6391

92+
#[cfg(feature = "codegen-rustfmt")]
93+
pub(crate) use rustfmt::*;
94+
6495
pub fn generate_sys_files(
6596
sys_gen_path: &Path,
6697
h_path: &Path,
@@ -99,6 +130,12 @@ pub fn generate_sys_files(
99130

100131
generate_sys_module_file(sys_gen_path, &mut submit_fn);
101132
watch.record("generate_module_file");
133+
134+
#[cfg(feature = "codegen-rustfmt")]
135+
{
136+
rustfmt_files();
137+
watch.record("rustfmt");
138+
}
102139
}
103140

104141
pub fn generate_core_files(core_gen_path: &Path) {
@@ -151,5 +188,11 @@ pub fn generate_core_files(core_gen_path: &Path) {
151188
);
152189
watch.record("generate_native_structures_files");
153190

191+
#[cfg(feature = "codegen-rustfmt")]
192+
{
193+
rustfmt_files();
194+
watch.record("rustfmt");
195+
}
196+
154197
watch.write_stats_to(&core_gen_path.join("codegen-stats.txt"));
155198
}

godot-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ homepage = "https://godot-rust.github.io"
1212

1313
[features]
1414
default = []
15-
codegen-fmt = ["godot-ffi/codegen-fmt", "godot-codegen/codegen-fmt"]
15+
codegen-rustfmt = ["godot-ffi/codegen-rustfmt", "godot-codegen/codegen-rustfmt"]
1616
codegen-full = ["godot-codegen/codegen-full"]
1717
codegen-lazy-fptrs = [
1818
"godot-ffi/codegen-lazy-fptrs",

godot-ffi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repository = "https://github.com/godot-rust/gdext"
1111
homepage = "https://godot-rust.github.io"
1212

1313
[features]
14-
codegen-fmt = ["godot-codegen/codegen-fmt"]
14+
codegen-rustfmt = ["godot-codegen/codegen-rustfmt"]
1515
codegen-lazy-fptrs = ["godot-codegen/codegen-lazy-fptrs"]
1616
experimental-godot-api = ["godot-codegen/experimental-godot-api"]
1717
experimental-threads = []

godot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ double-precision = ["godot-core/double-precision"]
1919
experimental-godot-api = ["godot-core/experimental-godot-api"]
2020
experimental-threads = ["godot-core/experimental-threads"]
2121
experimental-wasm = []
22-
formatted = ["godot-core/codegen-fmt"]
22+
rustfmt = ["godot-core/codegen-rustfmt"]
2323
lazy-function-tables = ["godot-core/codegen-lazy-fptrs"]
2424
serde = ["godot-core/serde"]
2525

godot/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
//! to explicitly opt in to any instabilities or rough edges that may result. Due to a limitation in Godot, it might currently not
103103
//! work Firefox browser.<br><br>
104104
//!
105-
//! * **`formatted`**
105+
//! * **`rustfmt`**
106106
//!
107107
//! Use rustfmt to format generated binding code. Because rustfmt is so slow, this is detrimental to initial compile time.
108108
//! Without it, we use a lightweight and fast custom formatter to enable basic human readability.

0 commit comments

Comments
 (0)