Skip to content

Commit 1508186

Browse files
authored
Add Setup Studio module (#4943)
1 parent 2aca646 commit 1508186

File tree

4 files changed

+155
-114
lines changed

4 files changed

+155
-114
lines changed

extension/src/setup/index.ts

Lines changed: 14 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { WebviewMessages } from './webview/messages'
1616
import { validateTokenInput } from './inputBox'
1717
import { findPythonBinForInstall } from './autoInstall'
1818
import { run, runWithRecheck, runWorkspace } from './runner'
19-
import { isStudioAccessToken } from './token'
19+
import { Studio } from './studio'
2020
import {
2121
PYTHON_EXTENSION_ACTION,
2222
pickFocusedProjects,
@@ -51,13 +51,7 @@ import { createFileSystemWatcher } from '../fileSystem/watcher'
5151
import { EventName } from '../telemetry/constants'
5252
import { WorkspaceScale } from '../telemetry/collect'
5353
import { gitPath } from '../cli/git/constants'
54-
import {
55-
Flag,
56-
ConfigKey,
57-
DOT_DVC,
58-
Args,
59-
SubCommand
60-
} from '../cli/dvc/constants'
54+
import { DOT_DVC, Args, SubCommand } from '../cli/dvc/constants'
6155
import { GLOBAL_WEBVIEW_DVCROOT } from '../webview/factory'
6256
import { getValidInput } from '../vscode/inputBox'
6357
import { Title } from '../vscode/title'
@@ -87,6 +81,7 @@ export class Setup
8781
private readonly config: Config
8882
private readonly status: Status
8983
private readonly internalCommands: InternalCommands
84+
private readonly studio: Studio
9085

9186
private readonly webviewMessages: WebviewMessages
9287
private readonly getHasData: () => boolean | undefined
@@ -103,9 +98,6 @@ export class Setup
10398
new EventEmitter()
10499
)
105100

106-
private readonly studioConnectionChanged: EventEmitter<void> =
107-
this.dispose.track(new EventEmitter())
108-
109101
private readonly onDidChangeWorkspace: Event<void> =
110102
this.workspaceChanged.event
111103

@@ -117,10 +109,6 @@ export class Setup
117109

118110
private dotFolderWatcher?: Disposer
119111

120-
private studioAccessToken: string | undefined = undefined
121-
private studioIsConnected = false
122-
private shareLiveToStudio: boolean | undefined = undefined
123-
124112
private focusedSection: SetupSection | undefined = undefined
125113

126114
constructor(
@@ -155,11 +143,13 @@ export class Setup
155143
}
156144

157145
this.collectWorkspaceScale = collectWorkspaceScale
158-
this.onDidChangeStudioConnection = this.studioConnectionChanged.event
159146

160147
this.setCommandsAvailability(false)
161148
this.setProjectAvailability()
162149

150+
this.studio = new Studio(internalCommands, () => this.getCwd())
151+
this.onDidChangeStudioConnection = this.studio.onDidChangeStudioConnection
152+
163153
this.webviewMessages = this.createWebviewMessageHandler()
164154

165155
void this.sendDataToWebview()
@@ -311,34 +301,12 @@ export class Setup
311301
}
312302
}
313303

314-
public async removeStudioAccessToken() {
304+
public removeStudioAccessToken() {
315305
if (!this.getCliCompatible()) {
316306
return
317307
}
318308

319-
if (this.dvcRoots.length !== 1) {
320-
const cwd = getFirstWorkspaceFolder()
321-
if (!cwd) {
322-
return
323-
}
324-
return await this.accessConfig(
325-
cwd,
326-
Flag.GLOBAL,
327-
Flag.UNSET,
328-
ConfigKey.STUDIO_TOKEN
329-
)
330-
}
331-
332-
const cwd = this.dvcRoots[0]
333-
334-
await this.accessConfig(cwd, Flag.LOCAL, Flag.UNSET, ConfigKey.STUDIO_TOKEN)
335-
336-
return await this.accessConfig(
337-
cwd,
338-
Flag.GLOBAL,
339-
Flag.UNSET,
340-
ConfigKey.STUDIO_TOKEN
341-
)
309+
return this.studio.removeStudioAccessToken(this.dvcRoots)
342310
}
343311

344312
public async saveStudioAccessToken() {
@@ -357,12 +325,12 @@ export class Setup
357325
return
358326
}
359327

360-
await this.accessConfig(cwd, Flag.GLOBAL, ConfigKey.STUDIO_TOKEN, token)
328+
await this.studio.saveStudioAccessTokenInConfig(cwd, token)
361329
return this.updateStudioAndSend()
362330
}
363331

364332
public getStudioAccessToken() {
365-
return this.studioAccessToken
333+
return this.studio.getStudioAccessToken()
366334
}
367335

368336
public sendInitialWebviewData() {
@@ -438,14 +406,14 @@ export class Setup
438406
isPythonEnvironmentGlobal,
439407
isPythonExtensionInstalled: this.config.isPythonExtensionInstalled(),
440408
isPythonExtensionUsed,
441-
isStudioConnected: this.studioIsConnected,
409+
isStudioConnected: this.studio.getStudioIsConnected(),
442410
needsGitCommit,
443411
needsGitInitialized,
444412
projectInitialized,
445413
pythonBinPath: getBinDisplayText(pythonBinPath),
446414
remoteList,
447415
sectionCollapsed: collectSectionCollapsed(this.focusedSection),
448-
shareLiveToStudio: !!this.shareLiveToStudio
416+
shareLiveToStudio: !!this.studio.getShareLiveToStudio()
449417
})
450418
this.focusedSection = undefined
451419
}
@@ -454,7 +422,7 @@ export class Setup
454422
const webviewMessages = new WebviewMessages(
455423
() => this.getWebview(),
456424
() => this.initializeGit(),
457-
(offline: boolean) => this.updateStudioOffline(offline),
425+
(offline: boolean) => this.studio.updateStudioOffline(offline),
458426
() => this.isPythonExtensionUsed(),
459427
() => this.updatePythonEnvironment()
460428
)
@@ -707,18 +675,10 @@ export class Setup
707675
}
708676

709677
private async updateStudioAndSend() {
710-
await this.updateIsStudioConnected()
678+
await this.studio.updateIsStudioConnected()
711679
return this.sendDataToWebview()
712680
}
713681

714-
private async updateIsStudioConnected() {
715-
await this.setStudioValues()
716-
const storedToken = this.getStudioAccessToken()
717-
const isConnected = isStudioAccessToken(storedToken)
718-
this.studioIsConnected = isConnected
719-
return setContextValue(ContextKey.STUDIO_CONNECTED, isConnected)
720-
}
721-
722682
private watchDvcConfigs() {
723683
const createWatcher = (watchedPath: string) =>
724684
createFileSystemWatcher(
@@ -760,51 +720,6 @@ export class Setup
760720
])
761721
}
762722

