Skip to content

Commit f354258

Browse files
authored
Write version and date to changelog when packaging (#1639)
* Write version and date to changelog when packaging The version in "package.json" will be baseline, each type of extension packaging can control the version string for that version of the log * Fix formatting issues
1 parent 9425bee commit f354258

File tree

7 files changed

+219
-8
lines changed

7 files changed

+219
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## 2.6.0 - TBD
3+
## {{releaseVersion}} - {{releaseDate}}
44

55
### Added
66

package-lock.json

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@
16821682
"unit-test": "npm test -- --label unitTests",
16831683
"coverage": "npm test -- --coverage",
16841684
"compile-tests": "del-cli ./assets/test/**/.build && npm run compile",
1685-
"package": "vsce package",
1685+
"package": "tsx ./scripts/package.ts",
16861686
"dev-package": "tsx ./scripts/dev_package.ts",
16871687
"preview-package": "tsx ./scripts/preview_package.ts",
16881688
"tag": "./scripts/tag_release.sh $npm_package_version",
@@ -1727,6 +1727,7 @@
17271727
"node-pty": "^1.0.0",
17281728
"octokit": "^3.2.2",
17291729
"prettier": "^3.6.0",
1730+
"replace-in-file": "^8.3.0",
17301731
"semver": "^7.7.2",
17311732
"simple-git": "^3.28.0",
17321733
"sinon": "^21.0.0",

scripts/dev_package.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
//===----------------------------------------------------------------------===//
1414
/* eslint-disable no-console */
1515

16-
import { exec, getExtensionVersion, getRootDirectory, main } from "./lib/utilities";
16+
import {
17+
exec,
18+
getExtensionVersion,
19+
getRootDirectory,
20+
main,
21+
updateChangelog,
22+
} from "./lib/utilities";
1723

1824
// eslint-disable-next-line @typescript-eslint/no-floating-promises
1925
main(async () => {
@@ -22,6 +28,8 @@ main(async () => {
2228
// Increment the patch version from the package.json
2329
const patch = version.patch + 1;
2430
const devVersion = `${version.major}.${version.minor}.${patch}-dev`;
31+
// Update version in CHANGELOG
32+
await updateChangelog(devVersion);
2533
// Use VSCE to package the extension
2634
await exec("npx", ["vsce", "package", "--no-update-package-json", devVersion], {
2735
cwd: rootDirectory,

scripts/lib/utilities.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { mkdtemp, readFile, rm } from "fs/promises";
1818
import * as path from "path";
1919
import * as os from "os";
2020
import * as semver from "semver";
21+
import { replaceInFile } from "replace-in-file";
2122

2223
/**
2324
* Executes the provided main function for the script while logging any errors.
@@ -42,13 +43,25 @@ export function getRootDirectory(): string {
4243
return path.join(__dirname, "..", "..");
4344
}
4445

46+
/**
47+
* Returns the path to the extension manifest.
48+
*/
49+
export function getManifest(): string {
50+
return path.join(getRootDirectory(), "package.json");
51+
}
52+
53+
/**
54+
* Returns the path to the extension changelog.
55+
*/
56+
export function getChangelog(): string {
57+
return path.join(getRootDirectory(), "CHANGELOG.md");
58+
}
59+
4560
/**
4661
* Retrieves the version number from the package.json.
4762
*/
4863
export async function getExtensionVersion(): Promise<semver.SemVer> {
49-
const packageJSON = JSON.parse(
50-
await readFile(path.join(getRootDirectory(), "package.json"), "utf-8")
51-
);
64+
const packageJSON = JSON.parse(await readFile(getManifest(), "utf-8"));
5265
if (typeof packageJSON.version !== "string") {
5366
throw new Error("Version number in package.json is not a string");
5467
}
@@ -112,3 +125,20 @@ export async function withTemporaryDirectory<T>(
112125
});
113126
}
114127
}
128+
129+
export async function updateChangelog(version: string): Promise<void> {
130+
await replaceInFile({
131+
files: getChangelog(),
132+
from: /{{releaseVersion}}/g,
133+
to: version,
134+
});
135+
const date = new Date();
136+
const year = date.getUTCFullYear().toString().padStart(4, "0");
137+
const month = (date.getUTCMonth() + 1).toString().padStart(2, "0");
138+
const day = date.getUTCDate().toString().padStart(2, "0");
139+
await replaceInFile({
140+
files: getChangelog(),
141+
from: /{{releaseDate}}/g,
142+
to: `${year}-${month}-${day}`,
143+
});
144+
}

scripts/package.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2025 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
/* eslint-disable no-console */
15+
16+
import {
17+
exec,
18+
getExtensionVersion,
19+
getRootDirectory,
20+
main,
21+
updateChangelog,
22+
} from "./lib/utilities";
23+
24+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
25+
main(async () => {
26+
const rootDirectory = getRootDirectory();
27+
const version = await getExtensionVersion();
28+
const versionString = `${version.major}.${version.minor}.${version.patch}`;
29+
// Update version in CHANGELOG
30+
await updateChangelog(versionString);
31+
// Use VSCE to package the extension
32+
await exec("npx", ["vsce", "package"], {
33+
cwd: rootDirectory,
34+
});
35+
});

scripts/preview_package.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414
/* eslint-disable no-console */
15-
16-
import { exec, getExtensionVersion, getRootDirectory, main } from "./lib/utilities";
15+
import {
16+
exec,
17+
getExtensionVersion,
18+
getRootDirectory,
19+
main,
20+
updateChangelog,
21+
} from "./lib/utilities";
1722

1823
/**
1924
* Formats the given date as a string in the form "YYYYMMdd".
@@ -43,6 +48,8 @@ main(async () => {
4348
" The version in the package.json has probably been incorrectly set to an odd minor version."
4449
);
4550
}
51+
// Update version in CHANGELOG
52+
await updateChangelog(previewVersion);
4653
// Use VSCE to package the extension
4754
await exec(
4855
"npx",

0 commit comments

Comments
 (0)