-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_version.js
357 lines (285 loc) · 10.9 KB
/
auto_version.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
const fs = require('fs')
const {git, escapeRegExp} = require('./library');
const Print = require('./library').Print.prototype;
const print = function (txt){Print.print(txt)}
const argParse = require('./argParse');
const argDefinitions = require('./arguments.json');
const statList = [];
function getLatestTag(kwargs) {
// Runs 'git describe --tags' command and return result.
let version, describeTags;
try{
kwargsTmp=Object.assign({}, kwargs);
kwargsTmp.raise_on_error = true;
describePattern = `${kwargs.version_prefix}*.*.*`;
describeTags = git(`describe --tags --match "${describePattern}"`, kwargsTmp, statList, kwargs.simulate).replace('\n','');
commitPattern = `^${kwargs.version_prefix}([0-9]+)\\.([0-9]+)\\.([0-9]+)-([0-9]+)-(\\S+)`;
versionPattern = `^${kwargs.version_prefix}([0-9]+)\\.([0-9]+)\\.([0-9]+)$`;
versionMatch = describeTags.match(versionPattern);
commitMatch = describeTags.match(commitPattern);
match = versionMatch || commitMatch;
[ full, major, minor, patch, commit, hash ] = match
version = `${kwargs.version_prefix}${major}.${minor}.${patch}`
commitPropogated = commitMatch != null
return {version, commitPropogated, commit, hash}
}
catch (error){
return error
}
}
function getVersionInfo (kwargs){
const versionInfo = getLatestTag(kwargs)
let {version, commitPropogated, commit, hash} = versionInfo;
let initVersion, processTagOnly;
if ('error' in versionInfo){
// If no tags found, get it from package.json
if ('stderr' in versionInfo.error){
if (!(versionInfo.error.stderr.includes('No names found'))){process.exit(1)}
}
print('No version tags found, trying to extract version information from package.json.')
try {
const content = fs.readFileSync(kwargs.package_file)
packageJson = JSON.parse(content);
version = `${kwargs.version_prefix}${packageJson[kwargs.package_version_field]}`;
commitPropogated = !git('status', kwargs, statList, true).includes('nothing to commit, working tree clean');
processTagOnly = true;
}
catch (error){
// on error set it to default version
commitPropogated = !git('status', kwargs, statList, true).includes('nothing to commit, working tree clean');
initVersion = true;
processTagOnly = true;
}
}
else {
if (commitPropogated){
print(`Version tag found ${version} with ${commit} commit${commit > 1 ? 's' : ''}. Latest hash: ${hash}\n`)
}
}
return {version , commitPropogated, initVersion, commit, hash, processTagOnly}
}
function bumpVersion(version, kwargs) {
// Increase semver version number
let [ major, minor, patch ] = version.substring(1).split('.');
switch (kwargs.operation){
case 'major' : major++ ;minor=0; patch = 0; break;
case 'minor' : minor++ ;patch=0; break;
case 'patch' : patch++ ;break;
}
return `${kwargs.version_prefix}${major}.${minor}.${patch}`;
}
function changePackageJsonVersion(version, kwargs) {
try {
const content = fs.readFileSync(kwargs.package_file)
packageJson = JSON.parse(content)
packageJson[kwargs.package_version_field] = version.substring(1)
let json = JSON.stringify(packageJson, null, 2);
fs.writeFileSync(kwargs.package_file, json);
print(`\n${kwargs.package_file} '${kwargs.package_version_field}' field modified succesfully as '${packageJson[kwargs.package_version_field]}'\n`)
}
catch (error){
throw error
}
}
function comparePackageJsonVersion(version, kwargs){
try {
const content = fs.readFileSync(kwargs.package_file)
packageJson = JSON.parse(content)
let versionSame = packageJson[kwargs.package_version_field] == version.substring(1)
if (versionSame){
print(`\n${kwargs.package_file} '${kwargs.package_version_field}' field same as version '${version}'\n`)
}
return versionSame
}
catch (error){
throw error
}
}
// Change to New Version
function changeVersion(version, kwargs){
try{
// Last Commit Autohor, E-Mail, Message
const seperator = '_#_'
const result = git (`log -1 --pretty=%an${seperator}%ae${seperator}%B`, kwargs, statList, kwargs.simulate)
const [lastCommitAuthor, lastCommitEmail, lastCommitMessage] = result.split(seperator)
const user = kwargs.user == 'last_commit_author' ? lastCommitAuthor : kwargs.user;
git (`config user.name "${user}"`, kwargs, statList)
const email = kwargs.email == 'last_commit_email' ? lastCommitEmail : kwargs.email;
git (`config user.email "${email}"`, kwargs, statList)
// Change package.json
const sameVersion = comparePackageJsonVersion(version, kwargs);
if (!sameVersion) {
if (kwargs.simulate) {
print(`simulate call to changePackageJsonVersion(${version})`)
} else {
changePackageJsonVersion(version, kwargs)
// Stage package.json
git (`add ${kwargs.package_file}`, kwargs, statList)
// Commit
let commitMessage = kwargs.commit_message == 'last_commit_message' ? lastCommitMessage : kwargs.commit_message;
if (kwargs.skip_ci){
commitMessage = `${commitMessage}\n${kwargs.skip_ci_pattern}`
}
git (`commit -m "${commitMessage}"`, kwargs, statList)
}
}
// Tag
const { force_update } = kwargs
git (`tag ${version}${force_update ? ' -f' : ''}`, kwargs, statList)
// Test
if (kwargs.test_run){
// Set test remote
git (`remote set-url --push origin ${kwargs.test_repo}`,kwargs, statList)
// Push test commit
let geturl = git (`remote get-url origin`, kwargs, statList)
print (`Pushing changes to remote test repository ${geturl}`)
git ('push -u origin master', kwargs, statList)
// Push Test Tag
git (`push origin --tags${force_update ? ' -f' : ''}`, kwargs, statList)
} else {
// Push commit
let geturl = git (`remote get-url origin`, kwargs, statList)
print (`Pushing changes to remote repository ${geturl}`)
git ('push -u origin master', kwargs, statList)
// Push Tag
git (`push origin --tags${force_update ? ' -f' : ''}`, kwargs, statList)
}
// Print Complete
print('Operation Complete.')
}
catch ( error ) {
console.error(error, error.stack.split('\n')[1])
process.exit(1)
}
}
function evaluateVersion(kwargs) {
// Evaluate current version and Bump
const versionInfo = getVersionInfo(kwargs);
const { version, commitPropogated, initVersion, processTagOnly } = versionInfo;
// If ccommitPropogated then there is a commit after latest versioning.
if (commitPropogated || processTagOnly ){
// Extract operation from commit message
if (kwargs.operation == 'last_commit_message' || kwargs.test_message){
const commitMessage = kwargs.test_message ? kwargs.test_message : git (`log -1 --pretty=%B`, kwargs, statList, true);
if (commitMessage.match(escapeRegExp(kwargs.major_pattern))){kwargs.operation = 'major';}
else if (commitMessage.match(escapeRegExp(kwargs.minor_pattern))){kwargs.operation = 'minor';}
else if (commitMessage.match(escapeRegExp(kwargs.patch_pattern))){kwargs.operation = 'patch';}
else if (commitMessage.match(escapeRegExp(kwargs.ignore_pattern))){kwargs.operation = 'ignore';}
else {
kwargs.operation = kwargs.default_action;
}
if (kwargs.operation == 'ignore'){
print(`Found ${kwargs.ignore_pattern} pattern in commit message. Auto Versioning has been cancelled.`);
process.exit(0)
}
}
if (!initVersion &! processTagOnly) {
//Bump Version
newVersion = bumpVersion (version, kwargs)
print (`Opereration '${kwargs.operation}' from old version ${version} to new version ${newVersion}`)
} else if (initVersion){
print(`Version info could not extracted from ${kwargs.package_file}, assigning default ${kwargs.version_prefix}${kwargs.initial_version}`)
newVersion = `${kwargs.version_prefix}${kwargs.initial_version}`;
} else {
print(`Version info extracted from ${kwargs.package_file}, as ${version}`)
newVersion = `${version}`;
}
//Change Version
changeVersion(newVersion, kwargs)
}
// There is no commit after latest versioning. Do Nothing.
else {
print (`No commit after latest version ${version}`)
}
}
function create_test_commit(kwargs){
const date = new Date();
result = fs.writeFileSync('test_file', date.toISOString())
git (`add .`, kwargs, statList)
git (`status`, kwargs, statList)
git (`commit -m "${kwargs.create_test_commit} ${date.toISOString()}"`, kwargs, statList)
git (`status`, kwargs, statList)
}
function run (argString) {
//Parse Args
let kwArgs = {};
try {kwArgs = argParse(argString, argDefinitions);}
catch (err){
print(err);
print(`Check Command Line Options :`);
print(`${argString}`);
process.exit(1);
}
//Environment Variable
const envVarArguments = process.env[kwArgs.env_var]
if (envVarArguments){
let envArgs = {};
try {envArgs = argParse(envVarArguments, argDefinitions);}
catch (err){
print(err);
if (err == 'Argument Error'){
print(`Check $${kwArgs.env_var} Environment Variable :`);
print(`${envVarArguments}`);
;process.exit(1);
}
}
// Overwrite environment arguments over command line arguments
for (let [key, value] of Object.entries(envArgs)){
kwArgs[key] = value;
}
}
//Verbose
if (kwArgs['verbose'] &! kwArgs['silent']) {
kwArgs['print_stdout'] = true;
kwArgs['print_command'] = true;
kwArgs['print_parameters'] = true;
kwArgs['print_arguments'] = true;
kwArgs['print_envvar'] = true;
}
//Silent
Print.silent = kwArgs['silent']
// Print Command Line Arguments
if (!kwArgs.silent && kwArgs.print_arguments){
console.log('Command Line Arguments : ', argString, '\n')
}
// Print environment Variable
if (!kwArgs.silent && kwArgs.print_arguments){
console.log(`Environment Variable $${kwArgs.env_var} : `, envVarArguments, '\n')
}
// Print Parameters
if (!kwArgs.silent && kwArgs.print_parameters){
console.log('Parameters : ')
Object.entries(kwArgs).forEach((item=>console.log(item[0],'=',item[1])))
console.log(' ')
}
// Test
if (kwArgs.test_run){
if (kwArgs.test_repo && kwArgs.test_folder){
try {process.chdir(kwArgs.test_folder);}
catch (error){
console.error(error)
process.exit(1)
}
} else {
console.error ('test_repo and test_folder should be assigned.');
process.exit(1);
}
}
print(`Operation Started. \n`)
//Create test commit
if (kwArgs.create_test_commit){
create_test_commit(kwArgs)
}
// Call Versining Function
evaluateVersion(kwArgs);
}
module.exports = run;
const __name__ = process.argv[1].split('/').pop();
if (__name__ == 'auto_version.js'){
//print(`Process : ${process.argv[1]}`)
if (process.argv.length > 2){
run(process.argv.slice(2).join(' '))
} else {
run()
}
}