Skip to content

Commit

Permalink
Merge pull request #13 from alisdair/git-and-version-commit-key-gener…
Browse files Browse the repository at this point in the history
…ators

Add git-tag-commit and version-commit key generators
  • Loading branch information
Aaron Chambers committed Aug 15, 2015
2 parents 0993043 + c519c72 commit 3ebb0e6
Show file tree
Hide file tree
Showing 29 changed files with 279 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ For detailed information on how configuration of plugins works, please refer to
The type of [Key Generator](#key-generators) to be used.

*Default:* `'file-hash'`
*Alternatives:* `'git-tag-commit'`, `'version-commit'`

## Key Generators

Expand Down Expand Up @@ -83,6 +84,24 @@ The list of built project files. This option should be relative to `distDir` and

*Default:* `context.distFiles`

### Git Tag Commit generator

Creates a key based on the most recent git tag and the currently checked-out commit. The tag is the tag identifier, followed by a `+` symbol, followed by the first 8 characters of the commit hash.

For example, if your most recent git tag is `v2.0.3`, and the current commit is `0993043d49f9e0[...]`, this generator will return a revision of `v2.0.3+0993043d`.

### Version Commit generator

Similar to the Git Tag Commit generator, but uses the `package.json` version string instead of the git tag. The JSON file containing the version string can be configured with the `versionFile` option.

#### Configuration Options

##### versionFile

The file containing your project's version number. Must be a JSON file with a top-level `version` key. Only used by the `version-commit` key generator.

*Default:* `package.json`

## Prerequisites

The following properties are expected to be present on the deployment `context` object:
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
distFiles: function(context) {
return context.distFiles;
},
versionFile: 'package.json',
},
didBuild: function(context) {
var self = this;
Expand Down
23 changes: 23 additions & 0 deletions lib/key-generators/git-tag-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var CoreObject = require('core-object');
var gitRepoInfo = require('git-repo-info');
var Promise = require('ember-cli/lib/ext/promise');

module.exports = CoreObject.extend({
generate: function() {
var path = gitRepoInfo._findRepo();

if (path === null) {
return Promise.reject('Could not find git repository');
}

var info = gitRepoInfo(path);
var tag = info.tag;
var sha = info.sha.slice(0, 8);

if (!info.tag || !sha) {
return Promise.reject('Could not build revision with tag `' + tag + '` and commit hash `' + sha + '`');
}

return Promise.resolve(info.tag + '+' + sha);
}
});
4 changes: 3 additions & 1 deletion lib/key-generators/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = {
"file-hash": require('./file-hash')
"file-hash": require('./file-hash'),
"git-tag-commit": require('./git-tag-commit'),
"version-commit": require('./version-commit')
};
37 changes: 37 additions & 0 deletions lib/key-generators/version-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var CoreObject = require('core-object');
var gitRepoInfo = require('git-repo-info');
var fs = require('fs');
var Promise = require('ember-cli/lib/ext/promise');

var denodeify = require('rsvp').denodeify;
var readFile = denodeify(fs.readFile);

module.exports = CoreObject.extend({
init: function(options) {
this._plugin = options.plugin;
},

generate: function() {
var versionFile = this._plugin.readConfig('versionFile');

var path = gitRepoInfo._findRepo();

if (path === null) {
return Promise.reject('Could not find git repository');
}

var info = gitRepoInfo(path);
var sha = info.sha.slice(0, 8);

return readFile(versionFile)
.then(function(contents) {
var json = JSON.parse(contents);

if (!json.version || !sha) {
return Promise.reject('Could not build revision with version `' + json.version + '` and commit hash `' + sha + '`');
}

return json.version + '+' + sha;
});
}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
"dependencies": {
"chalk": "^1.0.0",
"core-object": "^1.1.0",
"ember-cli-deploy-plugin": "0.1.3",
"ember-cli-babel": "^5.0.0",
"ember-cli-deploy-plugin": "0.1.3",
"git-repo-info": "^1.1.2",
"minimatch": "^2.0.4",
"rsvp": "^3.0.18"
},
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/not-a-repo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "3.2.1"
}
1 change: 1 addition & 0 deletions tests/fixtures/repo/dotgit/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
7 changes: 7 additions & 0 deletions tests/fixtures/repo/dotgit/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
Binary file added tests/fixtures/repo/dotgit/index
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x��A
1 @Q�=E��4�L����"Ӧ�Z��������������7H��g�0N�GN�샥A�'&&t9~�Gmp^��X��E�Mp�_;��ֶ��~\���ak�Z��.2��Ԯ����OB�
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions tests/fixtures/repo/dotgit/refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
41d41f081b45ad50935c08b1203220737d9739b4
1 change: 1 addition & 0 deletions tests/fixtures/repo/dotgit/refs/tags/2.3.4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
41d41f081b45ad50935c08b1203220737d9739b4
3 changes: 3 additions & 0 deletions tests/fixtures/repo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "3.2.1"
}
3 changes: 3 additions & 0 deletions tests/fixtures/repo/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "1.2.3"
}
1 change: 1 addition & 0 deletions tests/fixtures/tagless-repo/dotgit/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
7 changes: 7 additions & 0 deletions tests/fixtures/tagless-repo/dotgit/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
Binary file added tests/fixtures/tagless-repo/dotgit/index
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x��A
1 @Q�=E��$i;RQp��C������Z�/8���_���V�H��K�X8e�<E�,4+�M�2�b�D�KJN���:�{e�7���j�k��ym���G��G�� ��w>ʟ��>m�,�z�n�BM
1 change: 1 addition & 0 deletions tests/fixtures/tagless-repo/dotgit/refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9138ef996f3c7680aedcba68b0371d828b6dbb0b
3 changes: 3 additions & 0 deletions tests/fixtures/tagless-repo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "3.2.1"
}
2 changes: 1 addition & 1 deletion tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('the index', function() {
return previous;
}, []);

