Skip to content

Commit

Permalink
Naming fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Apr 15, 2024
1 parent 20b07d9 commit 35d5002
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -97,10 +98,10 @@ export class InstallFlowCLI extends Installer {
}
}

async checkVersion (vsn?: semver.SemVer): Promise<boolean> {
async checkVersion (vsn: semver.SemVer): Promise<boolean> {
// 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, {
Expand Down Expand Up @@ -128,7 +129,8 @@ export class InstallFlowCLI extends Installer {
async verifyInstall (): Promise<boolean> {
// 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
Expand Down
15 changes: 9 additions & 6 deletions extension/src/flow-cli/binary-versions-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
})

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions extension/src/flow-cli/cli-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
Expand Down Expand Up @@ -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.`)
}
})
Expand Down
10 changes: 5 additions & 5 deletions extension/src/flow-cli/cli-selection-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class CliSelectionProvider {
}
})
} else if (selected instanceof AvailableBinaryItem) {
void this.#cliProvider.setCurrentBinary(selected.path)
void this.#cliProvider.setCurrentBinary(selected.command)
}
}))

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
}

Expand Down
5 changes: 3 additions & 2 deletions extension/src/server/language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions extension/test/integration/1 - language-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ suite('Language Server & Emulator Integration', () => {

// create a mock cli provider without invokign the constructor
cliBinary$ = new BehaviorSubject<CliBinary>({
path: 'flow',
command: 'flow',
version: new SemVer('1.0.0')
})
const mockCliProvider = {
Expand Down Expand Up @@ -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')
})

Expand Down

0 comments on commit 35d5002

Please sign in to comment.