Skip to content

Commit

Permalink
rename & fix global configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Apr 16, 2024
1 parent 59e57a6 commit 68bf6b9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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'
import { KNOWN_FLOW_COMMANDS } from '../../flow-cli/cli-versions-provider'

// Command to check flow-cli
const COMPATIBLE_FLOW_CLI_VERSIONS = '>=1.6.0'
Expand Down
24 changes: 14 additions & 10 deletions extension/src/flow-cli/cli-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { StateCache } from '../utils/state-cache'
import * as vscode from 'vscode'
import { Settings } from '../settings/settings'
import { isEqual } from 'lodash'
import { CliBinary, BinaryVersionsProvider, KNOWN_FLOW_COMMANDS } from './binary-versions-provider'
import { CliBinary, CliVersionsProvider, KNOWN_FLOW_COMMANDS } from './cli-versions-provider'

export class CliProvider {
#selectedBinaryName: BehaviorSubject<string>
#currentBinary$: StateCache<CliBinary | null>
#binaryVersionsProvider: BinaryVersionsProvider
#cliVersionsProvider: CliVersionsProvider
#settings: Settings

constructor (settings: Settings) {
const initialBinaryPath = settings.getSettings().flowCommand

this.#settings = settings
this.#binaryVersionsProvider = new BinaryVersionsProvider([initialBinaryPath])
this.#cliVersionsProvider = new CliVersionsProvider([initialBinaryPath])
this.#selectedBinaryName = new BehaviorSubject<string>(initialBinaryPath)
this.#currentBinary$ = new StateCache(async () => {
const name: string = this.#selectedBinaryName.getValue()
const versionCache = this.#binaryVersionsProvider.get(name)
const versionCache = this.#cliVersionsProvider.get(name)
if (versionCache == null) return null
return await versionCache.getValue()
})
Expand All @@ -43,10 +43,10 @@ export class CliProvider {
// Subscribe to changes in the selected binary to update the caches
this.#selectedBinaryName.pipe(distinctUntilChanged(), startWith(null), pairwise()).subscribe(([prev, curr]) => {
// Remove the previous binary from the cache
if (prev != null) this.#binaryVersionsProvider.remove(prev)
if (prev != null) this.#cliVersionsProvider.remove(prev)

// Add the current binary to the cache
if (curr != null) this.#binaryVersionsProvider.add(curr)
if (curr != null) this.#cliVersionsProvider.add(curr)

// Invalidate the current binary cache
this.#currentBinary$.invalidate()
Expand All @@ -58,23 +58,27 @@ export class CliProvider {
}

async setCurrentBinary (name: string): Promise<void> {
await this.#settings.updateSettings({ flowCommand: name })
if (vscode.workspace.workspaceFolders == null) {
await this.#settings.updateSettings({ flowCommand: name }, vscode.ConfigurationTarget.Global)
} else {
await this.#settings.updateSettings({ flowCommand: name })
}
}

get currentBinary$ (): Observable<CliBinary | null> {
return this.#currentBinary$.pipe(distinctUntilChanged(isEqual))
}

async getBinaryVersions (): Promise<CliBinary[]> {
return await this.#binaryVersionsProvider.getVersions()
return await this.#cliVersionsProvider.getVersions()
}

get binaryVersions$ (): Observable<CliBinary[]> {
return this.#binaryVersionsProvider.versions$.pipe(distinctUntilChanged(isEqual))
return this.#cliVersionsProvider.versions$.pipe(distinctUntilChanged(isEqual))
}

// Refresh all cached binary versions
refresh (): void {
this.#binaryVersionsProvider.refresh()
this.#cliVersionsProvider.refresh()
}
}
2 changes: 1 addition & 1 deletion extension/src/flow-cli/cli-selection-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode'
import { zip } from 'rxjs'
import { CliProvider } from './cli-provider'
import { SemVer } from 'semver'
import { CliBinary } from './binary-versions-provider'
import { CliBinary } from './cli-versions-provider'

const CHANGE_CLI_BINARY = 'cadence.changeFlowCliBinary'
const CADENCE_V1_CLI_REGEX = /-cadence-v1.0.0/g
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface FlowVersionOutput {
version: string
}

export class BinaryVersionsProvider {
export class CliVersionsProvider {
#rootCache: StateCache<CliBinary[]>
#caches: { [key: string]: StateCache<CliBinary | null> } = {}

Expand Down
2 changes: 1 addition & 1 deletion extension/src/server/language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +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'
import { KNOWN_FLOW_COMMANDS } from '../flow-cli/cli-versions-provider'

// Identities for commands handled by the Language server
const RELOAD_CONFIGURATION = 'cadence.server.flow.reloadConfiguration'
Expand Down
2 changes: 1 addition & 1 deletion extension/test/integration/1 - language-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BehaviorSubject, Subject } from 'rxjs'
import { State } from 'vscode-languageclient'
import * as sinon from 'sinon'
import { SemVer } from 'semver'
import { CliBinary } from '../../src/flow-cli/binary-versions-provider'
import { CliBinary } from '../../src/flow-cli/cli-versions-provider'

suite('Language Server & Emulator Integration', () => {
let LS: LanguageServerAPI
Expand Down
2 changes: 1 addition & 1 deletion extension/test/unit/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert'
import { parseFlowCliVersion } from '../../src/flow-cli/binary-versions-provider'
import { parseFlowCliVersion } from '../../src/flow-cli/cli-versions-provider'
import { execDefault } from '../../src/utils/shell/exec'
import { ASSERT_EQUAL } from '../globals'
import * as semver from 'semver'
Expand Down

0 comments on commit 68bf6b9

Please sign in to comment.