# see https://github.com/karlicoss/pymplate for up-to-date reference

name: CI
on:
  push:
    branches: '*'
    tags: 'v[0-9]+.*' # only trigger on 'release' tags for PyPi

env:
  # not sure if tests should detect GITHUB_WORKFLOW instead??
  CI: true

jobs:
  build:
    strategy:
      matrix:
        platform: [ubuntu-latest, macos-latest] # todo windows-latest?
        python-version: [3.6, 3.7, 3.8]

    runs-on: ${{ matrix.platform }}

    steps:
    - run: echo "::add-path::$HOME/.local/bin"

    - uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}

    - uses: actions/checkout@v2
      with:
        submodules: recursive
   
    # uncomment for SSH debugging
    # - uses: mxschmitt/action-tmate@v2

    - run: scripts/ci/run

    # TODO move to a separate script too?
    - run: |
        pip3 install --user .
        pip3 install --user .[org] # exobrain is mostly org files
        promnesia demo --port 16789 https://github.com/karlicoss/exobrain & \
        sleep 10 && curl 'http://localhost:16789/search' --data 'url=github.com'


  install_and_run_test:
    # TODO use setup-python thing?
    # TODO FIXME run on macos too?
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      with:
        submodules: recursive

    - run: ci/github-ci-compat

    - run: |
        python3 -m pip install .
        export PATH=ci/fake-systemd:$PATH
        tests/install_and_run

  pypi:
    runs-on: ubuntu-latest
    needs: [build, install_and_run_test]

    steps:
    - run: echo "::add-path::$HOME/.local/bin"

    - uses: actions/setup-python@v1
      with:
        python-version: 3.7

    - uses: actions/checkout@v2
      with:
        submodules: recursive

    - name: 'release to test pypi'
      # always deploy merged master to test pypi
      if: github.event.ref == 'refs/heads/master'
      env:
        TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD_TEST }}
      run: pip3 install --user wheel twine && scripts/release --test
      # TODO run pip install just to test?

    - name: 'release to pypi'
      # always deploy tags to release pypi
      # NOTE: release tags are guarded by on: push: tags on the top
      if: startsWith(github.event.ref, 'refs/tags')
      env:
        TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}
      run: pip3 install --user wheel twine && scripts/release

  ###
  build_extension:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        submodules: recursive
    - uses: actions/setup-node@v1
      with:
        node-version: '14'
    - run: scripts/ci/extension

    # TODO ugh. can't share github actions artifacts publicly...
    # TODO for fuck's sake... why does it end up named as .zip.zip ????
    - uses: actions/upload-artifact@v2
      with:
        name: 'promnesia-chrome-latest.zip'
        path: 'extension/dist/chrome/web-ext-artifacts/*.zip'


    - uses: actions/upload-artifact@v2
      with:
        name: 'promnesia-firefox-latest.zip'
        path: 'extension/dist/firefox/web-ext-artifacts/*.zip'
