Skip to content

Commit

Permalink
Correct way to get name of config file from cabal --help
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasabel committed Jan 31, 2024
1 parent 802b61b commit 7ef066b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 33 deletions.
35 changes: 25 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 25 additions & 10 deletions lib/setup-haskell.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "setup-haskell",
"version": "2.3.6",
"version": "2.6.1",
"private": true,
"description": "setup haskell action",
"main": "lib/setup-haskell",
Expand Down
36 changes: 26 additions & 10 deletions src/setup-haskell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,32 @@ async function cabalConfig(): Promise<string> {
silent: true,
listeners: {stdout: append, stderr: append}
});
// The last line of the cabal help text is printing the config file, e.g.:
//
// > You can edit the cabal configuration file to set defaults:
// > <<HOME>>/.cabal/config
//
// So trimming the last line will give us the name of the config file.
//
// Needless to say this is very brittle, but we secure this by a test
// in Cabal's testsuite: https://github.com/haskell/cabal/pull/9614
return out.toString().trim().split('\n').slice(-1)[0].trim();
return configFileFromHelpText(out.toString());
}

// The end of the cabal help text is printing the config file, e.g.:
//
// > You can edit the cabal configuration file to set defaults:
// > <<HOME>>/.cabal/config
// > This file will be generated with sensible defaults if you run 'cabal update'.
//
// The last line here is only printed if the file does not exist yet.
//
// So trimming last following "You can edit..." will give us the name of the config file.
//
// Needless to say this is very brittle, but we secure this by a test
// in Cabal's testsuite: https://github.com/haskell/cabal/pull/9614
//
function configFileFromHelpText(txt: string): string {
const marker = 'You can edit the cabal configuration file to set defaults:';
const lines = txt.split('\n').map(line => line.trim());
const foundIndex = lines.findLastIndex(line => line === marker);

if (foundIndex !== -1 && foundIndex + 1 < lines.length) {
return lines[foundIndex + 1];
} else {
return '';
}
}

export default async function run(
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2021",
"lib": ["ES2021"],
"target": "esnext",
"lib": ["ESNEXT"],
"module": "commonjs",
"incremental": true,
"strict": true,
Expand Down

0 comments on commit 7ef066b

Please sign in to comment.