Skip to content
Navigation Menu
{{ message }}
forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubAPI.ts
More file actions
223 lines (204 loc) · 8.56 KB
/
Copy pathgithubAPI.ts
File metadata and controls
223 lines (204 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import { PackageVersion } from './packageVersion';
import * as util from './common';
import * as tmp from 'tmp';
import { PlatformInformation } from './platform';
import { OutgoingHttpHeaders } from 'http';
/**
* The object representation of a Build Asset. Each Asset corresponds to information about a release file on GitHub.
*/
interface Asset {
name: string;
browser_download_url: string;
}
/**
* The object representation of a release in the GitHub API's release JSON.
* Named Build so as to reduce confusion between a "Release" release and "Insiders" release.
*/
interface Build {
name: string;
assets: Asset[];
}
/**
* Search each Asset by name to retrieve the download URL for a VSIX package
* @param vsixName The name of the VSIX to search for
* @return The download URL of the VSIX
*/
function getVsixDownloadUrl(build: Build, vsixName: string): string {
const downloadUrl: string = build.assets.find(asset => {
return asset.name === vsixName;
}).browser_download_url;
if (!downloadUrl) {
throw new Error('Failed to find VSIX: ' + vsixName + ' in build: ' + build.name);
}
return downloadUrl;
}
/**
* Determine whether an object is of type Asset.
* @param input Incoming object.
* @return Whether input is of type Asset.
*/
function isAsset(input: any): input is Asset {
return input && input.name && typeof(input.name) === "string" &&
input.browser_download_url && typeof(input.browser_download_url) === "string";
}
/**
* Determine whether an object is of type Build. Note that earlier releases of the extension
* do not have 4 or greater Assets (Mac, Win, Linux 32/64). Only call this on more recent Builds.
* @param input Incoming object.
* @return Whether input is of type Build.
*/
function isBuild(input: any): input is Build {
return input && input.name && typeof(input.name) === "string" && isArrayOfAssets(input.assets) && input.assets.length >= 4;
}
/**
* Determine whether an object is of type Asset[].
* @param input Incoming object.
* @return Whether input is of type Asset[].
*/
function isArrayOfAssets(input: any): input is Asset[] {
return input instanceof Array && input.every(item => isAsset(item));
}
/**
* Determine whether an object is of type Build[].
* @param input Incoming object.
* @return Whether input is of type Build[].
*/
function isArrayOfBuilds(input: any): input is Build[] {
if (!input || !(input instanceof Array) || input.length === 0) {
return false;
}
// Only check the five most recent builds for validity -- no need to check all of them
for (let i: number = 0; i < 5 && i < input.length; i++) {
if (!isBuild(input[i])) {
return false;
}
}
return true;
}
/**
* Match the user's platform information to the VSIX name relevant to them.
* @param info Information about the user's operating system.
* @return VSIX filename for the extension's releases matched to the user's platform.
*/
function vsixNameForPlatform(info: PlatformInformation): string {
const vsixName: string = function(platformInfo): string {
switch (platformInfo.platform) {
case 'win32': return 'cpptools-win32.vsix';
case 'darwin': return 'cpptools-osx.vsix';
default: {
switch (platformInfo.architecture) {
case 'x86': return 'cpptools-linux32.vsix';
case 'x86_64': return 'cpptools-linux.vsix';
}
}
}
}(info);
if (!vsixName) {
throw new Error('Failed to match VSIX name for: ' + info.platform + ':' + info.architecture);
}
return vsixName;
}
/**
* Interface for return value of getTargetBuildInfo containing the download URL and version of a Build.
*/
export interface BuildInfo {
downloadUrl: string;
name: string;
}
/**
* Use the GitHub API to retrieve the download URL of the extension version the user should update to, if any.
* @param updateChannel The user's updateChannel setting.
* @return Download URL for the extension VSIX package that the user should install. If the user
* does not need to update, resolves to undefined.
*/
export async function getTargetBuildInfo(updateChannel: string): Promise<BuildInfo> {
return getReleaseJson()
.then(builds => getTargetBuild(builds, updateChannel))
.then(async build => {
if (!build) {
return Promise.resolve(undefined);
}
try {
const platformInfo: PlatformInformation = await PlatformInformation.GetPlatformInformation();
const vsixName: string = vsixNameForPlatform(platformInfo);
const downloadUrl: string = getVsixDownloadUrl(build, vsixName);
return { downloadUrl: downloadUrl, name: build.name };
} catch (error) {
return Promise.reject(error);
}
});
}
/**
* Determines whether there exists a Build in the given Build[] that should be installed.
* @param builds The GitHub release list parsed as an array of Builds.
* @param updateChannel The user's updateChannel setting.
* @return The Build if the user should update to it, otherwise undefined.
*/
function getTargetBuild(builds: Build[], updateChannel: string): Build {
// Get predicates to determine the build to install, if any
let needsUpdate: (installed: PackageVersion, target: PackageVersion) => boolean;
let useBuild: (build: Build) => boolean;
if (updateChannel === 'Insiders') {
needsUpdate = (installed: PackageVersion, target: PackageVersion) => { return target.isGreaterThan(installed); };
useBuild = (build: Build): boolean => { return true; };
} else if (updateChannel === 'Default') {
needsUpdate = function(installed: PackageVersion, target: PackageVersion): boolean { return installed.isGreaterThan(target); };
useBuild = (build: Build): boolean => { return build.name.indexOf('-') === -1; };
} else {
throw new Error('Incorrect updateChannel setting provided');
}
// Get the build to install
const targetBuild: Build = builds.find((build) => useBuild(build));
if (!targetBuild) {
throw new Error('Failed to determine installation candidate');
}
// Check current version against target's version to determine if the installation should happen
const userVersion: PackageVersion = new PackageVersion(util.packageJson.version);
const targetVersion: PackageVersion = new PackageVersion(targetBuild.name);
return needsUpdate(userVersion, targetVersion) ? targetBuild : undefined;
}
/**
* Download and parse the release list JSON from the GitHub API into a Build[].
* @return Information about the released builds of the C/C++ extension.
*/
async function getReleaseJson(): Promise<Build[]> {
return new Promise<Build[]>((resolve, reject) => {
// Create temp file to hold JSON
tmp.file(async (err, releaseJsonPath, fd, cleanupCallback) => {
if (err) {
reject(new Error('Failed to create release json file'));
return;
}
try {
// Download release JSON
const releaseUrl: string = 'https://api.github.com/repos/Microsoft/vscode-cpptools/releases';
const header: OutgoingHttpHeaders = { 'User-Agent': 'vscode-cpptools' };
await util.downloadFileToDestination(releaseUrl, releaseJsonPath, header)
.catch(() => { throw new Error('Failed to download release JSON'); });
// Read the release JSON file
const fileContent: string = await util.readFileText(releaseJsonPath)
.catch(() => { throw new Error('Failed to read release JSON file'); } );
// Parse the file
let releaseJson: any;
try {
releaseJson = JSON.parse(fileContent);
} catch (error) {
throw new Error('Failed to parse release JSON');
}
// Type check
if (isArrayOfBuilds(releaseJson)) {
resolve(releaseJson);
} else {
reject(releaseJson);
}
} catch (error) {
reject(error);
}
});
});
}
You can’t perform that action at this time.
