diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fab1d8a8..ca0cecadd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,12 +24,12 @@ endforeach() # Project details. -string(TIMESTAMP YEAR "%Y") -string(TIMESTAMP MONTH "%m") -string(TIMESTAMP DAY "%d") +file(READ "${CMAKE_SOURCE_DIR}/VERSION.txt" PROJECT_VERSION) + +string(STRIP "${PROJECT_VERSION}" PROJECT_VERSION) project(libOpenCOR - VERSION "0.${YEAR}${MONTH}${DAY}.0") + VERSION "${PROJECT_VERSION}") string(TOLOWER "${CMAKE_PROJECT_NAME}" CMAKE_PROJECT_NAME_LC) diff --git a/VERSION.txt b/VERSION.txt new file mode 100644 index 000000000..d7e6fe214 --- /dev/null +++ b/VERSION.txt @@ -0,0 +1 @@ +0.20250101.0 diff --git a/newversion b/newversion new file mode 100755 index 000000000..9c196cc97 --- /dev/null +++ b/newversion @@ -0,0 +1,38 @@ +#!/bin/sh + +# Major version. + +major_version=0 + +# Minor version. + +now=$(date +%Y%m%d) +minor_version=$now + +# Patch version. + +patch_version=0 + +if latest_tag_commit=$(git rev-list --tags --max-count=1 2> /dev/null); then + if git_describe=$(git describe --tags "$latest_tag_commit" 2> /dev/null); then + if echo "$git_describe" | grep -Eo '^v?[0-9]+\.[0-9]+\.[0-9]+' > /dev/null; then + minor=$(echo "$git_describe" | cut -d. -f2) + patch=$(echo "$git_describe" | cut -d. -f3) + patch_version=$patch + + if [ "$minor" = "$minor_version" ]; then + patch_version=$((patch_version + 1)) + else + patch_version=0 + fi + fi + fi +fi + +# Full version. + +version="$major_version.$minor_version.$patch_version" + +# Update our version file. + +echo $version > $(cd $(dirname $0); pwd)/VERSION.txt diff --git a/newversion.bat b/newversion.bat new file mode 100755 index 000000000..6e43f136b --- /dev/null +++ b/newversion.bat @@ -0,0 +1,50 @@ +@ECHO OFF + +SETLOCAL ENABLEDELAYEDEXPANSION + +REM Major version. + +SET MajorVersion=0 + +REM Minor version. + +FOR /F "tokens=2 delims==" %%I IN ('wmic os get localdatetime /value') DO SET Now=%%I +SET MinorVersion=!Now:~0,8! +ECHO "MinorVersion: !MinorVersion!" + +REM Patch version. + +SET PatchVersion=0 + +FOR /F %%I IN ('git rev-list --tags --max-count=1 2^> NUL') DO ( + SET LatestTagCommit=%%I +) +ECHO "LatestTagCommit: !LatestTagCommit!" + +IF DEFINED LatestTagCommit ( + FOR /F %%I IN ('git describe --tags !LatestTagCommit! 2^> NUL') DO ( + SET GitDescribe=%%I + ) + ECHO "GitDescribe: !GitDescribe!" + + FOR /F "tokens=2,3 delims=." %%A IN ("!GitDescribe!") DO ( + SET MinorVersion=%%A + ECHO "MinorVersion: !MinorVersion!" + SET PatchVersion=%%B + ECHO "PatchVersion: !PatchVersion!" + + IF "!MinorVersion!"=="!MinorVersion!" ( + SET /A PatchVersion+=1 + ) ELSE ( + SET PatchVersion=0 + ) + ) +) + +REM Full version. + +SET Version=!MajorVersion!.!MinorVersion!.!PatchVersion! + +REM Update our version file. + +ECHO !Version! > "%~dp0VERSION.txt" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index dd9226ba5..86e7b5ebe 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,6 +25,11 @@ if(NOT BASE64ENCODER_EXE) message(FATAL_ERROR "base64encoder could not be built...") endif() +# Copy of our version file over. + +file(COPY ${CMAKE_SOURCE_DIR}/VERSION.txt + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + # Include our different tests. include(api/file/tests.cmake) diff --git a/tests/api/version/tests.cpp b/tests/api/version/tests.cpp index 6134b825d..6ecb0fcdb 100644 --- a/tests/api/version/tests.cpp +++ b/tests/api/version/tests.cpp @@ -18,52 +18,45 @@ limitations under the License. #include #include +#include +#include #include TEST(VersionTest, libOpenCOR) { - static const auto VERSION_MAJOR = 0; - static const auto VERSION_PATCH = 0; - - auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - std::tm tm {}; - -#ifdef BUILDING_ON_WINDOWS - localtime_s(&tm, &now); // NOLINT -#else - localtime_r(&now, &tm); // NOLINT -#endif - - static const auto NINETEEN_HUNDRED = 1900; - static const auto ONE = 1; - - const auto year = NINETEEN_HUNDRED + tm.tm_year; - const auto month = ONE + tm.tm_mon; - const auto day = tm.tm_mday; - static const uint64_t TEN_BILLION = 10000000000; static const uint64_t TEN_THOUSAND = 10000; static const uint64_t HUNDRED = 100; static const uint64_t TEN = 10; + std::ifstream versionFile("../../VERSION.txt"); + std::string versionString; + std::getline(versionFile, versionString); + + versionFile.close(); + + const std::regex versionRegex(R"((\d+)\.(\d{8})\.(\d+))"); + std::smatch match; + + ASSERT_TRUE(std::regex_match(versionString, match, versionRegex)); + + const uint64_t majorVersion = static_cast(std::stoi(match[1])); + const uint64_t year = static_cast(std::stoi(match[2].str().substr(0, 4))); + const uint64_t month = static_cast(std::stoi(match[2].str().substr(4, 2))); + const uint64_t day = static_cast(std::stoi(match[2].str().substr(6, 2))); + const uint64_t patchVersion = static_cast(std::stoi(match[3])); uint64_t version = 0; - auto number = TEN_BILLION * VERSION_MAJOR + HUNDRED * (TEN_THOUSAND * static_cast(year) + HUNDRED * static_cast(month) + static_cast(day)) + VERSION_PATCH; + auto number = TEN_BILLION * majorVersion + + HUNDRED * (TEN_THOUSAND * year + HUNDRED * month + day) + + patchVersion; for (int i = 0; number != 0; i += 4) { version |= (number % TEN) << i; // NOLINT number /= TEN; } - static const int INT_TEN = 10; - - auto versionString = std::to_string(VERSION_MAJOR) - + "." - + std::to_string(year) + ((month < INT_TEN) ? "0" : "") + std::to_string(month) + ((day < INT_TEN) ? "0" : "") + std::to_string(day) - + "." - + std::to_string(VERSION_PATCH); - EXPECT_EQ(version, libOpenCOR::version()); - EXPECT_EQ(versionString.data(), libOpenCOR::versionString()); + EXPECT_EQ(versionString, libOpenCOR::versionString()); } TEST(VersionTest, Clang) diff --git a/tests/bindings/javascript/version.test.js b/tests/bindings/javascript/version.test.js index 088aad3cb..8f8a70295 100644 --- a/tests/bindings/javascript/version.test.js +++ b/tests/bindings/javascript/version.test.js @@ -14,25 +14,36 @@ See the License for the specific language governing permissions and limitations under the License. */ +import fs from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; import libOpenCOR from "./libopencor.js"; const loc = await libOpenCOR(); describe("Version tests", () => { test("libOpenCOR", () => { - const versionMajor = 0; - const versionPatch = 0; - - const now = new Date(); - const year = now.getFullYear(); - const month = now.getMonth() + 1; - const day = now.getDate(); + const versionStr = fs + .readFileSync( + path.resolve( + path.dirname(fileURLToPath(import.meta.url)), + "../../VERSION.txt", + ), + "utf8", + ) + .trim(); + const [majorVersionStr, dateStr, patchVersionStr] = versionStr.split("."); + const majorVersion = Number(majorVersionStr); + const patchVersion = Number(patchVersionStr); + const year = Number(dateStr.slice(0, 4)); + const month = Number(dateStr.slice(4, 6)); + const day = Number(dateStr.slice(6, 8)); let version = BigInt(0); let number = BigInt( - 10000000000 * versionMajor + + 10000000000 * majorVersion + 100 * (10000 * year + 100 * month + day) + - versionPatch, + patchVersion, ); let i = BigInt(0); @@ -47,7 +58,7 @@ describe("Version tests", () => { expect(loc.version()).toBe(version); expect(loc.versionString()).toBe( - `${versionMajor}.${year}${String(month).padStart(2, "0")}${String(day).padStart(2, "0")}.${versionPatch}`, + `${majorVersion}.${year}${String(month).padStart(2, "0")}${String(day).padStart(2, "0")}.${patchVersion}`, ); }); diff --git a/tests/bindings/python/test_version.py b/tests/bindings/python/test_version.py index ad2afe763..e4e0d3766 100644 --- a/tests/bindings/python/test_version.py +++ b/tests/bindings/python/test_version.py @@ -13,25 +13,27 @@ # limitations under the License. -import datetime import libopencor as loc +import pathlib -version_major = 0 -version_patch = 0 +with open(pathlib.Path(__file__).parent.parent.parent / "VERSION.txt") as file: + version_str = file.read().strip() -now = datetime.datetime.now() -year = now.year -month = now.month -day = now.day +major_version, date_str, patch_version = version_str.split(".") +major_version = int(major_version) +patch_version = int(patch_version) +year = int(date_str[:4]) +month = int(date_str[4:6]) +day = int(date_str[6:8]) def test_version(): version = 0 number = ( - 10000000000 * version_major + 10000000000 * major_version + 100 * (10000 * year + 100 * month + day) - + version_patch + + patch_version ) i = 0 @@ -45,7 +47,7 @@ def test_version(): def test_version_string(): - version = f"{version_major}.{year}{month:02}{day:02}.{version_patch}" + version = f"{major_version}.{year}{month:02}{day:02}.{patch_version}" assert loc.__version__ == version