3514: vscode: askBeforeDownload option r=matklad a=Veetaha

This is a small step towards #3402, also implements my proposal stated in #3403

Also renamed `BinarySource` to `ArtifactSource` in anticipation of nightlies installation that requires downloading not a binary itself but `.vsix` package, thus generalized to `artifact` term.

@matklad @lnicola 

Co-authored-by: Veetaha <gerzoh1@gmail.com>
This commit is contained in:
bors[bot] 2020-03-09 09:05:45 +00:00 committed by GitHub
commit fcaab59451
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 24 deletions

View file

@ -2,6 +2,13 @@
:toc: preamble
:sectanchors:
:page-layout: post
// https://gist.github.com/dcode/0cfbf2699a1fe9b46ff04c41721dda74#admonitions
:tip-caption: :bulb:
:note-caption: :information_source:
:important-caption: :heavy_exclamation_mark:
:caution-caption: :fire:
:warning-caption: :warning:
// Master copy of this document lives in the https://github.com/rust-analyzer/rust-analyzer repository
@ -30,7 +37,7 @@ $ rustup component add rust-src
=== VS Code
This the best supported editor at the moment.
This is the best supported editor at the moment.
rust-analyzer plugin for VS Code is maintained
https://github.com/rust-analyzer/rust-analyzer/tree/master/editors/code[in tree].
@ -40,6 +47,16 @@ By default, the plugin will prompt you to download the matching version of the s
image::https://user-images.githubusercontent.com/9021944/75067008-17502500-54ba-11ea-835a-f92aac50e866.png[]
[NOTE]
====
To disable this notification put the following to `settings.json`
[source,json]
----
{ "rust-analyzer.updates.askBeforeDownload": false }
----
====
The server binary is stored in `~/.config/Code/User/globalStorage/matklad.rust-analyzer`.
Note that we only support the latest version of VS Code.

View file

@ -219,6 +219,11 @@
}
}
},
"rust-analyzer.updates.askBeforeDownload": {
"type": "boolean",
"default": true,
"description": "Whether to ask for permission before downloading any files from the Internet"
},
"rust-analyzer.serverPath": {
"type": [
"null",

View file

@ -1,6 +1,6 @@
import * as os from "os";
import * as vscode from 'vscode';
import { BinarySource } from "./installation/interfaces";
import { ArtifactSource } from "./installation/interfaces";
import { log } from "./util";
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
@ -114,12 +114,12 @@ export class Config {
}
}
get serverSource(): null | BinarySource {
get serverSource(): null | ArtifactSource {
const serverPath = RA_LSP_DEBUG ?? this.cfg.get<null | string>("serverPath");
if (serverPath) {
return {
type: BinarySource.Type.ExplicitPath,
type: ArtifactSource.Type.ExplicitPath,
path: Config.replaceTildeWithHomeDir(serverPath)
};
}
@ -129,11 +129,12 @@ export class Config {
if (!prebuiltBinaryName) return null;
return {
type: BinarySource.Type.GithubRelease,
type: ArtifactSource.Type.GithubRelease,
dir: this.ctx.globalStoragePath,
file: prebuiltBinaryName,
storage: this.ctx.globalState,
version: Config.extensionVersion,
tag: Config.extensionVersion,
askBeforeDownload: this.cfg.get("updates.askBeforeDownload") as boolean,
repo: {
name: "rust-analyzer",
owner: "rust-analyzer",

View file

@ -14,14 +14,14 @@ export interface ArtifactReleaseInfo {
}
/**
* Represents the source of a binary artifact which is either specified by the user
* Represents the source of a an artifact which is either specified by the user
* explicitly, or bundled by this extension from GitHub releases.
*/
export type BinarySource = BinarySource.ExplicitPath | BinarySource.GithubRelease;
export type ArtifactSource = ArtifactSource.ExplicitPath | ArtifactSource.GithubRelease;
export namespace BinarySource {
export namespace ArtifactSource {
/**
* Type tag for `BinarySource` discriminated union.
* Type tag for `ArtifactSource` discriminated union.
*/
export const enum Type { ExplicitPath, GithubRelease }
@ -56,13 +56,18 @@ export namespace BinarySource {
/**
* Tag of github release that denotes a version required by this extension.
*/
version: string;
tag: string;
/**
* Object that provides `get()/update()` operations to store metadata
* about the actual binary, e.g. its actual version.
*/
storage: vscode.Memento;
/**
* Ask for the user permission before downloading the artifact.
*/
askBeforeDownload: boolean;
}
}

View file

@ -3,12 +3,12 @@ import * as path from "path";
import { promises as dns } from "dns";
import { spawnSync } from "child_process";
import { BinarySource } from "./interfaces";
import { ArtifactSource } from "./interfaces";
import { fetchArtifactReleaseInfo } from "./fetch_artifact_release_info";
import { downloadArtifact } from "./download_artifact";
import { log, assert } from "../util";
export async function ensureServerBinary(source: null | BinarySource): Promise<null | string> {
export async function ensureServerBinary(source: null | ArtifactSource): Promise<null | string> {
if (!source) {
vscode.window.showErrorMessage(
"Unfortunately we don't ship binaries for your platform yet. " +
@ -22,7 +22,7 @@ export async function ensureServerBinary(source: null | BinarySource): Promise<n
}
switch (source.type) {
case BinarySource.Type.ExplicitPath: {
case ArtifactSource.Type.ExplicitPath: {
if (isBinaryAvailable(source.path)) {
return source.path;
}
@ -34,11 +34,11 @@ export async function ensureServerBinary(source: null | BinarySource): Promise<n
);
return null;
}
case BinarySource.Type.GithubRelease: {
case ArtifactSource.Type.GithubRelease: {
const prebuiltBinaryPath = path.join(source.dir, source.file);
const installedVersion: null | string = getServerVersion(source.storage);
const requiredVersion: string = source.version;
const requiredVersion: string = source.tag;
log.debug("Installed version:", installedVersion, "required:", requiredVersion);
@ -46,12 +46,14 @@ export async function ensureServerBinary(source: null | BinarySource): Promise<n
return prebuiltBinaryPath;
}
const userResponse = await vscode.window.showInformationMessage(
`Language server version ${source.version} for rust-analyzer is not installed. ` +
"Do you want to download it now?",
"Download now", "Cancel"
);
if (userResponse !== "Download now") return null;
if (source.askBeforeDownload) {
const userResponse = await vscode.window.showInformationMessage(
`Language server version ${source.tag} for rust-analyzer is not installed. ` +
"Do you want to download it now?",
"Download now", "Cancel"
);
if (userResponse !== "Download now") return null;
}
if (!await downloadServer(source)) return null;
@ -60,9 +62,9 @@ export async function ensureServerBinary(source: null | BinarySource): Promise<n
}
}
async function downloadServer(source: BinarySource.GithubRelease): Promise<boolean> {
async function downloadServer(source: ArtifactSource.GithubRelease): Promise<boolean> {
try {
const releaseInfo = await fetchArtifactReleaseInfo(source.repo, source.file, source.version);
const releaseInfo = await fetchArtifactReleaseInfo(source.repo, source.file, source.tag);
await downloadArtifact(releaseInfo, source.file, source.dir, "language server");
await setServerVersion(source.storage, releaseInfo.releaseName);