name: Build Notepad Next

on:
  push:
    branches:
      - master
    tags:
      - '*'
  pull_request:


jobs:
  build-linux:
    strategy:
      fail-fast: false
      matrix:
        config:
        - 
          qt_version: "5.15.2"
          modules: ""
        - 
          qt_version: "6.2.4"
          modules: "qt5compat"
        - 
          qt_version: "6.5"
          modules: "qt5compat"

    runs-on: ubuntu-20.04

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3
        with:
          submodules: true

      - name: Install Qt
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ matrix.config.qt_version }}
          modules: ${{ matrix.config.modules }}
      
      - name: Setup
        run: sudo apt-get install libxkbcommon-dev libxkbcommon-x11-0 fuse libxcb-cursor-dev

      - name: Compile
        run: |
          mkdir build
          cd build
          qmake ../src/NotepadNext.pro "DISTRIBUTION=AppImage"
          make -j$(nproc)

      - name: Build AppImage
        run: |
          cd build
          make appimage

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: NotepadNext-Linux-Qt${{ matrix.config.qt_version }}-AppImage
          path: ${{ github.workspace }}/build/NotepadNext/NotepadNext*.AppImage

  build-mac:
    strategy:
      fail-fast: false
      matrix:
        config:
        -
          qt_version: "5.15.2"
          modules: ""
        -
          qt_version: "6.2.4"
          modules: "qt5compat"
        - 
          qt_version: "6.5"
          modules: "qt5compat"

    runs-on: macos-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3
        with:
          submodules: true

      - name: Install Qt
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ matrix.config.qt_version }}
          modules: ${{ matrix.config.modules }}

      - name: Compile
        run: |
          mkdir build
          cd build
          qmake ../src/NotepadNext.pro
          make -j$(sysctl -n hw.ncpu)

      - name: Build DMG
        run: |
          cd build
          make dmg

      - name: Upload Artifact
        uses: actions/upload-artifact@v3
        with:
          name: NotepadNext-macOS-Qt${{ matrix.config.qt_version }}
          path: ${{ github.workspace }}/build/NotepadNext/NotepadNext*.dmg

  build-windows:
    strategy:
      fail-fast: false
      matrix:
        config:
        - 
          qt_version: "5.15.2"
          modules: ""
        - 
          qt_version: "6.2.4"
          modules: "qt5compat"
        - 
          qt_version: "6.5"
          modules: "qt5compat"

    runs-on: windows-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3
        with:
          submodules: true

      - name: Install Qt
        uses: jurplel/install-qt-action@v3
        with:
          version: ${{ matrix.config.qt_version }}
          modules: ${{ matrix.config.modules }}

      - name: Setup Visual Studio Tools
        uses: egor-tensin/vs-shell@v2
        with:
          arch: x64

      - name: Install jom
        run: choco install jom

      - name: Run QMake
        run: |
          mkdir build
          cd build
          qmake ..\src\NotepadNext.pro

      - name: Build
        run: |
          cd build
          jom

      - name: Create zip Package
        run: |
          cd build
          jom package

      - name: Create Installer
        run: |
          cd build
          jom installer

      - name: Upload zip Package
        uses: actions/upload-artifact@v3
        with:
          name: NotepadNext-Windows-Qt${{ matrix.config.qt_version }}-Zip
          path: ${{ github.workspace }}/build/package/

      - name: Upload Installer
        uses: actions/upload-artifact@v3
        with:
          name: NotepadNext-Windows-Qt${{ matrix.config.qt_version }}-Installer
          path: ${{ github.workspace }}/installer/NotepadNext*.exe

  github:
    name: Draft GitHub Release
    runs-on: ubuntu-latest
    needs: [build-linux, build-mac, build-windows]

    if: github.repository == 'dail8859/NotepadNext' && github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3

      - name: Create Draft Release
        run: |
          gh release create ${{ github.ref_name }} --title ${{ github.ref_name }} --generate-notes --draft

      - name: Download all artifacts
        uses: actions/download-artifact@v3

      - name: Upload Windows Installer
        run: |
          gh release upload ${{ github.ref_name }} NotepadNext-Windows-Qt6.5-Installer/NotepadNext-${{ github.ref_name }}-Installer.exe

      # The artifact is unzipped, so need to zip it back up so it can be uploaded
      - name: Upload Windows Zip
        run: |
          cd NotepadNext-Windows-Qt6.5-Zip/
          zip -r ../NotepadNext-${{ github.ref_name }}-win64.zip .
          cd ..
          gh release upload ${{ github.ref_name }} NotepadNext-${{ github.ref_name }}-win64.zip


      - name: Upload Linux AppImage
        run: |
          gh release upload ${{ github.ref_name }} NotepadNext-Linux-Qt6.5-AppImage/NotepadNext-${{ github.ref_name }}-x86_64.AppImage

      - name: Upload macOS App
        run: |
          gh release upload ${{ github.ref_name }} NotepadNext-macOS-Qt6.5/NotepadNext-${{ github.ref_name }}.dmg

