Skip to content

Added mudletOlderThan()#1064

Merged
vadi2 merged 3 commits intodevelopmentfrom
add-mudletolderthan
Jun 5, 2017
Merged

Added mudletOlderThan()#1064
vadi2 merged 3 commits intodevelopmentfrom
add-mudletolderthan

Conversation

@vadi2
Copy link
Copy Markdown
Member

@vadi2 vadi2 commented Jun 3, 2017

We all know the pain of not having a proper function to do version comparison, so we shouldn't inflict it on Mudlet users.

Makes it easy to write if mudletOlderThan("3.2") then return end in case some features cannot be checked in the preferred way of if desiredMudletFunction then such as coroutine support - coroutines were always present and would instantly crash the application if you tried to use them. There's no way to check for that sans checking the major+minor version.

I've also dumped some tests but as busted doesn't work, they're incomplete. Somehow busted's tests are passing so we'll have to see how do they make it work.

Tagging @Mudlet/lua-interface for review.

Makes it easy to write if mudletOlderThan("3.2") then return end
@vadi2 vadi2 added the 1 - Ready label Jun 3, 2017
@vadi2 vadi2 self-assigned this Jun 3, 2017
@vadi2 vadi2 requested a review from keneanung June 3, 2017 13:18
@vadi2 vadi2 changed the title Added mudletOlderThan Added mudletOlderThan() Jun 3, 2017

-- strip trailing zeros from input version
local input = rex.gsub(version, [[(\.0+)+$]], ''):split('%.')
local mudlets = {getMudletVersion("table")}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use local major, minor, patch = getMudletVersion() to get just the numbers you want...?

@vadi2
Copy link
Copy Markdown
Member Author

vadi2 commented Jun 3, 2017 via email

Copy link
Copy Markdown
Member

@keneanung keneanung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general the indenting is funny.


local diff
for i = 1, minlength do
diff = tonumber(input[i]) - mudlets[i]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do a null check on the tonumber result first, in case we don't get a numbered version? Then we can return a better error than throwing an arithmic with nil error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tonumber() is there because string.split returns a list of strings. I don't think we should error on strings being passed as part of the version - it can happen - how about treating them as 0?

That'll also then remove the need to strip out -dev in the above lines.

@vadi2
Copy link
Copy Markdown
Member Author

vadi2 commented Jun 4, 2017

I've had a fresh look and rewritten the function, thanks @SlySven for the idea. I was looking at it from a more general version comparison perspective but as we only need this for Mudlet, we can afford to get a lot of efficiency gains.

Copy link
Copy Markdown
Member

@SlySven SlySven left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a typo fixing but otherwise OK.


assert(type(inputmajor) == "number", sformat("bad argument #1 type (major version as number expected, got %s!)", type(inputmajor)))
assert(inputminor == nil or type(inputminor) == "number", sformat("bad argument #2 type (optional minor version as number expected, got %s!)", type(inputminor)))
assert(inputpatch == nil or type(inputpatch) == "number", sformat("bad argument #2 type (optional patch version as number expected, got %s!)", type(inputpatch)))
Copy link
Copy Markdown
Member

@SlySven SlySven Jun 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that not be "bad argument #3..." in this one?

-- for example, it'll return true if you're on 2.1 and you do mudletOlderThan(3,1)
-- it'll return false if you're on 4.0 and you do mudletOlderThan(4,0,0)
function mudletOlderThan(inputmajor, inputminor, inputpatch)
local mudletmajor, mudletminor, mudletpatch = getMudletVersion("table")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😊 It is embarrassing but I really did cock-up the optional arguments for this command - no arguments produces a lua associative table containing four string-keys "major", "minor", "patch" and "build" whereas the return for a supplied argument of "table" is a multi-return set of four return values with those items - "build" being a nil for "release" versions in either case (so it disappears anyway)...!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... yeah...

Copy link
Copy Markdown
Member

@keneanung keneanung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much better. The tests don't work anymore now, but since they are broken anyways, I'm ok with it.

@vadi2 vadi2 merged commit c2a5bc3 into development Jun 5, 2017
@vadi2 vadi2 deleted the add-mudletolderthan branch June 5, 2017 16:53
@vadi2 vadi2 added this to the 3.2 milestone Jun 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants