name: Packaging(MacOS)

on:
  push:
    branches:
      - master
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
      - 'docs/**'

  pull_request:
    paths-ignore:
      - 'README.md'
      - 'LICENSE'
      - 'docs/**'
      - 'data/translations/*.ts'

  workflow_dispatch:

env:
  PRODUCT: flameshot

jobs:
  dmg-pack:
    name: Build dmg on ${{ matrix.dist.os }} ${{ matrix.dist.arch }}
    strategy:
      fail-fast: false
      matrix:
        dist:
          - {
              os: macos-14,
              arch: arm64
            }
          - {
              os: macos-13,
              arch: intel
            }
    runs-on: ${{ matrix.dist.os }}
    env:
      APP_NAME: flameshot
      DIR_BUILD: build
    steps:
      - name: Checkout Source code
        if: github.event_name == 'push'
        uses: actions/checkout@v4

      - name: Checkout Source code
        if: github.event_name == 'pull_request'
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Checkout Source code
        if: github.event_name == 'workflow_dispatch'
        uses: actions/checkout@v4
        with:
          ref: ${{ github.sha }}

      - name: Set env & Print flameshot version
        shell: bash
        run: |
          last_committed_tag=$(git tag -l --sort=-v:refname | head -1)
          git_revno=$(git rev-list $(git describe --tags --abbrev=0)..HEAD --count)
          git_hash=$(git rev-parse --short HEAD)
          ver_info=${last_committed_tag}+git${git_revno}.${git_hash}
          echo "=======FLAMESHOT VERSION========"
          echo ${last_committed_tag:1}
          echo "Details: ${ver_info}"
          echo "================================"
          # This will allow to build pre-preleases without git tag
          # echo "VERSION=${last_committed_tag:1}" >> $GITHUB_ENV
          echo "VERSION=$(cat CMakeLists.txt |grep 'set.*(.*FLAMESHOT_VERSION' | sed 's/[^0-9.]*//' |sed 's/)//g')" >> $GITHUB_ENV
          echo "VER_INFO=${ver_info}" >> $GITHUB_ENV

      - name: Install Qt
        run: brew install qt@6 cmake

      - name: Configure
        run: |
          rm -rf "${DIR_BUILD}"/src/flameshot.dmg "${DIR_BUILD}"/src/flameshot.app/
          cmake -GNinja -S . -B "${DIR_BUILD}" -DQt6_DIR=$(brew --prefix qt6)/lib/cmake/Qt6 -DUSE_MONOCHROME_ICON=True

      - name: Compile
        run: |
          cmake --build "${DIR_BUILD}"

      - name: Build dmg package
        run: |
          cd "${DIR_BUILD}"
          ninja create_dmg

      - name: Artifact Upload
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.PRODUCT }}-${{ env.VER_INFO }}-artifact-macos-${{ matrix.dist.arch }}
          path: ${{ github.workspace }}/build/src/Flameshot-${{ env.VERSION }}.dmg
          overwrite: true