Skip to content

Commit 3e1854d

Browse files
committed
add git scm-data-generator
1 parent 599ca17 commit 3e1854d

File tree

7 files changed

+158
-14
lines changed

7 files changed

+158
-14
lines changed

README.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ For detailed information on how configuration of plugins works, please refer to
5151
### Defaults
5252
```
5353
ENV["revision-data"] = {
54-
type: 'file-hash'
54+
type: 'file-hash',
55+
scm: function(context) {
56+
return require('./lib/scm-data-generators')['git'];
57+
}
5558
}
5659
```
5760
### type
@@ -61,6 +64,16 @@ The type of [Data Generator](#data-generators) to be used.
6164
*Default:* `'file-hash'`
6265
*Alternatives:* `'git-tag-commit'`, `'git-commit'`, `'version-commit'`
6366

67+
### scm
68+
69+
The type of the [SCM Data Generator](#scm-data-generator) to be used
70+
71+
*Default:* GitScmDataGenerator
72+
73+
You can set this to `null` if you don't want any Scm Data Generator to be used.
74+
75+
You can also pass your own custom scm generator class.
76+
6477
## Data Generators
6578

6679
Data generators are the strategies used to generate information about the revision being deployed. A data generator must return an object which contains a property called `revisionKey` which uniquely identifies the current revision. A generator can add any other data that it deems relevant to the data object that it returns.
@@ -155,6 +168,36 @@ The file containing your project's version number. Must be a JSON file with a to
155168

156169
*Default:* `package.json`
157170

171+
## Scm Data Generators
172+
173+
Scm Data generators are the strategies used to collect extra information about the revision being deployed. An scm data generator must return an object which contains properties that it deems relevant to the revision being deployed .
174+
175+
### Git generator
176+
177+
This generator uses the information available from the git repository of your ember-cli application.
178+
179+
#### Data fields returned
180+
181+
##### sha
182+
183+
The SHA of the commit being deployed
184+
185+
##### email
186+
187+
Committer's email
188+
189+
##### name
190+
191+
Committer's name
192+
193+
##### branch
194+
195+
Git branch being deployed
196+
197+
##### timestamp
198+
199+
Commit's timestamp
200+
158201
## Prerequisites
159202

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

index.js

+39-10
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,33 @@ module.exports = {
1414
defaultConfig: {
1515
type: 'file-hash',
1616
filePattern: 'index.html',
17+
versionFile: 'package.json',
1718
distDir: function(context) {
1819
return context.distDir;
1920
},
21+
2022
distFiles: function(context) {
2123
return context.distFiles;
2224
},
23-
versionFile: 'package.json',
25+
26+
scm: function(context) {
27+
return require('./lib/scm-data-generators')['git'];
28+
}
29+
2430
},
25-
prepare: function(context) {
31+
32+
prepare: function(/*context*/) {
2633
var self = this;
27-
var type = this.readConfig('type');
28-
var DataGenerator = require('./lib/data-generators')[type];
29-
var dataGenerator = new DataGenerator({
30-
plugin: this
31-
});
3234

33-
this.log('creating revision data using `' + type + '`', { verbose: true });
34-
return dataGenerator.generate()
35-
.then(function(data) {
35+
var promises = {
36+
data: this._getData(),
37+
scm: this._getScmData()
38+
};
39+
40+
return Promise.hash(promises)
41+
.then(function(results) {
42+
var data = results.data;
43+
data.scm = results.scm;
3644
self.log('generated revision data for revision: `' + data.revisionKey + '`', { verbose: true });
3745
return data;
3846
})
@@ -41,6 +49,27 @@ module.exports = {
4149
})
4250
.catch(this._errorMessage.bind(this));
4351
},
52+
53+
_getData: function() {
54+
var type = this.readConfig('type');
55+
this.log('creating revision data using `' + type + '`', { verbose: true });
56+
var DataGenerator = require('./lib/data-generators')[type];
57+
return new DataGenerator({
58+
plugin: this
59+
}).generate();
60+
},
61+
62+
_getScmData: function() {
63+
var ScmDataGenerator = this.readConfig('scm');
64+
if (ScmDataGenerator) {
65+
return new ScmDataGenerator({
66+
plugin: this
67+
}).generate();
68+
} else {
69+
return Promise.resolve();
70+
}
71+
},
72+
4473
_errorMessage: function(error) {
4574
this.log(error, { color: 'red' });
4675
return Promise.reject(error);

lib/scm-data-generators/git.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var CoreObject = require('core-object');
2+
var Promise = require('ember-cli/lib/ext/promise');
3+
var gitRepoInfo = require('git-repo-info');
4+
var simpleGit = require('simple-git');
5+
6+
module.exports = CoreObject.extend({
7+
init: function(path) {
8+
this.path = path;
9+
},
10+
11+
generate: function() {
12+
var _this = this;
13+
return new Promise(function(resolve, reject) {
14+
simpleGit(_this.path).log(function(err, log) {
15+
var info = log.latest;
16+
resolve({
17+
sha: info.hash.substring(1),
18+
email: info.author_email.substring(0, info.author_email.length - 1),
19+
name: info.author_name,
20+
timestamp: new Date(info.date).toISOString(),
21+
branch: gitRepoInfo().branch
22+
});
23+
});
24+
});
25+
}
26+
});

lib/scm-data-generators/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
"git": require('./git'),
3+
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"ember-cli-deploy-plugin": "^0.2.1",
5151
"git-repo-info": "^1.1.2",
5252
"minimatch": "^2.0.4",
53-
"rsvp": "^3.0.18"
53+
"rsvp": "^3.0.18",
54+
"simple-git": "^1.26.2"
5455
},
5556
"ember-addon": {
5657
"configPath": "tests/dummy/config"

tests/unit/index-nodetest.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('the index', function() {
7878
return previous;
7979
}, []);
8080

81-
assert.equal(messages.length, 5);
81+
assert.equal(messages.length, 6);
8282
});
8383

8484
it('adds default config to the config object', function() {
@@ -99,6 +99,7 @@ describe('the index', function() {
9999

100100
assert.isDefined(context.config['revision-data'].type);
101101
assert.isDefined(context.config['revision-data'].filePattern);
102+
assert.isDefined(context.config['revision-data'].scm);
102103
});
103104
});
104105

@@ -116,6 +117,9 @@ describe('the index', function() {
116117
"revision-data": {
117118
type: 'file-hash',
118119
filePattern: 'index.html',
120+
scm: function(context) {
121+
return require('../../lib/scm-data-generators')['git'];
122+
},
119123
distDir: function(context) {
120124
return context.distDir;
121125
},
@@ -131,8 +135,8 @@ describe('the index', function() {
131135
.then(function(result) {
132136
assert.equal(result.revisionData.revisionKey, 'ae1569f72495012cd5e8588e0f2f5d49');
133137
assert.isNotNull(result.revisionData.timestamp);
138+
assert.isNotNull(result.revisionData.scm.email);
134139
});
135140
});
136141
});
137142
});
138-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
var assert = require('ember-cli/tests/helpers/assert');
4+
var gitRepoInfo = require('git-repo-info');
5+
var ScmDataGenerator = require('../../../../lib/scm-data-generators/git');
6+
7+
describe('the git scm data generator', function() {
8+
var cwd;
9+
10+
before(function() {
11+
gitRepoInfo._changeGitDir('dotgit');
12+
});
13+
14+
beforeEach(function() {
15+
cwd = process.cwd();
16+
});
17+
18+
afterEach(function() {
19+
process.chdir(cwd);
20+
});
21+
22+
describe('#generate', function() {
23+
it('returns the correct data', function() {
24+
process.chdir('tests/fixtures/repo');
25+
26+
var subject = new ScmDataGenerator('dotgit');
27+
28+
return assert.isFulfilled(subject.generate())
29+
.then(function(data) {
30+
assert.equal(data.sha, '41d41f081b45ad50935c08b1203220737d9739b4');
31+
assert.equal(data.email, '[email protected]');
32+
assert.equal(data.name, 'Alisdair McDiarmid');
33+
assert.isNotNull(data.timestamp);
34+
assert.equal(data.branch, 'master');
35+
});
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)