Skip to content

Commit c4310c5

Browse files
feat: version update messages (#35)
1 parent 80ca50e commit c4310c5

File tree

13 files changed

+262
-842
lines changed

13 files changed

+262
-842
lines changed

package-lock.json

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

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "postgrestools",
33
"publisher": "Supabase",
44
"description": "Postgres Language Server right in your IDE.",
5-
"version": "1.1.7",
5+
"version": "1.2.0",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/supabase-community/postgrestools-vscode"
@@ -71,6 +71,12 @@
7171
"default": false,
7272
"scope": "resource"
7373
},
74+
"postgrestools.allowVersionChecks": {
75+
"type": "boolean",
76+
"description": "If set, the extension will check periodically whether a new version for PostgresTools is available and if so notify the user.",
77+
"default": true,
78+
"scope": "resource"
79+
},
7480
"postgrestools.configFile": {
7581
"type": "string",
7682
"description": "Path to the `postgrestools.jsonc` file. You don't need to set this if the file is on root level of your project.",

src/binary-finder-strategies.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CONSTANTS } from "./constants";
55
import { fileExists } from "./utils";
66
import { createRequire } from "node:module";
77
import { getConfig } from "./config";
8-
import { downloadPglt, getDownloadedVersion } from "./downloader";
8+
import { downloadPglt, getDownloadedBinary } from "./downloader";
99

