11from typing import List
22
33import os .path
4- import platform
54import subprocess
65import sys
76import re
@@ -301,11 +300,13 @@ def format_inline_code(path: str):
301300 f .write (block )
302301 if suf == "go" :
303302 added = add_header (file )
304- os . system ( f' gofmt -w " { file } "' )
303+ subprocess . check_call ([ " gofmt" , "-w" , file ] )
305304 if added :
306305 remove_header (file )
307306 else :
308- os .system (f'npx clang-format -i --style=file "{ file } "' )
307+ subprocess .check_call (
308+ ["npx" , "clang-format" , "-i" , "--style=file" , file ]
309+ )
309310 with open (file , "r" , encoding = "utf-8" ) as f :
310311 new_block = f .read ()
311312 if not new_block .endswith ("\n " ):
@@ -329,7 +330,7 @@ def format_inline_code(path: str):
329330 file = f"{ root } /tmp.rs"
330331 with open (file , "w" , encoding = "utf-8" ) as f :
331332 f .write (block )
332- os . system ( f' rustfmt " { file } "' )
333+ subprocess . check_call ([ " rustfmt" , file ] )
333334 with open (file , "r" , encoding = "utf-8" ) as f :
334335 new_block = f .read ()
335336 if not new_block .endswith ("\n " ):
@@ -341,53 +342,10 @@ def format_inline_code(path: str):
341342 f .write (content )
342343
343344
344- def format_rust_files_linux ():
345- # The find command to locate and format all .rs files in Linux
346- find_command = 'find . -name "*.rs" -exec rustfmt {} \\ ;'
347-
348- # Execute the command
349- process = subprocess .Popen (
350- find_command ,
351- shell = True ,
352- stdout = subprocess .PIPE ,
353- stderr = subprocess .PIPE ,
354- text = True ,
355- )
356-
357- # Get the output and errors
358- stdout , stderr = process .communicate ()
359-
360- if process .returncode == 0 :
361- print ("Rust files formatted successfully on Linux!" )
362- print (stdout )
363- else :
364- print ("Error formatting Rust files on Linux:" )
365- print (stderr )
366-
367-
368- def format_rust_files_windows ():
369- # PowerShell command to format all .rs files recursively in Windows
370- ps_command = (
371- "Get-ChildItem -Recurse -Filter *.rs | ForEach-Object { rustfmt $_.FullName }"
372- )
373-
374- # Execute the PowerShell command
375- process = subprocess .Popen (
376- ["powershell" , "-Command" , ps_command ],
377- stdout = subprocess .PIPE ,
378- stderr = subprocess .PIPE ,
379- text = True ,
380- )
381-
382- # Get the output and errors
383- stdout , stderr = process .communicate ()
384-
385- if process .returncode == 0 :
386- print ("Rust files formatted successfully on Windows!" )
387- print (stdout )
388- else :
389- print ("Error formatting Rust files on Windows:" )
390- print (stderr )
345+ def format_rust_files (paths : List [str ]) -> None :
346+ rs_files = [p for p in paths if p .endswith (".rs" )]
347+ for i in range (0 , len (rs_files ), 64 ):
348+ subprocess .check_call (["rustfmt" , * rs_files [i : i + 64 ]])
391349
392350
393351def run ():
@@ -399,20 +357,11 @@ def run():
399357 if add_header (path ):
400358 headered .append (path )
401359 if any (path .endswith (suf ) for suf in ["c" , "cpp" , "java" ]):
402- # format with clang-format
403- os .system (f'npx clang-format -i --style=file "{ path } "' )
404-
405- # format with prettier
406- os .system ('npx prettier --write "**/*.{js,ts,php,sql,md}"' )
360+ subprocess .check_call (["npx" , "clang-format" , "-i" , "--style=file" , path ])
407361
408- # format with gofmt
409- os .system ("gofmt -w ." )
410-
411- # format with rustfmt
412- if platform .system () == "Linux" :
413- format_rust_files_linux ()
414- else :
415- format_rust_files_windows ()
362+ subprocess .check_call (["npx" , "prettier" , "--write" , "**/*.{js,ts,php,sql,md}" ])
363+ subprocess .check_call (["gofmt" , "-w" , "." ])
364+ format_rust_files (paths )
416365
417366 for path in headered :
418367 remove_header (path )
0 commit comments