From 2e4ea47bdff48f41779f96668bcb32b2fb0a9851 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Thu, 27 Mar 2014 12:19:50 +0000 Subject: [PATCH] plugin version on constructor --- .bowerrc | 3 +++ lib/Plugin.js | 38 +++++++++++++++++++++------ lib/Project.js | 6 ++--- lib/commands/install.js | 6 +++-- lib/commands/uninstall.js | 2 +- package.json | 3 ++- test/specs/plugin_name_concerns.js | 41 +++++++++++++++++++++++++++--- 7 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 .bowerrc diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000..a3a019e --- /dev/null +++ b/.bowerrc @@ -0,0 +1,3 @@ +{ + "registry": "http://adapt-bower-repository.herokuapp.com/" +} \ No newline at end of file diff --git a/lib/Plugin.js b/lib/Plugin.js index 7854f35..0a3279e 100644 --- a/lib/Plugin.js +++ b/lib/Plugin.js @@ -1,25 +1,47 @@ -var slug = require('slug'); +var slug = require('slug'), + endpointParser = require('bower-endpoint-parser'), + zero = '0.0.0'; -var Plugin = function(name, packageNameOrContrib) { +var Plugin = function(name, versionOrIsContrib, isContrib) { this.name = name; - if(typeof packageNameOrContrib === 'boolean') { - this.packageName = makePackageName(name, true); + + if(typeof isContrib === 'undefined') { + isContrib = false; + } + if(typeof versionOrIsContrib === 'undefined') { + isContrib = false; + this.version = zero; + } else if(typeof versionOrIsContrib === 'boolean'){ + isContrib = versionOrIsContrib; + this.version = zero; } else { - this.packageName = packageNameOrContrib || makePackageName(name, false); - } - + this.version = versionOrIsContrib; + } + this.packageName = makePackageName(name, isContrib); + Object.defineProperty(this, 'isContrib', { get: function () { return /^adapt-contrib/.test(this.packageName); } }); }; + Plugin.prototype.toString = function() { - return ''+this.packageName; + var version = ''; + if(this.version !== zero) { + version = '#'+ this.version; + } + return ''+this.packageName + version; }; function makePackageName(name, isContrib) { return (/^adapt-/i.test(name) ? '' : 'adapt-') + (!isContrib ? '' : 'contrib-') + slug(name); } +Plugin.parse = function (endpoint) { + var ep = endpointParser.decompose(endpoint), + version = /^\*$/.test(ep.target) ? zero : ep.target; + return new Plugin(ep.name || ep.source, version); +}; + module.exports = Plugin; \ No newline at end of file diff --git a/lib/Project.js b/lib/Project.js index 6ce932e..f9d98cb 100644 --- a/lib/Project.js +++ b/lib/Project.js @@ -1,5 +1,5 @@ var Plugin = require('./Plugin'), - fs = require('fs') + fs = require('fs'), _ = require('lodash'), JsonLoader = require('./JsonLoader'), EmptyProject = { @@ -20,7 +20,7 @@ var Project = function (path) { }; Project.prototype.add = function (plugin) { - if(typeof Plugin !== 'Plugin') { + if(typeof Plugin !== 'object' && plugin.constructor !== Plugin) { plugin = new Plugin(plugin); } var manifest = parse(this.manifestFilePath); @@ -29,7 +29,7 @@ Project.prototype.add = function (plugin) { }; Project.prototype.remove = function (plugin) { - if(typeof Plugin !== 'Plugin') { + if(typeof Plugin !== 'object' && plugin.constructor !== Plugin) { plugin = new Plugin(plugin); } var manifest = parse(this.manifestFilePath); diff --git a/lib/commands/install.js b/lib/commands/install.js index 4b58051..292a981 100644 --- a/lib/commands/install.js +++ b/lib/commands/install.js @@ -14,9 +14,9 @@ module.exports = { var packageName = arguments.length >= 3 ? arguments[1] : null, location = arguments.length >= 4 ? arguments[2] : '', done = arguments[arguments.length-1] || function () {}; - + var project = new Project(Constants.DefaultProjectManifestPath); - plugins = packageName ? [new Plugin(packageName)] : project.plugins; + plugins = packageName ? [Plugin.parse(packageName)] : project.plugins; var tasks = plugins.map(function (plugin) { project.add(plugin); @@ -52,6 +52,8 @@ module.exports = { function install (plugin, config) { var deferred = Q.defer(); + console.log('installing', plugin.toString()) + bower.commands.install([plugin.toString()], { save: false }, config) .on('end', function(installed) { deferred.resolve(installed); diff --git a/lib/commands/uninstall.js b/lib/commands/uninstall.js index f6f82e9..5ab9ee8 100644 --- a/lib/commands/uninstall.js +++ b/lib/commands/uninstall.js @@ -20,7 +20,7 @@ module.exports = { return renderer.log(chalk.red('Please specify a plugin to uninstall.')); } - var plugin = new Plugin(packageName); + var plugin = Plugin.parse(packageName); PackageMeta.getKeywords(plugin) .then(function (keywords) { diff --git a/package.json b/package.json index 1dbd43b..eb1beea 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "install": "~0.1.7", "npm": "~1.3.24", "q-io": "~1.10.8", - "grunt": "~0.4.2" + "grunt": "~0.4.2", + "bower-endpoint-parser": "^0.2.1" }, "devDependencies": { "mocha": "~1.13.0", diff --git a/test/specs/plugin_name_concerns.js b/test/specs/plugin_name_concerns.js index 18625ba..1aae1c4 100644 --- a/test/specs/plugin_name_concerns.js +++ b/test/specs/plugin_name_concerns.js @@ -6,24 +6,57 @@ describe('Given I have a name', function () { describe('when I create a plugin from a package name', function () { it('should prefix the name with "adapt"', function () { - var plugin = new Plugin('package'); expect(plugin.packageName).to.match(/^adapt-package$/); }); + + it('should default the version to "0.0.0"', function () { + var plugin = new Plugin('package'); + expect(plugin.version).to.match(/^0.0.0$/); + }); + + it('should stringify to the bower endpoint', function () { + var plugin = new Plugin('package'); + expect(plugin.toString()).to.match(/^adapt-package$/); + }); + }); + + describe('when I create a plugin from a specific version package name', function () { + it('should parse the correct version', function () { + var plugin = new Plugin('package' ,'1.0.0'); + expect(plugin.packageName).to.match(/^adapt-package$/); + expect(plugin.version).to.match(/^1.0.0$/); + }); + + it('should stringify to the bower endpoint', function () { + var plugin = new Plugin('package', '1.0.0'); + expect(plugin.toString()).to.match(/^adapt-package#1.0.0$/); + }); }); - + describe('when I create a contrib plugin from a package name', function () { it('should be contrib', function () { - var plugin = new Plugin('package', true); expect(plugin.isContrib).to.be(true); }); it('should prefix the name with "adapt-contrib"', function () { - var plugin = new Plugin('package', true); expect(plugin.packageName).to.match(/^adapt-contrib-/); }); }); + describe('when I create a specific version of a contrib plugin from a package name', function () { + it('should be contrib', function () { + var plugin = new Plugin('package', '1.0.0', true); + expect(plugin.isContrib).to.be(true); + }); + + it('should prefix the name with "adapt-contrib"', function () { + var plugin = new Plugin('package', '1.0.0', true); + expect(plugin.packageName).to.match(/^adapt-contrib-/); + }); + }); + + }); \ No newline at end of file