diff --git a/lib/configure.js b/lib/configure.js index 3c09da0..15dc6e6 100644 --- a/lib/configure.js +++ b/lib/configure.js @@ -8,6 +8,7 @@ module.exports = exports = configure var fs = require('fs') , path = require('path') , glob = require('glob') + , nodeVersion = require('./util/node_version') , win = process.platform == 'win32' exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module' @@ -20,11 +21,11 @@ function configure (gyp, argv, callback) { if (gyp.opts.target) { // if --target was given, then ensure that version is installed - version = parseFloat(gyp.opts.target) + version = nodeVersion.parse(gyp.opts.target) gyp.verbose('compiling against --target node version', version) } else { // if no --target was specified then use the current host node version - version = parseFloat(process.versions.node) + version = nodeVersion.parse(process.versions.node) gyp.verbose('no --target version specified, falling back to host node version', version) } gyp.opts.ensure = true @@ -67,7 +68,7 @@ function configure (gyp, argv, callback) { , devDir = path.join(process.env.HOME, '.node-gyp', target) , gyp_addon = path.join(devDir, 'tools', 'gyp_addon') - if (win && version < 0.8) { + if (win && nodeVersion.lessThan(version, 0, 8)) { gyp.verbose('on Windows and target version is less than 0.8; applying #2685 patch') // if < 0.8, we need to manually apply the patch at joyent/node#2685, // since it got merged somewhere in 0.7.x. @@ -76,7 +77,7 @@ function configure (gyp, argv, callback) { argv.push(path.join(devDir, 'tools', 'patch.gypi')) } - if (!win && version < 0.8) { + if (!win && nodeVersion.lessThan(version, 0, 8)) { gyp.verbose('on Unix and target version is less than 0.8; applying #2722 patch') argv.push('-I') argv.push(path.join(devDir, 'tools', 'patch2722.gypi')) diff --git a/lib/install.js b/lib/install.js index 9cedc2f..38c84f7 100644 --- a/lib/install.js +++ b/lib/install.js @@ -15,6 +15,7 @@ var fs = require('fs') , mkdir = require('mkdirp') , request = require('request') , minimatch = require('minimatch') + , nodeVersion = require('./util/node_version') , distUrl = 'http://nodejs.org/dist' , win = process.platform == 'win32' // a map for legacy releases: @@ -36,12 +37,12 @@ function install (gyp, argv, callback) { } - var version = parseFloat(argv[0] || gyp.opts.target) + var version = nodeVersion.parse(argv[0] || gyp.opts.target) - if (isNaN(version)) { + if (!version) { return cb(new Error('You must specify a version to install (like "0.7")')) } - if (version < 0.6) { + if (nodeVersion.lessThan(version, 0, 6)) { return cb(new Error('Minimum target version is `0.6` or greater. Got: ' + version)) } @@ -79,7 +80,7 @@ function install (gyp, argv, callback) { // now download the node tarball // TODO: download the newest version instead of the .0 release - var tarballUrl = distUrl + '/v' + version.toFixed(1) + '.0/node-v' + version.toFixed(1) + '.0.tar.gz' + var tarballUrl = distUrl + '/v' + version + '.0/node-v' + version + '.0.tar.gz' , badDownload = false , parser = tar.Parse() @@ -147,7 +148,7 @@ function install (gyp, argv, callback) { gyp.verbose('done parsing tarball') var async = 0 - if (version < 0.7) { + if (nodeVersion.lessThan(version, 0, 7)) { // copy over gyp_addon, addon.gypi and common.gypi async++ copyLegacy(deref) @@ -159,12 +160,12 @@ function install (gyp, argv, callback) { downloadNodeLib(deref) } - if (win && version < 0.8) { + if (win && nodeVersion.lessThan(version, 0, 8)) { // before node 0.8 we need to manually link to node.lib async++ copy2685(deref) } - if (!win && version < 0.8) { + if (!win && nodeVersion.lessThan(version, 0, 8)) { async++ copy2722(deref) } diff --git a/lib/remove.js b/lib/remove.js index 64a4b67..e52fe50 100644 --- a/lib/remove.js +++ b/lib/remove.js @@ -10,6 +10,7 @@ exports.usage = 'Removes the node development files for the specified version' var fs = require('fs') , rm = require('rimraf') , path = require('path') + , nodeVersion = require('./util/node_version') function remove (gyp, argv, callback) { @@ -18,8 +19,8 @@ function remove (gyp, argv, callback) { gyp.verbose('using node-gyp dir', nodeGypDir) - var version = parseFloat(argv[0] || gyp.opts.target) - , versionPath = path.join(nodeGypDir, version.toString()) + var version = nodeVersion.parse(argv[0] || gyp.opts.target) + , versionPath = path.join(nodeGypDir, version) gyp.verbose('removing development files for version', version) diff --git a/lib/util/node_version.js b/lib/util/node_version.js new file mode 100644 index 0000000..fb9b2f2 --- /dev/null +++ b/lib/util/node_version.js @@ -0,0 +1,35 @@ + +/** + * Helper functions for parsing and testing node versions. + * Just plain `parseFloat` doesn't work in case there's ever a "x.10" release, + * which would be parsed as x.1 when represented as a Number. + */ + +var regexp = /^(\d+)\.(\d+)/ + +/** + * Accepts a String like "v0.10.4" and returns a String + * containing the major and minor versions ("0.10"). + */ + +exports.parse = function parse (str) { + str = String(str) + if (str[0] === 'v') { + str = str.substring(1) + } + return str.match(regexp)[0] +} + +/** + * Accepts a major-minor version string (from `parseVersion`) and a major + * and minor Number value to test that the given version is less that the + * specified version. Returns true or false. + */ + +exports.lessThan = function lessThan (ver, major, minor) { + var exec = regexp.exec(ver) + , inMaj = parseInt(exec[1]) + , inMin = parseInt(exec[2]) + if (inMaj > major) return false + return inMin < minor +}