@@ -105,7 +105,7 @@ def fn_extract(origin_path, snippet_path, _language, first_line, last_line):
105105 code_snippet = "\n " .join (content .split ("\n " )[first_line + 1 : last_line - 1 ])
106106
107107 with open (snippet_path , "w" ) as f :
108- f .write (code_snippet )
108+ f .write (code_snippet + " \n " )
109109 except IOError :
110110 raise UserError (f"Couldn't open '{ origin_path } ' or '{ snippet_path } '" )
111111
@@ -194,12 +194,14 @@ def fn_replace(origin_path, snippet_path, _language, first_line, last_line, inde
194194 return offset # TODO: offset can be undefined here
195195
196196
197- def fn_autoformat (_origin_path , snippet_path , language , _first_line , _last_line ):
197+ def fn_autoformat (
198+ _origin_path , snippet_path , language , _first_line , _last_line
199+ ) -> bool :
198200 assert language in ("cf" , "json" )
199201 match language :
200202 case "json" :
201203 try :
202- pretty_file (snippet_path )
204+ return pretty_file (snippet_path )
203205 except FileNotFoundError :
204206 raise UserError (
205207 f"Couldn't find the file '{ snippet_path } '. Run --extract to extract the inline code."
@@ -213,6 +215,7 @@ def fn_autoformat(_origin_path, snippet_path, language, _first_line, _last_line)
213215 case "cf" :
214216 # Note: Dead code - Not used for CFEngine policy yet
215217 format_policy_file (snippet_path , 80 , False )
218+ return False
216219
217220
218221def _translate_language (x ):
@@ -250,13 +253,13 @@ def _process_markdown_code_blocks(
250253
251254 origin_paths = sorted (parsed_markdowns ["files" ].keys ())
252255 origin_paths_len = len (origin_paths )
256+ formatted = False
253257
254258 if origin_paths_len == 0 :
255259 print ("No markdown files found." )
256260 return
257261
258262 if syntax_check :
259- # We currently only print the filenames during linting, not formatting
260263 print (
261264 f"Processing code blocks (snippets) inside { origin_paths_len } markdown files:"
262265 )
@@ -325,13 +328,17 @@ def _process_markdown_code_blocks(
325328 fn_check_output ()
326329
327330 if autoformat and "noautoformat" not in flags :
328- fn_autoformat (
331+ if fn_autoformat (
329332 origin_path ,
330333 snippet_path ,
331334 language ,
332335 first_line ,
333336 last_line ,
334- )
337+ ):
338+ if not formatted :
339+ print (f"Formatting code blocks in '{ path } '..." )
340+ print (f"File '{ os .path .abspath (origin_path )} ' was reformatted" )
341+ formatted = True
335342
336343 if replace and "noreplace" not in flags :
337344 offset = fn_replace (
@@ -347,9 +354,8 @@ def _process_markdown_code_blocks(
347354
348355
349356def _run_formatter (tool , args , cwd , install_hint ):
350- print (f"Formatting with { tool } ..." )
351357 try :
352- subprocess .run (
358+ result = subprocess .run (
353359 [tool , * args ],
354360 capture_output = True ,
355361 text = True ,
@@ -358,13 +364,24 @@ def _run_formatter(tool, args, cwd, install_hint):
358364 )
359365 except :
360366 raise UserError (f"Encountered an error running { tool } \n Install: { install_hint } " )
367+ return result
368+
369+
370+ def _run_black (path ) -> bool :
371+ result = _run_formatter ("black" , [path ], "." , "pipx install black" )
361372
373+ formatted = False
374+ for line in result .stderr .splitlines ():
375+ if line .startswith ("reformatted " ):
376+ if not formatted :
377+ print ("Formatting with black..." )
378+ print (f"File '{ os .path .abspath (line .split ()[1 ])} ' was reformatted" )
379+ formatted = True
362380
363- def _run_black (path ):
364- _run_formatter ("black" , [path ], "." , "pipx install black" )
381+ return formatted
365382
366383
367- def _run_prettier (path ):
384+ def _run_prettier (path ) -> bool :
368385 assert os .path .exists (path )
369386
370387 args = [path ]
@@ -379,16 +396,27 @@ def _run_prettier(path):
379396 if any (find (path , extension = ".md" )):
380397 args .append ("**.md" )
381398 if not args :
382- return
399+ return False
383400 # Warning: Beware of shell expansion if you try to run this in your terminal.
384401 # Wrong: prettier --write **.markdown **.md
385402 # Right: prettier --write '**.markdown' '**.md'
386- _run_formatter (
403+ result = _run_formatter (
387404 "prettier" ,
388- ["--embedded-language-formatting" , "off" , "--write" , * args ],
405+ ["--embedded-language-formatting" , "off" , "--write" , "--list-different" , * args ],
389406 directory ,
390407 "npm install --global prettier" ,
391408 )
409+ formatted = False
410+
411+ for line in result .stdout .splitlines ():
412+ if line .strip ():
413+ if not formatted :
414+ print ("Formatting with prettier..." )
415+ print (
416+ f"File '{ os .path .abspath (os .path .join (directory , line ))} ' was reformatted"
417+ )
418+ formatted = True
419+ return formatted
392420
393421
394422def _update_docs_single_arg (path ):
@@ -397,11 +425,13 @@ def _update_docs_single_arg(path):
397425 if not os .path .isfile (path ) and not os .path .isdir (path ):
398426 raise UserError (f"Argument '{ path } ' is not a file or a folder" )
399427
428+ formatted = False
400429 if os .path .isdir (path ) or path .endswith (".py" ):
401- _run_black (path )
430+ if _run_black (path ):
431+ formatted = True
402432 if os .path .isdir (path ) or path .endswith ((".md" , ".markdown" )):
403- _run_prettier (path )
404- print ( f"Formatting code blocks in ' { path } '..." )
433+ if _run_prettier (path ):
434+ formatted = True
405435 _process_markdown_code_blocks (
406436 path = path ,
407437 languages = ["json" ], # TODO: Add cfengine3 here
@@ -413,6 +443,8 @@ def _update_docs_single_arg(path):
413443 cleanup = True ,
414444 )
415445
446+ return formatted
447+
416448
417449def update_docs (paths ) -> int :
418450 """
0 commit comments