From bb0c5054d03eae138792197b50f8148186906a80 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 30 Dec 2023 15:01:23 +0100 Subject: [PATCH] Add stylua selection version command --- stylua-vscode/CHANGELOG.md | 1 + stylua-vscode/package-lock.json | 11 +++----- stylua-vscode/package.json | 6 +++++ stylua-vscode/src/download.ts | 13 +++++---- stylua-vscode/src/extension.ts | 47 +++++++++++++++++++++++++++++++-- stylua-vscode/src/github.ts | 10 ++++--- 6 files changed, 67 insertions(+), 21 deletions(-) diff --git a/stylua-vscode/CHANGELOG.md b/stylua-vscode/CHANGELOG.md index b00413a4..8d25fe3b 100644 --- a/stylua-vscode/CHANGELOG.md +++ b/stylua-vscode/CHANGELOG.md @@ -15,6 +15,7 @@ To view the changelog of the StyLua binary, see [here](https://github.com/Johnny - Added configuration option `stylua.configPath` to provide a direct path to a `stylua.toml` file. Note: this will override any workspace config lookup - Added configuration option `stylua.verify` to pass `--verify` to StyLua CLI when formatting a file. This enforces output verification +- Added command `StyLua: Select Version` to customize which version of StyLua to install. This command updates the `stylua.targetReleaseVersion` setting ### Changed diff --git a/stylua-vscode/package-lock.json b/stylua-vscode/package-lock.json index 7b3daff2..062bf59a 100644 --- a/stylua-vscode/package-lock.json +++ b/stylua-vscode/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "ignore": "^5.1.8", "node-fetch": "^2.6.1", + "semver": "^7.5.4", "unzipper": "^0.10.14" }, "devDependencies": { @@ -1910,7 +1911,6 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "dev": true, "dependencies": { "yallist": "^4.0.0" }, @@ -2458,7 +2458,6 @@ "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", - "dev": true, "dependencies": { "lru-cache": "^6.0.0" }, @@ -2859,8 +2858,7 @@ "node_modules/yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", - "dev": true + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "node_modules/yargs": { "version": "16.2.0", @@ -4296,7 +4294,6 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "dev": true, "requires": { "yallist": "^4.0.0" } @@ -4677,7 +4674,6 @@ "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", - "dev": true, "requires": { "lru-cache": "^6.0.0" } @@ -4985,8 +4981,7 @@ "yallist": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", - "dev": true + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "yargs": { "version": "16.2.0", diff --git a/stylua-vscode/package.json b/stylua-vscode/package.json index b4606855..a4538eda 100644 --- a/stylua-vscode/package.json +++ b/stylua-vscode/package.json @@ -47,6 +47,11 @@ "command": "stylua.showOutputChannel", "title": "Show Output Channel", "category": "StyLua" + }, + { + "command": "stylua.selectVersion", + "title": "Select Version", + "category": "StyLua" } ], "configuration": { @@ -142,6 +147,7 @@ "dependencies": { "ignore": "^5.1.8", "node-fetch": "^2.6.1", + "semver": "^7.5.4", "unzipper": "^0.10.14" } } diff --git a/stylua-vscode/src/download.ts b/stylua-vscode/src/download.ts index 1c7ca1b3..7d4ffdbc 100644 --- a/stylua-vscode/src/download.ts +++ b/stylua-vscode/src/download.ts @@ -17,7 +17,7 @@ export class StyluaDownloader { if (path === undefined) { await vscode.workspace.fs.createDirectory(this.storageDirectory); - await this.downloadStyLuaVisual(); + await this.downloadStyLuaVisual(util.getDesiredVersion()); return await this.getStyluaPath(); } else { if (!(await util.fileExists(path))) { @@ -69,19 +69,18 @@ export class StyluaDownloader { } } - public downloadStyLuaVisual(): Thenable { + public downloadStyLuaVisual(version: string): Thenable { return vscode.window.withProgress( { cancellable: false, location: vscode.ProgressLocation.Notification, - title: "Downloading StyLua", + title: `Downloading StyLua (${version})`, }, - () => this.downloadStylua() + () => this.downloadStylua(version) ); } - private async downloadStylua(): Promise { - const version = util.getDesiredVersion(); + private async downloadStylua(version: string): Promise { const release = await this.github.getRelease(version); const assetFilename = util.getAssetFilenamePattern(); const outputFilename = util.getDownloadOutputFilename(); @@ -129,7 +128,7 @@ export class StyluaDownloader { .then((option) => { switch (option) { case "Install": - this.downloadStyLuaVisual(); + this.downloadStyLuaVisual(release.tagName); break; case "Release Notes": vscode.env.openExternal(vscode.Uri.parse(release.htmlUrl)); diff --git a/stylua-vscode/src/extension.ts b/stylua-vscode/src/extension.ts index fc82a8de..a176020d 100644 --- a/stylua-vscode/src/extension.ts +++ b/stylua-vscode/src/extension.ts @@ -1,7 +1,9 @@ import * as vscode from "vscode"; +import * as semver from "semver"; import { formatCode, checkIgnored } from "./stylua"; import { GitHub } from "./github"; import { StyluaDownloader } from "./download"; +import { getDesiredVersion } from "./util"; /** * Convert a Position within a Document to a byte offset. @@ -42,7 +44,7 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand("stylua.reinstall", async () => { - await downloader.downloadStyLuaVisual(); + await downloader.downloadStyLuaVisual(getDesiredVersion()); styluaBinaryPath = await downloader.getStyluaPath(); }) ); @@ -59,6 +61,47 @@ export async function activate(context: vscode.ExtensionContext) { }) ); + context.subscriptions.push( + vscode.commands.registerCommand("stylua.selectVersion", async () => { + const versions = (await github.getAllReleases()).sort((a, b) => + semver.rcompare(a.tagName, b.tagName) + ); + if (versions.length === 0) { + return; + } + const latestVersion = versions[0]; + + const selectedVersion = await vscode.window.showQuickPick( + versions + .sort((a, b) => semver.rcompare(a.tagName, b.tagName)) + .map((release) => { + if (release.tagName === latestVersion.tagName) { + return { label: `${release.tagName} (latest)` }; + } else { + return { label: release.tagName }; + } + }), + { + placeHolder: "Select the version of StyLua to install", + } + ); + + if (selectedVersion) { + const updateConfigValue = selectedVersion.label.includes("latest") + ? "latest" + : selectedVersion.label; + await downloader.downloadStyLuaVisual(selectedVersion.label); + vscode.workspace + .getConfiguration("stylua") + .update( + "targetReleaseVersion", + updateConfigValue, + vscode.ConfigurationTarget.Workspace + ); + } + }) + ); + context.subscriptions.push( vscode.workspace.onDidChangeConfiguration(async (change) => { if (change.affectsConfiguration("stylua")) { @@ -100,7 +143,7 @@ export async function activate(context: vscode.ExtensionContext) { ) .then((option) => { if (option === "Install") { - downloader.downloadStyLuaVisual(); + vscode.commands.executeCommand("stylua.reinstall"); } }); return []; diff --git a/stylua-vscode/src/github.ts b/stylua-vscode/src/github.ts index 5e305124..c6e4d958 100644 --- a/stylua-vscode/src/github.ts +++ b/stylua-vscode/src/github.ts @@ -132,6 +132,11 @@ export class GitHub implements Disposable { return this.credential.authenticated; } + public async getAllReleases(): Promise { + const json = await fetchJson(RELEASES_URL, this.credential.token); + return Array.isArray(json) ? json.map(releaseFromJson) : []; + } + public async getRelease(version: string): Promise { if (version === "latest") { const json = await fetchJson(RELEASES_URL_LATEST, this.credential.token); @@ -139,10 +144,7 @@ export class GitHub implements Disposable { } version = version.startsWith("v") ? version : "v" + version; - const json = await fetchJson(RELEASES_URL, this.credential.token); - const releases: GitHubRelease[] = Array.isArray(json) - ? json.map(releaseFromJson) - : []; + const releases = await this.getAllReleases(); for (const release of releases) { if (release.tagName.startsWith(version)) { return release;