diff --git a/src/cfengine_cli/dev.py b/src/cfengine_cli/dev.py index 813dfa9..edb8b9d 100644 --- a/src/cfengine_cli/dev.py +++ b/src/cfengine_cli/dev.py @@ -8,8 +8,7 @@ update_dependency_tables as _update_dependency_tables, print_release_dependency_tables, ) -from cfengine_cli.docs import update_docs, check_docs -from cfengine_cli.format import format_paths +from cfengine_cli import docs from cfengine_cli.syntax_tree import syntax_tree @@ -48,21 +47,15 @@ def print_dependency_tables(args) -> int: return print_release_dependency_tables(args.versions) -def format_docs(files) -> int: +def format_docs() -> int: _expect_repo("documentation") - ret = update_docs(files) - # Also run the same logic as `cfengine format` so .cf / .json files - # are formatted without having to run that command manually. - ret |= format_paths(files, line_length=80, check=False) + ret = docs.format_docs() return ret -def lint_docs(files) -> int: +def lint_docs() -> int: _expect_repo("documentation") - ret = check_docs() - # Also run the same logic as `cfengine format --check` so .cf / .json - # files are checked without having to run that command manually. - ret |= format_paths(files, line_length=80, check=True) + ret = docs.lint_docs() return ret @@ -80,9 +73,9 @@ def dispatch_dev_subcommand(subcommand, args) -> int: if subcommand == "print-dependency-tables": return print_dependency_tables(args) if subcommand == "format-docs": - return format_docs(args.files) + return format_docs() if subcommand == "lint-docs": - return lint_docs(args.files) + return lint_docs() if subcommand == "syntax-tree": return syntax_tree(args.file) if subcommand == "generate-release-information": diff --git a/src/cfengine_cli/docs.py b/src/cfengine_cli/docs.py index 2347dd7..c1f2300 100644 --- a/src/cfengine_cli/docs.py +++ b/src/cfengine_cli/docs.py @@ -16,7 +16,7 @@ from cfbs.pretty import pretty_file from cfbs.utils import find -from cfengine_cli.format import format_policy_file +from cfengine_cli.format import format_policy_file, format_paths from cfengine_cli.lint import lint_args, lint_policy_file_snippet from cfengine_cli.utils import UserError @@ -420,12 +420,11 @@ def _run_prettier(path) -> bool: return formatted -def _update_docs_single_arg(path): +def _format_docs_single_arg(path): if not os.path.exists(path): raise UserError(f"The specified file/folder '{path}' does not exist") if not os.path.isfile(path) and not os.path.isdir(path): raise UserError(f"Argument '{path}' is not a file or a folder") - formatted = False if os.path.isdir(path) or path.endswith(".py"): if _run_black(path): @@ -443,13 +442,16 @@ def _update_docs_single_arg(path): replace=True, cleanup=True, ) - + if os.path.isdir(path) or path.endswith(".cf"): + r = format_paths([path], line_length=80, check=False) + if r != 0: + formatted = True return formatted -def update_docs(paths) -> int: +def format_docs() -> int: """ - Iterate through entire docs repo, or specified subfolders / files, autoformatting as much as possible: + Iterate through entire docs repo (CWD) autoformatting as much as possible: - python code with black - markdown files with prettier - code blocks inside markdown files are formatted for the formats supported by prettier @@ -458,23 +460,27 @@ def update_docs(paths) -> int: Run by the command: cfengine dev format-docs """ - if not paths: - _update_docs_single_arg(".") - return 0 - for path in paths: - _update_docs_single_arg(path) + _format_docs_single_arg(".") return 0 -def check_docs() -> int: +def lint_docs() -> int: """ Run checks / tests on docs. Run by the command: cfengine dev lint-docs""" + + # cfengine lint r = lint_args(["."], strict=False) if r != 0: + print(f"Failure - {r} errors while running cfengine lint .") return r + + # cfengine format --check + r = format_paths(["."], line_length=80, check=True) + + # Extract code blocks from markdown files and run checks on them: _process_markdown_code_blocks( path=".", languages=["json", "cf3"], @@ -485,4 +491,5 @@ def check_docs() -> int: replace=False, cleanup=True, ) + print("Success - all files look correct") return 0