-
Notifications
You must be signed in to change notification settings - Fork 20
/
publish_process.js
executable file
·370 lines (338 loc) · 15.5 KB
/
publish_process.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
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env node
/**
Perform the next step to publish the extension.
i. Verifies that it's on the master branch. If not so, terminate. Otherwise, pulls the latest master version.
ii. If the last message on master indicates Version X.Y.Z and it does not have a tag.
it means that steps iii. to vii. were executed correctly.
Ask whether it's ready to publish (ENTER or y)
- if so, create and publish the tag
- Otherwise, cancel.
iii. Fetches all tags, and verify that there is no version tag.
iv. Asks to bump the patch version (ENTER or p), the minor version (m) or the major version (M)
v. Populates the CHANGELOG.md with the most recent commit messages.
vi. If a newest version of Dafny is found, ask whether to refer to it as the latest (ENTER or y), or no (n)
vii. Follows the steps of CONTRIBUTING.
Ask to revise the CHANGELOG.md as needed
(n or anything else) : Abruptly stop.
(ENTER or y) : Creates a branch named "version-X.Y.Z"
Commits the files to this branch with message "Version X.Y.Z"
Push the branch.
Indicate that the PR should be merge without changing the commit message.
Indicate that the script just needs to be relaunched once the PR is approved and merged.
*/
const fs = require("fs");
const { promisify, getSystemErrorMap } = require('util');
const exec = require('child_process').exec;
const execAsync = promisify(exec);
const readline = require('readline');
const fetch = require('cross-fetch');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question = function(input) {
return new Promise((resolve, reject) => {
rl.question(input, resolve);
});
}
const changeLogFile = "CHANGELOG.md";
const dafnyReleasesURL = 'https://api.github.com/repos/dafny-lang/dafny/releases';
const constantsFile = "src/constants.ts";
const packageFile = "package.json";
const packageLockFile = "package-lock.json";
const ABORTED = "ABORTED";
const ACCEPT_HINT = "(ENTER)";
function ok(answer) {
return answer.toLowerCase() == "y" || answer == "";
}
async function getCurrentBranch() {
return (await execAsync("git branch --show-current")).stdout.trim();
}
// Ensures that the working directory is clean
async function ensureWorkingDirectoryClean() {
var unstagedChanges = (await execAsync("git diff")).stdout.trim() + (await execAsync("git diff --cached")).stdout.trim();
if(unstagedChanges != "") {
console.log("Please commit your changes before launching this script.");
//throw ABORTED;
}
}
async function ensureMaster() {
await ensureWorkingDirectoryClean();
var currentBranch = await getCurrentBranch();
if(currentBranch != "master") {
console.log(`You need to be on the 'master' branch to release a new version.`);
if(!ok(await question(`Switch from '${currentBranch}' to 'master'? ${ACCEPT_HINT}`))) {
console.log("Publishing script aborted.");
throw ABORTED;
}
console.log("switched to master branch");
console.log((await execAsync("git checkout master")).stdout);
currentBranch = await getCurrentBranch();
if(currentBranch != "master") {
console.log("Failed to checkout master");
throw ABORTED;
}
}
await execAsync("git pull");
console.log("Latest master checked out")
}
async function getCurrentTag() {
var tagList = (await execAsync("git show-ref --tags")).stdout.trim();
// recovers the last commit hash
var lastCommitHash = (await execAsync("git log -1 --pretty=%H")).stdout.trim();
// checks if the last commit hash is in the tag list:
var tagListRegex = new RegExp(`^${lastCommitHash}\\s*refs/tags/(v\\d+\\.\\d+\\.\\d+)`, 'm');
var match = tagListRegex.exec(tagList);
if(match == null) {
return null;
}
return match[1];
}
async function changeLogAndVersion() {
let changeLog = await fs.promises.readFile(changeLogFile, "utf8");
const currentDocumentVersionRegex = /^#\s*Release Notes\s*##\s*(\d+.\d+.\d+)/;
const match = currentDocumentVersionRegex.exec(changeLog);
if(match == null) {
console.log(`Could not find ${currentDocumentVersionRegex} in ${changeLogFile}`);
throw ABORTED;
}
const currentChangeLogVersion = match[1];
const updateChangeLogWith = ((changeLog, oldVersion) => async function(newVersion, messages, mostRecentDafnyRelease = undefined) {
const newChangeLog = changeLog.replace(currentDocumentVersionRegex, match =>
`# Release Notes\n\n## ${newVersion}\n${messages}\n\n## ${oldVersion}`);
await fs.promises.writeFile(changeLogFile, newChangeLog);
return true;
})(changeLog, currentChangeLogVersion);
return {updateChangeLogWith, currentChangeLogVersion};
}
async function getMostRecentDafnyRelease() {
let mostRecentDafnyRelease = null;
const dafnyReleases = await (await fetch(dafnyReleasesURL)).json();
for(var i = 0; i < dafnyReleases.length && mostRecentDafnyRelease == null; i++) {
if(dafnyReleases[i].tag_name != "nightly") {
mostRecentDafnyRelease = dafnyReleases[i].tag_name;
break;
}
}
if(mostRecentDafnyRelease == null) {
console.log(`Could not fetch the latest Dafny release version from ${dafnyReleasesURL}`);
throw ABORTED;
}
return mostRecentDafnyRelease;
}
async function readPackageJson() {
const packageContent = await fs.promises.readFile(packageFile);
const packageObj = JSON.parse(packageContent);
return packageObj;
}
async function writePackage(packageObj) {
await fs.promises.writeFile(packageFile, JSON.stringify(packageObj, null, 2));
}
// returns the new version number
async function nextVersion(currentVersion) {
const currentVersionRegex = /(\d+)\.(\d+)\.(\d+)/;
const match = currentVersionRegex.exec(currentVersion);
if(match == null) {
console.log(`Could not parse version ${currentVersion}`);
throw ABORTED;
}
var bumpedPatch = `${match[1]}.${match[2]}.${parseInt(match[3]) + 1}`;
var bumpedMinor = `${match[1]}.${parseInt(match[2]) + 1}.0`;
var bumpedMajor = `${parseInt(match[1]) + 1}.0.0`;
console.log("Should the next version be:");
console.log(`${currentVersion} => ${bumpedPatch}? ${ACCEPT_HINT}`);
console.log(`${currentVersion} => ${bumpedMinor}? (m)`);
var answer = await question(`${currentVersion} => ${bumpedMajor}? (M)\n`);
if(ok(answer)) {
return bumpedPatch;
} else if(answer == "m") {
return bumpedMinor;
} else if(answer == "M") {
return bumpedMajor;
} else {
console.log("Publishing script aborted.");
throw ABORTED;
}
}
async function getLastPreparedTag() {
const lastCommitMessage = (await execAsync("git log -1 --pretty=%B | head -n 1")).stdout.trim();
const lastCommitMessageRegex = /(v\d+\.\d+\.\d+)/;
const match = lastCommitMessageRegex.exec(lastCommitMessage);
if(match == null) {
return false;
}
return match[1];
}
function getCommandLine() {
switch (process.platform) {
case 'darwin' : return 'open';
case 'win32' : return 'start';
case 'win64' : return 'start';
default : return 'xdg-open';
}
}
async function getAllRecentCommitMessagesFormatted(currentChangeLogVersion) {
// Query git for all the commit messages from currentChangeLogVersion (excluded) to the latest commit (included)
var raw = (await execAsync(`git log --pretty=%B v${currentChangeLogVersion}..HEAD`)).stdout.trim();
raw = "- " + raw.trim().replace(/\n\s*/g, "\n- ");
raw = raw.replace(/\(#(\d+)\)/g, "(https://github.com/dafny-lang/ide-vscode/pull/$1)");
raw = raw.trim();
raw = raw.replace(/\n.*$/, ""); // Removes the last one, because it's the last release commit.
return raw.trim();
}
function close() {
rl.close();
return false;
}
async function isNewer(packageObj, mostRecentDafnyRelease) {
var versionList = packageObj["contributes"]["configuration"]["properties"]["dafny.version"]["enum"];
var previousDafnyVersion = versionList[1];
return previousDafnyVersion != mostRecentDafnyRelease;
}
async function updatePackageJson(packageObj, newVersion, mostRecentDafnyRelease) {
packageObj["version"] = newVersion;
var versionList = packageObj["contributes"]["configuration"]["properties"]["dafny.version"]["enum"];
// versionList starts with "latest", and then the last version
var previousDafnyVersion = versionList[1];
var updatedDafny = false;
if (mostRecentDafnyRelease !== undefined) {
var previousDafnyVersionListHead = versionList[1];
// If the previous dafny version is just different from mostRecentDafnyRelease by the patch number, replace it, otherwise insert it using splice
// We need to do pruning manually later, so that one could revert to a previous patch of Dafny immediately.
//if (previousDafnyVersionListHead == mostRecentDafnyRelease.substring(0, mostRecentDafnyRelease.lastIndexOf("."))) {
// versionList[1] = mostRecentDafnyRelease;
//} else {
versionList.splice(1, 0, mostRecentDafnyRelease);
//}
console.log("Updated Dafny version to " + mostRecentDafnyRelease);
var constantsContent = await fs.promises.readFile(constantsFile, { encoding: "utf8" });
var constantsContentRegex = /const\s*LatestVersion\s*=\s*'\d+.\d+.\d+';/;
constantsContent = constantsContent.replace(constantsContentRegex, `const LatestVersion = '${mostRecentDafnyRelease}';`);
await fs.promises.writeFile(constantsFile, constantsContent, { encoding: "utf8" });
updatedDafny = true;
} else {
console.log("The current Dafny version is still detected to be " + previousDafnyVersion);
}
await writePackage(packageObj);
return updatedDafny;
}
async function UpdateChangeLog(currentChangeLogVersion, packageObj, updateChangeLogWith, newVersion, mostRecentDafnyRelease) {
var allRecentCommitMessages = await getAllRecentCommitMessagesFormatted(currentChangeLogVersion);
if (packageObj["version"] == currentChangeLogVersion) {
if(mostRecentDafnyRelease !== undefined) {
allRecentCommitMessages = "- Added Dafny " + mostRecentDafnyRelease + "\n" + allRecentCommitMessages;
}
await updateChangeLogWith(newVersion, allRecentCommitMessages, mostRecentDafnyRelease);
console.log("I changed " + changeLogFile + " to reflect the new version.\nPlease make edits as needed and close the editing window.");
await execAsync(getCommandLine() + ' ' + changeLogFile);
if (!ok(await question(`Ready to continue? ${ACCEPT_HINT}`))) {
console.log("Aborting.");
throw ABORTED;
}
currentChangeLogVersionCheck = (await changeLogAndVersion()).currentChangeLogVersion;
if (currentChangeLogVersionCheck != newVersion) {
console.log(`The last version was supposed to be ${newVersion}, but the changelog was updated to ${currentChangeLogVersionCheck}. Aborting publishing.`);
throw ABORTED;
}
} else {
console.log("ChangeLog.md already up-to-date");
}
}
async function HandleFinalPublishingProcess(currentChangeLogVersion, lastPreparedTag) {
if ("v" + currentChangeLogVersion == lastPreparedTag) {
// Tag the current commit
console.log(`The changelog already mentions version ${currentChangeLogVersion}.\nYou now need to create the tag ${lastPreparedTag} and publish it to release this new version.`);
// ask for confirmation, and publish the tag.
if (ok(await question(`Create and publish the tag ${lastPreparedTag}? ${ACCEPT_HINT}`))) {
console.log(`Creating tag ${lastPreparedTag}...`);
await execAsync(`git tag ${lastPreparedTag}`);
console.log(`Publishing tag ${lastPreparedTag}...`);
await execAsync(`git push origin ${lastPreparedTag}`);
console.log(`${lastPreparedTag} published. The CI will take care of releasing the new VSCode extension.`);
} else {
console.log("Just run the script again when you are ready to publish the version. Aborting.");
throw ABORTED;
}
} else {
console.log("Something went wrong. I found " + lastPreparedTag + " in the last commit message, and this tag is not published yet.");
console.log("However, the changelog mentions a different version:" + currentChangeLogVersion);
console.log("Please fix the current state");
throw ABORTED;
}
}
async function isTagExists(tagName) {
var result = await execAsync(`git tag -l ${tagName}`);
return result.length > 0;
}
async function Main() {
try {
// verify that we are on the master branch.
await ensureMaster();
let tag = await getCurrentTag();
if(tag){
console.log(`The current master already has the tag ${tag}. Nothing needs to be done.\nIf you want to push the tag again, run 'git push --tags'`);
if(!ok(await question(`Do you want to publish a new version regardless? ${ACCEPT_HINT}`))){
throw ABORTED;
}
}
let {updateChangeLogWith, currentChangeLogVersion} = await changeLogAndVersion();
// Check if the last commit contains a message containing "vX.Y.Z", which indicates
// that we want to publish a new version of the extension
const lastPreparedTag = tag ? null : await getLastPreparedTag();
if(lastPreparedTag) {
// Check if the tag with the name lastPreparedTag does not exist yet
// If it exists locally, it means that the version is already published
// or that the tag was not pushed.
var tagExists = await isTagExists(lastPreparedTag);
// Here we assume that if it exists, it was already pushed.
if(!tagExists) {
await HandleFinalPublishingProcess(currentChangeLogVersion, lastPreparedTag);
return;
}
}
let newVersion = await nextVersion(currentChangeLogVersion);
let mostRecentDafnyRelease = (await getMostRecentDafnyRelease()).substring(1);
let packageObj = await readPackageJson();
console.log(`Going to proceed to publish ${newVersion}`);
var useNewVersion = false;
if(await isNewer(packageObj, mostRecentDafnyRelease)) {
if (ok(await question(`There is a new Dafny version available: (${mostRecentDafnyRelease}). Do you want to update it? ${ACCEPT_HINT}`))) {
// We keep that number
} else {
console.log("Ignoring new Dafny version.");
mostRecentDafnyRelease = undefined;
}
} else {
mostRecentDafnyRelease = undefined;
}
// Get all the commit messages since the last published tag
await UpdateChangeLog(currentChangeLogVersion, packageObj, updateChangeLogWith, newVersion, mostRecentDafnyRelease);
// All clear, we can modify constants.ts and package.json.
var updatedDafny = await updatePackageJson(packageObj, newVersion, mostRecentDafnyRelease);
// Execute npm install to ensure the package lock is up to date
console.log("Executing `npm install`...");
await execAsync("npm install");
// Create the new branch and git add all the files modified above
console.log("Creating new branch...");
const newBranch = `release-${newVersion}`;
await execAsync(`git checkout -b ${newBranch}`);
await execAsync(`git add ${changeLogFile} ${packageFile} ${constantsFile} ${packageLockFile}`);
if(ok(await question(`I made all the necessary edits. Push the changes to the remote repository? ${ACCEPT_HINT}`))) {
await execAsync(`git commit -m "Release v${newVersion}${ updatedDafny ? ` (updated Dafny to ${mostRecentDafnyRelease})` : "" }"`);
await execAsync(`git push origin --set-upstream ${newBranch}`);
console.log("Now, create the pull request by clicking the link below:");
console.log(`https://github.com/dafny-lang/ide-vscode/compare/${newBranch}?expand=1`);
console.log("When this PR is approved and merged, launch this script again to finish publishing the release.");
} else {
console.log("Aborting publishing.");
return close();
}
} catch(e) {
if(e != ABORTED) {
throw e;
}
} finally {
close();
}
}
Main();