From ccd1afb22a9b22124a0556a617a80422ac3948f0 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Mon, 4 Jul 2022 22:32:35 +0200 Subject: [PATCH] code, docs, tests: Add support for dot-separation --- docs/index.rst | 11 +++++------ tests/CMakeLists.txt | 5 +++++ tests/helpers.cmake | 12 ++++++------ version.cmake | 14 +++++++++----- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 23fc3d5..a9382c0 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -18,9 +18,7 @@ The following version constructs are currently supported: .. code-block:: - "." ["." ["." ]] - "." ["." ["." ]] [["-"] ] ["+" ] - + "." ["." ["." ]] [["-"] ["." [...]]] ["+" ["." [...]]] Parsing ^^^^^^^ @@ -31,7 +29,7 @@ Parsing version(PARSE ) -Attempts to parse the version in ```` and stores the individual compoents into ``_``. If a component is not present in the given version, it will be undefined. If an error occurred, ``_ERROR`` will be defined and contain the error message. +Attempts to parse the version in ```` and stores the individual compoents into ``_``. If a component is not present in the given version, it will be undefined. If an error occurred, ``_ERROR`` will be defined and contain the error message. The ``PRERELEASE`` and ``BUILD`` components support the dot-separation specifier and will be turned into a list if they are encountered. Generating ^^^^^^^^^^ @@ -42,7 +40,7 @@ Generating version(GENERATE [COMPRESS] [MAJOR ] [MINOR ] [PATCH ] [TWEAK ] [PRERELEASE ] [BUILD ]) -Generates a version from the components provided and stores the result in ````. The components ```` and ```` will default to ``0`` if not provided. If an error occurred, ``_ERROR`` will be defined and contain the error message. +Generates a version from the components provided and stores the result in ````. The components ```` and ```` will default to ``0`` if not provided. If an error occurred, ``_ERROR`` will be defined and contain the error message. The ``PRERELEASE`` and ``BUILD`` components support the dot-separation specifier and will be converted from a list if encountered. Modifying ^^^^^^^^^ @@ -71,6 +69,7 @@ Compares the version ```` against ```` and stores the result in ````, ```` will contain the componenent name prefixed by ``>``. - If a component is numerical and the value is larger in ````, ```` will contain the componenent name prefixed by ``<``. - If a component is alphanumerical and the value is different in either, ```` will contain the component name with no prefix. -- In all other cases, ```` will be empty. - If an error occurred, ``_ERROR`` will be defined and contain the error message, and ```` will be undefined. +- In all other cases, ```` will be empty. +There is no special handling for dot-separated ``PRERELEASE`` and ``BUILD`` component. They will be treated as if they were a string, as special handling for these is up to the application. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4eff2a6..7cd2c32 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,7 +15,9 @@ test_generate("0.0.3" PATCH 3) test_generate("0.0.0.4" TWEAK 4) test_generate("0.0-5" COMPRESS PRERELEASE 5) test_generate("0.0a5" COMPRESS PRERELEASE a5) +test_generate("0.0a5.5" COMPRESS PRERELEASE "a5;5") test_generate("0.0+6" BUILD 6) +test_generate("0.0+6.8" BUILD "6;8") # - Invalid Cases test_generate("-1.0.0" FAIL MAJOR "-1") test_generate("0.-1.0" FAIL MINOR "-1") @@ -47,6 +49,9 @@ test_parse("1.2.3.4-5+6" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 PRERELEASE 5 BUILD 6) test_parse("1.2a5+6" MAJOR 1 MINOR 2 PRERELEASE a5 BUILD 6) test_parse("1.2.3a5+6" MAJOR 1 MINOR 2 PATCH 3 PRERELEASE a5 BUILD 6) test_parse("1.2.3.4a5+6" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 PRERELEASE a5 BUILD 6) +test_parse("1.2a5.b5+6.66" MAJOR 1 MINOR 2 PRERELEASE "a5;b5" BUILD "6;66") +test_parse("1.2.3a5.b5+6.66" MAJOR 1 MINOR 2 PATCH 3 PRERELEASE "a5;b5" BUILD "6;66") +test_parse("1.2.3.4a5.b5+6.66" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 PRERELEASE "a5;b5" BUILD "6;66") # - Invalid Cases test_parse("" FAIL) test_parse("cmake" FAIL) diff --git a/tests/helpers.cmake b/tests/helpers.cmake index 3a2294b..621ce4b 100644 --- a/tests/helpers.cmake +++ b/tests/helpers.cmake @@ -66,7 +66,7 @@ function(test_parse) set(MESSAGE "MAJOR component missing during parse.") set(FAILED ON) elseif(NOT (_TEST_MAJOR STREQUAL _ARGS_MAJOR)) - set(MESSAGE "MAJOR component mismatch.") + set(MESSAGE "MAJOR component mismatch: '${_TEST_MAJOR}' != '${_ARGS_MAJOR}'") set(FAILED ON) endif() endif() @@ -76,7 +76,7 @@ function(test_parse) set(MESSAGE "MINOR component missing during parse.") set(FAILED ON) elseif(NOT (_TEST_MINOR STREQUAL _ARGS_MINOR)) - set(MESSAGE "MINOR component mismatch.") + set(MESSAGE "MINOR component mismatch: '${_TEST_MINOR}' != '${_ARGS_MINOR}'") set(FAILED ON) endif() endif() @@ -86,7 +86,7 @@ function(test_parse) set(MESSAGE "PATCH component missing during parse.") set(FAILED ON) elseif(NOT (_TEST_PATCH STREQUAL _ARGS_PATCH)) - set(MESSAGE "PATCH component mismatch.") + set(MESSAGE "PATCH component mismatch: '${_TEST_PATCH}' != '${_ARGS_PATCH}'") set(FAILED ON) endif() endif() @@ -96,7 +96,7 @@ function(test_parse) set(MESSAGE "TWEAK component missing during parse.") set(FAILED ON) elseif(NOT (_TEST_TWEAK STREQUAL _ARGS_TWEAK)) - set(MESSAGE "TWEAK component mismatch.") + set(MESSAGE "TWEAK component mismatch: '${_TEST_TWEAK}' != '${_ARGS_TWEAK}'") set(FAILED ON) endif() endif() @@ -106,7 +106,7 @@ function(test_parse) set(MESSAGE "PRERELEASE component missing during parse.") set(FAILED ON) elseif(NOT (_TEST_PRERELEASE STREQUAL _ARGS_PRERELEASE)) - set(MESSAGE "PRERELEASE component mismatch.") + set(MESSAGE "PRERELEASE component mismatch: '${_TEST_PRERELEASE}' != '${_ARGS_PRERELEASE}'") set(FAILED ON) endif() endif() @@ -116,7 +116,7 @@ function(test_parse) set(MESSAGE "BUILD component missing during parse.") set(FAILED ON) elseif(NOT (_TEST_BUILD STREQUAL _ARGS_BUILD)) - set(MESSAGE "BUILD component mismatch.") + set(MESSAGE "BUILD component mismatch: '${_TEST_BUILD}' != '${_ARGS_BUILD}'") set(FAILED ON) endif() endif() diff --git a/version.cmake b/version.cmake index dcf8d13..eaba570 100644 --- a/version.cmake +++ b/version.cmake @@ -125,10 +125,11 @@ function(version) endif() # {5} = PreRelease if(CMAKE_MATCH_5) + string(REPLACE "." ";" CMAKE_MATCH_5 "${CMAKE_MATCH_5}") # Handle dot separation as list. string(SUBSTRING "${CMAKE_MATCH_5}" 0 1 _TMP) if(_TMP STREQUAL "-") - string(SUBSTRING "${CMAKE_MATCH_5}" 1 -1 _TMP) - set(${OUT_VAR}_PRERELEASE "${_TMP}" PARENT_SCOPE) + string(SUBSTRING "${CMAKE_MATCH_5}" 1 -1 CMAKE_MATCH_5) + set(${OUT_VAR}_PRERELEASE "${CMAKE_MATCH_5}" PARENT_SCOPE) else() set(${OUT_VAR}_PRERELEASE "${CMAKE_MATCH_5}" PARENT_SCOPE) endif() @@ -137,8 +138,9 @@ function(version) endif() # {6} = Build if(CMAKE_MATCH_6) - string(SUBSTRING "${CMAKE_MATCH_6}" 1 -1 _TMP) - set(${OUT_VAR}_BUILD "${_TMP}" PARENT_SCOPE) + string(REPLACE "." ";" CMAKE_MATCH_6 "${CMAKE_MATCH_6}") # Handle dot separation as list. + string(SUBSTRING "${CMAKE_MATCH_6}" 1 -1 CMAKE_MATCH_6) + set(${OUT_VAR}_BUILD "${CMAKE_MATCH_6}" PARENT_SCOPE) else() unset(${OUT_VAR}_BUILD PARENT_SCOPE) endif() @@ -211,10 +213,11 @@ function(version) # Do we have the pre-release component, and is it valid? if(DEFINED _ARGS_PRERELEASE) string(STRIP "${_ARGS_PRERELEASE}" _ARGS_PRERELEASE) + list(JOIN _ARGS_PRERELEASE "." _ARGS_PRERELEASE) if(_ARGS_PRERELEASE STREQUAL "") unset(_ARGS_PRERELEASE) elseif(_ARGS_PRERELEASE MATCHES [[^[a-zA-Z0-9]+[a-zA-Z0-9\-\.]*$]]) - if(_ARGS_COMPRESS AND (_ARGS_PRERELEASE MATCHES [[[a-zA-Z]+[a-zA-Z0-9\\-\\.]*$]])) + if(_ARGS_COMPRESS AND (_ARGS_PRERELEASE MATCHES [[[a-zA-Z]+[a-zA-Z0-9\-\.]*$]])) set(_ "${_}${_ARGS_PRERELEASE}") else() set(_ "${_}-${_ARGS_PRERELEASE}") @@ -228,6 +231,7 @@ function(version) # Do we have the build component, and is it valid? if(DEFINED _ARGS_BUILD) string(STRIP "${_ARGS_BUILD}" _ARGS_PRERELEASE) + list(JOIN _ARGS_BUILD "." _ARGS_BUILD) if(_ARGS_BUILD STREQUAL "") unset(_ARGS_BUILD) elseif(_ARGS_BUILD MATCHES [[^[a-zA-Z0-9]+[a-zA-Z0-9\-\.]*$]])