Skip to content

Commit 3f654aa

Browse files
committed
Show a progress bar and allow the user to cancel
1 parent 314bf8a commit 3f654aa

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

l10n/bundle.l10n.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"Cancel": "Cancel",
3737
"Replace existing build and debug assets?": "Replace existing build and debug assets?",
3838
"Unable to generate assets to build and debug. {0}.": "Unable to generate assets to build and debug. {0}.",
39+
"Downloading Razor Telemetry Package": "Downloading Razor Telemetry Package",
3940
"Cannot load Razor language server because the directory was not found: '{0}'": "Cannot load Razor language server because the directory was not found: '{0}'",
4041
"Could not find '{0}' in or above '{1}'.": "Could not find '{0}' in or above '{1}'.",
4142
"Invalid trace setting for Razor language server. Defaulting to '{0}'": "Invalid trace setting for Razor language server. Defaulting to '{0}'",

src/packageManager/downloadAndInstallPackages.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import { InstallationFailure, IntegrityCheckFailure } from '../omnisharp/logging
1515
import { mkdirpSync } from 'fs-extra';
1616
import { PackageInstallStart } from '../omnisharp/loggingEvents';
1717
import { DownloadValidator } from './isValidDownload';
18+
import { CancellationToken } from 'vscode';
1819

