Renames hosting to remote

This commit is contained in:
Eric Amodio
2017-03-24 03:37:22 -04:00
parent 4d1cfd6413
commit 0feaf285cd
16 changed files with 74 additions and 69 deletions

View File

@@ -0,0 +1,32 @@
'use strict';
import { RemoteProvider } from './provider';
import { GitHubService } from './github';
import { Logger } from '../../logger';
export { RemoteProvider };
const providerMap = new Map<string, (domain: string, path: string) => RemoteProvider>([
['github.com', (domain: string, path: string) => new GitHubService(domain, path)]
]);
const UrlRegex = /^(?:git:\/\/(.*?)\/|https:\/\/(.*?)\/|http:\/\/(.*?)\/|git@(.*):\/\/|ssh:\/\/git@(.*?)\/)(.*)$/;
export class RemoteProviderFactory {
static getRemoteProvider(url: string): RemoteProvider {
try {
const match = UrlRegex.exec(url);
const domain = match[1] || match[2] || match[3] || match[4] || match[5];
const path = match[6].replace(/\.git/, '');
const creator = providerMap.get(domain);
if (!creator) return undefined;
return creator(domain, path);
}
catch (ex) {
Logger.error(ex);
return undefined;
}
}
}

26
src/git/remotes/github.ts Normal file
View File

@@ -0,0 +1,26 @@
'use strict';
import { RemoteProvider } from './provider';
export class GitHubService extends RemoteProvider {
constructor(public domain: string, public path: string) {
super(domain, path);
}
get name() {
return 'GitHub';
}
protected getUrlForBranch(branch: string): string {
return `${this.baseUrl}/tree/${branch}`;
}
protected getUrlForCommit(sha: string): string {
return `${this.baseUrl}/commit/${sha}`;
}
protected getUrlForFile(fileName: string, sha?: string): string {
if (sha) return `${this.baseUrl}/blob/${sha}/${fileName}`;
return `${this.baseUrl}?path=${fileName}`;
}
}

View File

@@ -0,0 +1,48 @@
'use strict';
import { commands, Uri } from 'vscode';
import { BuiltInCommands } from '../../constants';
export type RemoteProviderOpenType = 'branch' | 'commit' | 'file';
export abstract class RemoteProvider {
constructor(public domain: string, public path: string) { }
abstract get name(): string;
protected get baseUrl() {
return `https://${this.domain}/${this.path}`;
}
protected abstract getUrlForBranch(branch: string): string;
protected abstract getUrlForCommit(sha: string): string;
protected abstract getUrlForFile(fileName: string, sha?: string): string;
private async _openUrl(url: string): Promise<{}> {
return url && commands.executeCommand(BuiltInCommands.Open, Uri.parse(url));
}
open(type: 'branch', branch: string): Promise<{}>;
open(type: 'commit', sha: string): Promise<{}>;
open(type: 'file', fileName: string, sha?: string): Promise<{}>;
open(type: RemoteProviderOpenType, ...args: string[]): Promise<{}>;
open(type: RemoteProviderOpenType, branchOrShaOrFileName: string, sha?: string): Promise<{}> {
switch (type) {
case 'branch': return this.openBranch(branchOrShaOrFileName);
case 'commit': return this.openCommit(branchOrShaOrFileName);
case 'file': return this.openFile(branchOrShaOrFileName, sha);
}
}
openBranch(branch: string) {
return this._openUrl(this.getUrlForBranch(branch));
}
openCommit(sha: string) {
return this._openUrl(this.getUrlForCommit(sha));
}
openFile(fileName: string, sha?: string) {
return this._openUrl(this.getUrlForFile(fileName, sha));
}
}