Skip to content

Commit 7ef066b

Browse files
committed
Correct way to get name of config file from cabal --help
1 parent 802b61b commit 7ef066b

File tree

5 files changed

+79
-33
lines changed

5 files changed

+79
-33
lines changed

dist/index.js

+25-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/setup-haskell.js

+25-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setup-haskell",
3-
"version": "2.3.6",
3+
"version": "2.6.1",
44
"private": true,
55
"description": "setup haskell action",
66
"main": "lib/setup-haskell",

src/setup-haskell.ts

+26-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,32 @@ async function cabalConfig(): Promise<string> {
1616
silent: true,
1717
listeners: {stdout: append, stderr: append}
1818
});
19-
// The last line of the cabal help text is printing the config file, e.g.:
20-
//
21-
// > You can edit the cabal configuration file to set defaults:
22-
// > <<HOME>>/.cabal/config
23-
//
24-
// So trimming the last line will give us the name of the config file.
25-
//
26-
// Needless to say this is very brittle, but we secure this by a test
27-
// in Cabal's testsuite: https://github.com/haskell/cabal/pull/9614
28-
return out.toString().trim().split('\n').slice(-1)[0].trim();
19+
return configFileFromHelpText(out.toString());
20+
}
21+
22+
// The end of the cabal help text is printing the config file, e.g.:
23+
//
24+
// > You can edit the cabal configuration file to set defaults:
25+
// > <<HOME>>/.cabal/config
26+
// > This file will be generated with sensible defaults if you run 'cabal update'.
27+
//
28+
// The last line here is only printed if the file does not exist yet.
29+
//
30+
// So trimming last following "You can edit..." will give us the name of the config file.
31+
//
32+
// Needless to say this is very brittle, but we secure this by a test
33+
// in Cabal's testsuite: https://github.com/haskell/cabal/pull/9614
34+
//
35+
function configFileFromHelpText(txt: string): string {
36+
const marker = 'You can edit the cabal configuration file to set defaults:';
37+
const lines = txt.split('\n').map(line => line.trim());
38+
const foundIndex = lines.findLastIndex(line => line === marker);
39+
40+
if (foundIndex !== -1 && foundIndex + 1 < lines.length) {
41+
return lines[foundIndex + 1];
42+
} else {
43+
return '';
44+
}
2945
}
3046

3147
export default async function run(

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
3-
"target": "es2021",
4-
"lib": ["ES2021"],
3+
"target": "esnext",
4+
"lib": ["ESNEXT"],
55
"module": "commonjs",
66
"incremental": true,
77
"strict": true,

0 commit comments

Comments
 (0)