11'use strict' ;
2- import { ExtensionContext , workspace } from 'vscode' ;
2+ import { Objects } from '../../system' ;
3+ import { Event , EventEmitter , ExtensionContext , workspace } from 'vscode' ;
34import { BitbucketService } from './bitbucket' ;
45import { BitbucketServerService } from './bitbucket-server' ;
56import { CustomRemoteType , IConfig , IRemotesConfig } from '../../configuration' ;
@@ -9,71 +10,39 @@ import { GitLabService } from './gitlab';
910import { Logger } from '../../logger' ;
1011import { RemoteProvider } from './provider' ;
1112import { VisualStudioService } from './visualStudio' ;
12- import { Objects } from '../../system' ;
1313
1414export { RemoteProvider } ;
1515
16- const UrlRegex = / ^ (?: g i t : \/ \/ ( .* ?) \/ | h t t p s : \/ \/ ( .* ?) \/ | h t t p : \/ \/ ( .* ?) \/ | g i t @ ( .* ) : | s s h : \/ \/ (?: .* @ ) ? ( .* ?) (?: : .* ?) ? \/ ) ( .* ) $ / ;
17-
18- function getCustomProvider ( type : CustomRemoteType ) {
19- switch ( type ) {
20- case CustomRemoteType . Bitbucket : return ( domain : string , path : string ) => new BitbucketService ( domain , path , true ) ;
21- case CustomRemoteType . BitbucketServer : return ( domain : string , path : string ) => new BitbucketServerService ( domain , path , true ) ;
22- case CustomRemoteType . GitHub : return ( domain : string , path : string ) => new GitHubService ( domain , path , true ) ;
23- case CustomRemoteType . GitLab : return ( domain : string , path : string ) => new GitLabService ( domain , path , true ) ;
24- }
25- return undefined ;
26- }
27-
2816const defaultProviderMap = new Map < string , ( domain : string , path : string ) => RemoteProvider > ( [
2917 [ 'bitbucket.org' , ( domain : string , path : string ) => new BitbucketService ( domain , path ) ] ,
3018 [ 'github.com' , ( domain : string , path : string ) => new GitHubService ( domain , path ) ] ,
3119 [ 'gitlab.com' , ( domain : string , path : string ) => new GitLabService ( domain , path ) ] ,
3220 [ 'visualstudio.com' , ( domain : string , path : string ) => new VisualStudioService ( domain , path ) ]
3321] ) ;
3422
35- let providerMap : Map < string , ( domain : string , path : string ) => RemoteProvider > ;
36- let remotesCfg : IRemotesConfig [ ] ;
37-
38- function onConfigurationChanged ( ) {
39- const cfg = workspace . getConfiguration ( ) . get < IConfig > ( ExtensionKey ) ;
40- if ( cfg === undefined ) return ;
41-
42- if ( ! Objects . areEquivalent ( cfg . remotes , remotesCfg ) ) {
43- providerMap = new Map ( defaultProviderMap ) ;
23+ export class RemoteProviderFactory {
4424
45- remotesCfg = cfg . remotes ;
46- if ( remotesCfg != null && remotesCfg . length > 0 ) {
47- for ( const svc of remotesCfg ) {
48- const provider = getCustomProvider ( svc . type ) ;
49- if ( provider === undefined ) continue ;
25+ private static _providerMap : Map < string , ( domain : string , path : string ) => RemoteProvider > ;
26+ private static _remotesCfg : IRemotesConfig [ ] ;
5027
51- providerMap . set ( svc . domain . toLowerCase ( ) , provider ) ;
52- }
53- }
28+ private static _onDidChange = new EventEmitter < void > ( ) ;
29+ public static get onDidChange ( ) : Event < void > {
30+ return this . _onDidChange . event ;
5431 }
55- }
56-
57- export class RemoteProviderFactory {
5832
5933 static configure ( context : ExtensionContext ) {
60- context . subscriptions . push ( workspace . onDidChangeConfiguration ( onConfigurationChanged ) ) ;
61- onConfigurationChanged ( ) ;
34+ context . subscriptions . push ( workspace . onDidChangeConfiguration ( ( ) => this . onConfigurationChanged ( ) ) ) ;
35+ this . onConfigurationChanged ( true ) ;
6236 }
6337
64- static getRemoteProvider ( url : string ) : RemoteProvider | undefined {
38+ static getRemoteProvider ( domain : string , path : string ) : RemoteProvider | undefined {
6539 try {
66- const match = UrlRegex . exec ( url ) ;
67- if ( match == null ) return undefined ;
68-
69- const domain = match [ 1 ] || match [ 2 ] || match [ 3 ] || match [ 4 ] || match [ 5 ] ;
70- const path = match [ 6 ] . replace ( / \. g i t \/ ? $ / , '' ) ;
71-
72- const key = domain . toLowerCase ( ) . endsWith ( 'visualstudio.com' )
73- ? 'visualstudio.com'
74- : domain ;
40+ let key = domain . toLowerCase ( ) ;
41+ if ( key . endsWith ( 'visualstudio.com' ) ) {
42+ key = 'visualstudio.com' ;
43+ }
7544
76- const creator = providerMap . get ( key . toLowerCase ( ) ) ;
45+ const creator = this . _providerMap . get ( key ) ;
7746 if ( creator === undefined ) return undefined ;
7847
7948 return creator ( domain , path ) ;
@@ -83,4 +52,37 @@ export class RemoteProviderFactory {
8352 return undefined ;
8453 }
8554 }
55+
56+ private static onConfigurationChanged ( silent : boolean = false ) {
57+ const cfg = workspace . getConfiguration ( ) . get < IConfig > ( ExtensionKey ) ;
58+ if ( cfg === undefined ) return ;
59+
60+ if ( ! Objects . areEquivalent ( cfg . remotes , this . _remotesCfg ) ) {
61+ this . _providerMap = new Map ( defaultProviderMap ) ;
62+
63+ this . _remotesCfg = cfg . remotes ;
64+ if ( this . _remotesCfg != null && this . _remotesCfg . length > 0 ) {
65+ for ( const svc of this . _remotesCfg ) {
66+ const provider = this . getCustomProvider ( svc . type ) ;
67+ if ( provider === undefined ) continue ;
68+
69+ this . _providerMap . set ( svc . domain . toLowerCase ( ) , provider ) ;
70+ }
71+
72+ if ( ! silent ) {
73+ this . _onDidChange . fire ( ) ;
74+ }
75+ }
76+ }
77+ }
78+
79+ private static getCustomProvider ( type : CustomRemoteType ) {
80+ switch ( type ) {
81+ case CustomRemoteType . Bitbucket : return ( domain : string , path : string ) => new BitbucketService ( domain , path , true ) ;
82+ case CustomRemoteType . BitbucketServer : return ( domain : string , path : string ) => new BitbucketServerService ( domain , path , true ) ;
83+ case CustomRemoteType . GitHub : return ( domain : string , path : string ) => new GitHubService ( domain , path , true ) ;
84+ case CustomRemoteType . GitLab : return ( domain : string , path : string ) => new GitLabService ( domain , path , true ) ;
85+ }
86+ return undefined ;
87+ }
8688}
0 commit comments