name: Release Builds

on:
  release:
    types: [created]
  workflow_dispatch:
    inputs:
      tag:
        description: "Existing git tag to publish assets for (example: v3.0.0)"
        required: true
        type: string

permissions:
  contents: write

concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: false

jobs:
  package-macos:
    name: macOS / ${{ matrix.arch }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # - os: macos-15-intel
          #   arch: x86_64
          - os: macos-14
            arch: arm64
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          submodules: true

      - name: Resolve release tag
        id: meta
        shell: bash
        run: |
          if [[ "${{ github.event_name }}" == "release" ]]; then
            tag="${{ github.event.release.tag_name }}"
          else
            tag="${{ inputs.tag }}"
          fi

          if [[ -z "$tag" ]]; then
            echo "Release tag is required" >&2
            exit 1
          fi

          echo "tag=$tag" >> "$GITHUB_OUTPUT"

      - name: Install macOS dependencies
        shell: bash
        run: |
          brew update
          brew install cmake ninja qt@6 uchardet

      - name: Configure macOS Qt environment
        shell: bash
        run: |
          echo "PATH=$(brew --prefix qt@6)/bin:${PATH}" >> "$GITHUB_ENV"
          echo "CMAKE_PREFIX_PATH=$(brew --prefix qt@6)" >> "$GITHUB_ENV"
          echo "PKG_CONFIG_PATH=$(brew --prefix uchardet)/lib/pkgconfig:${PKG_CONFIG_PATH:-}" >> "$GITHUB_ENV"

      - name: Configure
        shell: bash
        run: cmake --preset release

      - name: Build
        run: cmake --build --preset release --parallel

      - name: Test
        run: ctest --preset release --output-on-failure

      - name: Package macOS artifacts
        shell: bash
        run: |
          build-tools/package-nightly-macos.sh \
            build/release \
            dist \
            release-root \
            "notepadqq-${{ steps.meta.outputs.tag }}-macos-${{ matrix.arch }}"

      - name: Upload macOS artifacts
        uses: actions/upload-artifact@v7
        with:
          name: notepadqq-release-macos-${{ matrix.arch }}
          path: dist/*
          if-no-files-found: error

  package-linux:
    name: Linux / ${{ matrix.arch }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-24.04
            arch: x86_64
          - os: ubuntu-24.04-arm
            arch: arm64
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          submodules: true

      - name: Resolve release tag
        id: meta
        shell: bash
        run: |
          if [[ "${{ github.event_name }}" == "release" ]]; then
            tag="${{ github.event.release.tag_name }}"
          else
            tag="${{ inputs.tag }}"
          fi

          if [[ -z "$tag" ]]; then
            echo "Release tag is required" >&2
            exit 1
          fi

          echo "tag=$tag" >> "$GITHUB_OUTPUT"

      - name: Install Linux dependencies
        shell: bash
        run: |
          sudo apt-get update
          xargs -a .github/ci/ubuntu-24.04-packages.txt sudo apt-get install -y --no-install-recommends

      - name: Configure
        shell: bash
        run: cmake --preset release

      - name: Build
        run: cmake --build --preset release --parallel

      - name: Test
        run: ctest --preset release --output-on-failure

      - name: Package Linux AppImage
        shell: bash
        run: |
          build-tools/package-nightly-linux.sh \
            build/release \
            dist \
            AppDir \
            "notepadqq-${{ steps.meta.outputs.tag }}-linux-${{ matrix.arch }}"

      - name: Upload Linux artifacts
        uses: actions/upload-artifact@v7
        with:
          name: notepadqq-release-linux-${{ matrix.arch }}
          path: dist/*
          if-no-files-found: error

  publish:
    name: Publish release assets
    needs: [package-macos, package-linux]
    runs-on: ubuntu-24.04
    steps:
      - name: Resolve release tag
        id: meta
        shell: bash
        run: |
          if [[ "${{ github.event_name }}" == "release" ]]; then
            tag="${{ github.event.release.tag_name }}"
          else
            tag="${{ inputs.tag }}"
          fi

          if [[ -z "$tag" ]]; then
            echo "Release tag is required" >&2
            exit 1
          fi

          echo "tag=$tag" >> "$GITHUB_OUTPUT"

      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          path: dist

      - name: Publish release assets
        uses: softprops/action-gh-release@v3
        with:
          tag_name: ${{ steps.meta.outputs.tag }}
          files: |
            dist/**/*.dmg
            dist/**/*.AppImage
          fail_on_unmatched_files: true

      - name: Publish release
        if: github.event_name == 'release'
        shell: bash
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release edit "${{ steps.meta.outputs.tag }}" --draft=false --latest
