Skip to content

Commit b34e8f3

Browse files
committed
Refactor toInstall shenanigans
1 parent a910132 commit b34e8f3

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/hlsBinaries.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -277,36 +277,35 @@ export async function findHaskellLanguageServer(
277277
if (promptBeforeDownloads) {
278278
const hlsInstalled = latestHLS
279279
? await toolInstalled(context, logger, 'hls', latestHLS)
280-
: new InstalledTool('hls');
280+
: undefined;
281281
const cabalInstalled = latestCabal
282282
? await toolInstalled(context, logger, 'cabal', latestCabal)
283-
: new InstalledTool('cabal');
283+
: undefined;
284284
const stackInstalled = latestStack
285285
? await toolInstalled(context, logger, 'stack', latestStack)
286-
: new InstalledTool('stack');
287-
const ghcInstalled = (await executableExists('ghc'))
288-
? new InstalledTool('ghc')
289-
// if recGHC is null, that means user disabled automatic handling,
290-
// so we pretend it's installed in order to ignore it
291-
: (recGHC !== null ? await toolInstalled(context, logger, 'ghc', recGHC) : new InstalledTool('ghc'));
292-
const toInstall = [hlsInstalled, cabalInstalled, stackInstalled, ghcInstalled]
293-
.filter((tool) => !tool.installed)
294-
.map((tool) => tool.nameWithVersion);
286+
: undefined;
287+
const ghcInstalled = executableExists('ghc')
288+
? new InstalledTool('ghc', await callAsync(`ghc${exeExt}`, ['--numeric-version'], logger, undefined, undefined, false))
289+
// if recGHC is null, that means user disabled automatic handling,
290+
// so we pretend it's installed in order to ignore it
291+
: (recGHC !== null ? await toolInstalled(context, logger, 'ghc', recGHC) : undefined);
292+
const toInstall: InstalledTool[] = [hlsInstalled, cabalInstalled, stackInstalled, ghcInstalled]
293+
.filter((tool) => tool && !tool.installed) as InstalledTool[];
295294
if (toInstall.length > 0) {
296295
const decision = await window.showInformationMessage(
297-
`Need to download ${toInstall.join(', ')}, continue?`,
296+
`Need to download ${toInstall.map(t => t.nameWithVersion).join(', ')}, continue?`,
298297
'Yes',
299298
'No',
300299
"Yes, don't ask again"
301300
);
302301
if (decision === 'Yes') {
303-
logger.info(`User accepted download for ${toInstall.join(', ')}.`);
302+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')}.`);
304303
} else if (decision === "Yes, don't ask again") {
305-
logger.info(`User accepted download for ${toInstall.join(', ')} and won't be asked again.`);
304+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')} and won't be asked again.`);
306305
workspace.getConfiguration('haskell').update('promptBeforeDownloads', false);
307306
} else {
308-
[hlsInstalled, cabalInstalled, stackInstalled, ghcInstalled].forEach((tool) => {
309-
if (!tool.installed) {
307+
toInstall.forEach((tool) => {
308+
if (tool !== undefined && !tool.installed) {
310309
if (tool.name === 'hls') {
311310
throw new MissingToolError('hls');
312311
} else if (tool.name === 'cabal') {
@@ -357,28 +356,27 @@ export async function findHaskellLanguageServer(
357356
if (promptBeforeDownloads) {
358357
const hlsInstalled = projectHls
359358
? await toolInstalled(context, logger, 'hls', projectHls)
360-
: new InstalledTool('hls');
359+
: undefined;
361360
const ghcInstalled = projectGhc
362361
? await toolInstalled(context, logger, 'ghc', projectGhc)
363-
: new InstalledTool('ghc');
364-
const toInstall = [hlsInstalled, ghcInstalled]
365-
.filter((tool) => !tool.installed)
366-
.map((tool) => tool.nameWithVersion);
362+
: undefined;
363+
const toInstall: InstalledTool[] = [hlsInstalled, ghcInstalled]
364+
.filter((tool) => tool && !tool.installed) as InstalledTool[];
367365
if (toInstall.length > 0) {
368366
const decision = await window.showInformationMessage(
369-
`Need to download ${toInstall.join(', ')}, continue?`,
367+
`Need to download ${toInstall.map(t => t.nameWithVersion).join(', ')}, continue?`,
370368
{ modal: true },
371369
'Yes',
372370
'No',
373371
"Yes, don't ask again"
374372
);
375373
if (decision === 'Yes') {
376-
logger.info(`User accepted download for ${toInstall.join(', ')}.`);
374+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')}.`);
377375
} else if (decision === "Yes, don't ask again") {
378-
logger.info(`User accepted download for ${toInstall.join(', ')} and won't be asked again.`);
376+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')} and won't be asked again.`);
379377
workspace.getConfiguration('haskell').update('promptBeforeDownloads', false);
380378
} else {
381-
[hlsInstalled, ghcInstalled].forEach((tool) => {
379+
toInstall.forEach((tool) => {
382380
if (!tool.installed) {
383381
if (tool.name === 'hls') {
384382
throw new MissingToolError('hls');
@@ -403,7 +401,7 @@ export async function findHaskellLanguageServer(
403401
...(projectGhc ? ['--ghc', projectGhc] : []),
404402
'--install',
405403
],
406-
`Installing project specific toolchain: HLS-${projectHls}, GHC-${projectGhc}, cabal-${latestCabal}, stack-${latestStack}`,
404+
`Installing project specific toolchain: ${[['hls', projectHls], ['GHC', projectGhc], ['cabal', latestCabal], ['stack', latestStack]].filter(t => t[1]).map(t => `${t[0]}-${t[1]}`).join(', ')}`,
407405
true
408406
);
409407

0 commit comments

Comments
 (0)