diff options
author | Dmitry <[email protected]> | 2020-02-16 17:36:48 +0000 |
---|---|---|
committer | Dmitry <[email protected]> | 2020-02-16 17:36:48 +0000 |
commit | 58e15d12e4211306273b0128cb4a80b552889543 (patch) | |
tree | 70e129b7cd76c55d5cb86f9b084db001f73a2003 /editors/code/src/installation/language_server.ts | |
parent | b446384956243ce6bdcef2a869123a2605e597ed (diff) | |
parent | 742b3b5744fbca1a5587e2898cd5b74d55853a47 (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'editors/code/src/installation/language_server.ts')
-rw-r--r-- | editors/code/src/installation/language_server.ts | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/editors/code/src/installation/language_server.ts b/editors/code/src/installation/language_server.ts deleted file mode 100644 index 4797c3f01..000000000 --- a/editors/code/src/installation/language_server.ts +++ /dev/null | |||
@@ -1,148 +0,0 @@ | |||
1 | import * as vscode from "vscode"; | ||
2 | import * as path from "path"; | ||
3 | import { strict as assert } from "assert"; | ||
4 | import { promises as fs } from "fs"; | ||
5 | import { promises as dns } from "dns"; | ||
6 | import { spawnSync } from "child_process"; | ||
7 | import { throttle } from "throttle-debounce"; | ||
8 | |||
9 | import { BinarySource } from "./interfaces"; | ||
10 | import { fetchLatestArtifactMetadata } from "./fetch_latest_artifact_metadata"; | ||
11 | import { downloadFile } from "./download_file"; | ||
12 | |||
13 | export async function downloadLatestLanguageServer( | ||
14 | {file: artifactFileName, dir: installationDir, repo}: BinarySource.GithubRelease | ||
15 | ) { | ||
16 | const { releaseName, downloadUrl } = (await fetchLatestArtifactMetadata( | ||
17 | repo, artifactFileName | ||
18 | ))!; | ||
19 | |||
20 | await fs.mkdir(installationDir).catch(err => assert.strictEqual( | ||
21 | err?.code, | ||
22 | "EEXIST", | ||
23 | `Couldn't create directory "${installationDir}" to download `+ | ||
24 | `language server binary: ${err.message}` | ||
25 | )); | ||
26 | |||
27 | const installationPath = path.join(installationDir, artifactFileName); | ||
28 | |||
29 | console.time("Downloading ra_lsp_server"); | ||
30 | await vscode.window.withProgress( | ||
31 | { | ||
32 | location: vscode.ProgressLocation.Notification, | ||
33 | cancellable: false, // FIXME: add support for canceling download? | ||
34 | title: `Downloading language server (${releaseName})` | ||
35 | }, | ||
36 | async (progress, _cancellationToken) => { | ||
37 | let lastPrecentage = 0; | ||
38 | const filePermissions = 0o755; // (rwx, r_x, r_x) | ||
39 | await downloadFile(downloadUrl, installationPath, filePermissions, throttle( | ||
40 | 200, | ||
41 | /* noTrailing: */ true, | ||
42 | (readBytes, totalBytes) => { | ||
43 | const newPercentage = (readBytes / totalBytes) * 100; | ||
44 | progress.report({ | ||
45 | message: newPercentage.toFixed(0) + "%", | ||
46 | increment: newPercentage - lastPrecentage | ||
47 | }); | ||
48 | |||
49 | lastPrecentage = newPercentage; | ||
50 | }) | ||
51 | ); | ||
52 | } | ||
53 | ); | ||
54 | console.timeEnd("Downloading ra_lsp_server"); | ||
55 | } | ||
56 | export async function ensureLanguageServerBinary( | ||
57 | langServerSource: null | BinarySource | ||
58 | ): Promise<null | string> { | ||
59 | |||
60 | if (!langServerSource) { | ||
61 | vscode.window.showErrorMessage( | ||
62 | "Unfortunately we don't ship binaries for your platform yet. " + | ||
63 | "You need to manually clone rust-analyzer repository and " + | ||
64 | "run `cargo xtask install --server` to build the language server from sources. " + | ||
65 | "If you feel that your platform should be supported, please create an issue " + | ||
66 | "about that [here](https://github.com/rust-analyzer/rust-analyzer/issues) and we " + | ||
67 | "will consider it." | ||
68 | ); | ||
69 | return null; | ||
70 | } | ||
71 | |||
72 | switch (langServerSource.type) { | ||
73 | case BinarySource.Type.ExplicitPath: { | ||
74 | if (isBinaryAvailable(langServerSource.path)) { | ||
75 | return langServerSource.path; | ||
76 | } | ||
77 | |||
78 | vscode.window.showErrorMessage( | ||
79 | `Unable to run ${langServerSource.path} binary. ` + | ||
80 | `To use the pre-built language server, set "rust-analyzer.raLspServerPath" ` + | ||
81 | "value to `null` or remove it from the settings to use it by default." | ||
82 | ); | ||
83 | return null; | ||
84 | } | ||
85 | case BinarySource.Type.GithubRelease: { | ||
86 | const prebuiltBinaryPath = path.join(langServerSource.dir, langServerSource.file); | ||
87 | |||
88 | if (isBinaryAvailable(prebuiltBinaryPath)) { | ||
89 | return prebuiltBinaryPath; | ||
90 | } | ||
91 | |||
92 | const userResponse = await vscode.window.showInformationMessage( | ||
93 | "Language server binary for rust-analyzer was not found. " + | ||
94 | "Do you want to download it now?", | ||
95 | "Download now", "Cancel" | ||
96 | ); | ||
97 | if (userResponse !== "Download now") return null; | ||
98 | |||
99 | try { | ||
100 | await downloadLatestLanguageServer(langServerSource); | ||
101 | } catch (err) { | ||
102 | vscode.window.showErrorMessage( | ||
103 | `Failed to download language server from ${langServerSource.repo.name} ` + | ||
104 | `GitHub repository: ${err.message}` | ||
105 | ); | ||
106 | |||
107 | console.error(err); | ||
108 | |||
109 | dns.resolve('example.com').then( | ||
110 | addrs => console.log("DNS resolution for example.com was successful", addrs), | ||
111 | err => { | ||
112 | console.error( | ||
113 | "DNS resolution for example.com failed, " + | ||
114 | "there might be an issue with Internet availability" | ||
115 | ); | ||
116 | console.error(err); | ||
117 | } | ||
118 | ); | ||
119 | |||
120 | return null; | ||
121 | } | ||
122 | |||
123 | if (!isBinaryAvailable(prebuiltBinaryPath)) assert(false, | ||
124 | `Downloaded language server binary is not functional.` + | ||
125 | `Downloaded from: ${JSON.stringify(langServerSource)}` | ||
126 | ); | ||
127 | |||
128 | |||
129 | vscode.window.showInformationMessage( | ||
130 | "Rust analyzer language server was successfully installed 🦀" | ||
131 | ); | ||
132 | |||
133 | return prebuiltBinaryPath; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | function isBinaryAvailable(binaryPath: string) { | ||
138 | const res = spawnSync(binaryPath, ["--version"]); | ||
139 | |||
140 | // ACHTUNG! `res` type declaration is inherently wrong, see | ||
141 | // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/42221 | ||
142 | |||
143 | console.log("Checked binary availablity via --version", res); | ||
144 | console.log(binaryPath, "--version output:", res.output?.map(String)); | ||
145 | |||
146 | return res.status === 0; | ||
147 | } | ||
148 | } | ||