Skip to content

Commit 3ebb0e6

Browse files
author
Aaron Chambers
committed
Merge pull request #13 from alisdair/git-and-version-commit-key-generators
Add git-tag-commit and version-commit key generators
2 parents 0993043 + c519c72 commit 3ebb0e6

29 files changed

+279
-3
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ For detailed information on how configuration of plugins works, please refer to
5454
The type of [Key Generator](#key-generators) to be used.
5555

5656
*Default:* `'file-hash'`
57+
*Alternatives:* `'git-tag-commit'`, `'version-commit'`
5758

5859
## Key Generators
5960

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

8485
*Default:* `context.distFiles`
8586

87+
### Git Tag Commit generator
88+
89+
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.
90+
91+
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`.
92+
93+
### Version Commit generator
94+
95+
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.
96+
97+
#### Configuration Options
98+
99+
##### versionFile
100+
101+
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.
102+
103+
*Default:* `package.json`
104+
86105
## Prerequisites
87106

88107
The following properties are expected to be present on the deployment `context` object:

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
distFiles: function(context) {
2121
return context.distFiles;
2222
},
23+
versionFile: 'package.json',
2324
},
2425
didBuild: function(context) {
2526
var self = this;

lib/key-generators/git-tag-commit.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var CoreObject = require('core-object');
2+
var gitRepoInfo = require('git-repo-info');
3+
var Promise = require('ember-cli/lib/ext/promise');
4+
5+
module.exports = CoreObject.extend({
6+
generate: function() {
7+
var path = gitRepoInfo._findRepo();
8+
9+
if (path === null) {
10+
return Promise.reject('Could not find git repository');
11+
}
12+
13+
var info = gitRepoInfo(path);
14+
var tag = info.tag;
15+
var sha = info.sha.slice(0, 8);
16+
17+
if (!info.tag || !sha) {
18+
return Promise.reject('Could not build revision with tag `' + tag + '` and commit hash `' + sha + '`');
19+
}
20+
21+
return Promise.resolve(info.tag + '+' + sha);
22+
}
23+
});

lib/key-generators/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
2-
"file-hash": require('./file-hash')
2+
"file-hash": require('./file-hash'),
3+
"git-tag-commit": require('./git-tag-commit'),
4+
"version-commit": require('./version-commit')
35
};

lib/key-generators/version-commit.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var CoreObject = require('core-object');
2+
var gitRepoInfo = require('git-repo-info');
3+
var fs = require('fs');
4+
var Promise = require('ember-cli/lib/ext/promise');
5+
6+
var denodeify = require('rsvp').denodeify;
7+
var readFile = denodeify(fs.readFile);
8+
9+
module.exports = CoreObject.extend({
10+
init: function(options) {
11+
this._plugin = options.plugin;
12+
},
13+
14+
generate: function() {
15+
var versionFile = this._plugin.readConfig('versionFile');
16+
17+
var path = gitRepoInfo._findRepo();
18+
19+
if (path === null) {
20+
return Promise.reject('Could not find git repository');
21+
}
22+
23+
var info = gitRepoInfo(path);
24+
var sha = info.sha.slice(0, 8);
25+
26+
return readFile(versionFile)
27+
.then(function(contents) {
28+
var json = JSON.parse(contents);
29+
30+
if (!json.version || !sha) {
31+
return Promise.reject('Could not build revision with version `' + json.version + '` and commit hash `' + sha + '`');
32+
}
33+
34+
return json.version + '+' + sha;
35+
});
36+
}
37+
});

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
"dependencies": {
4747
"chalk": "^1.0.0",
4848
"core-object": "^1.1.0",
49-
"ember-cli-deploy-plugin": "0.1.3",
5049
"ember-cli-babel": "^5.0.0",
50+
"ember-cli-deploy-plugin": "0.1.3",
51+
"git-repo-info": "^1.1.2",
5152
"minimatch": "^2.0.4",
5253
"rsvp": "^3.0.18"
5354
},
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "3.2.1"
3+
}

tests/fixtures/repo/dotgit/HEAD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master

tests/fixtures/repo/dotgit/config

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true

tests/fixtures/repo/dotgit/index

225 Bytes
Binary file not shown.

tests/fixtures/repo/dotgit/objects/41/d41f081b45ad50935c08b1203220737d9739b4

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x��A
2+
1 @Q�=E��4�L����"Ӧ�Z��������������7H��g�0N�GN�샥A�'&&t9~�Gmp^��X��E�Mp�_;��ֶ��~\���ak�Z��.2��Ԯ����OB�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
41d41f081b45ad50935c08b1203220737d9739b4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
41d41f081b45ad50935c08b1203220737d9739b4

tests/fixtures/repo/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "3.2.1"
3+
}

tests/fixtures/repo/version.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "1.2.3"
3+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true
145 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x��A
2+
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9138ef996f3c7680aedcba68b0371d828b6dbb0b
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "3.2.1"
3+
}

tests/unit/index-nodetest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('the index', function() {
7777
return previous;
7878
}, []);
7979

80-
assert.equal(messages.length, 4);
80+
assert.equal(messages.length, 5);
8181
});
8282

8383
it('adds default config to the config object', function() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
var assert = require('ember-cli/tests/helpers/assert');
4+
var gitRepoInfo = require('git-repo-info');
5+
6+
describe('the git-tag-commit key generator', function() {
7+
var KeyGenerator;
8+
var cwd;
9+
10+
before(function() {
11+
KeyGenerator = require('../../../../lib/key-generators/git-tag-commit');
12+
gitRepoInfo._changeGitDir('dotgit');
13+
});
14+
15+
beforeEach(function() {
16+
cwd = process.cwd();
17+
});
18+
19+
afterEach(function() {
20+
process.chdir(cwd);
21+
});
22+
23+
describe('#generate', function() {
24+
it('concatenates the git tag and the git commit hash', function() {
25+
process.chdir('tests/fixtures/repo');
26+
27+
var subject = new KeyGenerator();
28+
29+
return assert.isFulfilled(subject.generate())
30+
.then(function(revision) {
31+
assert.equal(revision, '2.3.4+41d41f08');
32+
});
33+
});
34+
35+
it('rejects if no repository found', function() {
36+
process.chdir('tests/fixtures/not-a-repo');
37+
38+
var subject = new KeyGenerator();
39+
40+
return assert.isRejected(subject.generate())
41+
.then(function(error) {
42+
assert.equal(error, 'Could not find git repository');
43+
});
44+
});
45+
46+
it('rejects if no git tag found', function() {
47+
process.chdir('tests/fixtures/tagless-repo');
48+
49+
var subject = new KeyGenerator();
50+
51+
return assert.isRejected(subject.generate())
52+
.then(function(error) {
53+
assert.equal(error, 'Could not build revision with tag `null` and commit hash `9138ef99`');
54+
});
55+
});
56+
});
57+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict';
2+
3+
var assert = require('ember-cli/tests/helpers/assert');
4+
var gitRepoInfo = require('git-repo-info');
5+
6+
describe('the version-commit key generator', function() {
7+
var KeyGenerator;
8+
var cwd;
9+
10+
before(function() {
11+
KeyGenerator = require('../../../../lib/key-generators/version-commit');
12+
gitRepoInfo._changeGitDir('dotgit');
13+
});
14+
15+
beforeEach(function() {
16+
cwd = process.cwd();
17+
});
18+
19+
afterEach(function() {
20+
process.chdir(cwd);
21+
});
22+
23+
describe('#generate', function() {
24+
it('concatenates the package version and the git commit hash', function() {
25+
process.chdir('tests/fixtures/repo');
26+
27+
var plugin = {
28+
stubConfig: {
29+
versionFile: 'package.json'
30+
},
31+
readConfig: function(key) { return this.stubConfig[key]; }
32+
};
33+
34+
var subject = new KeyGenerator({
35+
plugin: plugin
36+
});
37+
38+
return assert.isFulfilled(subject.generate())
39+
.then(function(revision) {
40+
assert.equal(revision, '3.2.1+41d41f08');
41+
});
42+
});
43+
44+
it('rejects if no repository found', function() {
45+
process.chdir('tests/fixtures/not-a-repo');
46+
47+
var plugin = {
48+
stubConfig: {
49+
versionFile: 'package.json'
50+
},
51+
readConfig: function(key) { return this.stubConfig[key]; }
52+
};
53+
54+
var subject = new KeyGenerator({
55+
plugin: plugin
56+
});
57+
58+
return assert.isRejected(subject.generate())
59+
.then(function(error) {
60+
assert.equal(error, 'Could not find git repository');
61+
});
62+
});
63+
64+
it('has version source file option', function() {
65+
process.chdir('tests/fixtures/repo');
66+
67+
var plugin = {
68+
stubConfig: {
69+
versionFile: 'version.json'
70+
},
71+
readConfig: function(key) { return this.stubConfig[key]; }
72+
};
73+
74+
var subject = new KeyGenerator({
75+
plugin: plugin
76+
});
77+
78+
return assert.isFulfilled(subject.generate())
79+
.then(function(revision) {
80+
assert.equal(revision, '1.2.3+41d41f08');
81+
});
82+
});
83+
84+
it('rejects when the version source file doesn\'t exist', function() {
85+
process.chdir('tests/fixtures/repo');
86+
87+
var plugin = {
88+
stubConfig: {
89+
versionFile: 'tests/fixtures/missing-version.json'
90+
},
91+
readConfig: function(key) { return this.stubConfig[key]; }
92+
};
93+
94+
var subject = new KeyGenerator({
95+
plugin: plugin
96+
});
97+
98+
return assert.isRejected(subject.generate());
99+
});
100+
});
101+
});

0 commit comments

Comments
 (0)