Skip to content

Commit c5343c2

Browse files
Break out getLatestNightlyFromGhcup
1 parent 65f7251 commit c5343c2

File tree

3 files changed

+96
-48
lines changed

3 files changed

+96
-48
lines changed

dist/index.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13302,20 +13302,7 @@ async function configureOutputs(tool, version, path, os) {
1330213302
// can't put this in resolve() because it might run before ghcup is installed
1330313303
let resolvedVersion = version;
1330413304
if (version === 'latest-nightly') {
13305-
try {
13306-
const ghcupExe = await ghcupBin(os);
13307-
const out = await new Promise((resolve, reject) => child_process.execFile(ghcupExe, ['list', '-nr'], { encoding: 'utf-8' }, (error, stdout) => (error ? reject(error) : resolve(stdout))));
13308-
resolvedVersion =
13309-
out
13310-
.split('\n')
13311-
.map(line => line.split(' '))
13312-
.filter(line => line[2] === 'latest-nightly')
13313-
.map(line => line[1])
13314-
.at(0) ?? version;
13315-
}
13316-
catch (error) {
13317-
// swallow failures, just leave version as 'latest-nightly'
13318-
}
13305+
resolvedVersion = (await getLatestNightlyFromGhcup(os)) ?? version;
1331913306
}
1332013307
core.setOutput(`${tool}-version`, resolvedVersion);
1332113308
}
@@ -13548,6 +13535,34 @@ async function addGhcupReleaseChannel(channel, os) {
1354813535
await exec(bin, ['config', 'add-release-channel', channel.toString()]);
1354913536
}
1355013537
exports.addGhcupReleaseChannel = addGhcupReleaseChannel;
13538+
/**
13539+
* Get the resolved version of the `latest-nightly` GHC tag from ghcup,
13540+
* e.g. '9.9.20230711'
13541+
*/
13542+
async function getLatestNightlyFromGhcup(os) {
13543+
try {
13544+
const ghcupExe = await ghcupBin(os);
13545+
/* Example output:
13546+
13547+
ghc 9.0.2 base-4.15.1.0
13548+
ghc 9.7.20230526 nightly 2023-06-27
13549+
ghc 9.9.20230711 latest-nightly 2023-07-12
13550+
cabal 2.4.1.0 no-bindist
13551+
cabal 3.6.2.0 recommended
13552+
*/
13553+
const out = await new Promise((resolve, reject) => child_process.execFile(ghcupExe, ['list', '-nr'], { encoding: 'utf-8' }, (error, stdout) => (error ? reject(error) : resolve(stdout))));
13554+
return (out
13555+
.split('\n')
13556+
.map(line => line.split(' '))
13557+
.filter(line => line[2] === 'latest-nightly')
13558+
.map(line => line[1])
13559+
.at(0) ?? null);
13560+
}
13561+
catch (error) {
13562+
// swallow failures, just return null
13563+
return null;
13564+
}
13565+
}
1355113566
async function ghcup(tool, version, os) {
1355213567
core.info(`Attempting to install ${tool} ${version} using ghcup`);
1355313568
const bin = await ghcupBin(os);

lib/installer.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,7 @@ async function configureOutputs(tool, version, path, os) {
5757
// can't put this in resolve() because it might run before ghcup is installed
5858
let resolvedVersion = version;
5959
if (version === 'latest-nightly') {
60-
try {
61-
const ghcupExe = await ghcupBin(os);
62-
const out = await new Promise((resolve, reject) => child_process.execFile(ghcupExe, ['list', '-nr'], { encoding: 'utf-8' }, (error, stdout) => (error ? reject(error) : resolve(stdout))));
63-
resolvedVersion =
64-
out
65-
.split('\n')
66-
.map(line => line.split(' '))
67-
.filter(line => line[2] === 'latest-nightly')
68-
.map(line => line[1])
69-
.at(0) ?? version;
70-
}
71-
catch (error) {
72-
// swallow failures, just leave version as 'latest-nightly'
73-
}
60+
resolvedVersion = (await getLatestNightlyFromGhcup(os)) ?? version;
7461
}
7562
core.setOutput(`${tool}-version`, resolvedVersion);
7663
}
@@ -303,6 +290,34 @@ async function addGhcupReleaseChannel(channel, os) {
303290
await exec(bin, ['config', 'add-release-channel', channel.toString()]);
304291
}
305292
exports.addGhcupReleaseChannel = addGhcupReleaseChannel;
293+
/**
294+
* Get the resolved version of the `latest-nightly` GHC tag from ghcup,
295+
* e.g. '9.9.20230711'
296+
*/
297+
async function getLatestNightlyFromGhcup(os) {
298+
try {
299+
const ghcupExe = await ghcupBin(os);
300+
/* Example output:
301+
302+
ghc 9.0.2 base-4.15.1.0
303+
ghc 9.7.20230526 nightly 2023-06-27
304+
ghc 9.9.20230711 latest-nightly 2023-07-12
305+
cabal 2.4.1.0 no-bindist
306+
cabal 3.6.2.0 recommended
307+
*/
308+
const out = await new Promise((resolve, reject) => child_process.execFile(ghcupExe, ['list', '-nr'], { encoding: 'utf-8' }, (error, stdout) => (error ? reject(error) : resolve(stdout))));
309+
return (out
310+
.split('\n')
311+
.map(line => line.split(' '))
312+
.filter(line => line[2] === 'latest-nightly')
313+
.map(line => line[1])
314+
.at(0) ?? null);
315+
}
316+
catch (error) {
317+
// swallow failures, just return null
318+
return null;
319+
}
320+
}
306321
async function ghcup(tool, version, os) {
307322
core.info(`Attempting to install ${tool} ${version} using ghcup`);
308323
const bin = await ghcupBin(os);

src/installer.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,7 @@ async function configureOutputs(
3838
// can't put this in resolve() because it might run before ghcup is installed
3939
let resolvedVersion = version;
4040
if (version === 'latest-nightly') {
41-
try {
42-
const ghcupExe = await ghcupBin(os);
43-
const out = await new Promise<string>((resolve, reject) =>
44-
child_process.execFile(
45-
ghcupExe,
46-
['list', '-nr'],
47-
{encoding: 'utf-8'},
48-
(error, stdout) => (error ? reject(error) : resolve(stdout))
49-
)
50-
);
51-
resolvedVersion =
52-
out
53-
.split('\n')
54-
.map(line => line.split(' '))
55-
.filter(line => line[2] === 'latest-nightly')
56-
.map(line => line[1])
57-
.at(0) ?? version;
58-
} catch (error) {
59-
// swallow failures, just leave version as 'latest-nightly'
60-
}
41+
resolvedVersion = (await getLatestNightlyFromGhcup(os)) ?? version;
6142
}
6243
core.setOutput(`${tool}-version`, resolvedVersion);
6344
}
@@ -351,6 +332,43 @@ export async function addGhcupReleaseChannel(
351332
await exec(bin, ['config', 'add-release-channel', channel.toString()]);
352333
}
353334

335+
/**
336+
* Get the resolved version of the `latest-nightly` GHC tag from ghcup,
337+
* e.g. '9.9.20230711'
338+
*/
339+
async function getLatestNightlyFromGhcup(os: OS): Promise<string | null> {
340+
try {
341+
const ghcupExe = await ghcupBin(os);
342+
/* Example output:
343+
344+
ghc 9.0.2 base-4.15.1.0
345+
ghc 9.7.20230526 nightly 2023-06-27
346+
ghc 9.9.20230711 latest-nightly 2023-07-12
347+
cabal 2.4.1.0 no-bindist
348+
cabal 3.6.2.0 recommended
349+
*/
350+
const out = await new Promise<string>((resolve, reject) =>
351+
child_process.execFile(
352+
ghcupExe,
353+
['list', '-nr'],
354+
{encoding: 'utf-8'},
355+
(error, stdout) => (error ? reject(error) : resolve(stdout))
356+
)
357+
);
358+
return (
359+
out
360+
.split('\n')
361+
.map(line => line.split(' '))
362+
.filter(line => line[2] === 'latest-nightly')
363+
.map(line => line[1])
364+
.at(0) ?? null
365+
);
366+
} catch (error) {
367+
// swallow failures, just return null
368+
return null;
369+
}
370+
}
371+
354372
async function ghcup(tool: Tool, version: string, os: OS): Promise<void> {
355373
core.info(`Attempting to install ${tool} ${version} using ghcup`);
356374
const bin = await ghcupBin(os);

0 commit comments

Comments
 (0)