From cb34b3515f3b948e1a6bdd97f5e851e3af8ef873 Mon Sep 17 00:00:00 2001 From: Michael Rapp Date: Wed, 3 Jan 2024 01:05:21 +0100 Subject: [PATCH] Add build targets "format_md" and "test_format_md". --- scons/code_style.py | 26 ++++++++++++++++++++++++++ scons/sconstruct.py | 24 +++++++++++++++--------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/scons/code_style.py b/scons/code_style.py index a2ebe1fc47..dc68943b29 100644 --- a/scons/code_style.py +++ b/scons/code_style.py @@ -43,6 +43,16 @@ def __clang_format(directory: str, enforce_changes: bool = False): run_program('clang-format', *args, *cpp_header_files, *cpp_source_files) +def __mdformat(directory: str, enforce_changes: bool = False): + md_files = glob(path.join(directory, '*.md')) + args = ['--number', '--wrap', 'no'] + + if not enforce_changes: + args.append('--check') + + run_program('mdformat', *args, *md_files) + + def check_python_code_style(**_): """ Checks if the Python source files adhere to the code style definitions. If this is not the case, an error is raised. @@ -82,3 +92,19 @@ def enforce_cpp_code_style(**_): directory = CPP_MODULE.root_dir print('Formatting C++ code in directory "' + directory + '"...') __clang_format(directory, enforce_changes=True) + + +def check_md_code_style(**_): + """ + Check if the Markdown files adhere to the code style definitions. If this is not the case, an error is raised. + """ + print('Checking Markdown code style in the root directory...') + __mdformat('.') + + +def enforce_md_code_style(**_): + """ + Enforces the Markdown files to adhere to the code style definitions. + """ + print('Formatting Markdown code style in the root directory...') + __mdformat('.', enforce_changes=True) diff --git a/scons/sconstruct.py b/scons/sconstruct.py index 244d5d9419..3095ff8171 100644 --- a/scons/sconstruct.py +++ b/scons/sconstruct.py @@ -8,7 +8,8 @@ from functools import reduce from os import path -from code_style import check_cpp_code_style, check_python_code_style, enforce_cpp_code_style, enforce_python_code_style +from code_style import check_cpp_code_style, check_md_code_style, check_python_code_style, enforce_cpp_code_style, \ + enforce_md_code_style, enforce_python_code_style from compilation import compile_cpp, compile_cython, install_cpp, install_cython, setup_cpp, setup_cython from documentation import apidoc_cpp, apidoc_cpp_tocfile, apidoc_python, apidoc_python_tocfile, doc from modules import BUILD_MODULE, CPP_MODULE, DOC_MODULE, PYTHON_MODULE @@ -33,9 +34,11 @@ def __print_if_clean(environment, message: str): TARGET_NAME_TEST_FORMAT = 'test_format' TARGET_NAME_TEST_FORMAT_PYTHON = TARGET_NAME_TEST_FORMAT + '_python' TARGET_NAME_TEST_FORMAT_CPP = TARGET_NAME_TEST_FORMAT + '_cpp' +TARGET_NAME_TEST_FORMAT_MD = TARGET_NAME_TEST_FORMAT + '_md' TARGET_NAME_FORMAT = 'format' TARGET_NAME_FORMAT_PYTHON = TARGET_NAME_FORMAT + '_python' TARGET_NAME_FORMAT_CPP = TARGET_NAME_FORMAT + '_cpp' +TARGET_NAME_FORMAT_MD = TARGET_NAME_FORMAT + '_md' TARGET_NAME_VENV = 'venv' TARGET_NAME_COMPILE = 'compile' TARGET_NAME_COMPILE_CPP = TARGET_NAME_COMPILE + '_cpp' @@ -54,11 +57,12 @@ def __print_if_clean(environment, message: str): TARGET_NAME_DOC = 'doc' VALID_TARGETS = { - TARGET_NAME_TEST_FORMAT, TARGET_NAME_TEST_FORMAT_PYTHON, TARGET_NAME_TEST_FORMAT_CPP, TARGET_NAME_FORMAT, - TARGET_NAME_FORMAT_PYTHON, TARGET_NAME_FORMAT_CPP, TARGET_NAME_VENV, TARGET_NAME_COMPILE, TARGET_NAME_COMPILE_CPP, - TARGET_NAME_COMPILE_CYTHON, TARGET_NAME_INSTALL, TARGET_NAME_INSTALL_CPP, TARGET_NAME_INSTALL_CYTHON, - TARGET_NAME_BUILD_WHEELS, TARGET_NAME_INSTALL_WHEELS, TARGET_NAME_TESTS, TARGET_NAME_TESTS_CPP, - TARGET_NAME_TESTS_PYTHON, TARGET_NAME_APIDOC, TARGET_NAME_APIDOC_CPP, TARGET_NAME_APIDOC_PYTHON, TARGET_NAME_DOC + TARGET_NAME_TEST_FORMAT, TARGET_NAME_TEST_FORMAT_PYTHON, TARGET_NAME_TEST_FORMAT_CPP, TARGET_NAME_TEST_FORMAT_MD, + TARGET_NAME_FORMAT, TARGET_NAME_FORMAT_PYTHON, TARGET_NAME_FORMAT_CPP, TARGET_NAME_FORMAT_MD, TARGET_NAME_VENV, + TARGET_NAME_COMPILE, TARGET_NAME_COMPILE_CPP, TARGET_NAME_COMPILE_CYTHON, TARGET_NAME_INSTALL, + TARGET_NAME_INSTALL_CPP, TARGET_NAME_INSTALL_CYTHON, TARGET_NAME_BUILD_WHEELS, TARGET_NAME_INSTALL_WHEELS, + TARGET_NAME_TESTS, TARGET_NAME_TESTS_CPP, TARGET_NAME_TESTS_PYTHON, TARGET_NAME_APIDOC, TARGET_NAME_APIDOC_CPP, + TARGET_NAME_APIDOC_PYTHON, TARGET_NAME_DOC } DEFAULT_TARGET = TARGET_NAME_INSTALL_WHEELS @@ -78,14 +82,16 @@ def __print_if_clean(environment, message: str): # Define targets for checking code style definitions... target_test_format_python = __create_phony_target(env, TARGET_NAME_TEST_FORMAT_PYTHON, action=check_python_code_style) target_test_format_cpp = __create_phony_target(env, TARGET_NAME_TEST_FORMAT_CPP, action=check_cpp_code_style) +target_test_format_md = __create_phony_target(env, TARGET_NAME_TEST_FORMAT_MD, action=check_md_code_style) target_test_format = __create_phony_target(env, TARGET_NAME_TEST_FORMAT) -env.Depends(target_test_format, [target_test_format_python, target_test_format_cpp]) +env.Depends(target_test_format, [target_test_format_python, target_test_format_cpp, target_test_format_md]) # Define targets for enforcing code style definitions... target_format_python = __create_phony_target(env, TARGET_NAME_FORMAT_PYTHON, action=enforce_python_code_style) target_format_cpp = __create_phony_target(env, TARGET_NAME_FORMAT_CPP, action=enforce_cpp_code_style) -target_format = __create_phony_target(env, TARGET_NAME_FORMAT) -env.Depends(target_format, [target_format_python, target_format_cpp]) +target_format_md = __create_phony_target(env, TARGET_NAME_FORMAT_MD, action=enforce_md_code_style) +target_format = __create_phony_target(env, TARGET_NAME_FORMAT, TARGET_NAME_FORMAT_MD) +env.Depends(target_format, [target_format_python, target_format_cpp, target_format_md]) # Define target for installing runtime dependencies... target_venv = __create_phony_target(env, TARGET_NAME_VENV, action=install_runtime_dependencies)