763-
private async setStudioValues() {
764-
const cwd = this.getCwd()
765-
766-
const previousStudioAccessToken = this.studioAccessToken
767-
768-
if (!cwd) {
769-
this.studioAccessToken = undefined
770-
this.shareLiveToStudio = undefined
771-
772-
if (previousStudioAccessToken) {
773-
this.studioConnectionChanged.fire()
774-
}
775-
return
776-
}
777-
778-
const [studioAccessToken, shareLiveToStudio] = await Promise.all([
779-
this.accessConfig(cwd, ConfigKey.STUDIO_TOKEN),
780-
(await this.accessConfig(cwd, ConfigKey.STUDIO_OFFLINE)) !== 'true'
781-
])
782-
783-
this.studioAccessToken = studioAccessToken
784-
this.shareLiveToStudio = shareLiveToStudio
785-
786-
if (previousStudioAccessToken !== this.studioAccessToken) {
787-
this.studioConnectionChanged.fire()
788-
}
789-
}
790-
791-
private async updateStudioOffline(shareLive: boolean) {
792-
const offline = !shareLive
793-
794-
const cwd = this.getCwd()
795-
796-
if (!cwd) {
797-
return
798-
}
799-
800-
await this.accessConfig(
801-
cwd,
802-
Flag.GLOBAL,
803-
ConfigKey.STUDIO_OFFLINE,
804-
String(offline)
805-
)
806-
}
807-
808723
private getCwd() {
809724
if (!this.getCliCompatible()) {
810725
return
@@ -814,14 +729,6 @@ export class Setup
814729
: getFirstWorkspaceFolder()
815730
}
816731

817-
private accessConfig(cwd: string, ...args: Args) {
818-
return this.internalCommands.executeCommand(
819-
AvailableCommands.CONFIG,
820-
cwd,
821-
...args
822-
)
823-
}
824-
825732
private accessRemote(cwd: string, ...args: Args) {
826733
return this.internalCommands.executeCommand(
827734
AvailableCommands.REMOTE,

extension/src/setup/inputBox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isStudioAccessToken } from './token'
1+
import { isStudioAccessToken } from './studio'
22

33
export const validateTokenInput = (input: string | undefined) => {
44
if (!isStudioAccessToken(input)) {

extension/src/setup/studio.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { Event, EventEmitter } from 'vscode'
2+
import { AvailableCommands, InternalCommands } from '../commands/internal'
3+
import { getFirstWorkspaceFolder } from '../vscode/workspaceFolders'
4+
import { Args, ConfigKey, Flag } from '../cli/dvc/constants'
5+
import { ContextKey, setContextValue } from '../vscode/context'
6+
import { Disposable } from '../class/dispose'
7+
8+
export const isStudioAccessToken = (text?: string): boolean => {
9+
if (!text) {
10+
return false
11+
}
12+
return text.startsWith('isat_') && text.length >= 53
13+
}
14+
15+
export class Studio extends Disposable {
16+
public readonly onDidChangeStudioConnection: Event<void>
17+
private readonly studioConnectionChanged: EventEmitter<void> =
18+
this.dispose.track(new EventEmitter())
19+
20+
private readonly getCwd: () => string | undefined
21+
private readonly internalCommands: InternalCommands
22+
private studioAccessToken: string | undefined = undefined
23+
private studioIsConnected = false
24+
private shareLiveToStudio: boolean | undefined = undefined
25+
26+
constructor(
27+
internalCommands: InternalCommands,
28+
getCwd: () => string | undefined
29+
) {
30+
super()
31+
32+
this.internalCommands = internalCommands
33+
this.getCwd = getCwd
34+
this.onDidChangeStudioConnection = this.studioConnectionChanged.event
35+
}
36+
37+
public getStudioAccessToken() {
38+
return this.studioAccessToken
39+
}
40+
41+
public getStudioIsConnected() {
42+
return this.studioIsConnected
43+
}
44+
45+
public getShareLiveToStudio() {
46+
return this.shareLiveToStudio
47+
}
48+
49+
public async removeStudioAccessToken(dvcRoots: string[]) {
50+
if (dvcRoots.length !== 1) {
51+
const cwd = getFirstWorkspaceFolder()
52+
if (!cwd) {
53+
return
54+
}
55+
56+
return await this.accessConfig(
57+
cwd,
58+
Flag.GLOBAL,
59+
Flag.UNSET,
60+
ConfigKey.STUDIO_TOKEN
61+
)
62+
}
63+
64+
const cwd = dvcRoots[0]
65+
66+
await this.accessConfig(cwd, Flag.LOCAL, Flag.UNSET, ConfigKey.STUDIO_TOKEN)
67+
68+
return await this.accessConfig(
69+
cwd,
70+
Flag.GLOBAL,
71+
Flag.UNSET,
72+
ConfigKey.STUDIO_TOKEN
73+
)
74+
}
75+
76+
public saveStudioAccessTokenInConfig(cwd: string, token: string) {
77+
return this.accessConfig(cwd, Flag.GLOBAL, ConfigKey.STUDIO_TOKEN, token)
78+
}
79+
80+
public async updateIsStudioConnected() {
81+
await this.setStudioValues()
82+
const storedToken = this.getStudioAccessToken()
83+
const isConnected = isStudioAccessToken(storedToken)
84+
this.studioIsConnected = isConnected
85+
return setContextValue(ContextKey.STUDIO_CONNECTED, isConnected)
86+
}
87+
88+
public async updateStudioOffline(shareLive: boolean) {
89+
const offline = !shareLive
90+
91+
const cwd = this.getCwd()
92+
93+
if (!cwd) {
94+
return
95+
}
96+
97+
await this.accessConfig(
98+
cwd,
99+
Flag.GLOBAL,
100+
ConfigKey.STUDIO_OFFLINE,
101+
String(offline)
102+
)
103+
}
104+
105+
private async setStudioValues() {
106+
const cwd = this.getCwd()
107+
108+
const previousStudioAccessToken = this.studioAccessToken
109+
110+
if (!cwd) {
111+
this.studioAccessToken = undefined
112+
this.shareLiveToStudio = undefined
113+
114+
if (previousStudioAccessToken) {
115+
this.studioConnectionChanged.fire()
116+
}
117+
return
118+
}
119+
120+
const [studioAccessToken, shareLiveToStudio] = await Promise.all([
121+
this.accessConfig(cwd, ConfigKey.STUDIO_TOKEN),
122+
(await this.accessConfig(cwd, ConfigKey.STUDIO_OFFLINE)) !== 'true'
123+
])
124+
125+
this.studioAccessToken = studioAccessToken
126+
this.shareLiveToStudio = shareLiveToStudio
127+
128+
if (previousStudioAccessToken !== this.studioAccessToken) {
129+
this.studioConnectionChanged.fire()
130+
}
131+
}
132+
133+
private accessConfig(cwd: string, ...args: Args) {
134+
return this.internalCommands.executeCommand(
135+
AvailableCommands.CONFIG,
136+
cwd,
137+
...args
138+
)
139+
}
140+
}

extension/src/setup/token.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)