@@ -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" ) ) ]
5555fn 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+
6495pub 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
104141pub 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}
0 commit comments