Skip to content

Commit

Permalink
plugin version on constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Jones committed Mar 27, 2014
1 parent 8025e76 commit 2e4ea47
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"registry": "http://adapt-bower-repository.herokuapp.com/"
}
38 changes: 30 additions & 8 deletions lib/Plugin.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions lib/Project.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Plugin = require('./Plugin'),
fs = require('fs')
fs = require('fs'),
_ = require('lodash'),
JsonLoader = require('./JsonLoader'),
EmptyProject = {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 37 additions & 4 deletions test/specs/plugin_name_concerns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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-/);
});
});


});

0 comments on commit 2e4ea47

Please sign in to comment.