From 715738f977f0001ae00a1cce384da06549940b09 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 17 Aug 2023 23:39:42 +0300 Subject: [PATCH 1/4] chore(deps): Add new dependency on pure Lua semver parser --- .cirrus.yml | 1 + configure.ac | 1 + flake.nix | 1 + sile.rockspec.in | 1 + 4 files changed, 4 insertions(+) diff --git a/.cirrus.yml b/.cirrus.yml index fe2e5dbc7..3f9877d36 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -40,6 +40,7 @@ task: - luarocks54 install luarepl - luarocks54 install luautf8 - luarocks54 install penlight + - luarocks54 install semver - luarocks54 install vstruct bootstrap_script: - git fetch --prune --tags ||: diff --git a/configure.ac b/configure.ac index ad1ebe932..ae85d9164 100644 --- a/configure.ac +++ b/configure.ac @@ -182,6 +182,7 @@ AM_COND_IF([DEPENDENCY_CHECKS], [ AX_LUA_MODULE([repl], [luarepl]) AX_LUA_MODULE([ssl], [luasec]) AX_LUA_MODULE([socket], [luasocket]) + AX_LUA_MODULE([semver], [semver]) AX_LUA_MODULE([lua-utf8], [luautf8]) AX_LUA_MODULE([pl], [penlight]) AX_LUA_MODULE([vstruct], [vstruct]) diff --git a/flake.nix b/flake.nix index ea0adc2ca..aa1278d66 100644 --- a/flake.nix +++ b/flake.nix @@ -58,6 +58,7 @@ luasocket luautf8 penlight + semver vstruct # lua packages needed for testing busted diff --git a/sile.rockspec.in b/sile.rockspec.in index 24f2baf4d..c40745559 100644 --- a/sile.rockspec.in +++ b/sile.rockspec.in @@ -44,6 +44,7 @@ dependencies = { "luasocket == 3.1.0-1", "luautf8 == 0.1.5-1", "penlight == 1.13.1-1", + "semver == 1.2.1-1", "vstruct == 2.1.1-1" } From 5f0fed51b2a9597272da62f00c15f8836f8c7bd1 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Fri, 18 Aug 2023 00:26:46 +0300 Subject: [PATCH 2/4] fix(utilities): Use real semver parser for deprecation warnings --- core/utilities.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/utilities.lua b/core/utilities.lua index 4959efe61..4c42d111c 100644 --- a/core/utilities.lua +++ b/core/utilities.lua @@ -1,5 +1,6 @@ local bitshim = require("bitshim") local luautf8 = require("lua-utf8") +local semver = require("semver") local utilities = {} @@ -89,18 +90,19 @@ utilities.gtoke = function (string, pattern) end utilities.deprecated = function (old, new, warnat, errorat, extra) + warnat, errorat = semver(warnat or 0), semver(errorat or 0) + local current = SILE.version and semver(SILE.version:match("v([0-9]*.[0-9]*.[0-9]*)")) or warnat -- SILE.version is defined *after* most of SILE loads. It’s available at -- runtime but not useful if we encounter deprecated code in core code. Users -- will never encounter this failure, but as a developer it’s hard to test a -- deprecation when core code refactoring is an all-or-nothing proposition. -- Hence we fake it ‘till we make it, all deprecations internally are warings. local brackets = old:sub(1,1) == '\\' and "" or "()" - local _semver = SILE.version and SILE.version:match("v([0-9]*.[0-9]*.[0-9]*)") or warnat local _new = new and "Please use " .. (new .. brackets) .. " instead." or "Plase don't use it." - local msg = (old .. brackets) .. " was deprecated in SILE v" .. warnat .. ". " .. _new .. (extra and "\n" .. extra .. "\n\n" or "") - if errorat and _semver >= errorat then + local msg = (old .. brackets) .. " was deprecated in SILE v" .. tostring(warnat) .. ". " .. _new .. (extra and "\n" .. extra .. "\n\n" or "") + if errorat and current >= errorat then SU.error(msg) - elseif warnat and _semver >= warnat then + elseif warnat and current >= warnat then SU.warn(msg) end end From ed461ecc5a837cac9d4ff86b9d46c517eb1c9bac Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Fri, 18 Aug 2023 00:52:17 +0300 Subject: [PATCH 3/4] test(utilities): Add test for semver parsing and deprecator --- spec/utilities_spec.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/utilities_spec.lua b/spec/utilities_spec.lua index 68b207dab..8c3747286 100644 --- a/spec/utilities_spec.lua +++ b/spec/utilities_spec.lua @@ -5,6 +5,15 @@ describe("SILE.utilities", function() assert.is.truthy(SU) end) + describe("deprecated", function () + it("should compute errors based on semver", function() + SILE.version = "v0.1.10.r4-h5d5dd3b" + SU.warn = function () end + assert.has.errors(function() SU.deprecated("foo", "bar", "0.1.9", "0.1.9") end) + assert.has_no.errors(function() SU.deprecated("foo", "bar", "0.1.11", "0.1.11") end) + end) + end) + describe("utf8_to_utf16be_hexencoded ", function() it("should hex encode input", function() local str = "foo" From 785b800005ae9a760221704fa60c2986987c942c Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Sat, 19 Aug 2023 18:30:39 +0300 Subject: [PATCH 4/4] chore(deps): Replace external dependency with minimal local copy Co-authored-by: Omikhleia --- .cirrus.yml | 1 - configure.ac | 1 - flake.nix | 1 - lua-libraries/semver.lua | 46 ++++++++++++++++++++++++++++++++++++++++ sile.rockspec.in | 1 - 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 lua-libraries/semver.lua diff --git a/.cirrus.yml b/.cirrus.yml index 3f9877d36..fe2e5dbc7 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -40,7 +40,6 @@ task: - luarocks54 install luarepl - luarocks54 install luautf8 - luarocks54 install penlight - - luarocks54 install semver - luarocks54 install vstruct bootstrap_script: - git fetch --prune --tags ||: diff --git a/configure.ac b/configure.ac index ae85d9164..ad1ebe932 100644 --- a/configure.ac +++ b/configure.ac @@ -182,7 +182,6 @@ AM_COND_IF([DEPENDENCY_CHECKS], [ AX_LUA_MODULE([repl], [luarepl]) AX_LUA_MODULE([ssl], [luasec]) AX_LUA_MODULE([socket], [luasocket]) - AX_LUA_MODULE([semver], [semver]) AX_LUA_MODULE([lua-utf8], [luautf8]) AX_LUA_MODULE([pl], [penlight]) AX_LUA_MODULE([vstruct], [vstruct]) diff --git a/flake.nix b/flake.nix index aa1278d66..ea0adc2ca 100644 --- a/flake.nix +++ b/flake.nix @@ -58,7 +58,6 @@ luasocket luautf8 penlight - semver vstruct # lua packages needed for testing busted diff --git a/lua-libraries/semver.lua b/lua-libraries/semver.lua new file mode 100644 index 000000000..867eec95f --- /dev/null +++ b/lua-libraries/semver.lua @@ -0,0 +1,46 @@ +-- Loosely inspired from https://github.com/kikito/semver.lua +-- (MIT License (c) 2011 Enrique García Cota) +-- but simplified to our bare needs. + +local semver = {} +local mt = {} + +function mt:__eq(other) + return self.major == other.major and + self.minor == other.minor and + self.patch == other.patch +end + +function mt:__lt(other) + if self.major ~= other.major then return self.major < other.major end + if self.minor ~= other.minor then return self.minor < other.minor end + if self.patch ~= other.patch then return self.patch < other.patch end + return false +end + +function mt:__le(other) + if self.major ~= other.major then return self.major <= other.major end + if self.minor ~= other.minor then return self.minor <= other.minor end + if self.patch ~= other.patch then return self.patch <= other.patch end + return true +end + +function mt:__tostring() + return ("%d.%d.%d"):format(self.major, self.minor, self.patch) +end + +local function new (vstr) + local major, minor, patch = vstr:match("^v?(%d+)%.(%d+)%.(%d+)") + local result = { major = tonumber(major), minor = tonumber(minor), patch = tonumber(patch) } + if not result.major and not result.minor and not result.patch then + error("Invalid version string: "..vstr) + end + local o = setmetatable(result, mt) + return o +end + +setmetatable(semver, { + __call = function(_, ...) return new(...) end +}) + +return semver diff --git a/sile.rockspec.in b/sile.rockspec.in index c40745559..24f2baf4d 100644 --- a/sile.rockspec.in +++ b/sile.rockspec.in @@ -44,7 +44,6 @@ dependencies = { "luasocket == 3.1.0-1", "luautf8 == 0.1.5-1", "penlight == 1.13.1-1", - "semver == 1.2.1-1", "vstruct == 2.1.1-1" }