# Workflow adapted from gaphor/gaphor repo.
name: Build macOS installer

on: [push, pull_request]
env:
  python_version: '3.11'

jobs:
  macos:
    runs-on: macos-11
    permissions:
      contents: write
    timeout-minutes: 45
    if: "!contains(github.event.head_commit.message, 'skip ci')"
    outputs:
      artifact: ${{ steps.create.outputs.artifact }}
    env:
      LDFLAGS: -L/usr/local/opt/python@${python_version}/lib
      PKG_CONFIG_PATH: /usr/local/opt/libffi/lib/pkgconfig:/usr/local/opt/python@${python_version}/lib/pkgconfig:${PKG_CONFIG_PATH:-}
    steps:
      - uses: actions/checkout@v3.2.0
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - name: Set up Python
        uses: actions/setup-python@v4.4.0
        with:
          python-version: ${{ env.python_version }}
      - name: Install macOS Dependencies
        run: |
          # brew install adwaita-icon-theme cairo enchant glib librsvg  # TODO: is any of these packages needed?
          brew install create-dmg enchant gtk+3 gtk-mac-integration gtksourceview4 gobject-introspection upx
          # We need pygobject from PyPI. Using only pygobject3 from homebrew leads to "gi module not found".
          python3 -m pip install pyenchant pyyaml pygobject pyinstaller pyinstaller-hooks-contrib
          python3 -m pip freeze
        shell: bash
      - name: Show environment
        run: |
          python --version
          python3 --version
          python3 -c "import multiprocessing as m; print('CPUs:', m.cpu_count())"
          python3 -c "import gi"
      - name: Build app
        run: |
          python3 dev/build_translations.py share/locale
          python3 -m PyInstaller \
            --name "rednotebook" \
            --windowed \
            --add-data ./rednotebook/files:files/ \
            --add-data ./rednotebook/images:images/ \
            --add-data ./share/locale:share/locale/ \
            ./rednotebook/journal.py
        shell: bash
      - name: Build DMG file
        run: |
          create-dmg rednotebook.dmg dist/rednotebook.app
        shell: bash
      #- uses: actions/upload-artifact@v3
      #  with:
      #    name: rednotebook-appdir
      #    path: dist/rednotebook
      - uses: actions/upload-artifact@v3
        with:
          name: rednotebook-app
          path: dist/rednotebook.app
      - uses: actions/upload-artifact@v3
        with:
          name: rednotebook-dmg
          path: rednotebook.dmg
