Skip to content

Commit 5687141

Browse files
committed
Refactor toInstall shenanigans
1 parent a910132 commit 5687141

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

Diff for: src/hlsBinaries.ts

+23-26
Original file line numberDiff line numberDiff line change
@@ -277,36 +277,34 @@ 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+
: (recGHC !== null ? await toolInstalled(context, logger, 'ghc', recGHC) : undefined);
291+
const toInstall: InstalledTool[] = [hlsInstalled, cabalInstalled, stackInstalled, ghcInstalled]
292+
.filter((tool) => tool && !tool.installed) as InstalledTool[];
295293
if (toInstall.length > 0) {
296294
const decision = await window.showInformationMessage(
297-
`Need to download ${toInstall.join(', ')}, continue?`,
295+
`Need to download ${toInstall.map(t => t.nameWithVersion).join(', ')}, continue?`,
298296
'Yes',
299297
'No',
300298
"Yes, don't ask again"
301299
);
302300
if (decision === 'Yes') {
303-
logger.info(`User accepted download for ${toInstall.join(', ')}.`);
301+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')}.`);
304302
} else if (decision === "Yes, don't ask again") {
305-
logger.info(`User accepted download for ${toInstall.join(', ')} and won't be asked again.`);
303+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')} and won't be asked again.`);
306304
workspace.getConfiguration('haskell').update('promptBeforeDownloads', false);
307305
} else {
308-
[hlsInstalled, cabalInstalled, stackInstalled, ghcInstalled].forEach((tool) => {
309-
if (!tool.installed) {
306+
toInstall.forEach((tool) => {
307+
if (tool !== undefined && !tool.installed) {
310308
if (tool.name === 'hls') {
311309
throw new MissingToolError('hls');
312310
} else if (tool.name === 'cabal') {
@@ -357,28 +355,27 @@ export async function findHaskellLanguageServer(
357355
if (promptBeforeDownloads) {
358356
const hlsInstalled = projectHls
359357
? await toolInstalled(context, logger, 'hls', projectHls)
360-
: new InstalledTool('hls');
358+
: undefined;
361359
const ghcInstalled = projectGhc
362360
? 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);
361+
: undefined;
362+
const toInstall: InstalledTool[] = [hlsInstalled, ghcInstalled]
363+
.filter((tool) => tool && !tool.installed) as InstalledTool[];
367364
if (toInstall.length > 0) {
368365
const decision = await window.showInformationMessage(
369-
`Need to download ${toInstall.join(', ')}, continue?`,
366+
`Need to download ${toInstall.map(t => t.nameWithVersion).join(', ')}, continue?`,
370367
{ modal: true },
371368
'Yes',
372369
'No',
373370
"Yes, don't ask again"
374371
);
375372
if (decision === 'Yes') {
376-
logger.info(`User accepted download for ${toInstall.join(', ')}.`);
373+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')}.`);
377374
} else if (decision === "Yes, don't ask again") {
378-
logger.info(`User accepted download for ${toInstall.join(', ')} and won't be asked again.`);
375+
logger.info(`User accepted download for ${toInstall.map(t => t.nameWithVersion).join(', ')} and won't be asked again.`);
379376
workspace.getConfiguration('haskell').update('promptBeforeDownloads', false);
380377
} else {
381-
[hlsInstalled, ghcInstalled].forEach((tool) => {
378+
toInstall.forEach((tool) => {
382379
if (!tool.installed) {
383380
if (tool.name === 'hls') {
384381
throw new MissingToolError('hls');
@@ -403,7 +400,7 @@ export async function findHaskellLanguageServer(
403400
...(projectGhc ? ['--ghc', projectGhc] : []),
404401
'--install',
405402
],
406-
`Installing project specific toolchain: HLS-${projectHls}, GHC-${projectGhc}, cabal-${latestCabal}, stack-${latestStack}`,
403+
`Installing project specific toolchain: ${[['hls', projectHls], ['GHC', projectGhc], ['cabal', latestCabal], ['stack', latestStack]].filter(t => t[1]).map(t => `${t[0]}-${t[1]}`).join(', ')}`,
407404
true
408405
);
409406

0 commit comments

Comments
 (0)