assert.equal(messages.length, 4);
assert.equal(messages.length, 5);
});

it('adds default config to the config object', function() {
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/lib/key-generators/git-tag-commit-nodetest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

var assert = require('ember-cli/tests/helpers/assert');
var gitRepoInfo = require('git-repo-info');

describe('the git-tag-commit key generator', function() {
var KeyGenerator;
var cwd;

before(function() {
KeyGenerator = require('../../../../lib/key-generators/git-tag-commit');
gitRepoInfo._changeGitDir('dotgit');
});

beforeEach(function() {
cwd = process.cwd();
});

afterEach(function() {
process.chdir(cwd);
});

describe('#generate', function() {
it('concatenates the git tag and the git commit hash', function() {
process.chdir('tests/fixtures/repo');

var subject = new KeyGenerator();

return assert.isFulfilled(subject.generate())
.then(function(revision) {
assert.equal(revision, '2.3.4+41d41f08');
});
});

it('rejects if no repository found', function() {
process.chdir('tests/fixtures/not-a-repo');

var subject = new KeyGenerator();

return assert.isRejected(subject.generate())
.then(function(error) {
assert.equal(error, 'Could not find git repository');
});
});

it('rejects if no git tag found', function() {
process.chdir('tests/fixtures/tagless-repo');

var subject = new KeyGenerator();

return assert.isRejected(subject.generate())
.then(function(error) {
assert.equal(error, 'Could not build revision with tag `null` and commit hash `9138ef99`');
});
});
});
});
101 changes: 101 additions & 0 deletions tests/unit/lib/key-generators/version-commit-nodetest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'use strict';

var assert = require('ember-cli/tests/helpers/assert');
var gitRepoInfo = require('git-repo-info');

describe('the version-commit key generator', function() {
var KeyGenerator;
var cwd;

before(function() {
KeyGenerator = require('../../../../lib/key-generators/version-commit');
gitRepoInfo._changeGitDir('dotgit');
});

beforeEach(function() {
cwd = process.cwd();
});

afterEach(function() {
process.chdir(cwd);
});

describe('#generate', function() {
it('concatenates the package version and the git commit hash', function() {
process.chdir('tests/fixtures/repo');

var plugin = {
stubConfig: {
versionFile: 'package.json'
},
readConfig: function(key) { return this.stubConfig[key]; }
};

var subject = new KeyGenerator({
plugin: plugin
});

return assert.isFulfilled(subject.generate())
.then(function(revision) {
assert.equal(revision, '3.2.1+41d41f08');
});
});

it('rejects if no repository found', function() {
process.chdir('tests/fixtures/not-a-repo');

var plugin = {
stubConfig: {
versionFile: 'package.json'
},
readConfig: function(key) { return this.stubConfig[key]; }
};

var subject = new KeyGenerator({
plugin: plugin
});

return assert.isRejected(subject.generate())
.then(function(error) {
assert.equal(error, 'Could not find git repository');
});
});

it('has version source file option', function() {
process.chdir('tests/fixtures/repo');

var plugin = {
stubConfig: {
versionFile: 'version.json'
},
readConfig: function(key) { return this.stubConfig[key]; }
};

var subject = new KeyGenerator({
plugin: plugin
});

return assert.isFulfilled(subject.generate())
.then(function(revision) {
assert.equal(revision, '1.2.3+41d41f08');
});
});

it('rejects when the version source file doesn\'t exist', function() {
process.chdir('tests/fixtures/repo');

var plugin = {
stubConfig: {
versionFile: 'tests/fixtures/missing-version.json'
},
readConfig: function(key) { return this.stubConfig[key]; }
};

var subject = new KeyGenerator({
plugin: plugin
});

return assert.isRejected(subject.generate());
});
});
});

0 comments on commit 3ebb0e6

Please sign in to comment.