-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (158 loc) · 5.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
var argv = require('yargs').argv;
var bump = require('gulp-bump');
var conventionalChangelog = require('gulp-conventional-changelog');
var conventionalRecommendedBump = require('conventional-recommended-bump');
var git = require('gulp-git');
var semver = require('semver');
var tap = require("gulp-tap");
var filter = require('gulp-filter');
var xmleditor = require('gulp-xml-transformer');
module.exports = function(gulp, userConfig) {
// default configuration
var config = {
versionNumber: argv.v,
hotfixBranch: argv.b,
bumpFiles: ['./package.json', './bower.json'],
changelogFile: './CHANGELOG.md',
conventionalChangelog: 'angular',
commitMessage: 'bump version number ',
tagPrefix: '',
masterBranch: 'master',
developBranch: 'develop',
origin: 'origin',
releaseBranch: 'release/',
push: false
};
// overwrite with user configuration
if (userConfig && userConfig.release) {
for (var configKey in userConfig.release) {
config[configKey] = userConfig.release[configKey];
}
}
// tasks
gulp.task('checkoutMaster', function(done) {
git.checkout(config.masterBranch, {}, done);
});
gulp.task('pullMaster', ['checkoutMaster'], function(done) {
git.pull(config.origin, config.masterBranch, {}, done);
});
gulp.task('checkoutSourceBranch', ['pullMaster'], function(done) {
if (config.hotfixBranch) {
git.checkout(config.hotfixBranch, {}, done);
} else {
git.checkout(config.developBranch, {}, done);
}
});
gulp.task('pullSourceBranch', ['checkoutSourceBranch'], function(done) {
if (!config.hotfixBranch) {
git.pull(config.origin, config.developBranch, {args: '--ff-only'}, done);
} else {
done();
}
});
gulp.task('bump', ['pullSourceBranch'], function() {
var jsonFilter = filter('**/*.json', {restore: true});
var xmlFilter = filter('**/*.xml', {restore: true});
if (config.versionNumber) {
// use passed version number
if (!semver.valid(config.versionNumber)) {
throw 'Failed: specify a semver valid version "-v X.X.X';
} else {
// bump json files using gulp-bump and xml files using gulp-xml-editor
return gulp.src(config.bumpFiles, {base: "./"})
.pipe(jsonFilter)
.pipe(bump({version: config.versionNumber}))
.pipe(gulp.dest('./')) // TODO check if this is needed twice
.pipe(jsonFilter.restore)
.pipe(xmlFilter)
.pipe(xmleditor([
{path: '.', attr: {'version': config.versionNumber}}
]))
.pipe(xmlFilter.restore)
.pipe(gulp.dest('./'));
}
} else {
// generate new version
conventionalRecommendedBump({
preset: config.conventionalChangelog
}, function(err, releaseAs) {
gulp.src(config.bumpFiles, {base: "./"})
.pipe(jsonFilter)
.pipe(bump({type: releaseAs}))
.pipe(tap(function(file, t) {
// extract new version
if (!config.versionNumber) {
var json = JSON.parse(String(file.contents));
config.versionNumber = json.version;
gulp.src(config.bumpFiles, {base: "./"})
.pipe(xmlFilter)
.pipe(xmleditor([
{path: '.', attr: {'version': config.versionNumber}}
]))
.pipe(gulp.dest('./'));
}
return t;
}))
.pipe(gulp.dest('./'));
});
}
});
gulp.task('changelog', ['bump'], function() {
return gulp.src(config.changelogFile, {
buffer: false
})
.pipe(conventionalChangelog({
preset: config.conventionalChangelog
}))
.pipe(gulp.dest('./'));
});
gulp.task('createBranch', ['bump'], function(cb) {
if (!config.hotfixBranch) {
git.checkout(config.releaseBranch + 'newRelease', {args: '-b'}, cb);
} else {
cb();
}
});
gulp.task('commit', ['bump', 'changelog', 'createBranch'], function() {
var files = config.bumpFiles.concat(config.changelogFile);
return gulp.src(files)
.pipe(git.commit(config.commitMessage + config.versionNumber));
});
gulp.task('release', ['commit'], function(cb) {
checkoutMaster();
function checkoutMaster() {
git.checkout(config.masterBranch, {}, mergeInMaster);
}
function mergeInMaster() {
if (config.hotfixBranch) {
git.merge(config.hotfixBranch, {args: '--no-ff'}, tagVersion);
} else {
git.merge(config.releaseBranch + 'newRelease', {args: '--no-ff'}, tagVersion);
}
}
function tagVersion() {
git.tag(config.tagPrefix + config.versionNumber, config.versionNumber, {}, checkoutDevelop);
}
function checkoutDevelop() {
git.checkout(config.developBranch, {}, mergeInDevelop);
}
function mergeInDevelop() {
git.merge(config.masterBranch, {}, deleteBranch);
}
function deleteBranch() {
if (config.hotfixBranch) {
git.branch(config.hotfixBranch, {args: '-d'}, pushBranches);
} else {
git.branch(config.releaseBranch + 'newRelease', {args: '-d'}, pushBranches);
}
}
function pushBranches() {
if (config.push) {
git.push(config.origin, [config.developBranch, config.masterBranch], {args: " --tags"}, cb);
} else {
cb();
}
}
});
};