Skip to content

Commit cafcc80

Browse files
hasufellfendor
authored andcommitted
Factor out callGHCup
1 parent 68451d3 commit cafcc80

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/hlsBinaries.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ export async function downloadHaskellLanguageServer(
183183
wrapper = downloadedWrapper;
184184
}
185185

186-
const ghcup = path.join(storagePath, `ghcup${exeExt}`);
187186
const updateBehaviour = workspace.getConfiguration('haskell').get('updateBehavior') as UpdateBehaviour;
188187
const [installableHls, latestHlsVersion, projectGhc] = await getLatestSuitableHLS(
189188
context,
@@ -208,18 +207,14 @@ export async function downloadHaskellLanguageServer(
208207
throw new Error('No version of HLS installed or found and installation was denied, giving up...');
209208
}
210209
}
211-
await callAsync(
212-
ghcup,
213-
['--no-verbose', 'install', 'hls', installableHls],
214-
storagePath,
210+
await callGHCup(
211+
context,
215212
logger,
213+
['install', 'hls', installableHls],
216214
`Installing HLS ${installableHls}`,
217215
true,
218-
{ GHCUP_INSTALL_BASE_PREFIX: storagePath }
219216
);
220-
await callAsync(ghcup, ['--no-verbose', 'set', 'hls', installableHls], storagePath, logger, undefined, false, {
221-
GHCUP_INSTALL_BASE_PREFIX: storagePath,
222-
});
217+
await callGHCup(context, logger, ['set', 'hls', installableHls], undefined, false);
223218
return downloadedWrapper;
224219
} else {
225220
// version of active hls wrapper
@@ -273,39 +268,46 @@ export async function downloadHaskellLanguageServer(
273268
// isolated symlinked dir with only the given HLS in place, so
274269
// this works for installing and setting
275270
const symHLSPath = path.join(storagePath, 'hls', installableHls);
276-
await callAsync(
277-
ghcup,
278-
['--no-verbose', 'run', '--hls', installableHls, '-b', symHLSPath, '-i'],
279-
storagePath,
280-
logger,
271+
await callGHCup(context, logger,
272+
['run', '--hls', installableHls, '-b', symHLSPath, '-i'],
281273
needInstall ? `Installing HLS ${installableHls}` : undefined,
282-
needInstall,
283-
{ GHCUP_INSTALL_BASE_PREFIX: storagePath }
274+
needInstall
284275
);
285276
return path.join(symHLSPath, `haskell-language-server-wrapper${exeExt}`);
286277
}
287278
return wrapper;
288279
}
289280
}
290281

282+
async function callGHCup(
283+
context: ExtensionContext,
284+
logger: Logger,
285+
args: string[],
286+
title?: string,
287+
cancellable?: boolean
288+
): Promise<string> {
289+
const storagePath: string = await getStoragePath(context);
290+
const ghcup = path.join(storagePath, `ghcup${exeExt}`);
291+
return await callAsync(ghcup, ['--no-verbose'].concat(args), storagePath, logger, title, cancellable, {
292+
GHCUP_INSTALL_BASE_PREFIX: storagePath,
293+
});
294+
}
295+
291296
async function getLatestSuitableHLS(
292297
context: ExtensionContext,
293298
logger: Logger,
294299
workingDir: string,
295300
wrapper?: string
296301
): Promise<[string, string, string | null]> {
297302
const storagePath: string = await getStoragePath(context);
298-
const ghcup = path.join(storagePath, `ghcup${exeExt}`);
299303

300304
// get latest hls version
301-
const hlsVersions = await callAsync(
302-
ghcup,
303-
['--no-verbose', 'list', '-t', 'hls', '-c', 'available', '-r'],
304-
storagePath,
305-
logger,
305+
const hlsVersions = await callGHCup(
306+
context,
307+
logger,
308+
['list', '-t', 'hls', '-c', 'available', '-r'],
306309
undefined,
307310
false,
308-
{ GHCUP_INSTALL_BASE_PREFIX: storagePath }
309311
);
310312
const latestHlsVersion = hlsVersions.split(/\r?\n/).pop()!.split(' ')[1];
311313

@@ -401,8 +403,8 @@ export async function downloadGHCup(context: ExtensionContext, logger: Logger):
401403
// ghcup exists, just upgrade
402404
if (fs.existsSync(ghcup)) {
403405
logger.info('ghcup already installed, trying to upgrade');
404-
const args = ['--no-verbose', 'upgrade', '-i'];
405-
await callAsync(ghcup, args, storagePath, logger, undefined, false, { GHCUP_INSTALL_BASE_PREFIX: storagePath });
406+
const args = ['upgrade', '-i'];
407+
await callGHCup(context, logger, args, undefined, false);
406408
} else {
407409
// needs to download ghcup
408410
const plat = match(process.platform)

0 commit comments

Comments
 (0)