Skip to content

Commit

Permalink
Implement parse-cabal-file
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Apr 5, 2023
0 parents commit b6d5652
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: parse-cabal-file
on:
pull_request:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@v3
-
run: ghc -Wall -Werror ParseCabalFile.hs

test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- ubuntu-22.04
- ubuntu-20.04
- ubuntu-18.04
- macos-latest
- macos-12
- macos-11
- windows-latest
- windows-2022
- windows-2019
defaults:
run:
shell: bash
steps:
-
uses: actions/checkout@v3
-
name: Write test cabal file
run: |
echo 'name: foo' >> foo.cabal
echo 'version: 12.34' >> foo.cabal
-
name: Run action
uses: ./
id: cabal_file
with:
cabal_file: foo.cabal
-
name: Check action worked
run: |
if [[ "${{ steps.cabal_file.outputs.version }}" != "12.34" ]]; then
echo 'Action failed!' >&2
exit 1
fi
lint:
runs-on: ubuntu-latest
steps:
-
uses: actions/checkout@v3
-
uses: fourmolu/fourmolu-action@v7
with:
pattern: '*.hs'
extra-args: >
--indentation 2
--indent-wheres true
36 changes: 36 additions & 0 deletions ParseCabalFile.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE LambdaCase #-}

import Data.ByteString qualified as BS
import Data.List (intercalate)
import Distribution.Package (packageVersion)
import Distribution.PackageDescription.Parsec (parseGenericPackageDescriptionMaybe)
import Distribution.Version (versionNumbers)
import System.Environment (getArgs)
import System.IO (hPutStrLn, stderr, stdout)

main :: IO ()
main = do
cabalFile <-
getArgs >>= \case
[cabalFile] -> return cabalFile
_ -> errorWithoutStackTrace "Expected exactly one argument: CABAL_FILE"

cabalFileContents <- BS.readFile cabalFile
packageDesc <-
maybe (errorWithoutStackTrace "Could not parse cabal file") return $
parseGenericPackageDescriptionMaybe cabalFileContents

output
[ ("version", intercalate "." . map show . versionNumbers . packageVersion $ packageDesc)
]

output :: [(String, String)] -> IO ()
output = mapM_ (uncurry output')
where
output' key value = do
let kv = key ++ "=" ++ value
-- log to stderr
hPutStrLn stderr $ "Setting: " ++ kv
-- output to stdout into $GITHUB_OUTPUT
hPutStrLn stdout kv
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# `haskell-actions/parse-cabal-file`

GitHub Action: Parse Cabal file

Assumes the runner has GHC installed.

## Inputs

* `cabal_file` (required): The path to a cabal file

## Outputs

* `version`: The version in the Cabal file

## Example

```yaml
jobs:
my-job:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: haskell-actions/parse-cabal-file@v1
id: cabal_file
with:
cabal_file: my-library.cabal

- run: echo ${{ steps.cabal_file.outputs.version }}
```
23 changes: 23 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Parse Cabal File
description: Parse information in a provided cabal file

inputs:
cabal_file:
description: Path to cabal file
required: true

outputs:
version:
description: The version in the cabal file
value: ${{ steps.cabal_file.outputs.version }}

runs:
using: composite
steps:
- run: |
BINDIR=${GITHUB_ACTION_PATH}/bin
mkdir -p ${BINDIR}
ghc ${GITHUB_ACTION_PATH}/ParseCabalFile.hs -outputdir /tmp -o ${BINDIR}/parse-cabal-file
${BINDIR}/parse-cabal-file "${{ inputs.cabal_file }}" >> "${GITHUB_OUTPUT}"
id: cabal_file
shell: bash

0 comments on commit b6d5652

Please sign in to comment.