|
1 | 1 | import fetch from "node-fetch";
|
| 2 | +import * as vscode from "vscode"; |
| 3 | +import * as path from "path"; |
2 | 4 | import * as fs from "fs";
|
3 | 5 | import * as stream from "stream";
|
4 | 6 | import * as util from "util";
|
5 | 7 | import { log, assert } from "../util";
|
| 8 | +import { ArtifactReleaseInfo } from "./interfaces"; |
6 | 9 |
|
7 | 10 | const pipeline = util.promisify(stream.pipeline);
|
8 | 11 |
|
@@ -49,3 +52,46 @@ export async function downloadFile(
|
49 | 52 | // Issue at nodejs repo: https://github.com/nodejs/node/issues/31776
|
50 | 53 | });
|
51 | 54 | }
|
| 55 | + |
| 56 | +/** |
| 57 | + * Downloads artifact from given `downloadUrl`. |
| 58 | + * Creates `installationDir` if it is not yet created and puts the artifact under |
| 59 | + * `artifactFileName`. |
| 60 | + * Displays info about the download progress in an info message printing the name |
| 61 | + * of the artifact as `displayName`. |
| 62 | + */ |
| 63 | +export async function downloadArtifactWithProgressUi( |
| 64 | + { downloadUrl, releaseName }: ArtifactReleaseInfo, |
| 65 | + artifactFileName: string, |
| 66 | + installationDir: string, |
| 67 | + displayName: string, |
| 68 | +) { |
| 69 | + await fs.promises.mkdir(installationDir).catch(err => assert( |
| 70 | + err?.code === "EEXIST", |
| 71 | + `Couldn't create directory "${installationDir}" to download ` + |
| 72 | + `${artifactFileName} artifact: ${err?.message}` |
| 73 | + )); |
| 74 | + |
| 75 | + const installationPath = path.join(installationDir, artifactFileName); |
| 76 | + |
| 77 | + await vscode.window.withProgress( |
| 78 | + { |
| 79 | + location: vscode.ProgressLocation.Notification, |
| 80 | + cancellable: false, // FIXME: add support for canceling download? |
| 81 | + title: `Downloading rust-analyzer ${displayName} (${releaseName})` |
| 82 | + }, |
| 83 | + async (progress, _cancellationToken) => { |
| 84 | + let lastPrecentage = 0; |
| 85 | + const filePermissions = 0o755; // (rwx, r_x, r_x) |
| 86 | + await downloadFile(downloadUrl, installationPath, filePermissions, (readBytes, totalBytes) => { |
| 87 | + const newPercentage = (readBytes / totalBytes) * 100; |
| 88 | + progress.report({ |
| 89 | + message: newPercentage.toFixed(0) + "%", |
| 90 | + increment: newPercentage - lastPrecentage |
| 91 | + }); |
| 92 | + |
| 93 | + lastPrecentage = newPercentage; |
| 94 | + }); |
| 95 | + } |
| 96 | + ); |
| 97 | +} |
0 commit comments