From 35d50026401fba82f6f9d8983bdebd6cdaf66387 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Mon, 15 Apr 2024 15:26:25 -0700 Subject: [PATCH] Naming fixup --- .../installers/flow-cli-installer.ts | 8 +++++--- .../src/flow-cli/binary-versions-provider.ts | 15 +++++++++------ extension/src/flow-cli/cli-provider.ts | 4 ++-- extension/src/flow-cli/cli-selection-provider.ts | 10 +++++----- extension/src/server/language-server.ts | 5 +++-- .../test/integration/1 - language-server.test.ts | 4 ++-- 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/extension/src/dependency-installer/installers/flow-cli-installer.ts b/extension/src/dependency-installer/installers/flow-cli-installer.ts index e4a56dde..bededc68 100644 --- a/extension/src/dependency-installer/installers/flow-cli-installer.ts +++ b/extension/src/dependency-installer/installers/flow-cli-installer.ts @@ -6,6 +6,7 @@ import { Installer, InstallerConstructor, InstallerContext } from '../installer' import * as semver from 'semver' import fetch from 'node-fetch' import { HomebrewInstaller } from './homebrew-installer' +import { KNOWN_FLOW_COMMANDS } from '../../flow-cli/binary-versions-provider' // Command to check flow-cli const COMPATIBLE_FLOW_CLI_VERSIONS = '>=1.6.0' @@ -97,10 +98,10 @@ export class InstallFlowCLI extends Installer { } } - async checkVersion (vsn?: semver.SemVer): Promise { + async checkVersion (vsn: semver.SemVer): Promise { // Get user's version informaton this.#context.cliProvider.refresh() - const version = vsn ?? await this.#context.cliProvider.getBinaryVersions().then(x => x.find(y => y.path === 'flow')?.version) + const version = vsn if (version == null) return false if (!semver.satisfies(version, COMPATIBLE_FLOW_CLI_VERSIONS, { @@ -128,7 +129,8 @@ export class InstallFlowCLI extends Installer { async verifyInstall (): Promise { // Check if flow version is valid to verify install this.#context.cliProvider.refresh() - const version = await this.#context.cliProvider.getBinaryVersions().then(x => x.find(y => y.path === 'flow')?.version) + const installedVersions = await this.#context.cliProvider.getBinaryVersions() + const version = installedVersions.find(y => y.command === KNOWN_FLOW_COMMANDS.DEFAULT)?.version if (version == null) return false // Check flow-cli version number diff --git a/extension/src/flow-cli/binary-versions-provider.ts b/extension/src/flow-cli/binary-versions-provider.ts index 9ca6df8f..8e512f52 100644 --- a/extension/src/flow-cli/binary-versions-provider.ts +++ b/extension/src/flow-cli/binary-versions-provider.ts @@ -7,12 +7,15 @@ import { isEqual } from 'lodash' const CHECK_FLOW_CLI_CMD = (flowCommand: string): string => `${flowCommand} version --output=json` const CHECK_FLOW_CLI_CMD_NO_JSON = (flowCommand: string): string => `${flowCommand} version` -const KNOWN_BINS = ['flow', 'flow-c1'] +export enum KNOWN_FLOW_COMMANDS { + DEFAULT = 'flow', + CADENCE_V1 = 'flow-c1', +} const LEGACY_VERSION_REGEXP = /Version:\s*(v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(\s|$)/m export interface CliBinary { - path: string + command: string version: semver.SemVer } @@ -26,7 +29,7 @@ export class BinaryVersionsProvider { constructor (seedBinaries: string[] = []) { // Seed the caches with the known binaries - KNOWN_BINS.forEach((bin) => { + Object.values(KNOWN_FLOW_COMMANDS).forEach((bin) => { this.add(bin) }) @@ -60,7 +63,7 @@ export class BinaryVersionsProvider { remove (path: string): void { // Known binaries cannot be removed - if (this.#caches[path] == null || KNOWN_BINS.includes(path)) return + if (this.#caches[path] == null || (Object.values(KNOWN_FLOW_COMMANDS) as string[]).includes(path)) return // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete this.#caches[path] this.#rootCache?.invalidate() @@ -85,7 +88,7 @@ export class BinaryVersionsProvider { const version: semver.SemVer | null = semver.parse(versionInfo.version) if (version === null) return null - return { path: bin, version } + return { command: bin, version } } catch { // Fallback to old method if JSON is not supported/fails return await this.#fetchBinaryInformationOld(bin) @@ -114,7 +117,7 @@ export class BinaryVersionsProvider { const version: semver.SemVer | null = semver.parse(versionStr) if (version === null) return null - return { path: bin, version } + return { command: bin, version } } catch { return null } diff --git a/extension/src/flow-cli/cli-provider.ts b/extension/src/flow-cli/cli-provider.ts index 1a4ce76f..3926e5a5 100644 --- a/extension/src/flow-cli/cli-provider.ts +++ b/extension/src/flow-cli/cli-provider.ts @@ -3,7 +3,7 @@ import { StateCache } from '../utils/state-cache' import * as vscode from 'vscode' import { Settings } from '../settings/settings' import { isEqual } from 'lodash' -import { CliBinary, BinaryVersionsProvider } from './binary-versions-provider' +import { CliBinary, BinaryVersionsProvider, KNOWN_FLOW_COMMANDS } from './binary-versions-provider' export class CliProvider { #selectedBinaryName: BehaviorSubject @@ -31,7 +31,7 @@ export class CliProvider { // Display warning to user if binary doesn't exist (only if not using the default binary) this.currentBinary$.subscribe((binary) => { - if (binary === null && this.#selectedBinaryName.getValue() !== 'flow') { + if (binary === null && this.#selectedBinaryName.getValue() !== KNOWN_FLOW_COMMANDS.DEFAULT) { void vscode.window.showErrorMessage(`The configured Flow CLI binary "${this.#selectedBinaryName.getValue()}" does not exist. Please check your settings.`) } }) diff --git a/extension/src/flow-cli/cli-selection-provider.ts b/extension/src/flow-cli/cli-selection-provider.ts index eb2364ee..ffcb12a0 100644 --- a/extension/src/flow-cli/cli-selection-provider.ts +++ b/extension/src/flow-cli/cli-selection-provider.ts @@ -75,7 +75,7 @@ export class CliSelectionProvider { } }) } else if (selected instanceof AvailableBinaryItem) { - void this.#cliProvider.setCurrentBinary(selected.path) + void this.#cliProvider.setCurrentBinary(selected.command) } })) @@ -84,7 +84,7 @@ export class CliSelectionProvider { items.push(new CustomBinaryItem()) // Hoist the current binary to the top of the list - const currentBinaryIndex = items.findIndex(item => item instanceof AvailableBinaryItem && item.path === currentBinary?.path) + const currentBinaryIndex = items.findIndex(item => item instanceof AvailableBinaryItem && item.command === currentBinary?.command) if (currentBinaryIndex != null) { const currentBinaryItem = items[currentBinaryIndex] items.splice(currentBinaryIndex, 1) @@ -135,11 +135,11 @@ class AvailableBinaryItem implements vscode.QuickPickItem { } get description (): string { - return `(${this.#binary.path})` + return `(${this.#binary.command})` } - get path (): string { - return this.#binary.path + get command (): string { + return this.#binary.command } } diff --git a/extension/src/server/language-server.ts b/extension/src/server/language-server.ts index 5ec3b245..30f3afaa 100644 --- a/extension/src/server/language-server.ts +++ b/extension/src/server/language-server.ts @@ -7,6 +7,7 @@ import { BehaviorSubject, Subscription, filter, firstValueFrom, skip } from 'rxj import { envVars } from '../utils/shell/env-vars' import { FlowConfig } from './flow-config' import { CliProvider } from '../flow-cli/cli-provider' +import { KNOWN_FLOW_COMMANDS } from '../flow-cli/binary-versions-provider' // Identities for commands handled by the Language server const RELOAD_CONFIGURATION = 'cadence.server.flow.reloadConfiguration' @@ -75,12 +76,12 @@ export class LanguageServerAPI { const accessCheckMode: string = this.#settings.getSettings().accessCheckMode const configPath: string | null = this.#config.configPath - const binaryPath = (await this.#cliProvider.getCurrentBinary())?.path + const binaryPath = (await this.#cliProvider.getCurrentBinary())?.command if (binaryPath == null) { throw new Error('No flow binary found') } - if (binaryPath !== 'flow') { + if (binaryPath !== KNOWN_FLOW_COMMANDS.DEFAULT) { try { exec('killall dlv') // Required when running language server locally on mac } catch (err) { void err } diff --git a/extension/test/integration/1 - language-server.test.ts b/extension/test/integration/1 - language-server.test.ts index c1f51a86..befb0fd6 100644 --- a/extension/test/integration/1 - language-server.test.ts +++ b/extension/test/integration/1 - language-server.test.ts @@ -33,7 +33,7 @@ suite('Language Server & Emulator Integration', () => { // create a mock cli provider without invokign the constructor cliBinary$ = new BehaviorSubject({ - path: 'flow', + command: 'flow', version: new SemVer('1.0.0') }) const mockCliProvider = { @@ -63,7 +63,7 @@ suite('Language Server & Emulator Integration', () => { fileModified$.next() pathChanged$.next('foo') cliBinary$.next({ - path: 'flow', + command: 'flow', version: new SemVer('1.0.1') })