Skip to content

Commit

Permalink
custom version parser and comparer
Browse files Browse the repository at this point in the history
semver apparently cant handle version numbers with more
than two dots
e.g.: 0.9.15.1
  • Loading branch information
archaeron committed May 22, 2015
1 parent 888d1d6 commit 245c92a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/idris-model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ parse = require './parse'
utils = require('./utils')
EventEmitter = require('events').EventEmitter
{Logger} = require './Logger'
semver = require 'semver'

class IdrisModel extends EventEmitter
constructor: (@version) ->
Expand All @@ -18,8 +17,9 @@ class IdrisModel extends EventEmitter

start: ->
pathToIdris = atom.config.get("language-idris.pathToIdris")

ideCommand =
if semver.gt @version, '0.9.16'
if utils.versionGreaterEq @version, [0, 9, 16]
'--ide-mode'
else
'--ideslave'
Expand Down
5 changes: 2 additions & 3 deletions lib/language-idris.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ IdrisController = require './idris-controller'
IdrisModel = require './idris-model'
{CompositeDisposable} = require 'atom'
utils = require './utils'
semver = require 'semver'

module.exports =
config:
Expand All @@ -22,8 +21,8 @@ module.exports =
# and switch to ideslave for older ones
idrisVersionPromise = utils.execPromise "#{pathToIdris} --version --nobanner"

idrisVersionPromise.then ((vers) ->
Promise.resolve semver.clean(vers.trim().replace('-', ''))
idrisVersionPromise.then ((version) ->
Promise.resolve utils.parseVersion(version)
), (error) ->
atom.notifications.addWarning warningNoIdris
.then @startIdrisProcesses
Expand Down
25 changes: 25 additions & 0 deletions lib/utils.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
exec = require('child_process').exec

parseVersion = (vers) ->
vers.trim().replace('-', '').split('.').map (n) ->
parseInt n, 10

versionGreaterEq = (a, b) ->

padArray = (length, ary) ->
if ary.length < length
ary.concat (0 for _ in [0...length-ary.length])
else
ary

zip2 = (a, b) ->
for i in [0...a.length]
[a[i], b[i]]

maxLength = Math.max a.length, b.length
a = padArray maxLength, a
b = padArray maxLength, b
zip2(a, b)
.map ([a, b]) -> a >= b
.reduce ((acc, xs) -> if !acc then acc else xs), true

execPromise = (command, args) ->
promise = new Promise (resolve, reject) ->
process = exec command, args
Expand Down Expand Up @@ -47,6 +70,8 @@ formatSexp = (sexp) ->
sexp

module.exports =
parseVersion: parseVersion
versionGreaterEq: versionGreaterEq
execPromise: execPromise
serialize: serialize
hexLength: hexLength
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"atom-message-panel": ">=0.1.0",
"atom-space-pen-views": "^2.0.3",
"bennu": "17.3.0",
"nu-stream": "3.3.1",
"semver": "4.3.4"
"nu-stream": "3.3.1"
}
}
14 changes: 14 additions & 0 deletions spec/version-spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
utils = require '../lib/utils'

describe "The version", ->
it "should parse correctly.", ->
expect(utils.parseVersion('0.9.15.1')).toEqual([0, 9, 15, 1])
expect(utils.parseVersion('0.9.17')).toEqual([0, 9, 17])
expect(utils.parseVersion('0.9.18-')).toEqual([0, 9, 18])
expect(utils.parseVersion("0.9.18-\n")).toEqual([0, 9, 18])

it "should compare correctly.", ->
expect(utils.versionGreaterEq([0, 9, 15, 1], [0, 9, 17])).toEqual(false)
expect(utils.versionGreaterEq([0, 9, 14], [0, 9, 17])).toEqual(false)
expect(utils.versionGreaterEq([0, 9, 18], [0, 9, 17])).toEqual(true)
expect(utils.versionGreaterEq([0, 9, 17], [0, 9, 17])).toEqual(true)

0 comments on commit 245c92a

Please sign in to comment.