Skip to content

Commit 1145644

Browse files
Add vsce pre-release step to CI (#399)
* Add vsce pre-release step to CI * Rebase on current master * bump-version: handle case when main version minor gets bumped * bump-version: handle case when main version major gets bumped
1 parent d180824 commit 1145644

File tree

4 files changed

+156
-6
lines changed

4 files changed

+156
-6
lines changed

.github/workflows/bump-version.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const semver = require("semver");
4+
5+
const latestPublish = process.argv[2];
6+
7+
const packageJson = fs.readFileSync(path.join("./", "package.json"), {
8+
encoding: "utf-8",
9+
});
10+
11+
let release = JSON.parse(packageJson).version;
12+
13+
let newVersion = latestPublish;
14+
15+
// If the main release gets a major bump but did not get published yet, the package.json version
16+
// will be higher than the one retrieved from the marketplace, so we need to increment from the main release
17+
// E.g. package.json gets bumped to 1.5.0 -> 1.6.0
18+
if (semver.major(release) - semver.major(latestPublish) === 1) {
19+
newVersion = semver.inc(release, "minor", semver.rel);
20+
}
21+
// A prepublished version must be one minor higher than a regular published version.
22+
// E.g. if package.json has version 1.3.0 and there is no prepublished version yet,
23+
// increment minor by one -> 1.4.0.
24+
else if (semver.minor(latestPublish) === semver.minor(release)) {
25+
newVersion = semver.inc(newVersion, "minor", semver.rel);
26+
}
27+
// Increment the version patch. E.g. if we fetch version 1.4.0 as the latest pre-release,
28+
// increment patch by one -> 1.4.1.
29+
else if (semver.minor(latestPublish) > semver.minor(release)) {
30+
newVersion = semver.inc(newVersion, "patch", semver.rel);
31+
}
32+
// If the main release gets a minor bump but did not get published yet, the package.json version
33+
// will be higher than the one retrieved from the marketplace, so we need to increment from the main release
34+
// E.g. package.json gets bumped to 1.5.0 -> 1.6.0
35+
else if (semver.minor(release) - semver.minor(latestPublish) === 1) {
36+
newVersion = semver.inc(release, "minor", semver.rel);
37+
}
38+
// Otherwise throw an error, because the pre-release version should always be just one
39+
// minor higher than the release version.
40+
else {
41+
throw new Error(
42+
"Version number minors are more than off by one, check package.json and (pre-)published versions manually."
43+
);
44+
}
45+
46+
if (!semver.valid(newVersion)) {
47+
throw new Error("Invalid version string: ", newVersion);
48+
}
49+
50+
console.log(`::set-output name=new_version::${newVersion}`);

.github/workflows/ci.yml

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,18 @@ jobs:
8181
package:
8282
needs: test
8383
runs-on: ubuntu-18.04
84-
84+
8585
steps:
8686
- uses: actions/[email protected]
87-
87+
8888
- name: Use Node.js
8989
uses: actions/[email protected]
9090
with:
9191
node-version: 14.4.0
9292

9393
- run: npm ci
9494
- run: npm run compile
95-
95+
9696
- name: Download MacOS binary
9797
uses: actions/[email protected]
9898
with:
@@ -126,11 +126,52 @@ jobs:
126126
env:
127127
COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
128128
run: echo "::set-output name=sha_short::${COMMIT_SHA:0:7}"
129-
129+
130+
- name: Get current pre-release version
131+
if: github.ref == 'refs/heads/master'
132+
id: get_pre_release
133+
run: |
134+
JSON=$(npx vsce show chenglou92.rescript-vscode --json)
135+
VERSION=$(echo $JSON | jq '.versions | .[0] | .["version"]')
136+
echo "::set-output name=current_version::${VERSION}"
137+
138+
- name: Increment pre-release version
139+
if: github.ref == 'refs/heads/master'
140+
id: increment_pre_release
141+
run: |
142+
NEW_VERSION=$(echo ${{ steps.get_pre_release.outputs.current_version }})
143+
node .github/workflows/bump-version.js ${NEW_VERSION}
144+
130145
- name: Package Extension
131-
run: npx vsce package -o rescript-vscode-${{ steps.vars.outputs.sha_short }}.vsix
132-
146+
if: github.ref != 'refs/heads/master'
147+
run: npx vsce package -o rescript-vscode-${{ steps.vars.outputs.sha_short }}.vsix
148+
149+
- name: Package Extension
150+
if: github.ref == 'refs/heads/master'
151+
run: npx vsce package -o rescript-vscode-${{ steps.increment_pre_release.outputs.new_version }}.vsix ${{ steps.increment_pre_release.outputs.new_version }} --no-git-tag-version
152+
133153
- uses: actions/upload-artifact@v2
154+
if: github.ref != 'refs/heads/master'
134155
with:
135156
name: rescript-vscode-${{ steps.vars.outputs.sha_short }}.vsix
136157
path: rescript-vscode-${{ steps.vars.outputs.sha_short }}.vsix
158+
159+
- uses: actions/upload-artifact@v2
160+
if: github.ref == 'refs/heads/master'
161+
with:
162+
name: rescript-vscode-${{ steps.increment_pre_release.outputs.new_version }}.vsix
163+
path: rescript-vscode-${{ steps.increment_pre_release.outputs.new_version }}.vsix
164+
165+
- name: Publish latest master to GitHub
166+
if: github.ref == 'refs/heads/master'
167+
uses: marvinpinto/action-automatic-releases@latest
168+
with:
169+
repo_token: "${{ secrets.GITHUB_TOKEN }}"
170+
automatic_release_tag: "latest-master"
171+
prerelease: true
172+
title: "Latest master"
173+
files: rescript-vscode-${{ steps.increment_pre_release.outputs.new_version }}.vsix
174+
175+
- name: Publish extension as pre-release
176+
if: github.ref == 'refs/heads/master'
177+
run: npx vsce publish --pat ${{ secrets.MARKETPLACE_TOKEN }} --pre-release ${{ steps.increment_pre_release.outputs.new_version }} --no-git-tag-version

package-lock.json

Lines changed: 58 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
"devDependencies": {
200200
"@types/node": "^14.14.41",
201201
"@types/vscode": "1.68.0",
202+
"semver": "^7.3.7",
202203
"typescript": "^4.7.3"
203204
}
204205
}

0 commit comments

Comments
 (0)