Skip to content

Commit b6d5652

Browse files
Implement parse-cabal-file
0 parents  commit b6d5652

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

.github/workflows/test.yml

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: parse-cabal-file
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
-
13+
uses: actions/checkout@v3
14+
-
15+
run: ghc -Wall -Werror ParseCabalFile.hs
16+
17+
test:
18+
runs-on: ${{ matrix.os }}
19+
strategy:
20+
matrix:
21+
os:
22+
- ubuntu-latest
23+
- ubuntu-22.04
24+
- ubuntu-20.04
25+
- ubuntu-18.04
26+
- macos-latest
27+
- macos-12
28+
- macos-11
29+
- windows-latest
30+
- windows-2022
31+
- windows-2019
32+
defaults:
33+
run:
34+
shell: bash
35+
steps:
36+
-
37+
uses: actions/checkout@v3
38+
-
39+
name: Write test cabal file
40+
run: |
41+
echo 'name: foo' >> foo.cabal
42+
echo 'version: 12.34' >> foo.cabal
43+
-
44+
name: Run action
45+
uses: ./
46+
id: cabal_file
47+
with:
48+
cabal_file: foo.cabal
49+
-
50+
name: Check action worked
51+
run: |
52+
if [[ "${{ steps.cabal_file.outputs.version }}" != "12.34" ]]; then
53+
echo 'Action failed!' >&2
54+
exit 1
55+
fi
56+
57+
lint:
58+
runs-on: ubuntu-latest
59+
steps:
60+
-
61+
uses: actions/checkout@v3
62+
-
63+
uses: fourmolu/fourmolu-action@v7
64+
with:
65+
pattern: '*.hs'
66+
extra-args: >
67+
--indentation 2
68+
--indent-wheres true

ParseCabalFile.hs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{-# LANGUAGE ImportQualifiedPost #-}
2+
{-# LANGUAGE LambdaCase #-}
3+
4+
import Data.ByteString qualified as BS
5+
import Data.List (intercalate)
6+
import Distribution.Package (packageVersion)
7+
import Distribution.PackageDescription.Parsec (parseGenericPackageDescriptionMaybe)
8+
import Distribution.Version (versionNumbers)
9+
import System.Environment (getArgs)
10+
import System.IO (hPutStrLn, stderr, stdout)
11+
12+
main :: IO ()
13+
main = do
14+
cabalFile <-
15+
getArgs >>= \case
16+
[cabalFile] -> return cabalFile
17+
_ -> errorWithoutStackTrace "Expected exactly one argument: CABAL_FILE"
18+
19+
cabalFileContents <- BS.readFile cabalFile
20+
packageDesc <-
21+
maybe (errorWithoutStackTrace "Could not parse cabal file") return $
22+
parseGenericPackageDescriptionMaybe cabalFileContents
23+
24+
output
25+
[ ("version", intercalate "." . map show . versionNumbers . packageVersion $ packageDesc)
26+
]
27+
28+
output :: [(String, String)] -> IO ()
29+
output = mapM_ (uncurry output')
30+
where
31+
output' key value = do
32+
let kv = key ++ "=" ++ value
33+
-- log to stderr
34+
hPutStrLn stderr $ "Setting: " ++ kv
35+
-- output to stdout into $GITHUB_OUTPUT
36+
hPutStrLn stdout kv

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# `haskell-actions/parse-cabal-file`
2+
3+
GitHub Action: Parse Cabal file
4+
5+
Assumes the runner has GHC installed.
6+
7+
## Inputs
8+
9+
* `cabal_file` (required): The path to a cabal file
10+
11+
## Outputs
12+
13+
* `version`: The version in the Cabal file
14+
15+
## Example
16+
17+
```yaml
18+
jobs:
19+
my-job:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v3
23+
24+
- uses: haskell-actions/parse-cabal-file@v1
25+
id: cabal_file
26+
with:
27+
cabal_file: my-library.cabal
28+
29+
- run: echo ${{ steps.cabal_file.outputs.version }}
30+
```

action.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Parse Cabal File
2+
description: Parse information in a provided cabal file
3+
4+
inputs:
5+
cabal_file:
6+
description: Path to cabal file
7+
required: true
8+
9+
outputs:
10+
version:
11+
description: The version in the cabal file
12+
value: ${{ steps.cabal_file.outputs.version }}
13+
14+
runs:
15+
using: composite
16+
steps:
17+
- run: |
18+
BINDIR=${GITHUB_ACTION_PATH}/bin
19+
mkdir -p ${BINDIR}
20+
ghc ${GITHUB_ACTION_PATH}/ParseCabalFile.hs -outputdir /tmp -o ${BINDIR}/parse-cabal-file
21+
${BINDIR}/parse-cabal-file "${{ inputs.cabal_file }}" >> "${GITHUB_OUTPUT}"
22+
id: cabal_file
23+
shell: bash

0 commit comments

Comments
 (0)