| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env node | |
| 2 | |
| 3 // TODO(vojta): pre-commit hook for validating messages | |
| 4 // TODO(vojta): report errors, currently Q silence everything which really sucks | |
| 5 | |
| 6 var child = require('child_process'); | |
| 7 var fs = require('fs'); | |
| 8 var util = require('util'); | |
| 9 var q = require('qq'); | |
| 10 | |
| 11 var GIT_LOG_CMD = 'git log --grep="%s" -E --format=%s %s'; | |
| 12 var GIT_TAG_CMD = 'git describe --tags --abbrev=0'; | |
| 13 | |
| 14 var HEADER_TPL = '<a name="%s"></a>\n# %s (%s)\n\n'; | |
| 15 var LINK_ISSUE = '[#%s](https://github.com/angular/di.dart/issues/%s)'; | |
| 16 var LINK_COMMIT = '[%s](https://github.com/angular/di.dart/commit/%s)'; | |
| 17 | |
| 18 var EMPTY_COMPONENT = '$$'; | |
| 19 | |
| 20 | |
| 21 var warn = function() { | |
| 22 console.log('WARNING:', util.format.apply(null, arguments)); | |
| 23 }; | |
| 24 | |
| 25 | |
| 26 var parseRawCommit = function(raw) { | |
| 27 if (!raw) return null; | |
| 28 | |
| 29 var lines = raw.split('\n'); | |
| 30 var msg = {}, match; | |
| 31 | |
| 32 msg.hash = lines.shift(); | |
| 33 msg.subject = lines.shift(); | |
| 34 msg.closes = []; | |
| 35 msg.breaks = []; | |
| 36 | |
| 37 lines.forEach(function(line) { | |
| 38 match = line.match(/(?:Closes|Fixes)\s#(\d+)/); | |
| 39 if (match) msg.closes.push(parseInt(match[1])); | |
| 40 }); | |
| 41 | |
| 42 match = raw.match(/BREAKING CHANGE:([\s\S]*)/); | |
| 43 if (match) { | |
| 44 msg.breaking = match[1]; | |
| 45 } | |
| 46 | |
| 47 | |
| 48 msg.body = lines.join('\n'); | |
| 49 match = msg.subject.match(/^(.*)\((.*)\)\:\s(.*)$/); | |
| 50 | |
| 51 if (!match || !match[1] || !match[3]) { | |
| 52 warn('Incorrect message: %s %s', msg.hash, msg.subject); | |
| 53 return null; | |
| 54 } | |
| 55 | |
| 56 msg.type = match[1]; | |
| 57 msg.component = match[2]; | |
| 58 msg.subject = match[3]; | |
| 59 | |
| 60 return msg; | |
| 61 }; | |
| 62 | |
| 63 | |
| 64 var linkToIssue = function(issue) { | |
| 65 return util.format(LINK_ISSUE, issue, issue); | |
| 66 }; | |
| 67 | |
| 68 | |
| 69 var linkToCommit = function(hash) { | |
| 70 return util.format(LINK_COMMIT, hash.substr(0, 8), hash); | |
| 71 }; | |
| 72 | |
| 73 | |
| 74 var currentDate = function() { | |
| 75 var now = new Date(); | |
| 76 var pad = function(i) { | |
| 77 return ('0' + i).substr(-2); | |
| 78 }; | |
| 79 | |
| 80 return util.format('%d-%s-%s', now.getFullYear(), pad(now.getMonth() + 1), pad
(now.getDate())); | |
| 81 }; | |
| 82 | |
| 83 | |
| 84 var printSection = function(stream, title, section, printCommitLinks) { | |
| 85 printCommitLinks = printCommitLinks === undefined ? true : printCommitLinks; | |
| 86 var components = Object.getOwnPropertyNames(section).sort(); | |
| 87 | |
| 88 if (!components.length) return; | |
| 89 | |
| 90 stream.write(util.format('\n## %s\n\n', title)); | |
| 91 | |
| 92 components.forEach(function(name) { | |
| 93 var prefix = '-'; | |
| 94 var nested = section[name].length > 1; | |
| 95 | |
| 96 if (name !== EMPTY_COMPONENT) { | |
| 97 if (nested) { | |
| 98 stream.write(util.format('- **%s:**\n', name)); | |
| 99 prefix = ' -'; | |
| 100 } else { | |
| 101 prefix = util.format('- **%s:**', name); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 section[name].forEach(function(commit) { | |
| 106 if (printCommitLinks) { | |
| 107 stream.write(util.format('%s %s\n (%s', prefix, commit.subject, linkToC
ommit(commit.hash))); | |
| 108 if (commit.closes.length) { | |
| 109 stream.write(',\n ' + commit.closes.map(linkToIssue).join(', ')); | |
| 110 } | |
| 111 stream.write(')\n'); | |
| 112 } else { | |
| 113 stream.write(util.format('%s %s', prefix, commit.subject)); | |
| 114 } | |
| 115 }); | |
| 116 }); | |
| 117 | |
| 118 stream.write('\n'); | |
| 119 }; | |
| 120 | |
| 121 | |
| 122 var readGitLog = function(grep, from) { | |
| 123 var deferred = q.defer(); | |
| 124 | |
| 125 // TODO(vojta): if it's slow, use spawn and stream it instead | |
| 126 console.log(util.format(GIT_LOG_CMD, grep, '%H%n%s%n%b%n==END==', from)); | |
| 127 child.exec(util.format(GIT_LOG_CMD, grep, '%H%n%s%n%b%n==END==', from), functi
on(code, stdout, stderr) { | |
| 128 var commits = []; | |
| 129 | |
| 130 stdout.split('\n==END==\n').forEach(function(rawCommit) { | |
| 131 var commit = parseRawCommit(rawCommit); | |
| 132 if (commit) commits.push(commit); | |
| 133 }); | |
| 134 | |
| 135 deferred.resolve(commits); | |
| 136 }); | |
| 137 | |
| 138 return deferred.promise; | |
| 139 }; | |
| 140 | |
| 141 | |
| 142 var writeChangelog = function(stream, commits, version) { | |
| 143 var sections = { | |
| 144 fix: {}, | |
| 145 feat: {}, | |
| 146 perf: {}, | |
| 147 breaks: {} | |
| 148 }; | |
| 149 | |
| 150 sections.breaks[EMPTY_COMPONENT] = []; | |
| 151 | |
| 152 commits.forEach(function(commit) { | |
| 153 var section = sections[commit.type]; | |
| 154 var component = commit.component || EMPTY_COMPONENT; | |
| 155 | |
| 156 if (section) { | |
| 157 section[component] = section[component] || []; | |
| 158 section[component].push(commit); | |
| 159 } | |
| 160 | |
| 161 if (commit.breaking) { | |
| 162 sections.breaks[component] = sections.breaks[component] || []; | |
| 163 sections.breaks[component].push({ | |
| 164 subject: util.format("due to %s,\n %s", linkToCommit(commit.hash), commi
t.breaking), | |
| 165 hash: commit.hash, | |
| 166 closes: [] | |
| 167 }); | |
| 168 }; | |
| 169 }); | |
| 170 | |
| 171 stream.write(util.format(HEADER_TPL, version, version, currentDate())); | |
| 172 printSection(stream, 'Bug Fixes', sections.fix); | |
| 173 printSection(stream, 'Features', sections.feat); | |
| 174 printSection(stream, 'Performance Improvements', sections.perf); | |
| 175 printSection(stream, 'Breaking Changes', sections.breaks, false); | |
| 176 } | |
| 177 | |
| 178 | |
| 179 var generate = function(tagRange, file) { | |
| 180 console.log('Reading git log for', tagRange); | |
| 181 readGitLog('^fix|^feat|^perf|BREAKING', tagRange).then(function(commits) { | |
| 182 console.log('Parsed', commits.length, 'commits'); | |
| 183 console.log('Generating changelog to', file || 'stdout', '(', tagRange, ')')
; | |
| 184 writeChangelog(file ? fs.createWriteStream(file) : process.stdout, commits,
tagRange); | |
| 185 }); | |
| 186 }; | |
| 187 | |
| 188 | |
| 189 // publish for testing | |
| 190 exports.parseRawCommit = parseRawCommit; | |
| 191 | |
| 192 // hacky start if not run by jasmine :-D | |
| 193 if (process.argv.join('').indexOf('jasmine-node') === -1) { | |
| 194 generate(process.argv[2], process.argv[3]); | |
| 195 } | |
| OLD | NEW |