Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way to get name of config file from cabal --help #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading