Skip to content

Commit

Permalink
Add auto-updater for Swift-MesonLSP
Browse files Browse the repository at this point in the history
  • Loading branch information
JCWasmx86 authored and tristan957 committed Oct 23, 2023
1 parent 9442371 commit 43e2354
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## next

- Add configuration options specific for Swift-MesonLSP.
- Add auto-updater for Swift-MesonLSP.

## 1.13.0

Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ export async function activate(ctx: vscode.ExtensionContext) {
}
}),
);

await client.update(ctx);
ctx.subscriptions.push(client);
client.start();
await client.reloadConfig();
Expand Down
20 changes: 10 additions & 10 deletions src/lsp/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import { LanguageServer } from "../types";
import { SwiftMesonLspLanguageClient } from "./swift-mesonlsp";
import { Uri } from "vscode";

export function serverToClass(server: LanguageServer): any {
switch (server) {
case "Swift-MesonLSP":
return SwiftMesonLspLanguageClient;
default:
return null;
}
}

export async function createLanguageServerClient(
server: LanguageServer,
download: boolean,
context: vscode.ExtensionContext,
): Promise<LanguageServerClient | null> {
const serverToClass = (server: LanguageServer) => {
switch (server) {
case "Swift-MesonLSP":
return SwiftMesonLspLanguageClient;
default:
return null;
}
};

const klass = serverToClass(server);
if (klass == null) {
return null;
Expand Down Expand Up @@ -54,5 +54,5 @@ export async function createLanguageServerClient(
}
}

return new klass(languageServerPath, context);
return new klass(languageServerPath, context, klass.version);
}
25 changes: 23 additions & 2 deletions src/lsp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "vscode-languageclient/node";
import * as storage from "../storage";
import { LanguageServer } from "../types";
import { getOutputChannel } from "../utils";
import { serverToClass } from "./common";

export abstract class LanguageServerClient {
private static readonly clientOptions: LanguageClientOptions = {
Expand All @@ -27,6 +27,7 @@ export abstract class LanguageServerClient {
private readonly context: vscode.ExtensionContext;

protected languageServerPath: vscode.Uri | null;
protected referenceVersion: string;
readonly server: LanguageServer;

static readonly repoURL: string;
Expand All @@ -36,10 +37,16 @@ export abstract class LanguageServerClient {
protected abstract get debugExe(): Executable;
protected abstract get runExe(): Executable;

protected constructor(server: LanguageServer, languageServerPath: vscode.Uri, context: vscode.ExtensionContext) {
protected constructor(
server: LanguageServer,
languageServerPath: vscode.Uri,
context: vscode.ExtensionContext,
referenceVersion: string,
) {
this.server = server;
this.languageServerPath = languageServerPath;
this.context = context;
this.referenceVersion = referenceVersion;
}

private static cachedLanguageServer(server: LanguageServer, context: vscode.ExtensionContext): vscode.Uri | null {
Expand Down Expand Up @@ -205,4 +212,18 @@ export abstract class LanguageServerClient {
};
await this.ls!.sendNotification(DidChangeConfigurationNotification.type, params);
}

async update(context: vscode.ExtensionContext): Promise<void> {
const lspDir = storage.uri(storage.Location.LSP, context).fsPath;
const versionFile = path.join(lspDir, "version");
if (!fs.existsSync(versionFile)) return; // Either we use binaries from PATH or something is broken.

const currentVersion = fs.readFileSync(versionFile, { encoding: "utf-8" });
if (currentVersion == this.referenceVersion) return;

vscode.window.showInformationMessage(`Updating language server to ${this.referenceVersion}`);
this.dispose();
await serverToClass(this.server).download(this.server, "2.4.4", context);
this.restart();
}
}
4 changes: 2 additions & 2 deletions src/lsp/swift-mesonlsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class SwiftMesonLspLanguageClient extends LanguageServerClient {
};
}

constructor(languageServerPath: vscode.Uri, context: vscode.ExtensionContext) {
super("Swift-MesonLSP", languageServerPath, context);
constructor(languageServerPath: vscode.Uri, context: vscode.ExtensionContext, referenceVersion: string) {
super("Swift-MesonLSP", languageServerPath, context, referenceVersion);
}

static override artifact(): { url: string; hash: string } | null {
Expand Down

0 comments on commit 43e2354

Please sign in to comment.