---
name: Build

on:
  push:
    branches:
      - master
  pull_request:
  workflow_dispatch:

concurrency: 
  group: build-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [windows-latest, ubuntu-latest, macos-latest]
        qt-version: [5.15.2, 5.12.12]
        pch: [true]
        include:
          - os: ubuntu-latest
            qt-version: 5.15.2
            pch: false
      fail-fast: false

    steps:
      - name: Set environment variables for windows-latest
        if: matrix.os == 'windows-latest'
        run: |
            echo "vs_version=2022" >> $GITHUB_ENV
        shell: bash

      - uses: actions/checkout@v3
        with:
          submodules: true
          fetch-depth: 0  # allows for tags access

      - name: Install Qt
        uses: jurplel/install-qt-action@v3.0.0
        with:
          cache: true
          cache-key-prefix: ${{ runner.os }}-QtCache-${{ matrix.qt-version }}
          version: ${{ matrix.qt-version }}
          dir: "${{ github.workspace }}/qt/"

      # WINDOWS
      - name: Cache conan packages part 1
        if: startsWith(matrix.os, 'windows')
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-conan-user-${{ hashFiles('**/conanfile.txt') }}
          path:  ~/.conan/

      - name: Cache conan packages part 2
        if: startsWith(matrix.os, 'windows')
        uses: actions/cache@v3
        with:
          key: ${{ runner.os }}-conan-root-${{ hashFiles('**/conanfile.txt') }}
          path: C:/.conan/

      - name: Add Conan to path
        if: startsWith(matrix.os, 'windows')
        run: echo "C:\Program Files\Conan\conan\" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        
      - name: Install dependencies (Windows)
        if: startsWith(matrix.os, 'windows')
        run: |
            choco install conan -y
        
      - name: Enable Developer Command Prompt
        if: startsWith(matrix.os, 'windows')
        uses: ilammy/msvc-dev-cmd@v1.12.0

      - name: Build (Windows)
        if: startsWith(matrix.os, 'windows')
        run: |
            mkdir build
            cd build
            conan install ..  -b missing
            cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DUSE_CONAN=ON ..
            set cl=/MP
            nmake /S /NOLOGO
            windeployqt bin/chatterino.exe --release --no-compiler-runtime --no-translations --no-opengl-sw --dir Chatterino2/
            cp bin/chatterino.exe Chatterino2/
            echo nightly > Chatterino2/modes
            7z a chatterino-windows-x86-64.zip Chatterino2/

      - name: Upload artifact (Windows)
        if: startsWith(matrix.os, 'windows')
        uses: actions/upload-artifact@v3
        with:
          name: chatterino-windows-x86-64-${{ matrix.qt-version }}.zip
          path: build/chatterino-windows-x86-64.zip

      - name: Clean Conan pkgs
        if: startsWith(matrix.os, 'windows')
        run: conan remove "*" -fsb
        shell: bash
        
      # LINUX
      - name: Install dependencies (Ubuntu)
        if: startsWith(matrix.os, 'ubuntu')
        run: |
            sudo apt-get update
            sudo apt-get -y install \
                cmake \
                virtualenv \
                rapidjson-dev \
                libssl-dev \
                libboost-dev \
                libxcb-randr0-dev \
                libboost-system-dev \
                libboost-filesystem-dev \
                libpulse-dev \
                libxkbcommon-x11-0 \
                libgstreamer-plugins-base1.0-0 \
                build-essential \
                libgl1-mesa-dev \
                libxcb-icccm4 \
                libxcb-image0 \
                libxcb-keysyms1 \
                libxcb-render-util0 \
                libxcb-xinerama0

      - name: Build (Ubuntu)
        if: startsWith(matrix.os, 'ubuntu')
        run: |
            mkdir build
            cd build
            cmake \
              -DCMAKE_INSTALL_PREFIX=appdir/usr/ \
              -DCMAKE_BUILD_TYPE=Release \
              -DPAJLADA_SETTINGS_USE_BOOST_FILESYSTEM=On \
              -DUSE_PRECOMPILED_HEADERS=${{ matrix.pch }} \
              -DCMAKE_EXPORT_COMPILE_COMMANDS=On \
              ..
            make -j$(nproc)
        shell: bash

      - name: clang-tidy review
        if: (startsWith(matrix.os, 'ubuntu') && matrix.pch == false && matrix.qt-version == '5.15.2' && github.event_name == 'pull_request')
        uses: ZedThree/clang-tidy-review@v0.10.1
        id: review
        with:
          build_dir: build
          config_file: '.clang-tidy'
          split_workflow: true

      - uses: actions/upload-artifact@v3
        if: (startsWith(matrix.os, 'ubuntu') && matrix.pch == false && matrix.qt-version == '5.15.2' && github.event_name == 'pull_request')
        with:
          name: clang-tidy-review
          path: |
            clang-tidy-review-output.json
            clang-tidy-review-metadata.json

      - name: Package - AppImage (Ubuntu)
        if: startsWith(matrix.os, 'ubuntu')
        run: |
            cd build
            sh ./../.CI/CreateAppImage.sh
        shell: bash

      - name: Package - .deb (Ubuntu)
        if: startsWith(matrix.os, 'ubuntu')
        run: |
            cd build
            sh ./../.CI/CreateUbuntuDeb.sh
        shell: bash

      - name: Upload artifact - AppImage (Ubuntu)
        if: startsWith(matrix.os, 'ubuntu')
        uses: actions/upload-artifact@v3
        with:
          name: Chatterino-x86_64-${{ matrix.qt-version }}.AppImage
          path: build/Chatterino-x86_64.AppImage

      - name: Upload artifact - .deb (Ubuntu)
        if: startsWith(matrix.os, 'ubuntu')
        uses: actions/upload-artifact@v3
        with:
          name: Chatterino-${{ matrix.qt-version }}.deb
          path: build/Chatterino-x86_64.deb

      # MACOS
      - name: Install dependencies (MacOS)
        if: startsWith(matrix.os, 'macos')
        run: |
            brew install boost openssl rapidjson p7zip create-dmg cmake tree
        shell: bash

      - name: Build (MacOS)
        if: startsWith(matrix.os, 'macos')
        run: |
            mkdir build
            cd build
            cmake \
                -DCMAKE_BUILD_TYPE=Release \
                -DCMAKE_OSX_DEPLOYMENT_TARGET=10.15 \
                -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl \
                -DUSE_PRECOMPILED_HEADERS=${{ matrix.pch }} \
                ..
            make -j$(sysctl -n hw.logicalcpu)
        shell: bash

      - name: Package (MacOS)
        if: startsWith(matrix.os, 'macos')
        run: |
            ls -la
            pwd
            ls -la build || true
            cd build
            sh ./../.CI/CreateDMG.sh
        shell: bash

      - name: Upload artifact (MacOS)
        if: startsWith(matrix.os, 'macos')
        uses: actions/upload-artifact@v3
        with:
          name: chatterino-osx-${{ matrix.qt-version }}.dmg
          path: build/chatterino-osx.dmg

  create-release:
    needs: build
    runs-on: ubuntu-latest
    if: (github.event_name == 'push' && github.ref == 'refs/heads/master')

    steps:
      - uses: actions/download-artifact@v3
        with:
          name: chatterino-windows-x86-64-5.15.2.zip
          path: release-artifacts/

      - uses: actions/download-artifact@v3
        with:
          name: Chatterino-x86_64-5.15.2.AppImage
          path: release-artifacts/

      - uses: actions/download-artifact@v3
        with:
          name: Chatterino-5.15.2.deb
          path: release-artifacts/

      - uses: actions/download-artifact@v3
        with:
          name: chatterino-osx-5.15.2.dmg
          path: release-artifacts/

      - name: Create release
        uses: ncipollo/release-action@v1.11.2
        with:
          removeArtifacts: true
          allowUpdates: true
          artifactErrorsFailBuild: true
          artifacts: "release-artifacts/*"
          body: ${{ github.event.head_commit.message }}
          prerelease: true
          name: Nightly Release
          tag: nightly-build
