From c91517e00160571df27bfae32c6bf6dfb0ed2dcf Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Tue, 5 May 2020 19:16:41 -0700 Subject: [PATCH] Make CMake linter line length configurable Signed-off-by: Scott K Logan --- .../cmake/ament_lint_cmake.cmake | 8 +++++++- ament_lint_cmake/ament_lint_cmake/cmakelint.py | 14 ++++++++++++-- ament_lint_cmake/ament_lint_cmake/main.py | 5 +++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ament_cmake_lint_cmake/cmake/ament_lint_cmake.cmake b/ament_cmake_lint_cmake/cmake/ament_lint_cmake.cmake index 72983dca..3360e0a3 100644 --- a/ament_cmake_lint_cmake/cmake/ament_lint_cmake.cmake +++ b/ament_cmake_lint_cmake/cmake/ament_lint_cmake.cmake @@ -17,13 +17,16 @@ # # :param TESTNAME: the name of the test, default: "lint_cmake" # :type TESTNAME: string +# :param MAX_LINE_LENGTH: override the maximum line length, +# the default is defined in ament_lint_cmake +# :type MAX_LINE_LENGTH: integer # :param ARGN: the files or directories to check # :type ARGN: list of strings # # @public # function(ament_lint_cmake) - cmake_parse_arguments(ARG "" "TESTNAME" "" ${ARGN}) + cmake_parse_arguments(ARG "" "MAX_LINE_LENGTH;TESTNAME" "" ${ARGN}) if(NOT ARG_TESTNAME) set(ARG_TESTNAME "lint_cmake") endif() @@ -35,6 +38,9 @@ function(ament_lint_cmake) set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${ARG_TESTNAME}.xunit.xml") set(cmd "${ament_lint_cmake_BIN}" "--xunit-file" "${result_file}") + if(DEFINED ARG_MAX_LINE_LENGTH) + list(APPEND cmd "--linelength" "${ARG_MAX_LINE_LENGTH}") + endif() list(APPEND cmd ${ARG_UNPARSED_ARGUMENTS}) file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/ament_lint_cmake") diff --git a/ament_lint_cmake/ament_lint_cmake/cmakelint.py b/ament_lint_cmake/ament_lint_cmake/cmakelint.py index 72754bd3..b83fa1f5 100644 --- a/ament_lint_cmake/ament_lint_cmake/cmakelint.py +++ b/ament_lint_cmake/ament_lint_cmake/cmakelint.py @@ -84,6 +84,7 @@ def __init__(self): self.config = 0 self.errors = 0 self.spaces = 2 + self.linelength = 80 self.allowed_categories = _ERROR_CATEGORIES.split() def SetFilters(self, filters): @@ -110,6 +111,9 @@ def SetFilters(self, filters): def SetSpaces(self, spaces): self.spaces = int(spaces.strip()) + def SetLineLength(self, linelength): + self.linelength = int(linelength) + class _CMakePackageState(object): def __init__(self): self.sets = [] @@ -198,7 +202,7 @@ def CheckLineLength(filename, linenumber, clean_lines, errors): Check for lines longer than the recommended length """ line = clean_lines.raw_lines[linenumber] - if len(line) > 80: + if len(line) > _lint_state.linelength: if _RE_STRING.match(line): lineno = linenumber while lineno > 0: @@ -215,7 +219,8 @@ def CheckLineLength(filename, linenumber, clean_lines, errors): filename, linenumber, 'linelength', - 'Lines should be <= 80 characters long') + 'Lines should be <= %d characters long' % + (_lint_state.linelength)) def ContainsCommand(line): return _RE_COMMAND.match(line) @@ -448,6 +453,7 @@ def PrintCategories(): def ParseOptionFile(contents, ignore_space): filters = None spaces = None + linelength = None for line in contents: line = line.strip() if not line or line.startswith('#'): @@ -456,9 +462,13 @@ def ParseOptionFile(contents, ignore_space): filters = line.replace('filter=', '') if line.startswith('spaces='): spaces = line.replace('spaces=', '') + if line.startswith('linelength='): + linelength = line.replace('linelength=', '') _lint_state.SetFilters(filters) if spaces and not ignore_space: _lint_state.SetSpaces(spaces) + if linelength is not None: + _lint_state.SetLineLength(linelength) def ParseArgs(argv): try: diff --git a/ament_lint_cmake/ament_lint_cmake/main.py b/ament_lint_cmake/ament_lint_cmake/main.py index 91e17370..15a88c5c 100755 --- a/ament_lint_cmake/ament_lint_cmake/main.py +++ b/ament_lint_cmake/ament_lint_cmake/main.py @@ -48,6 +48,9 @@ def main(argv=sys.argv[1:]): default='', help='Filters for lint_cmake, for a list of filters see: ' 'https://github.com/richq/cmake-lint/blob/master/README.md#usage') + parser.add_argument( + '--linelength', metavar='N', type=int, + help='The maximum line length') # not using a file handle directly # in order to prevent leaving an empty file when something fails early parser.add_argument( @@ -70,6 +73,8 @@ def main(argv=sys.argv[1:]): # invoke cmake lint cmakelint._lint_state.config = cmakelint._DEFAULT_CMAKELINTRC cmakelint._lint_state.SetFilters(args.filters) + if args.linelength is not None: + cmakelint._lint_state.SetLineLength(str(args.linelength)) for filename in files: # hook into error reporting errors = []