api.ts 2.7 KB
Newer Older
M
Matt Bierner 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as semver from 'semver';
7
import * as nls from 'vscode-nls';
8

9
const localize = nls.loadMessageBundle();
M
Matt Bierner 已提交
10 11

export default class API {
M
Matt Bierner 已提交
12
	private static fromSimpleString(value: string): API {
13
		return new API(value, value, value);
M
Matt Bierner 已提交
14 15 16 17 18 19 20 21 22 23
	}

	public static readonly defaultVersion = API.fromSimpleString('1.0.0');
	public static readonly v240 = API.fromSimpleString('2.4.0');
	public static readonly v250 = API.fromSimpleString('2.5.0');
	public static readonly v260 = API.fromSimpleString('2.6.0');
	public static readonly v270 = API.fromSimpleString('2.7.0');
	public static readonly v280 = API.fromSimpleString('2.8.0');
	public static readonly v290 = API.fromSimpleString('2.9.0');
	public static readonly v291 = API.fromSimpleString('2.9.1');
24
	public static readonly v292 = API.fromSimpleString('2.9.2');
M
Matt Bierner 已提交
25
	public static readonly v300 = API.fromSimpleString('3.0.0');
26
	public static readonly v310 = API.fromSimpleString('3.1.0');
M
Matt Bierner 已提交
27
	public static readonly v314 = API.fromSimpleString('3.1.4');
28
	public static readonly v320 = API.fromSimpleString('3.2.0');
29
	public static readonly v330 = API.fromSimpleString('3.3.0');
30
	public static readonly v333 = API.fromSimpleString('3.3.3');
31
	public static readonly v340 = API.fromSimpleString('3.4.0');
M
Matt Bierner 已提交
32
	public static readonly v345 = API.fromSimpleString('3.4.5');
33
	public static readonly v350 = API.fromSimpleString('3.5.0');
34
	public static readonly v380 = API.fromSimpleString('3.8.0');
M
Matt Bierner 已提交
35

36 37 38
	public static fromVersionString(versionString: string): API {
		let version = semver.valid(versionString);
		if (!version) {
39
			return new API(localize('invalidVersion', 'invalid version'), '1.0.0', '1.0.0');
40
		}
M
Matt Bierner 已提交
41

42
		// Cut off any prerelease tag since we sometimes consume those on purpose.
43 44 45
		const index = versionString.indexOf('-');
		if (index >= 0) {
			version = version.substr(0, index);
M
Matt Bierner 已提交
46
		}
47
		return new API(versionString, version, versionString);
M
Matt Bierner 已提交
48 49
	}

50
	private constructor(
51 52 53
		/**
		 * Human readable string for the current version. Displayed in the UI
		 */
54
		public readonly displayName: string,
55 56 57 58 59 60 61 62 63 64

		/**
		 * Semver version, e.g. '3.9.0'
		 */
		public readonly version: string,

		/**
		 * Full version string including pre-release tags, e.g. '3.9.0-beta'
		 */
		public readonly fullVersionString: string,
65 66
	) { }

M
Matt Bierner 已提交
67 68
	public gte(other: API): boolean {
		return semver.gte(this.version, other.version);
69
	}
70 71 72 73

	public lt(other: API): boolean {
		return !this.gte(other);
	}
74
}