Skip to content

Commit

Permalink
Add Zig to PATH in integrated terminal
Browse files Browse the repository at this point in the history
The VS Code version has been bumped to `1.80.0` which is when the `description` field became available.

closes #171
fixes #159 (except that you are not being asked about it)
  • Loading branch information
Techatrix committed Apr 2, 2024
1 parent 0098c1d commit 6757c96
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
"url": "https://github.com/ziglang/vscode-zig"
},
"engines": {
"vscode": "^1.68.0"
"vscode": "^1.80.0"
},
"categories": [
"Programming Languages"
],
"activationEvents": [
"onLanguage:zig",
"workspaceContains:*/build.zig",
"workspaceContains:*/build.zig.zon"
],
Expand Down Expand Up @@ -409,7 +408,7 @@
"@types/lodash-es": "^4.17.12",
"@types/mocha": "^2.2.48",
"@types/node": "^18.0.0",
"@types/vscode": "^1.68.0",
"@types/vscode": "^1.80.0",
"@types/which": "^2.0.1",
"@vscode/vsce": "^2.24.0",
"esbuild": "^0.12.1",
Expand Down
35 changes: 29 additions & 6 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import childProcess from "child_process";
import crypto from "crypto";
import fs from "fs";
import path from "path";

import axios from "axios";
import mkdirp from "mkdirp";
import semver from "semver";
import vscode from "vscode";
import which from "which";

import { getHostZigName, getVersion, isWindows, shouldCheckUpdate } from "./zigUtil";
import { getHostZigName, getVersion, getZigPath, isWindows, shouldCheckUpdate } from "./zigUtil";
import { install as installZLS } from "./zls";

const DOWNLOAD_INDEX = "https://ziglang.org/download/index.json";
Expand Down Expand Up @@ -120,6 +121,10 @@ async function install(context: vscode.ExtensionContext, version: ZigVersion) {

const configuration = vscode.workspace.getConfiguration("zig");
await configuration.update("path", zigPath, true);

void vscode.window.showInformationMessage(
`Zig has been installed successfully. Relaunch your integrated terminal to make it available.`,
);
},
);
}
Expand Down Expand Up @@ -212,6 +217,19 @@ async function getUpdatedVersion(context: vscode.ExtensionContext): Promise<ZigV
return null;
}

function updateZigEnvironmentVariableCollection(context: vscode.ExtensionContext) {
try {
const zigPath = getZigPath();
const envValue = path.delimiter + path.dirname(zigPath);
// Calling `append` means that zig from a user-defined PATH value will take precedence.
// The added value may have already been added by the user but since we
// append, it doesn't have any observable.
context.environmentVariableCollection.append("PATH", envValue);
} catch {
context.environmentVariableCollection.delete("PATH");
}
}

export async function setupZig(context: vscode.ExtensionContext) {
vscode.commands.registerCommand("zig.install", async () => {
await selectVersionAndInstall(context);
Expand All @@ -222,6 +240,14 @@ export async function setupZig(context: vscode.ExtensionContext) {
await checkUpdate(context);
});

context.environmentVariableCollection.description = "Add Zig to PATH";
updateZigEnvironmentVariableCollection(context);
vscode.workspace.onDidChangeConfiguration((change) => {
if (change.affectsConfiguration("zig.path")) {
updateZigEnvironmentVariableCollection(context);
}
}, context.subscriptions);

const configuration = vscode.workspace.getConfiguration("zig");
if (!configuration.get<boolean>("initialSetupDone")) {
await configuration.update("initialSetupDone", await initialSetup(context), true);
Expand All @@ -247,11 +273,8 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>
switch (zigResponse) {
case "Install":
await selectVersionAndInstall(context);
const path = zigConfig.get<string>("path");
if (!path) return false;
void vscode.window.showInformationMessage(
`Zig was installed at '${path}', add it to PATH to use it from the terminal`,
);
const zigPath = zigConfig.get<string>("path");
if (!zigPath) return false;
break;
case "Specify path":
const uris = await vscode.window.showOpenDialog({
Expand Down

0 comments on commit 6757c96

Please sign in to comment.