提交 78d61d55 编写于 作者: J Joao Moreno

git: use simpler versioning scheme

上级 f9982d29
......@@ -1197,7 +1197,6 @@
"file-type": "^7.2.0",
"iconv-lite": "0.4.19",
"jschardet": "^1.6.0",
"semver": "^5.5.1",
"vscode-extension-telemetry": "0.0.18",
"vscode-nls": "^3.2.4",
"which": "^1.3.0"
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { Model } from '../model';
import { API } from './git';
import * as semver from 'semver';
interface ApiCtor {
new(model: Model): API;
}
const versions: string[] = [];
const apis = new Map<string, ApiCtor>();
export function getAPI(model: Model, range: string): API {
if (!range) {
throw new Error(`Please provide a Git extension API version range. Available versions: [${versions.join(', ')}]`);
}
const version = semver.maxSatisfying(versions, range);
if (!version) {
throw new Error(`There's no available Git extension API for the given range: '${range}'. Available versions: [${versions.join(', ')}]`);
}
const api = apis.get(version)!;
return new api(model);
}
export function Api(version: string): Function {
return function (ctor: ApiCtor) {
if (apis.has(version)) {
throw new Error(`Git extension API version ${version} already registered.`);
}
versions.push(version);
apis.set(version, ctor);
};
}
export function deprecated(target: any, key: string, descriptor: any): void {
if (typeof descriptor.value !== 'function') {
throw new Error('not supported');
}
const fn = descriptor.value;
descriptor.value = function () {
console.warn(`Git extension API method '${key}' is deprecated.`);
return fn.apply(this, arguments);
};
}
\ No newline at end of file
......@@ -7,11 +7,9 @@
import { Model } from '../model';
import { Repository as BaseRepository } from '../repository';
import { InputBox, ExecResult, SpawnOptions, Git, API, Repository } from './git';
import { Api } from './api';
import { InputBox, Git, API, Repository } from './git';
import { Event, SourceControlInputBox, Uri } from 'vscode';
import { mapEvent } from '../util';
import * as cp from 'child_process';
class ApiInputBox implements InputBox {
set value(value: string) { this._inputBox.value = value; }
......@@ -35,17 +33,8 @@ export class ApiGit implements Git {
get path(): string { return this._model.git.path; }
constructor(private _model: Model) { }
exec(args: string[], options: SpawnOptions): Promise<ExecResult<string>> {
return this._model.git.exec2(args, options);
}
spawn(args: string[], options: SpawnOptions): cp.ChildProcess {
return this._model.git.spawn(args, options);
}
}
@Api('1.0.0')
export class ApiImpl implements API {
readonly git = new ApiGit(this._model);
......
......@@ -7,8 +7,19 @@
import { Model } from '../model';
import { GitExtension, Repository, API } from './git';
import { getAPI, deprecated } from './api';
import { ApiRepository } from './api1';
import { ApiRepository, ApiImpl } from './api1';
export function deprecated(target: any, key: string, descriptor: any): void {
if (typeof descriptor.value !== 'function') {
throw new Error('not supported');
}
const fn = descriptor.value;
descriptor.value = function () {
console.warn(`Git extension API method '${key}' is deprecated.`);
return fn.apply(this, arguments);
};
}
class NoModelGitExtension implements GitExtension {
......@@ -41,8 +52,12 @@ class GitExtensionImpl implements GitExtension {
return this._model.repositories.map(repository => new ApiRepository(repository));
}
getAPI(range: string): API {
return getAPI(this._model, range);
getAPI(version: number): API {
if (version !== 1) {
throw new Error(`No API version ${version} found.`);
}
return new ApiImpl(this._model);
}
}
......
......@@ -6,24 +6,8 @@
import { Uri, SourceControlInputBox, Event, CancellationToken } from 'vscode';
import * as cp from 'child_process';
export interface ExecResult<T extends string | Buffer> {
readonly exitCode: number;
readonly stdout: T;
readonly stderr: string;
}
export interface SpawnOptions extends cp.SpawnOptions {
readonly cwd: string;
readonly input?: string;
readonly encoding?: string;
readonly log?: boolean;
readonly cancellationToken?: CancellationToken;
}
export interface Git {
readonly path: string;
exec(args: string[], options: SpawnOptions): Promise<ExecResult<string>>;
spawn(args: string[], options: SpawnOptions): cp.ChildProcess;
}
export interface API {
......@@ -42,30 +26,13 @@ export interface Repository {
readonly inputBox: InputBox;
}
declare module GitExtension { }
export interface GitExtension {
/**
* Returns the latest available API compatible with the
* provided version range.
* Returns a specific API version.
*
* @param range Semver version range for API compatibility.
* @param version Version number.
* @returns API instance
*/
getAPI(range: string): API;
/**
* Returns the collection of active repositories.
*
* @deprecated Use `API.repositories` instead.
*/
getRepositories(): Promise<Repository[]>;
/**
* Returns the path to the current git executable.
*
* @deprecated Use `API.gitPath` instead.
*/
getGitPath(): Promise<string>;
getAPI(version: 1): API;
}
\ No newline at end of file
......@@ -21,8 +21,6 @@ import { GitExtension } from './api/git';
import { GitProtocolHandler } from './protocolHandler';
import { createGitExtension } from './api/extension';
import './api/api1';
const deactivateTasks: { (): Promise<any>; }[] = [];
export async function deactivate(): Promise<any> {
......
......@@ -251,10 +251,6 @@ semver@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"
semver@^5.5.1:
version "5.5.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.1.tgz#7dfdd8814bdb7cabc7be0fb1d734cfb66c940477"
supports-color@3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.1.2.tgz#72a262894d9d408b956ca05ff37b2ed8a6e2a2d5"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册