diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2025-08-06 17:51:51 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-06 17:51:51 +0200 |
| commit | 0167f6c8f58bba8a014d53e3015f741b0b150fe9 (patch) | |
| tree | 7795ea5203175d8ca3cefdaf31742ecf4f68a105 | |
| parent | 32f9ea0d405900a94a21bea0f182ffdb6028c637 (diff) | |
| parent | 09edf3ce23455544c509f9c1bd27d132a4788501 (diff) | |
Merge pull request #826 from BradLewis/fix/check-for-updates-async
Check for updates in the vscode extension asynchronously
| -rw-r--r-- | editors/vscode/package.json | 4 | ||||
| -rw-r--r-- | editors/vscode/src/extension.ts | 76 |
2 files changed, 52 insertions, 28 deletions
diff --git a/editors/vscode/package.json b/editors/vscode/package.json index 6e32cc3..e6c57d8 100644 --- a/editors/vscode/package.json +++ b/editors/vscode/package.json @@ -7,7 +7,7 @@ "type": "git", "url": "git://github.com/DanielGavin/ols.git" }, - "version": "0.1.37", + "version": "0.1.39", "engines": { "vscode": "^1.96.0" }, @@ -187,4 +187,4 @@ "editor.semanticHighlighting.enabled": true } } -}
\ No newline at end of file +} diff --git a/editors/vscode/src/extension.ts b/editors/vscode/src/extension.ts index 17fdcbd..8b05455 100644 --- a/editors/vscode/src/extension.ts +++ b/editors/vscode/src/extension.ts @@ -61,6 +61,8 @@ export async function activate(context: vscode.ExtensionContext) { throw new Error(message); }); + checkForUpdates(config, state, false) + const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; @@ -301,15 +303,7 @@ function serverPath(config: Config): string | null { return config.serverPath; } -async function getServer(config: Config, state: PersistentState): Promise<string | undefined> { - const explicitPath = serverPath(config); - if (explicitPath) { - if (explicitPath.startsWith("~/")) { - return os.homedir() + explicitPath.slice("~".length); - } - return explicitPath; - }; - +function getPlatform(): string { const platforms: { [key: string]: string } = { "x64 win32": "x86_64-pc-windows-msvc", "x64 linux": "x86_64-unknown-linux-gnu", @@ -318,7 +312,39 @@ async function getServer(config: Config, state: PersistentState): Promise<string }; let platform = platforms[`${process.arch} ${process.platform}`]; + return platform +} + +function getDestFolder(config: Config, releaseId: number | undefined): string { + return path.join(config.globalStorageUri.fsPath, (releaseId ?? 0).toString()); +} + +function getExt(): string { + const platform = getPlatform() + const isWindows = platform.indexOf("-windows-") !== -1; + const ext = isWindows ? ".exe" : ""; + + return ext +} + + +function getExecutable(config: Config, releaseId: number | undefined): string { + const platform = getPlatform() + const ext = getExt() + return path.join(getDestFolder(config, releaseId), `ols-${platform}${ext}`); +} + +async function getServer(config: Config, state: PersistentState): Promise<string | undefined> { + const explicitPath = serverPath(config); + if (explicitPath) { + if (explicitPath.startsWith("~/")) { + return os.homedir() + explicitPath.slice("~".length); + } + return explicitPath; + }; + + const platform = getPlatform() if (platform === undefined) { await vscode.window.showErrorMessage( "Unfortunately we don't ship binaries for your platform yet. " + @@ -333,35 +359,30 @@ async function getServer(config: Config, state: PersistentState): Promise<string } */ - const isWindows = platform.indexOf("-windows-") !== -1; - const ext = isWindows ? ".exe" : ""; - // use a separate folder for each release on windows because we can't overwrite files while they are still in use - const getDestFolder = (releaseId: number | undefined) => path.join(config.globalStorageUri.fsPath, (releaseId ?? 0).toString()); - const getExecutable = (releaseId: number | undefined) => path.join(getDestFolder(releaseId), `ols-${platform}${ext}`); - const zipFolder = config.globalStorageUri.fsPath; - const destExecutable = getExecutable(state.releaseId); + const destExecutable = getExecutable(config, state.releaseId); const exists = await fs.stat(destExecutable).then(() => true, () => false); if (!exists) { await state.updateReleaseId(0); + await checkForUpdates(config, state, !exists) } /* Temp: right now it doesn't check for versions, since ols has no versioning right now */ + return destExecutable +} - if (exists && state.lastCheck !== undefined && state.lastCheck + (3 * 60 * 60 * 1000) > Date.now()) { - return destExecutable; - } - - const release = await downloadWithRetryDialog(state, !exists, async () => { +async function checkForUpdates(config: Config, state: PersistentState, required: boolean): Promise<void> { + const platform = getPlatform() + const release = await downloadWithRetryDialog(state, required, async () => { return await fetchRelease("nightly", state.githubToken, config.httpProxy); }); if (release === undefined || release.id === state.releaseId) { await state.updateLastCheck(Date.now()); - return destExecutable; + return; } const userResponse = await vscode.window.showInformationMessage( @@ -370,12 +391,13 @@ async function getServer(config: Config, state: PersistentState): Promise<string ); if (userResponse !== "Update") { - return destExecutable; + return; } const artifact = release.assets.find(artifact => artifact.name === `ols-${platform}.zip`); assert(!!artifact, `Bad release: ${JSON.stringify(release)}`); + const zipFolder = config.globalStorageUri.fsPath; const destZip = path.join(zipFolder, `ols-${platform}.zip`); await downloadWithRetryDialog(state, true, async () => { @@ -389,8 +411,9 @@ async function getServer(config: Config, state: PersistentState): Promise<string var zip = new AdmZip(destZip); - const latestDestFolder = getDestFolder(release.id); - const latestExecutable = getExecutable(release.id); + + const latestDestFolder = getDestFolder(config, release.id); + const latestExecutable = getExecutable(config, release.id); if (!await fs.stat(latestDestFolder).then(() => true, () => false)) { await fs.mkdir(latestDestFolder) @@ -398,6 +421,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string zip.extractAllTo(latestDestFolder, true); + const ext = getExt() if (ext !== ".exe") { fs.chmod(latestExecutable, 0o755); } @@ -407,7 +431,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string await state.updateLastCheck(Date.now()); await vscode.commands.executeCommand("workbench.action.reloadWindow"); - return latestExecutable; + return; } async function downloadWithRetryDialog<T>(state: PersistentState, required: boolean, downloadFunc: () => Promise<T>): Promise<T> { |