Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 7bdafd2

Browse files
committed
Check active rustup override toolchains for updates
1 parent 8b9b49b commit 7bdafd2

File tree

1 file changed

+83
-41
lines changed

1 file changed

+83
-41
lines changed

lib/index.js

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ async function exec(command, { cwd }={}) {
3232
}
3333

3434
function logErr(e, logFn = console.warn) {
35-
const message = e && '' + e
36-
message && logFn(message)
35+
e && logFn('' + e)
3736
}
3837

3938
function clearIdeRustInfos() {
@@ -96,6 +95,19 @@ async function hasCommand(rustCommand) {
9695
}
9796
}
9897

98+
async function rustupOverrides() {
99+
let { stdout } = await exec("rustup override list")
100+
return stdout.split(/[\r\n]+/g)
101+
.map(line => {
102+
let lastSpace = line.lastIndexOf(' ')
103+
return {
104+
path: line.slice(0, lastSpace).trim(),
105+
toolchain: line.slice(lastSpace).trim()
106+
}
107+
})
108+
.filter(({ path, toolchain }) => path && toolchain)
109+
}
110+
99111
/** @return {?string} developer override of the command to start a Rls instance */
100112
function rlsCommandOverride() {
101113
return atom.config.get('ide-rust.rlsCommandOverride')
@@ -291,6 +303,7 @@ class RustLanguageClient extends AutoLanguageClient {
291303
super()
292304
/** (projectPath -> RlsProject) mappings */
293305
this.projects = {}
306+
this.activeOverrides = new Set()
294307
this.disposables = new CompositeDisposable()
295308

296309
/** Configuration schema */
@@ -343,14 +356,31 @@ class RustLanguageClient extends AutoLanguageClient {
343356
}
344357
}
345358

359+
async _refreshActiveOverrides() {
360+
try {
361+
const overrides = (await rustupOverrides())
362+
.filter(({ path }) => Object.keys(this.projects).some(project => project.startsWith(path)))
363+
.map(override => override.toolchain)
364+
const oldActive = this.activeOverrides
365+
this.activeOverrides = new Set(overrides)
366+
if (this.activeOverrides.size > oldActive.size) {
367+
const confToolchain = configToolchain() || await rustupDefaultToolchain()
368+
if (confToolchain) oldActive.add(confToolchain)
369+
this._promptToUpdateToolchain({ ignore: oldActive })
370+
}
371+
} catch (e) {
372+
if (await hasCommand("rustup")) logErr(e)
373+
}
374+
}
375+
346376
/** @param {?string} reason Reason for the restart shown in the notification */
347377
async _restartLanguageServers(reason) {
348378
await this.restartAllServers()
349379
if (reason) atom.notifications.addSuccess(reason, { _src: 'ide-rust' })
350380
}
351381

352382
// check for toolchain updates if installed & not dated
353-
async _promptToUpdateToolchain() {
383+
async _promptToUpdateToolchain({ ignore } = {}) {
354384
if (!atom.config.get('ide-rust.checkForToolchainUpdates')) return
355385

356386
if (!await hasCommand("rustup")) {
@@ -359,49 +389,58 @@ class RustLanguageClient extends AutoLanguageClient {
359389
return
360390
}
361391

362-
const confToolchain = configToolchain() || await rustupDefaultToolchain()
363-
if (!confToolchain) return
392+
const toolchains = new Set(this.activeOverrides)
364393

365-
const dated = confToolchain.match(DATED_REGEX)
366-
const toolchain = (dated ? dated[1] : confToolchain)
367-
const switching = confToolchain !== toolchain
394+
const confToolchain = configToolchain() || await rustupDefaultToolchain()
395+
if (confToolchain) toolchains.add(confToolchain)
396+
397+
Array.from(toolchains)
398+
.filter(toolchain => !ignore || !ignore.has(toolchain))
399+
.forEach(async confToolchain => {
400+
const dated = confToolchain.match(DATED_REGEX)
401+
let toolchain = (dated ? dated[1] : confToolchain)
402+
if (!dated && toolchain.includes('-')) {
403+
toolchain = toolchain.split('-')[0]
404+
}
368405

369-
let { stdout: currentVersion } = await rustupRun(confToolchain, "rustc --version")
370-
let newVersion = await fetchLatestDist({ toolchain, currentVersion }).catch(() => false)
371-
if (!newVersion) return
406+
let { stdout: currentVersion } = await rustupRun(confToolchain, "rustc --version")
407+
let newVersion = await fetchLatestDist({ toolchain, currentVersion }).catch(() => false)
408+
if (!newVersion) return
372409

373-
atom.notifications.addInfo(`Rls \`${toolchain}\` toolchain update available`, {
374-
description: newVersion,
375-
_src: 'ide-rust',
376-
dismissable: true,
377-
buttons: [{
378-
text: switching ? 'Update & Switch' : 'Update' ,
379-
onDidClick: () => {
380-
clearIdeRustInfos()
381-
382-
const updatePromise = exec(`rustup update ${toolchain}`)
383-
// set config in case going from dated -> latest
384-
.then(() => switching && atom.config.set('ide-rust.rlsToolchain', toolchain))
385-
.then(() => this._checkToolchain())
386-
.then(() => checkRls())
387-
.then(() => this._restartLanguageServers(`Updated Rls toolchain`))
388-
.catch(e => {
389-
logErr(e)
390-
e && atom.notifications.addError(`\`rustup update ${toolchain}\` failed`, {
391-
detail: e,
392-
dismissable: true,
410+
atom.notifications.addInfo(`Rls \`${toolchain}\` toolchain update available`, {
411+
description: newVersion,
412+
_src: 'ide-rust',
413+
dismissable: true,
414+
buttons: [{
415+
text: dated ? 'Update & Switch' : 'Update' ,
416+
onDidClick: () => {
417+
clearIdeRustInfos()
418+
419+
const updatePromise = exec(`rustup update ${toolchain}`)
420+
// set config in case going from dated -> latest
421+
.then(() => dated && atom.config.set('ide-rust.rlsToolchain', toolchain))
422+
.then(() => this._checkToolchain())
423+
.then(() => checkRls())
424+
.then(() => this._restartLanguageServers(`Updated Rls toolchain`))
425+
.catch(e => {
426+
logErr(e)
427+
e && atom.notifications.addError(`\`rustup update ${toolchain}\` failed`, {
428+
detail: e,
429+
dismissable: true,
430+
})
393431
})
394-
})
395432

396-
if (this.busySignalService) {
397-
this.busySignalService.reportBusyWhile(
398-
`Updating rust \`${toolchain}\` toolchain`,
399-
() => updatePromise
400-
)
401-
}
402-
}
403-
}]
404-
})
433+
if (this.busySignalService) {
434+
this.busySignalService.reportBusyWhile(
435+
`Updating rust \`${toolchain}\` toolchain`,
436+
() => updatePromise
437+
)
438+
}
439+
}
440+
}]
441+
})
442+
})
443+
405444
}
406445

407446
/**
@@ -620,9 +659,12 @@ class RustLanguageClient extends AutoLanguageClient {
620659

621660
server.process.on('exit', () => {
622661
delete this.projects[server.projectPath]
662+
this._refreshActiveOverrides()
623663
})
624664

625665
project.sendRlsTomlConfig()
666+
667+
this._refreshActiveOverrides()
626668
}
627669

628670
getGrammarScopes() {

0 commit comments

Comments
 (0)