Skip to content

Commit 2ba2ceb

Browse files
committed
CFE-4665: cfengine dev format docs should tell you what files it formatted
Ticket: CFE-4665
1 parent 71bf087 commit 2ba2ceb

1 file changed

Lines changed: 41 additions & 15 deletions

File tree

src/cfengine_cli/docs.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

218221
def _translate_language(x):
@@ -256,7 +259,6 @@ def _process_markdown_code_blocks(
256259
return
257260

258261
if syntax_check:
259-
# We currently only print the filenames during linting, not formatting
260262
print(
261263
f"Processing code blocks (snippets) inside {origin_paths_len} markdown files:"
262264
)
@@ -325,13 +327,14 @@ def _process_markdown_code_blocks(
325327
fn_check_output()
326328

327329
if autoformat and "noautoformat" not in flags:
328-
fn_autoformat(
330+
if fn_autoformat(
329331
origin_path,
330332
snippet_path,
331333
language,
332334
first_line,
333335
last_line,
334-
)
336+
):
337+
print(f"File '{os.path.abspath(origin_path)}' was reformatted")
335338

336339
if replace and "noreplace" not in flags:
337340
offset = fn_replace(
@@ -349,7 +352,7 @@ def _process_markdown_code_blocks(
349352
def _run_formatter(tool, args, cwd, install_hint):
350353
print(f"Formatting with {tool}...")
351354
try:
352-
subprocess.run(
355+
result = subprocess.run(
353356
[tool, *args],
354357
capture_output=True,
355358
text=True,
@@ -358,13 +361,22 @@ def _run_formatter(tool, args, cwd, install_hint):
358361
)
359362
except:
360363
raise UserError(f"Encountered an error running {tool}\nInstall: {install_hint}")
364+
return result
365+
366+
367+
def _run_black(path) -> bool:
368+
result = _run_formatter("black", [path], ".", "pipx install black")
361369

370+
formatted = False
371+
for line in result.stderr.splitlines():
372+
if line.startswith("reformatted "):
373+
print(f"File '{os.path.abspath(line.split()[1])}' was reformatted")
374+
formatted = True
362375

363-
def _run_black(path):
364-
_run_formatter("black", [path], ".", "pipx install black")
376+
return formatted
365377

366378

367-
def _run_prettier(path):
379+
def _run_prettier(path) -> bool:
368380
assert os.path.exists(path)
369381

370382
args = [path]
@@ -379,16 +391,25 @@ def _run_prettier(path):
379391
if any(find(path, extension=".md")):
380392
args.append("**.md")
381393
if not args:
382-
return
394+
return False
383395
# Warning: Beware of shell expansion if you try to run this in your terminal.
384396
# Wrong: prettier --write **.markdown **.md
385397
# Right: prettier --write '**.markdown' '**.md'
386-
_run_formatter(
398+
result = _run_formatter(
387399
"prettier",
388-
["--embedded-language-formatting", "off", "--write", *args],
400+
["--embedded-language-formatting", "off", "--write", "--list-different", *args],
389401
directory,
390402
"npm install --global prettier",
391403
)
404+
formatted = False
405+
406+
for line in result.stdout.splitlines():
407+
if line.strip():
408+
print(
409+
f"File '{os.path.abspath(os.path.join(directory, line))}' was reformatted"
410+
)
411+
formatted = True
412+
return formatted
392413

393414

394415
def _update_docs_single_arg(path):
@@ -397,10 +418,13 @@ def _update_docs_single_arg(path):
397418
if not os.path.isfile(path) and not os.path.isdir(path):
398419
raise UserError(f"Argument '{path}' is not a file or a folder")
399420

421+
formatted = False
400422
if os.path.isdir(path) or path.endswith(".py"):
401-
_run_black(path)
423+
if _run_black(path):
424+
formatted = True
402425
if os.path.isdir(path) or path.endswith((".md", ".markdown")):
403-
_run_prettier(path)
426+
if _run_prettier(path):
427+
formatted = True
404428
print(f"Formatting code blocks in '{path}'...")
405429
_process_markdown_code_blocks(
406430
path=path,
@@ -413,6 +437,8 @@ def _update_docs_single_arg(path):
413437
cleanup=True,
414438
)
415439

440+
return formatted
441+
416442

417443
def update_docs(paths) -> int:
418444
"""

0 commit comments

Comments
 (0)