Skip to content

Commit

Permalink
feat: click status bar icon to select nargo install
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Feb 16, 2024
1 parent 01425f7 commit f95fb4c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 29 deletions.
15 changes: 13 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ import {
window,
ProgressLocation,
} from 'vscode';
import os from 'os';

import { languageId } from './constants';
import Client from './client';
import findNargo from './find-nargo';
import { lspClients, editorLineDecorationManager, getNoirStatusBarItem, handleClientStartError } from './noir';
import findNargo, { findNargoBinaries } from './find-nargo';
import { lspClients, editorLineDecorationManager } from './noir';
import { getNoirStatusBarItem, handleClientStartError } from './noir';

const activeCommands: Map<string, Disposable> = new Map();

Expand Down Expand Up @@ -168,6 +170,15 @@ function registerCommands(uri: Uri) {
});
commands$.push(hideProfileInformationCommand$);

const selectNargoPathCommand$ = commands.registerCommand('nargo.config.path.select', async (..._args) => {
const homeDir = os.homedir();
const foundNargoBinaries = findNargoBinaries(homeDir);
const result = await window.showQuickPick(foundNargoBinaries, { placeHolder: 'Select the Nargo binary to use' });
const config = workspace.getConfiguration('noir', uri);
config.update('nargoPath', result);
});
commands$.push(selectNargoPathCommand$);

activeCommands.set(file, Disposable.from(...commands$));
}

Expand Down
70 changes: 43 additions & 27 deletions src/find-nargo.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
import os from 'os';
import os, { homedir } from 'os';

Check warning on line 1 in src/find-nargo.ts

View workflow job for this annotation

GitHub Actions / eslint

'homedir' is defined but never used. Allowed unused vars must match /^_/u
import path from 'path';
import fs from 'fs';
import which from 'which';
import { NargoNotFoundError } from './noir';
import { MarkdownString } from 'vscode';

// List of possible nargo binaries to find on Path
// We prioritize 'aztec-nargo' as more specialised case
const nargoBinaries = ['aztec-nargo', 'nargo'];
// We prioritize 'nargo' as the more standard version.
const NARGO_BINARIES = ['nargo', 'aztec-nargo'];
// List of possible default installations in users folder
const nargoInstallLocationPostix = ['.aztec/bin/aztec-nargo', '.nargo/bin/nargo'];
const NARGO_INSTALL_LOCATION_POSTFIXES = ['.nargo/bin/nargo', '.aztec/bin/aztec-nargo'];

export default function findNargo() {
for (const bin of nargoBinaries) {
function absoluteInstallLocationPaths(homeDir: string): string[] {
return NARGO_INSTALL_LOCATION_POSTFIXES.map((postfix) => path.join(homeDir, postfix)).filter((filePath) =>
fs.existsSync(filePath),
);
}

export function findNargoBinaries(homeDir: string): string[] {
// Note that JS sets maintain insertion order.
const nargoBinaryPaths: Set<string> = new Set();

for (const bin of NARGO_BINARIES) {
try {
const nargo = which.sync(bin);
const path = which.sync(bin);
// If it didn't throw, we found a nargo binary
return nargo;
nargoBinaryPaths.add(path);
} catch (err) {
// Not found
}
}

const homeDir = os.homedir();
// So far we have not found installations on path
// Let's check default installation locations
for (const postfix of nargoInstallLocationPostix) {
const filePath = path.join(homeDir, postfix);
if (fs.existsSync(filePath)) {
return filePath;
}
for (const filePath of absoluteInstallLocationPaths(homeDir)) {
nargoBinaryPaths.add(filePath);
}

const message = new MarkdownString();
message.appendText(`Could not locate any of\n`);
for (const nargoBinary of nargoBinaries) {
message.appendMarkdown(`\`${nargoBinary}\``);
message.appendText(`\n`);
}
return [...nargoBinaryPaths];
}

message.appendText(`on \`$PATH\`, or one of default installation locations\n`);
for (const postfix of nargoInstallLocationPostix) {
const filePath = path.join(homeDir, postfix);
message.appendMarkdown(`\`${filePath}\``);
message.appendText(`\n`);
}
export default function findNargo() {
const homeDir = os.homedir();
const nargoBinaryPaths = findNargoBinaries(homeDir);

if (nargoBinaryPaths.length > 0) {
return nargoBinaryPaths[0];
} else {
const message = new MarkdownString();
message.appendText(`Could not locate any of\n`);
for (const nargoBinary of NARGO_BINARIES) {
message.appendMarkdown(`\`${nargoBinary}\``);
message.appendText(`\n`);
}

message.appendText(`on \`$PATH\`, or one of default installation locations\n`);
for (const postfix of NARGO_INSTALL_LOCATION_POSTFIXES) {
const filePath = path.join(homeDir, postfix);
message.appendMarkdown(`\`${filePath}\``);
message.appendText(`\n`);
}

throw new NargoNotFoundError(message);
throw new NargoNotFoundError(message);
}
}
1 change: 1 addition & 0 deletions src/noir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function getNoirStatusBarItem() {
// we will show/update it depending on file user is working with
noirStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Right, 100);
noirStatusBarItem.text = 'Nargo';
noirStatusBarItem.command = 'nargo.config.path.select';

return noirStatusBarItem;
}
Expand Down

0 comments on commit f95fb4c

Please sign in to comment.