# Runs on notable pushes to ci-mac, and weekly on master.

# Builds all modules optimised and runs unit/doc/functional tests on mac,
# using GHC 8.6,
# and uploads a binaries artifact.

name: mac CI

on:
  schedule:
  - cron: "0 07 * * 0"  # sunday midnight pacific

  push:
    branches: [ ci-mac ]
    paths:
      - '.github/workflows/mac.yml'
      - 'stack*.yaml'
      - 'hledger-lib/**'
      - 'hledger/**'
      - 'hledger-ui/**'
      - 'hledger-web/**'
      - 'bin/*.hs'
      # ignore changes to example files, though currently some func tests depend on them
      - 'examples/**'
      - '!**.journal'
      - '!**.j'
      - '!**.ledger'
      - '!**.csv'
      # ignore changes to doc source files
      - '!**.m4'
      - '!**.md'
      - '!**.1'
      - '!**.5'
      - '!**.info'
      - '!**.txt'

  workflow_dispatch:

jobs:
  build:
    runs-on: macos-latest
    strategy:
      fail-fast: false
      matrix:
        plan:
        # - { ghc: "86"    , stack: "stack --stack-yaml=stack8.6.yaml" }
        # - { ghc: "88"  , stack: "stack --stack-yaml=stack8.8.yaml" }
        # - { ghc: "810" , stack: "stack --stack-yaml=stack8.10.yaml" }
        # XXX func tests in bin should be run only with GHC 8.10 for now (see shelltest below) (?)
        - { ghc: "90" , stack: "stack --stack-yaml=stack.yaml" }

    steps:

    - name: Check out
      uses: actions/checkout@v2
      # have to fetch everything for git describe for --version
      with: 
        fetch-depth: 0        

    # keep synced in all workflows which do this
    - name: Check commit messages
      # For a PR, the range will be: master..origin/$GITHUB_HEAD_REF
      # For a push it will be: $BEFORE..
      # For a force push, BEFORE is the previous HEAD, and on github (though not locally) this is an "invalid revision range".
      # In this and any case where the range is invalid, we'll just check the last N commits.
      # related: https://stackoverflow.com/questions/64708371/how-to-run-github-workflow-on-every-commit-of-a-push
      env:
        BEFORE: ${{ github.event.before }}
        NUM:    20
      run: |
        RANGE=${BEFORE:-origin/master}..${GITHUB_HEAD_REF:+origin/$GITHUB_HEAD_REF}
        git rev-list --quiet $RANGE \
          && bin/commitlint  $RANGE \
          || ( echo "could not identify commits, checking last $NUM instead:"; bin/commitlint -$NUM )

    - name: Skip remaining CI steps if latest commit message begins with ;
      run: |
        git log -1 --pretty='%s' ${GITHUB_HEAD_REF:+origin/$GITHUB_HEAD_REF} \
        | grep -qE '^ *;' || echo "CONTINUE=true" >> $GITHUB_ENV 

    # things to be cached/restored:

    - name: Cache stack global package db
      id:   stack-global
      uses: actions/cache@v2
      with:
        path: ~/.stack
        key: ${{ runner.os }}-stack-global-after20210110-${{ hashFiles('**.yaml') }}
        restore-keys: |
             ${{ runner.os }}-stack-global-after202110110
      if: env.CONTINUE

    - name: Cache stack-installed programs in ~/.local/bin
      id:   stack-programs
      uses: actions/cache@v2
      with:
        path: ~/.local/bin
        key: ${{ runner.os }}-stack-programs-${{ hashFiles('**.yaml') }}
        restore-keys: |
             ${{ runner.os }}-stack-programs
      if: env.CONTINUE

    - name: Cache .stack-work
      uses: actions/cache@v2
      with:
        path: .stack-work
        key: ${{ runner.os }}-stack-work-${{ hashFiles('**.yaml') }}
        restore-keys: |
             ${{ runner.os }}-stack-work
      if: env.CONTINUE

    - name: Cache hledger-lib/.stack-work
      uses: actions/cache@v2
      with:
        path: hledger-lib/.stack-work
        key: ${{ runner.os }}-hledger-lib-stack-work-${{ hashFiles('hledger-lib/package.yaml') }}
        restore-keys: |
             ${{ runner.os }}-hledger-lib-stack-work
      if: env.CONTINUE

    - name: Cache hledger/.stack-work
      uses: actions/cache@v2
      with:
        path: hledger/.stack-work
        key: ${{ runner.os }}-hledger-stack-work-${{ hashFiles('hledger/package.yaml') }}
        restore-keys: |
             ${{ runner.os }}-hledger-stack-work
      if: env.CONTINUE

    - name: Cache hledger-ui/.stack-work
      uses: actions/cache@v2
      with:
        path: hledger-ui/.stack-work
        key: ${{ runner.os }}-hledger-ui-stack-work-${{ hashFiles('hledger-ui/package.yaml') }}
        restore-keys: |
             ${{ runner.os }}-hledger-ui-stack-work
      if: env.CONTINUE

    - name: Cache hledger-web/.stack-work
      uses: actions/cache@v2
      with:
        path: hledger-web/.stack-work
        key: ${{ runner.os }}-hledger-web-stack-work-${{ hashFiles('hledger-web/package.yaml') }}
        restore-keys: |
             ${{ runner.os }}-hledger-web-stack-work
      if: env.CONTINUE

    # actions:

    - name: Install stack
      # run: |
      #   brew install haskell-stack
      #   $stack --version
      run: |
        mkdir -p ~/.local/bin
        export PATH=~/.local/bin:$PATH        
        # brew install gnu-tar; curl -sL https://get.haskellstack.org/stable/osx-x86_64.tar.gz | gtar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'; chmod a+x ~/.local/bin/stack
        if [[ ! -x ~/.local/bin/stack ]]; then brew install gnu-tar; curl -sL https://get.haskellstack.org/stable/osx-x86_64.tar.gz | gtar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'; chmod a+x ~/.local/bin/stack; fi
        stack --version
      if: env.CONTINUE

    - name: Install GHC
      env:
        stack: ${{ matrix.plan.stack }}
      run: |
        $stack setup --install-ghc
      if: env.CONTINUE

    - name: Install haskell deps
      env:
        stack: ${{ matrix.plan.stack }}
      run: |
        $stack build --test --only-dependencies
      if: env.CONTINUE

    - name: Build hledger and test unit tests, doc tests
      env:
        stack: ${{ matrix.plan.stack }}
      run: |
        $stack install --test --force-dirty --ghc-options=-fforce-recomp --ghc-options=-Werror
        # --ghc-options=-split-sections doesn't work on mac
        #  --pedantic
      if: env.CONTINUE

    - name: Install shelltestrunner
      env:
        stack: ${{ matrix.plan.stack }}
      run: |
        export PATH=~/.local/bin:$PATH
        if [[ ! -x ~/.local/bin/shelltest ]]; then $stack install shelltestrunner-1.9; fi
        shelltest --version
      if: env.CONTINUE

    - name: Test functional tests (excluding addons)
      env:
        stack: ${{ matrix.plan.stack }}
      run: |
        export PATH=~/.local/bin:$PATH
        COLUMNS=80 $stack exec -- shelltest --execdir -j16 hledger/test -x /_ -x /addons # bin
      if: env.CONTINUE

    # artifacts:

    - name: Gather executables
      run: |
        mkdir tmp
        cd tmp
        mkdir hledger
        cd hledger
        cp ~/.local/bin/hledger .
        cp ~/.local/bin/hledger-ui .
        cp ~/.local/bin/hledger-web .
      if: env.CONTINUE

    # XXX intermittent upload failures
    - name: Upload executables artifact
      uses: actions/upload-artifact@v2
      with:
        name: hledger-macos
        path: tmp/hledger
      if: env.CONTINUE
