Skip to content

Commit

Permalink
Merge pull request #1844 from alerque/semver
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque authored Aug 19, 2023
2 parents f764872 + 785b800 commit c8a15fb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/utilities.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local bitshim = require("bitshim")
local luautf8 = require("lua-utf8")
local semver = require("semver")

local utilities = {}

Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions lua-libraries/semver.lua
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spec/utilities_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c8a15fb

Please sign in to comment.