Skip to content

Commit

Permalink
move cli usage in setup onto internal commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Mar 9, 2023
1 parent 83d27bc commit 51e849b
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 175 deletions.
4 changes: 3 additions & 1 deletion extension/src/cli/dvc/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export const autoRegisteredCommands = {
DATA_STATUS: 'dataStatus',
EXP_SHOW: 'expShow',
PLOTS_DIFF: 'plotsDiff',
STAGE_LIST: 'listStages'
ROOT: 'root',
STAGE_LIST: 'listStages',
VERSION: 'version'
} as const

export class DvcReader extends DvcCli {
Expand Down
3 changes: 2 additions & 1 deletion extension/src/cli/git/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getOptions } from './options'
import { typeCheckCommands } from '..'

export const autoRegisteredCommands = {
GIT_INIT: 'gitInit',
GIT_PUSH_BRANCH: 'pushBranch',
GIT_RESET_WORKSPACE: 'resetWorkspace',
GIT_STAGE_ALL: 'stageAll',
Expand All @@ -17,7 +18,7 @@ export class GitExecutor extends GitCli {
this
)

public init(cwd: string) {
public gitInit(cwd: string) {
const options = getOptions(cwd, Command.INITIALIZE)

return this.executeProcess(options)
Expand Down
1 change: 1 addition & 0 deletions extension/src/cli/git/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const autoRegisteredCommands = {
GIT_GET_REMOTE_URL: 'getRemoteUrl',
GIT_GET_REPOSITORY_ROOT: 'getGitRepositoryRoot',
GIT_HAS_CHANGES: 'hasChanges',
GIT_HAS_NO_COMMITS: 'hasNoCommits',
GIT_LIST_UNTRACKED: 'listUntracked'
} as const

Expand Down
2 changes: 2 additions & 0 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { LanguageClient } from './languageClient'
import { collectRunningExperimentPids } from './experiments/processExecution/collect'
import { registerPatchCommand } from './patch'
import { DvcViewer } from './cli/dvc/viewer'
import { registerSetupCommands } from './setup/register'
export class Extension extends Disposable {
protected readonly internalCommands: InternalCommands

Expand Down Expand Up @@ -215,6 +216,7 @@ export class Extension extends Disposable {
this.connect
)
registerPlotsCommands(this.plots, this.internalCommands, this.setup)
registerSetupCommands(this.setup, this.internalCommands, config)
this.internalCommands.registerExternalCommand(
RegisteredCommands.EXPERIMENT_AND_PLOTS_SHOW,
async (context: VsCodeContext) => {
Expand Down
185 changes: 71 additions & 114 deletions extension/src/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Event, EventEmitter, ViewColumn, commands } from 'vscode'
import { Event, EventEmitter, ViewColumn } from 'vscode'
import { Disposable, Disposer } from '@hediet/std/disposable'
import isEmpty from 'lodash.isempty'
import { SetupData as TSetupData } from './webview/contract'
import { WebviewMessages } from './webview/messages'
import { findPythonBinForInstall } from './autoInstall'
import { run, runWithRecheck, runWorkspace } from './runner'
import { pickFocusedProjects } from './quickPick'
import { BaseWebview } from '../webview'
import { ViewKey } from '../webview/constants'
import { BaseRepository } from '../webview/repository'
Expand All @@ -30,8 +29,8 @@ import { ContextKey, setContextValue } from '../vscode/context'
import { DvcExecutor } from '../cli/dvc/executor'
import { GitReader } from '../cli/git/reader'
import { GitExecutor } from '../cli/git/executor'
import { RegisteredCliCommands, RegisteredCommands } from '../commands/external'
import { InternalCommands } from '../commands/internal'
import { RegisteredCommands } from '../commands/external'
import { AvailableCommands, InternalCommands } from '../commands/internal'
import { Status } from '../status'
import { WorkspaceExperiments } from '../experiments/workspace'
import { DvcRunner } from '../cli/dvc/runner'
Expand Down Expand Up @@ -59,11 +58,8 @@ export class Setup
private dvcRoots: string[] = []

private readonly config: Config
private readonly dvcReader: DvcReader
private readonly dvcExecutor: DvcExecutor
private readonly gitReader: GitReader
private readonly gitExecutor: GitExecutor
private readonly status: Status
private readonly internalCommands: InternalCommands

private readonly webviewMessages: WebviewMessages
private readonly showExperiments: () => void
Expand Down Expand Up @@ -100,19 +96,17 @@ export class Setup
super(GLOBAL_WEBVIEW_DVCROOT, webviewIcon)

this.config = config
this.dvcExecutor = dvcExecutor
this.dvcReader = dvcReader
this.gitExecutor = gitExecutor
this.gitReader = gitReader

this.internalCommands = internalCommands

this.status = this.dispose.track(
new Status(
this.config,
this.dvcExecutor,
this.dvcReader,
dvcExecutor,
dvcReader,
dvcRunner,
this.gitExecutor,
this.gitReader
gitExecutor,
gitReader
)
)

Expand All @@ -134,8 +128,6 @@ export class Setup
void experiments.showWebview(this.dvcRoots[0], ViewColumn.Active)
}

this.registerCommands(internalCommands)

this.getHasData = () => experiments.getHasData()
const onDidChangeHasData = experiments.columnsChanged.event
this.dispose.track(
Expand All @@ -161,7 +153,11 @@ export class Setup
public async getCliVersion(cwd: string, tryGlobalCli?: true) {
await this.config.isReady()
try {
return await this.dvcReader.version(cwd, tryGlobalCli)
return await this.internalCommands.executeCommand(
AvailableCommands.VERSION,
cwd,
tryGlobalCli as unknown as string
)
} catch {}
}

Expand Down Expand Up @@ -257,21 +253,40 @@ export class Setup
})
}

private createWebviewMessageHandler() {
const webviewMessages = new WebviewMessages(
() => this.getWebview(),
() => this.initializeDvc(),
() => this.initializeGit()
)
this.dispose.track(
this.onDidReceivedWebviewMessage(message =>
webviewMessages.handleMessageFromWebview(message)
public async setupWorkspace() {
const stopWatch = new StopWatch()
try {
const previousCliPath = this.config.getCliPath()
const previousPythonPath = this.config.getPythonBinPath()

const completed = await this.runWorkspace()
sendTelemetryEvent(
RegisteredCommands.EXTENSION_SETUP_WORKSPACE,
{ completed },
{
duration: stopWatch.getElapsedTime()
}
)
)
return webviewMessages

const executionDetailsUnchanged =
this.config.getCliPath() === previousPythonPath &&
this.config.getPythonBinPath() === previousCliPath

if (completed && !this.cliAccessible && executionDetailsUnchanged) {
this.workspaceChanged.fire()
}

return completed
} catch (error: unknown) {
return sendTelemetryEventAndThrow(
RegisteredCommands.EXTENSION_SETUP_WORKSPACE,
error as Error,
stopWatch.getElapsedTime()
)
}
}

private async findWorkspaceDvcRoots(): Promise<string[]> {
public async findWorkspaceDvcRoots(): Promise<string[]> {
let dvcRoots: Set<string> = new Set()

for (const workspaceFolder of getWorkspaceFolders()) {
Expand All @@ -284,7 +299,10 @@ export class Setup
await this.config.isReady()
const absoluteRoot = await findAbsoluteDvcRootPath(
workspaceFolder,
this.dvcReader.root(workspaceFolder)
this.internalCommands.executeCommand(
AvailableCommands.ROOT,
workspaceFolder
)
)
if (absoluteRoot) {
dvcRoots.add(absoluteRoot)
Expand All @@ -297,6 +315,19 @@ export class Setup
return [...dvcRoots]
}

private createWebviewMessageHandler() {
const webviewMessages = new WebviewMessages(
() => this.getWebview(),
() => this.initializeGit()
)
this.dispose.track(
this.onDidReceivedWebviewMessage(message =>
webviewMessages.handleMessageFromWebview(message)
)
)
return webviewMessages
}

private setCommandsAvailability(available: boolean) {
void setContextValue(ContextKey.COMMANDS_AVAILABLE, available)
}
Expand All @@ -310,13 +341,6 @@ export class Setup
}
}

private async initializeDvc() {
const root = getFirstWorkspaceFolder()
if (root) {
await this.dvcExecutor.init(root)
}
}

private async canGitInitialize(needsGitInit: boolean) {
if (!needsGitInit) {
return false
Expand All @@ -341,7 +365,10 @@ export class Setup
}

try {
return !(await this.gitReader.getGitRepositoryRoot(cwd))
return !(await this.internalCommands.executeCommand(
AvailableCommands.GIT_GET_REPOSITORY_ROOT,
cwd
))
} catch {
return true
}
Expand All @@ -350,7 +377,7 @@ export class Setup
private initializeGit() {
const cwd = getFirstWorkspaceFolder()
if (cwd) {
void this.gitExecutor.init(cwd)
void this.internalCommands.executeCommand(AvailableCommands.GIT_INIT, cwd)
}
}

Expand All @@ -363,80 +390,10 @@ export class Setup
if (!cwd) {
return true
}
return this.gitReader.hasNoCommits(cwd)
}

private registerCommands(internalCommands: InternalCommands) {
internalCommands.registerExternalCliCommand(
RegisteredCliCommands.INIT,
() => this.initializeDvc()
)

internalCommands.registerExternalCommand(
RegisteredCommands.EXTENSION_CHECK_CLI_COMPATIBLE,
() => run(this)
)

this.dispose.track(
commands.registerCommand(
RegisteredCommands.EXTENSION_SETUP_WORKSPACE,
() => this.setupWorkspace()
)
return this.internalCommands.executeCommand<boolean>(
AvailableCommands.GIT_HAS_NO_COMMITS,
cwd
)

internalCommands.registerExternalCommand(
RegisteredCommands.SETUP_SHOW,
async () => {
await this.showSetup()
}
)

internalCommands.registerExternalCommand(
RegisteredCommands.SELECT_FOCUSED_PROJECTS,
async () => {
const dvcRoots = await this.findWorkspaceDvcRoots()
const focusedProjects = await pickFocusedProjects(
dvcRoots,
this.dvcRoots
)
if (focusedProjects) {
this.config.setFocusedProjectsOption(focusedProjects)
}
}
)
}

private async setupWorkspace() {
const stopWatch = new StopWatch()
try {
const previousCliPath = this.config.getCliPath()
const previousPythonPath = this.config.getPythonBinPath()

const completed = await this.runWorkspace()
sendTelemetryEvent(
RegisteredCommands.EXTENSION_SETUP_WORKSPACE,
{ completed },
{
duration: stopWatch.getElapsedTime()
}
)

const executionDetailsUnchanged =
this.config.getCliPath() === previousPythonPath &&
this.config.getPythonBinPath() === previousCliPath

if (completed && !this.cliAccessible && executionDetailsUnchanged) {
this.workspaceChanged.fire()
}

return completed
} catch (error: unknown) {
return sendTelemetryEventAndThrow(
RegisteredCommands.EXTENSION_SETUP_WORKSPACE,
error as Error,
stopWatch.getElapsedTime()
)
}
}

private runWorkspace() {
Expand Down
56 changes: 56 additions & 0 deletions extension/src/setup/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { commands } from 'vscode'
import { Setup } from '.'
import { run } from './runner'
import { pickFocusedProjects } from './quickPick'
import { AvailableCommands, InternalCommands } from '../commands/internal'
import { Config } from '../config'
import { RegisteredCliCommands, RegisteredCommands } from '../commands/external'
import { getFirstWorkspaceFolder } from '../vscode/workspaceFolders'

export const registerSetupCommands = (
setup: Setup,
internalCommands: InternalCommands,
config: Config
) => {
internalCommands.registerExternalCliCommand(
RegisteredCliCommands.INIT,
async () => {
const root = getFirstWorkspaceFolder()
if (root) {
await internalCommands.executeCommand(AvailableCommands.INIT, root)
}
}
)

internalCommands.registerExternalCommand(
RegisteredCommands.EXTENSION_CHECK_CLI_COMPATIBLE,
() => run(setup)
)

setup.dispose.track(
commands.registerCommand(RegisteredCommands.EXTENSION_SETUP_WORKSPACE, () =>
setup.setupWorkspace()
)
)

internalCommands.registerExternalCommand(
RegisteredCommands.SETUP_SHOW,
async () => {
await setup.showSetup()
}
)

internalCommands.registerExternalCommand(
RegisteredCommands.SELECT_FOCUSED_PROJECTS,
async () => {
const dvcRoots = await setup.findWorkspaceDvcRoots()
const focusedProjects = await pickFocusedProjects(
dvcRoots,
setup.getRoots()
)
if (focusedProjects) {
config.setFocusedProjectsOption(focusedProjects)
}
}
)
}
Loading

0 comments on commit 51e849b

Please sign in to comment.