1920
export async function downloadAndInstallPackages(
2021
packages: AbsolutePathPackage[],
2122
provider: NetworkSettingsProvider,
2223
eventStream: EventStream,
23-
downloadValidator: DownloadValidator
24+
downloadValidator: DownloadValidator,
25+
token?: CancellationToken
2426
): Promise<boolean> {
2527
eventStream.post(new PackageInstallStart());
2628
for (const pkg of packages) {
@@ -33,7 +35,14 @@ export async function downloadAndInstallPackages(
3335
while (willTryInstallingPackage()) {
3436
count = count + 1;
3537
installationStage = 'downloadPackage';
36-
const buffer = await DownloadFile(pkg.description, eventStream, provider, pkg.url, pkg.fallbackUrl);
38+
const buffer = await DownloadFile(
39+
pkg.description,
40+
eventStream,
41+
provider,
42+
pkg.url,
43+
pkg.fallbackUrl,
44+
token
45+
);
3746
if (downloadValidator(buffer, pkg.integrity, eventStream)) {
3847
installationStage = 'installPackage';
3948
await InstallZip(buffer, pkg.description, pkg.installPath, pkg.binaries, eventStream);

src/packageManager/fileDownloader.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ import { NestedError } from '../nestedError';
1717
import { parse as parseUrl } from 'url';
1818
import { getProxyAgent } from './proxy';
1919
import { NetworkSettingsProvider } from '../networkSettings';
20+
import { CancellationToken } from 'vscode';
2021

2122
export async function DownloadFile(
2223
description: string,
2324
eventStream: EventStream,
2425
networkSettingsProvider: NetworkSettingsProvider,
2526
url: string,
26-
fallbackUrl?: string
27+
fallbackUrl?: string,
28+
token?: CancellationToken
2729
): Promise<Buffer> {
2830
eventStream.post(new DownloadStart(description));
2931

3032
try {
31-
const buffer = await downloadFile(description, url, eventStream, networkSettingsProvider);
33+
const buffer = await downloadFile(description, url, eventStream, networkSettingsProvider, token);
3234
eventStream.post(new DownloadSuccess(` Done!`));
3335
return buffer;
3436
} catch (primaryUrlError) {
@@ -54,7 +56,8 @@ async function downloadFile(
5456
description: string,
5557
urlString: string,
5658
eventStream: EventStream,
57-
networkSettingsProvider: NetworkSettingsProvider
59+
networkSettingsProvider: NetworkSettingsProvider,
60+
token?: CancellationToken
5861
): Promise<Buffer> {
5962
const url = parseUrl(urlString);
6063
const networkSettings = networkSettingsProvider();
@@ -66,12 +69,15 @@ async function downloadFile(
6669
agent: getProxyAgent(url, proxy, strictSSL),
6770
port: url.port,
6871
rejectUnauthorized: strictSSL,
69-
timeout: 5 * 60 * 1000, // timeout is in milliseconds
7072
};
7173

7274
const buffers: any[] = [];
7375

7476
return new Promise<Buffer>((resolve, reject) => {
77+
token?.onCancellationRequested(() => {
78+
return reject(new NestedError(`Cancelled downloading ${urlString}.`));
79+
});
80+
7581
const request = https.request(options, (response) => {
7682
if (response.statusCode === 301 || response.statusCode === 302) {
7783
// Redirect - download from new location
@@ -82,7 +88,7 @@ async function downloadFile(
8288
return reject(new NestedError('Missing location'));
8389
}
8490
return resolve(
85-
downloadFile(description, response.headers.location, eventStream, networkSettingsProvider)
91+
downloadFile(description, response.headers.location, eventStream, networkSettingsProvider, token)
8692
);
8793
} else if (response.statusCode !== 200) {
8894
// Download failed - print error message
@@ -130,10 +136,6 @@ async function downloadFile(
130136
});
131137
});
132138

133-
request.on('timeout', () => {
134-
request.destroy(new Error(`Timed out trying to download ${urlString}.`));
135-
});
136-
137139
request.on('error', (err) => {
138140
reject(new NestedError(`Request error: ${err.message || 'NONE'}`, err));
139141
});

src/razor/razorTelemetryDownloader.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import * as vscode from 'vscode';
67
import { PlatformInformation } from '../shared/platform';
78
import { PackageInstallation, LogPlatformInfo, InstallationSuccess } from '../omnisharp/loggingEvents';
89
import { EventStream } from '../eventStream';
@@ -42,17 +43,27 @@ export class RazorTelemetryDownloader {
4243
if (packagesToInstall.length > 0) {
4344
this.eventStream.post(new PackageInstallation(`Razor Telemetry Version = ${version}`));
4445
this.eventStream.post(new LogPlatformInfo(this.platformInfo));
45-
if (
46-
await downloadAndInstallPackages(
47-
packagesToInstall,
48-
this.networkSettingsProvider,
49-
this.eventStream,
50-
isValidDownload
51-
)
52-
) {
53-
this.eventStream.post(new InstallationSuccess());
54-
return true;
55-
}
46+
await vscode.window.withProgress(
47+
{
48+
location: vscode.ProgressLocation.Notification,
49+
title: vscode.l10n.t('Downloading Razor Telemetry Package'),
50+
cancellable: true,
51+
},
52+
async (_, token) => {
53+
if (
54+
await downloadAndInstallPackages(
55+
packagesToInstall,
56+
this.networkSettingsProvider,
57+
this.eventStream,
58+
isValidDownload,
59+
token
60+
)
61+
) {
62+
this.eventStream.post(new InstallationSuccess());
63+
return true;
64+
}
65+
}
66+
);
5667
}
5768

5869
return false;

tasks/offlinePackagingTasks.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { getPackageJSON } from '../tasks/packageJson';
2929
import { createPackageAsync } from '../tasks/vsceTasks';
3030
import { isValidDownload } from '../src/packageManager/isValidDownload';
3131
import path = require('path');
32+
import { CancellationToken } from 'vscode';
3233
// There are no typings for this library.
3334
// eslint-disable-next-line @typescript-eslint/no-var-requires
3435
const argv = require('yargs').argv;
@@ -153,7 +154,8 @@ async function installDebugger(packageJSON: any, platformInfo: PlatformInformati
153154
async function installPackageJsonDependency(
154155
dependencyName: string,
155156
packageJSON: any,
156-
platformInfo: PlatformInformation
157+
platformInfo: PlatformInformation,
158+
token?: CancellationToken
157159
) {
158160
const eventStream = new EventStream();
159161
const logger = new Logger((message) => process.stdout.write(message));
@@ -168,7 +170,7 @@ async function installPackageJsonDependency(
168170
codeExtensionPath
169171
);
170172
const provider = () => new NetworkSettings('', true);
171-
if (!(await downloadAndInstallPackages(packagesToInstall, provider, eventStream, isValidDownload))) {
173+
if (!(await downloadAndInstallPackages(packagesToInstall, provider, eventStream, isValidDownload, token))) {
172174
throw Error('Failed to download package.');
173175
}
174176
}

0 commit comments

Comments
 (0)