This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
/
rustup.ts
227 lines (198 loc) · 7.7 KB
/
rustup.ts
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
224
225
226
227
// Copyright 2017 The RLS Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
'use strict';
import * as child_process from 'child_process';
import { window } from 'vscode';
import { execChildProcess } from './utils/child_process';
import { startSpinner, stopSpinner } from './spinner';
export class RustupConfig {
channel: string;
path: string;
componentName: string;
constructor(channel: string, path: string, componentName: string) {
this.channel = channel;
this.path = path;
this.componentName = componentName;
}
}
// This module handles running the RLS via rustup, including checking that rustup
// is installed and installing any required components/toolchains.
export async function runRlsViaRustup(env: any, config: RustupConfig): Promise<child_process.ChildProcess> {
await ensureToolchain(config);
await checkForRls(config);
return child_process.spawn(config.path, ['run', config.channel, 'rls'], { env });
}
export async function rustupUpdate(config: RustupConfig) {
startSpinner('RLS', 'Updating…');
try {
const { stdout } = await execChildProcess(config.path + ' update');
// This test is imperfect because if the user has multiple toolchains installed, they
// might have one updated and one unchanged. But I don't want to go too far down the
// rabbit hole of parsing rustup's output.
if (stdout.indexOf('unchanged') > -1) {
stopSpinner('Up to date.');
} else {
stopSpinner('Up to date. Restart extension for changes to take effect.');
}
} catch (e) {
console.log(e);
stopSpinner('An error occurred whilst trying to update.');
}
}
// Check for the nightly toolchain (and that rustup exists)
async function ensureToolchain(config: RustupConfig): Promise<void> {
const toolchainInstalled = await hasToolchain(config);
if (toolchainInstalled) {
return;
}
const clicked = await window.showInformationMessage(config.channel + ' toolchain not installed. Install?', 'Yes');
if (clicked === 'Yes') {
await tryToInstallToolchain(config);
}
else {
throw new Error();
}
}
async function hasToolchain(config: RustupConfig): Promise<boolean> {
try {
const { stdout } = await execChildProcess(config.path + ' toolchain list');
const hasToolchain = stdout.indexOf(config.channel) > -1;
return hasToolchain;
}
catch (e) {
console.log(e);
// rustup not present
window.showErrorMessage('Rustup not available. Install from https://www.rustup.rs/');
throw e;
}
}
async function tryToInstallToolchain(config: RustupConfig): Promise<void> {
startSpinner('RLS', 'Installing toolchain…');
try {
const { stdout, stderr } = await execChildProcess(config.path + ' toolchain install ' + config.channel);
console.log(stdout);
console.log(stderr);
stopSpinner(config.channel + ' toolchain installed successfully');
}
catch (e) {
console.log(e);
window.showErrorMessage('Could not install ' + config.channel + ' toolchain');
stopSpinner('Could not install ' + config.channel + ' toolchain');
throw e;
}
}
// Check for rls components.
async function checkForRls(config: RustupConfig): Promise<void> {
const hasRls = await hasRlsComponents(config);
if (hasRls) {
return;
}
// missing component
const clicked = await Promise.resolve(window.showInformationMessage('RLS not installed. Install?', 'Yes'));
if (clicked === 'Yes') {
await installRls(config);
}
else {
throw new Error();
}
}
async function hasRlsComponents(config: RustupConfig): Promise<boolean> {
try {
const { stdout } = await execChildProcess(config.path + ' component list --toolchain ' + config.channel);
const componentName = new RegExp('^' + config.componentName + '.* \\((default|installed)\\)$', 'm');
if (
stdout.search(componentName) === -1 ||
stdout.search(/^rust-analysis.* \((default|installed)\)$/m) === -1 ||
stdout.search(/^rust-src.* \((default|installed)\)$/m) === -1) {
return false;
}
else {
return true;
}
}
catch (e) {
console.log(e);
// rustup error?
window.showErrorMessage('Unexpected error initialising RLS - error running rustup');
throw e;
}
}
async function installRls(config: RustupConfig): Promise<void> {
startSpinner('RLS', 'Installing components…');
const tryFn: (component: string) => Promise<(Error | null)> = async (component: string) => {
try {
const { stdout, stderr, } = await execChildProcess(config.path + ` component add ${component} --toolchain ` + config.channel);
console.log(stdout);
console.log(stderr);
return null;
}
catch (_e) {
window.showErrorMessage(`Could not install RLS component (${component})`);
const err = new Error(`installing ${component} failed`);
return err;
}
};
{
const e = await tryFn('rust-analysis');
if (e !== null) {
stopSpinner('Could not install RLS');
throw e;
}
}
{
const e = await tryFn('rust-src');
if (e !== null) {
stopSpinner('Could not install RLS');
throw e;
}
}
console.log('install rls');
{
const e = await tryFn(config.componentName);
if (e !== null) {
stopSpinner('Could not install RLS');
throw e;
}
}
stopSpinner('RLS components installed successfully');
}
/**
* Parses given output of `rustup show` and retrieves the local active toolchain.
*/
export function parseActiveToolchain(rustupOutput: string): string {
// There may a default entry under 'installed toolchains' section, so search
// for currently active/overridden one only under 'active toolchain' section
const activeToolchainsIndex = rustupOutput.search('active toolchain');
if (activeToolchainsIndex === -1) {
throw new Error(`couldn't find active toolchains`);
}
rustupOutput = rustupOutput.substr(activeToolchainsIndex);
const matchActiveChannel = new RegExp(/^(\S*) \((?:default|overridden)/gm);
const match = matchActiveChannel.exec(rustupOutput);
if (match === null) {
throw new Error(`couldn't find active toolchain under 'active toolchains'`);
} else if (match.length > 2) {
throw new Error(`multiple active toolchains found under 'active toolchains'`);
}
return match[1];
}
/**
* Returns active (including local overrides) toolchain, as specified by rustup.
* May throw if rustup at specified path can't be executed.
*/
export function getActiveChannel(rustupPath: string, wsPath: string): string {
// rustup info might differ depending on where it's executed
// (e.g. when a toolchain is locally overriden), so executing it
// under our current workspace root should give us close enough result
const output = child_process.execSync(`${rustupPath} show`, { cwd: wsPath }).toString();
const activeChannel = parseActiveToolchain(output);
console.info(`Detected active channel: ${activeChannel} (since 'rust-client.channel' is unspecified)`);
return activeChannel;
}