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

Commit 63dd0f7

Browse files
committed
Refactor checkRls split & async/await
1 parent 82083fb commit 63dd0f7

File tree

1 file changed

+59
-66
lines changed

1 file changed

+59
-66
lines changed

lib/index.js

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -125,80 +125,74 @@ async function serverEnv(toolchain) {
125125
return env
126126
}
127127

128+
/**
129+
* Install rls-preview, rust-src, rust-analysis
130+
* @param {string} toolchain
131+
*/
132+
async function installRlsComponents(toolchain) {
133+
try {
134+
await exec(`rustup component add rls-preview --toolchain ${toolchain}`)
135+
}
136+
catch (e) {
137+
let suggestedVersion
138+
if (toolchain.startsWith('nightly')) {
139+
// 'rls-preview' not available search for a decent suggestion
140+
suggestedVersion = await suggestChannelOrDated('nightly').catch(e => console.warn(e))
141+
}
142+
const note = {
143+
detail: 'Try configuring another toolchain, like a dated nightly or `beta`',
144+
dismissable: true,
145+
_src: 'ide-rust',
146+
buttons: [{
147+
text: 'Configure',
148+
onDidClick: () => atom.workspace.open('atom://config/packages/ide-rust')
149+
}]
150+
}
151+
if (suggestedVersion) {
152+
note.buttons.push({
153+
text: `Use ${suggestedVersion}`,
154+
onDidClick: () => atom.config.set('ide-rust.rlsToolchain', suggestedVersion)
155+
})
156+
}
157+
atom.notifications.addError(`\`rls-preview\` was not found on \`${toolchain}\``, note)
158+
throw e
159+
}
160+
161+
try {
162+
await exec(`rustup component add rust-src --toolchain ${toolchain}`)
163+
await exec(`rustup component add rust-analysis --toolchain ${toolchain}`)
164+
}
165+
catch (e) {
166+
atom.notifications.addError(`\`rust-src\`/\`rust-analysis\` not found on \`${toolchain}\``, {
167+
dismissable: true
168+
})
169+
throw e
170+
}
171+
}
172+
128173
// ongoing promise
129174
let _checkingRls
130175

131176
/**
132177
* Check for and install Rls
133-
* @param {BusySignalService} [busySignalService]
178+
* @param {?BusySignalService} busySignalService
134179
* @return {Promise<*>} rls installed
135180
*/
136-
function checkRls(busySignalService) {
137-
let toolchain = configToolchain()
138-
181+
async function checkRls(busySignalService) {
139182
if (_checkingRls) return _checkingRls
140183

141-
_checkingRls = exec(`rustup component list --toolchain ${toolchain}`).then(results => {
142-
const { stdout } = results
184+
const toolchain = configToolchain()
185+
_checkingRls = (async () => {
186+
let { stdout: toolchainList } = await exec(`rustup component list --toolchain ${toolchain}`)
143187
if (
144-
stdout.search(/^rls-preview.* \((default|installed)\)$/m) >= 0 &&
145-
stdout.search(/^rust-analysis.* \((default|installed)\)$/m) >= 0 &&
146-
stdout.search(/^rust-src.* \((default|installed)\)$/m) >= 0
188+
toolchainList.search(/^rls-preview.* \((default|installed)\)$/m) >= 0 &&
189+
toolchainList.search(/^rust-analysis.* \((default|installed)\)$/m) >= 0 &&
190+
toolchainList.search(/^rust-src.* \((default|installed)\)$/m) >= 0
147191
) {
148-
// Have RLS
149-
return
192+
return // have rls
150193
}
151-
152-
// Don't have RLS
153-
let installRlsPromise = exec(`rustup component add rls-preview --toolchain ${toolchain}`)
154-
.catch(async e => {
155-
if (toolchain.startsWith('nightly')) {
156-
// 'rls-preview' not available search for a decent suggestion
157-
let suggestedVersion
158-
try {
159-
suggestedVersion = await suggestChannelOrDated('nightly')
160-
} catch (e) {
161-
console.warn(e)
162-
}
163-
if (suggestedVersion) throw [e, suggestedVersion]
164-
}
165-
throw [e]
166-
})
167-
.catch(e => {
168-
let latestRlsNightly = e[1]
169-
170-
const note = {
171-
detail: 'Try configuring another toolchain, like a dated nightly or `beta`',
172-
dismissable: true,
173-
_src: 'ide-rust',
174-
buttons: [{
175-
text: 'Configure',
176-
onDidClick: () => atom.workspace.open('atom://config/packages/ide-rust')
177-
}]
178-
}
179-
if (latestRlsNightly) {
180-
note.buttons.push({
181-
text: `Use ${latestRlsNightly}`,
182-
onDidClick: () => atom.config.set('ide-rust.rlsToolchain', latestRlsNightly)
183-
})
184-
}
185-
186-
atom.notifications.addError(`\`rls-preview\` was not found on \`${toolchain}\``, note)
187-
188-
e[0]._logged = true
189-
throw e[0]
190-
})
191-
.then(() => exec(`rustup component add rust-src --toolchain ${toolchain}`))
192-
.then(() => exec(`rustup component add rust-analysis --toolchain ${toolchain}`))
193-
.catch(e => {
194-
if (!e._logged) {
195-
atom.notifications.addError(`\`rust-src\`/\`rust-analysis\` not found on \`${toolchain}\``, {
196-
dismissable: true
197-
})
198-
}
199-
throw e
200-
})
201-
194+
// try to install rls
195+
const installRlsPromise = installRlsComponents(toolchain)
202196
if (busySignalService) {
203197
busySignalService.reportBusyWhile(
204198
`Adding components rls-preview, rust-src, rust-analysis`,
@@ -207,14 +201,13 @@ function checkRls(busySignalService) {
207201
}
208202

209203
return installRlsPromise
210-
})
204+
})()
211205

212206
try {
213-
return _checkingRls
207+
return await _checkingRls
214208
}
215209
finally {
216-
let clearOngoing = () => _checkingRls = null
217-
_checkingRls.then(clearOngoing).catch(clearOngoing)
210+
_checkingRls = null
218211
}
219212
}
220213

0 commit comments

Comments
 (0)