4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
6
import * as vscode from 'vscode' ;
7
+ import { Disposable } from './common/dispose' ;
8
+ import Log from './common/logger' ;
7
9
import TelemetryReporter from './telemetryReporter' ;
8
10
9
11
interface ConfigurationSyncStore {
@@ -14,72 +16,101 @@ interface ConfigurationSyncStore {
14
16
authenticationProviders : Record < string , { scopes : string [ ] } > ;
15
17
}
16
18
17
- function getGitpodSyncProviderConfig ( serviceUrl : string ) : ConfigurationSyncStore {
18
- const syncStoreURL = ` ${ new URL ( serviceUrl ) . toString ( ) . replace ( / \/ $ / , '' ) } /code-sync` ;
19
- return {
20
- url : syncStoreURL ,
21
- stableUrl : syncStoreURL ,
22
- insidersUrl : syncStoreURL ,
23
- canSwitch : false ,
24
- authenticationProviders : {
25
- gitpod : {
26
- scopes : [
27
- 'function:accessCodeSyncStorage' ,
28
- 'function:getLoggedInUser' ,
29
- 'resource:default'
30
- ]
19
+ export default class SettingsSync extends Disposable {
20
+ constructor ( private readonly logger : Log , private readonly telemetry : TelemetryReporter ) {
21
+ super ( ) ;
22
+
23
+ this . _register ( vscode . workspace . onDidChangeConfiguration ( async e => {
24
+ if ( e . affectsConfiguration ( 'gitpod.host' ) || e . affectsConfiguration ( 'configurationSync.store' ) ) {
25
+ const addedSyncProvider = await this . updateSyncContext ( ) ;
26
+ if ( ! addedSyncProvider ) {
27
+ const action = 'Settings Sync: Enable Sign In with Gitpod' ;
28
+ const result = await vscode . window . showInformationMessage ( 'Gitpod Settings Sync configuration invalidated, Settings Sync is disabled.' , action ) ;
29
+ if ( result === action ) {
30
+ vscode . commands . executeCommand ( 'gitpod.syncProvider.add' ) ;
31
+ }
32
+ }
31
33
}
32
- }
33
- } ;
34
- }
34
+ } ) ) ;
35
+ this . _register ( vscode . commands . registerCommand ( 'gitpod.syncProvider.add' , ( ) => this . enableSettingsSync ( true ) ) ) ;
36
+ this . _register ( vscode . commands . registerCommand ( 'gitpod.syncProvider.remove' , ( ) => this . enableSettingsSync ( false ) ) ) ;
35
37
36
- /**
37
- * Updates the VS Code context to reflect whether the user added Gitpod as their Settings Sync provider.
38
- */
39
- export async function updateSyncContext ( ) : Promise < boolean > {
40
- const config = vscode . workspace . getConfiguration ( ) ;
41
- const syncProviderConfig = config . get ( 'configurationSync.store' ) ;
42
- const serviceUrl = config . get < string > ( 'gitpod.host' ) ! ;
43
- const gitpodSyncProviderConfig = getGitpodSyncProviderConfig ( serviceUrl ) ;
44
- const addedSyncProvider = ! ! syncProviderConfig && JSON . stringify ( syncProviderConfig ) === JSON . stringify ( gitpodSyncProviderConfig ) ;
45
- await vscode . commands . executeCommand ( 'setContext' , 'gitpod.addedSyncProvider' , addedSyncProvider ) ;
46
- return addedSyncProvider ;
47
- }
38
+ this . updateSyncContext ( ) ;
39
+ }
48
40
49
- /**
50
- * Adds an authentication provider as a possible provider for code sync.
51
- * It adds some key configuration to the user settings, so that the user can choose the Gitpod provider when deciding what to use with setting sync.
52
- * @param enabled - indicates whether to add or remove the configuration
53
- */
54
- export async function enableSettingsSync ( enabled : boolean , telemetry : TelemetryReporter ) : Promise < void > {
55
- let newSyncProviderConfig : ConfigurationSyncStore | undefined ;
56
- let newIgnoredSettingsConfig : string [ ] | undefined ;
57
- const config = vscode . workspace . getConfiguration ( ) ;
58
- const currentSyncProviderConfig : ConfigurationSyncStore | undefined = config . get ( 'configurationSync.store' ) ;
59
- const currentIgnoredSettingsConfig : string [ ] | undefined = config . get ( 'settingsSync.ignoredSettings' ) ;
60
- const serviceUrl = config . get < string > ( 'gitpod.host' ) ! ;
61
- const gitpodSyncProviderConfig = getGitpodSyncProviderConfig ( serviceUrl ) ;
62
- if ( enabled ) {
63
- if ( JSON . stringify ( currentSyncProviderConfig ) === JSON . stringify ( gitpodSyncProviderConfig ) ) {
64
- return ;
65
- }
66
- newSyncProviderConfig = gitpodSyncProviderConfig ;
67
- newIgnoredSettingsConfig = currentIgnoredSettingsConfig ?? [ ] ;
68
- if ( ! newIgnoredSettingsConfig . find ( s => s === 'configurationSync.store' ) ) {
69
- newIgnoredSettingsConfig . push ( 'configurationSync.store' ) ;
70
- }
71
- } else {
72
- if ( currentSyncProviderConfig === undefined ) {
73
- return ;
74
- }
75
- newSyncProviderConfig = undefined ;
76
- newIgnoredSettingsConfig = currentIgnoredSettingsConfig ?. filter ( s => s !== 'configurationSync.store' ) ;
41
+ /**
42
+ * Updates the VS Code context to reflect whether the user added Gitpod as their Settings Sync provider.
43
+ */
44
+ private async updateSyncContext ( ) : Promise < boolean > {
45
+ const config = vscode . workspace . getConfiguration ( ) ;
46
+ const syncProviderConfig = config . get ( 'configurationSync.store' ) ;
47
+ const serviceUrl = config . get < string > ( 'gitpod.host' ) ! ;
48
+ const gitpodSyncProviderConfig = this . getGitpodSyncProviderConfig ( serviceUrl ) ;
49
+ const addedSyncProvider = ! ! syncProviderConfig && JSON . stringify ( syncProviderConfig ) === JSON . stringify ( gitpodSyncProviderConfig ) ;
50
+ await vscode . commands . executeCommand ( 'setContext' , 'gitpod.addedSyncProvider' , addedSyncProvider ) ;
51
+ return addedSyncProvider ;
77
52
}
78
53
79
- await config . update ( 'settingsSync.ignoredSettings' , newIgnoredSettingsConfig , vscode . ConfigurationTarget . Global ) ;
80
- await config . update ( 'configurationSync.store' , newSyncProviderConfig , vscode . ConfigurationTarget . Global ) ;
54
+ /**
55
+ * Adds an authentication provider as a possible provider for code sync.
56
+ * It adds some key configuration to the user settings, so that the user can choose the Gitpod provider when deciding what to use with setting sync.
57
+ * @param enabled - indicates whether to add or remove the configuration
58
+ */
59
+ private async enableSettingsSync ( enabled : boolean ) : Promise < void > {
60
+ try {
61
+ let newSyncProviderConfig : ConfigurationSyncStore | undefined ;
62
+ let newIgnoredSettingsConfig : string [ ] | undefined ;
63
+ const config = vscode . workspace . getConfiguration ( ) ;
64
+ const currentSyncProviderConfig : ConfigurationSyncStore | undefined = config . get ( 'configurationSync.store' ) ;
65
+ const currentIgnoredSettingsConfig : string [ ] | undefined = config . get ( 'settingsSync.ignoredSettings' ) ;
66
+ const serviceUrl = config . get < string > ( 'gitpod.host' ) ! ;
67
+ const gitpodSyncProviderConfig = this . getGitpodSyncProviderConfig ( serviceUrl ) ;
68
+ if ( enabled ) {
69
+ if ( JSON . stringify ( currentSyncProviderConfig ) === JSON . stringify ( gitpodSyncProviderConfig ) ) {
70
+ return ;
71
+ }
72
+ newSyncProviderConfig = gitpodSyncProviderConfig ;
73
+ newIgnoredSettingsConfig = currentIgnoredSettingsConfig ?? [ ] ;
74
+ if ( ! newIgnoredSettingsConfig . find ( s => s === 'configurationSync.store' ) ) {
75
+ newIgnoredSettingsConfig . push ( 'configurationSync.store' ) ;
76
+ }
77
+ } else {
78
+ if ( currentSyncProviderConfig === undefined ) {
79
+ return ;
80
+ }
81
+ newSyncProviderConfig = undefined ;
82
+ newIgnoredSettingsConfig = currentIgnoredSettingsConfig ?. filter ( s => s !== 'configurationSync.store' ) ;
83
+ }
84
+
85
+ await config . update ( 'settingsSync.ignoredSettings' , newIgnoredSettingsConfig , vscode . ConfigurationTarget . Global ) ;
86
+ await config . update ( 'configurationSync.store' , newSyncProviderConfig , vscode . ConfigurationTarget . Global ) ;
81
87
82
- telemetry . sendTelemetryEvent ( 'gitpod_desktop_settings_sync' , { enabled : String ( enabled ) } ) ;
88
+ this . telemetry . sendTelemetryEvent ( 'gitpod_desktop_settings_sync' , { enabled : String ( enabled ) } ) ;
83
89
84
- vscode . window . showInformationMessage ( 'Quit VS Code for the new Settings Sync configuration to take effect.' ) ;
90
+ vscode . window . showInformationMessage ( 'Quit VS Code for the new Settings Sync configuration to take effect.' , { modal : true } ) ;
91
+ } catch ( e ) {
92
+ const outputMessage = `Error setting up Settings Sync with Gitpod: ${ e } ` ;
93
+ vscode . window . showErrorMessage ( outputMessage ) ;
94
+ this . logger . error ( outputMessage ) ;
95
+ }
96
+ }
97
+
98
+ private getGitpodSyncProviderConfig ( serviceUrl : string ) : ConfigurationSyncStore {
99
+ const syncStoreURL = `${ new URL ( serviceUrl ) . toString ( ) . replace ( / \/ $ / , '' ) } /code-sync` ;
100
+ return {
101
+ url : syncStoreURL ,
102
+ stableUrl : syncStoreURL ,
103
+ insidersUrl : syncStoreURL ,
104
+ canSwitch : false ,
105
+ authenticationProviders : {
106
+ gitpod : {
107
+ scopes : [
108
+ 'function:accessCodeSyncStorage' ,
109
+ 'function:getLoggedInUser' ,
110
+ 'resource:default'
111
+ ]
112
+ }
113
+ }
114
+ } ;
115
+ }
85
116
}
0 commit comments