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

# Builds all modules optimised on windows,
# and uploads a binaries artifact.

name: windows CI

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

  push:
    branches: [ ci-windows ]
    paths:
      - '.github/workflows/windows.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: windows-latest
    # defaults:
    #   run:
    #     shell: bash
    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
      shell: bash
      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 ;
      shell: bash
      run: |
        echo "git log -1 --pretty='%s' ${GITHUB_HEAD_REF:+origin/$GITHUB_HEAD_REF} >> $$.gitlog"
        (git log -1 --pretty='%s' ${GITHUB_HEAD_REF:+origin/$GITHUB_HEAD_REF} >> $$.gitlog \
          && (grep -qE '^ *;' $$.gitlog || echo "CONTINUE=true" >> $GITHUB_ENV)) \
          || ( echo "could not identify commit range, continuing CI steps"; echo "CONTINUE=true" >> $GITHUB_ENV )

    # things to be cached/restored:

    - name: Cache stack global package db
      id:   stack-global-package-db
      uses: actions/cache@v2
      with:
        path: C:\Users\runneradmin\AppData\Roaming\stack\
        key: ${{ runner.os }}-appdata-roaming-stack-${{ hashFiles('**.yaml') }}
        restore-keys: |
             ${{ runner.os }}-appdata-roaming-stack
      if: env.CONTINUE

    - name: Cache stack programs dir   # ghc, ghc-included packages and their haddocks, mingw, msys2
      id:   stack-programs-dir
      uses: actions/cache@v2
      with:
        path: C:\Users\runneradmin\AppData\Local\Programs\stack\
        # which files signal a change in stack's global db ?
        # **.yaml includes */package.yaml and stack.yaml* (too many), and hopefully no other changing yamls
        key: ${{ runner.os }}-appdata-local-programs-stack-${{ hashFiles('**.yaml') }}
        restore-keys: |
             ${{ runner.os }}-appdata-local-programs-stack
      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

    # - name: showStuff
    #   shell: bash
    #   run: |
    #     ls -lFRa /c/users/runneradmin/appdata/roaming/stack
    #     ls -lFRa /c/users/runneradmin/appdata/local/programs/stack

    # actions:

    # - name: Add stack local bin to PATH
    #   run: echo "::add-path::C:\Users\runneradmin\AppData\Roaming\stack\local\bin"

    # - name: Install stack
    #   shell: bash
    #   run: |
    #     mkdir -p ~/.local/bin
    #     export PATH=~/.local/bin:$PATH
    #     if [[ ! -x ~/.local/bin/stack ]]; then curl -sL https://get.haskellstack.org/stable/linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'; chmod a+x ~/.local/bin/stack; fi
    #     $stack --version
    #   env:
    #     stack: ${{ matrix.plan.stack }}

    - name: Install stack
      #if: steps.stack-programs-dir.outputs.cache-hit != 'true'
      # this step is needed to get stack.exe into PATH, for now
      run: |
        curl -sL https://get.haskellstack.org/stable/windows-x86_64.zip -o stack.zip
        7z x stack.zip stack.exe
        which stack
        stack --version
        which ./stack
        ./stack --version
      if: env.CONTINUE

    - name: Install GHC
      # if: steps.stack-programs-dir.outputs.cache-hit != 'true'
      # set PATH=C:\Users\runneradmin\AppData\Local\Programs\stack\local\bin;%PATH%
      run: |
        ./stack --no-terminal setup --install-ghc
      if: env.CONTINUE

    # - name: Install shelltestrunner
    ## - export PATH=~/.local/bin:$PATH
    # - if [[ ! -x ~/.local/bin/shelltest ]]; then stack install shelltestrunner-1.9; fi
    # - shelltest --version

    - name: Install haskell deps
      run: |
        ./stack --no-terminal build --only-dependencies
      if: env.CONTINUE

    # use whichever GHC is in default stack.yaml

    - name: Build all hledger modules warning free, optimised and minimised
      run: |
        ./stack --no-terminal install --force-dirty --ghc-options=-fforce-recomp --ghc-options=-Werror
      # --ghc-options=-split-sections doesn't work on windows, "too many sections"
      # --pedantic
      if: env.CONTINUE

    # run hledger-lib/hledger functional tests, skipping the ones for addons
    ## - export PATH=~/.local/bin:$PATH
    #- COLUMNS=80 stack exec -- shelltest --execdir -j16 hledger/test -x /_ -x /addons

    # artifacts:

    - name: Gather executables
      run: |
        mkdir tmp
        cd tmp
        mkdir hledger
        cd hledger
        copy C:\Users\runneradmin\AppData\Roaming\local\bin\hledger.exe .
        copy C:\Users\runneradmin\AppData\Roaming\local\bin\hledger-web.exe .
      if: env.CONTINUE
             
    # XXX intermittent upload failures
    - name: Create executables artifact
      uses: actions/upload-artifact@v2
      with:
        name: hledger-windows
        path: tmp/hledger
      if: env.CONTINUE