1010
export interface BinaryFindStrategy {
1111
name: string;
@@ -280,14 +280,14 @@ export const downloadPgltStrategy: BinaryFindStrategy = {
280280
async find() {
281281
logger.debug(`Trying to find downloaded PostgresTools binary`);
282282

283-
const downloadedVersion = await getDownloadedVersion();
283+
const downloadedBinary = await getDownloadedBinary();
284284

285-
if (downloadedVersion) {
285+
if (downloadedBinary) {
286286
logger.info(
287-
`Using previously downloaded version ${downloadedVersion.version} at ${downloadedVersion.binPath.fsPath}`
287+
`Using previously downloaded version ${downloadedBinary.version} at ${downloadedBinary.binPath.fsPath}`
288288
);
289289

290-
return downloadedVersion.binPath;
290+
return downloadedBinary.binPath;
291291
}
292292

293293
const proceed =

src/binary-finder.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RelativePattern, Uri, workspace } from "vscode";
1+
import { Uri } from "vscode";
22
import {
33
BinaryFindStrategy,
44
downloadPgltStrategy,
@@ -10,41 +10,47 @@ import {
1010
import { logger } from "./logger";
1111

1212
type Strategy = {
13+
label: string;
1314
strategy: BinaryFindStrategy;
1415
onSuccess: (u: Uri) => void;
1516
condition?: (path?: Uri) => Promise<boolean>;
1617
};
1718

1819
const LOCAL_STRATEGIES: Strategy[] = [
1920
{
21+
label: "VSCode Settings",
2022
strategy: vsCodeSettingsStrategy,
2123
onSuccess: (uri) =>
2224
logger.debug(`Found Binary in VSCode Settings (postgrestools.bin)`, {
2325
path: uri.fsPath,
2426
}),
2527
},
2628
{
29+
label: "NPM node_modules",
2730
strategy: nodeModulesStrategy,
2831
onSuccess: (uri) =>
2932
logger.debug(`Found Binary in Node Modules`, {
3033
path: uri.fsPath,
3134
}),
3235
},
3336
{
37+
label: "Yarn Plug'n'Play node_modules",
3438
strategy: yarnPnpStrategy,
3539
onSuccess: (uri) =>
3640
logger.debug(`Found Binary in Yarn PnP`, {
3741
path: uri.fsPath,
3842
}),
3943
},
4044
{
45+
label: "PATH Environment Variable",
4146
strategy: pathEnvironmentVariableStrategy,
4247
onSuccess: (uri) =>
4348
logger.debug(`Found Binary in PATH Environment Variable`, {
4449
path: uri.fsPath,
4550
}),
4651
},
4752
{
53+
label: "Downloaded Binary",
4854
strategy: downloadPgltStrategy,
4955
onSuccess: (uri) =>
5056
logger.debug(`Found downloaded binary`, {
@@ -65,7 +71,7 @@ export class BinaryFinder {
6571
}
6672

6773
private static async attemptFind(strategies: Strategy[], path: Uri) {
68-
for (const { strategy, onSuccess, condition } of strategies) {
74+
for (const { strategy, onSuccess, condition, label } of strategies) {
6975
if (condition && !(await condition(path))) {
7076
continue;
7177
}
@@ -74,7 +80,7 @@ export class BinaryFinder {
7480
const binary = await strategy.find(path);
7581
if (binary) {
7682
onSuccess(binary);
77-
return { bin: binary };
83+
return { bin: binary, label };
7884
} else {
7985
logger.info(`Binary not found with strategy`, {
8086
strategy: strategy.name,

src/commands.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { window } from "vscode";
2-
import { downloadPglt, getDownloadedVersion } from "./downloader";
2+
import { downloadPglt, getDownloadedBinary } from "./downloader";
33
import { restart, start, stop } from "./lifecycle";
44
import { logger } from "./logger";
55
import { state } from "./state";
6-
import { clearGlobalBinaries, clearTemporaryBinaries } from "./utils";
6+
import {
7+
clearGlobalBinaries,
8+
clearTemporaryBinaries,
9+
getVersion,
10+
} from "./utils";
11+
import { Releases } from "./releases";
712

813
/**
914
* These commands are exposed to the user via the Command Palette.
@@ -37,20 +42,36 @@ export class UserFacingCommands {
3742
await stop();
3843
await clearTemporaryBinaries();
3944
await clearGlobalBinaries();
40-
await state.context.globalState.update("downloadedVersion", undefined);
45+
46+
await Releases.refresh();
47+
48+
await state.context.globalState.update("lastNotifiedOfUpdate", undefined);
49+
4150
state.activeSession = undefined;
4251
state.activeProject = undefined;
4352
logger.info("PostgresTools extension was reset");
53+
4454
await start();
4555
}
4656

4757
static async currentVersion() {
48-
const result = await getDownloadedVersion();
49-
if (!result) {
58+
const session = state.activeSession;
59+
60+
if (!session) {
61+
window.showInformationMessage("No PostgresTools version installed.");
62+
return;
63+
}
64+
65+
const version = await getVersion(session.bin);
66+
67+
if (!version) {
5068
window.showInformationMessage("No PostgresTools version installed.");
5169
} else {
5270
window.showInformationMessage(
53-
`Currently installed PostgresTools version is ${result.version}.`
71+
`Currently installed PostgresTools version is ${version}.`
72+
);
73+
window.showInformationMessage(
74+
`Using binary from "${session.binaryStrategyLabel}".`
5475
);
5576
}
5677
}

src/downloader.ts

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import {
88
import { logger } from "./logger";
99
import { state } from "./state";
1010
import { CONSTANTS } from "./constants";
11-
import { fileExists } from "./utils";
12-
import { getConfig } from "./config";
11+
import { fileExists, getVersion } from "./utils";
1312
import { chmodSync } from "fs";
14-
import { getAllReleases } from "./releases";
1513

1614
export async function downloadPglt(): Promise<Uri | null> {
1715
logger.debug(`Downloading PostgresTools`);
@@ -31,7 +29,7 @@ export async function downloadPglt(): Promise<Uri | null> {
3129
() => downloadPgltVersion(versionToDownload.label)
3230
);
3331

34-
const downloaded = await getDownloadedVersion();
32+
const downloaded = await getDownloadedBinary();
3533

3634
return downloaded?.binPath ?? null;
3735
}
@@ -67,47 +65,41 @@ async function downloadPgltVersion(version: string): Promise<void> {
6765
const successMsg = `Downloaded PostgresTools ${version} to ${binPath.fsPath}`;
6866
logger.info(successMsg);
6967
window.showInformationMessage(successMsg);
70-
state.context.globalState.update("downloadedVersion", version);
7168
} catch (error) {
7269
logger.error(`Failed to save downloaded binary`, { error });
7370
window.showErrorMessage(`Failed to save binary.\n\n${error}`);
7471
return;
7572
}
7673
}
7774

78-
export async function getDownloadedVersion(): Promise<{
75+
export async function getDownloadedBinary(): Promise<{
7976
version: string;
8077
binPath: Uri;
8178
} | null> {
8279
logger.debug(`Getting downloaded version`);
8380

84-
const version = state.context.globalState.get<string>("downloadedVersion");
85-
if (!version) {
86-
logger.debug(`No downloaded version stored in global state context.`);
87-
return null;
88-
}
81+
const bin = getInstalledBinaryPath();
8982

90-
const binPath = getInstalledBinaryPath();
83+
if (await fileExists(bin)) {
84+
const version = await getVersion(bin);
85+
if (!version) {
86+
throw new Error("Just verified file exists, but it doesn't anymore.");
87+
}
9188

92-
if (await fileExists(binPath)) {
9389
logger.debug(`Found downloaded version and binary`, {
94-
path: binPath.fsPath,
90+
path: bin.fsPath,
9591
version,
9692
});
9793

9894
return {
99-
binPath,
95+
binPath: bin,
10096
version,
10197
};
10298
}
10399

104-
logger.info(
105-
`Downloaded version found in global state context, but binary does not exist.`,
106-
{
107-
binPath,
108-
version,
109-
}
110-
);
100+
logger.info(`Downloaded binary does not exist.`, {
101+
binPath: bin,
102+
});
111103

112104
return null;
113105
}
@@ -117,23 +109,16 @@ async function promptVersionToDownload() {
117109

118110
const itemsPromise: Promise<QuickPickItem[]> = new Promise(
119111
async (resolve) => {
120-
const downloadedVersion = await getDownloadedVersion()
112+
const downloadedBinary = await getDownloadedBinary()
121113
.then((it) => it?.version)
122114
.catch(() => undefined);
123115

124-
logger.debug(`Retrieved downloaded version`, { downloadedVersion });
125-
126-
const withPrereleases =
127-
getConfig<boolean>("allowDownloadPrereleases") ?? false;
128-
129-
const availableVersions = await getAllReleases({
130-
withPrereleases,
131-
}).catch(() => []);
132-
133-
logger.debug(`Found ${availableVersions.length} downloadable versions`, {
134-
withPrereleases,
116+
logger.debug(`Retrieved downloaded version`, {
117+
downloadedVersion: downloadedBinary,
135118
});
136119

120+
const availableVersions = state.releases.all();
121+
137122
const items: QuickPickItem[] = availableVersions.map((release, index) => {
138123
const descriptions = [];
139124

@@ -145,13 +130,13 @@ async function promptVersionToDownload() {
145130
descriptions.push("prerelease");
146131
}
147132

133+
if (downloadedBinary === release.tag_name) {
134+
descriptions.push("(currently installed)");
135+
}
136+
148137
return {
149138
label: release.tag_name,
150139
description: descriptions.join(", "),
151-
detail:
152-
downloadedVersion === release.tag_name
153-
? "(currently installed)"
154-
: "",
155140
alwaysShow: index < 3,
156141
};
157142
});

src/lifecycle.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { logger } from "./logger";
2+
import { Releases } from "./releases";
23
import { createActiveSession, destroySession } from "./session";
34
import { state } from "./state";
45

@@ -37,6 +38,7 @@ export const restart = async () => {
3738

3839
const doStart = async () => {
3940
try {
41+
state.releases = await Releases.load();
4042
await createActiveSession();
4143
} catch (e: unknown) {
4244
if (e instanceof Error) {

0 commit comments

Comments
 (0)