name: macOS Release
on:
  push:
    tags:
      - v*
    branches:
      - master
    paths:
      - "go/**"
      - "!go/**.md"
      - ".goreleaser"
      - "go.*"
      - "**.go"
      - ".github/workflows/go.yml"
      - "js/**"
      - "config/**"
      - ".github/workflows/js.yml"
  pull_request:
    paths:
      - "go/**"
      - "!go/**.md"
      - ".goreleaser"
      - "go.*"
      - "**.go"
      - ".github/workflows/go.yml"
      - "js/**"
      - "config/**"
      - ".github/workflows/js.yml"

jobs:
  mac_runner_matrix_builder:
    name: macOS matrix builder
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - id: set-matrix
        run: |
          # usage: node .github/workflows/mac-runner-matrix-builder.js STRATEGY
          #
          #  STRATEGY
          #    self-hosted    pick the self-hosted runner configuration
          #    github         pick the github runner configuration
          #    optimized      pick a dc4 runner if available or fallback on github one
          #
          node .github/workflows/utils/mac-runner-matrix-builder.js optimized

  build-macos-app:
    name: Build Electron app (macOS)
    needs: mac_runner_matrix_builder
    runs-on: ${{ matrix.runner }}
    env:
      CACHE_DIRS: js/android/.gomobile-cache; js/ios/.gomobile-cache; js/ios/.xcodegen-cache
    strategy:
      fail-fast: false
      matrix: ${{fromJson(needs.mac_runner_matrix_builder.outputs.matrix)}}
    steps:
      - name: Pre-checkout cleanup
        if: ${{ matrix.selfhosted }}
        run: |
          cache_dirs=(${CACHE_DIRS//;/ })
          for cache_dir in ${cache_dirs[@]}; do
            if [ -d "$cache_dir" ]; then
              mkdir -p "$RUNNER_TEMP/$cache_dir"
              rm -rf "$RUNNER_TEMP/$cache_dir"
              mv "$cache_dir" "$RUNNER_TEMP/$cache_dir"
            fi
          done

      - name: Checkout
        uses: actions/checkout@v4

      - name: Post-checkout cleanup
        if: ${{ matrix.selfhosted }}
        run: |
          cache_dirs=(${CACHE_DIRS//;/ })
          for cache_dir in ${cache_dirs[@]}; do
            if [ -d "$RUNNER_TEMP/$cache_dir" ]; then
              mv "$RUNNER_TEMP/$cache_dir" "$cache_dir"
            fi
          done

      - name: Load variables from file
        uses: antifree/json-to-variables@v1.0.1
        with:
          filename: .github/workflows/utils/variables.json

      - name: Setup asdf
        if: ${{ !matrix.selfhosted }}
        uses: asdf-vm/actions/setup@9cd779f40fe38688dd19505ccbc4eaaf018b44e7
        with:
          asdf_version: 0.16.7

      - name: Setup go
        run: |
          asdf plugin add golang || ${{ matrix.selfhosted }}
          asdf install golang
          echo "go_version=$(asdf current golang | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Setup node
        working-directory: js
        run: |
          asdf plugin add nodejs || ${{ matrix.selfhosted }}
          asdf install nodejs
          echo "node_version=$(asdf current nodejs | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Setup yarn
        working-directory: js
        run: |
          asdf plugin add yarn || ${{ matrix.selfhosted }}
          asdf install yarn

      - name: Cache go modules
        uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-${{ hashFiles('go/**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-

      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: js/node_modules
          key: ${{ runner.OS }}-node-${{ env.node_version }}-${{ env.json_cache-versions_node }}-${{ hashFiles('js/yarn.lock') }}
          restore-keys: ${{ runner.OS }}-node-${{ env.node_version }}-${{ env.json_cache-versions_node }}-

      - name: Cache web node modules
        uses: actions/cache@v4
        with:
          path: js/web/node_modules
          key: ${{ runner.OS }}-nodeweb-${{ env.node_version }}-${{ env.json_cache-versions_nodeweb }}-${{ hashFiles('js/web/yarn.lock') }}
          restore-keys: ${{ runner.OS }}-nodeweb-${{ env.node_version }}-${{ env.json_cache-versions_nodeweb }}-

      - name: Build berty binary
        working-directory: go
        run: |
          touch gen.sum # avoid triggering make generate
          make go.install

      - name: Check go.mod and go.sum
        run: |
          go mod tidy -v
          git --no-pager diff go.mod go.sum
          git --no-pager diff --quiet go.mod go.sum

      - name: Fetch node modules
        working-directory: js
        run: make node_modules web/node_modules

      - name: Build the app (PR)
        if: github.event_name == 'pull_request'
        env:
          CSC_IDENTITY_AUTO_DISCOVERY: false
        working-directory: js
        run: make electron.mac

      - name: Build the app (Merge)
        if: github.event_name != 'pull_request'
        env:
          CSC_LINK: ${{ secrets.MAC_APP_CSC_LINK }}
          CSC_KEY_PASSWORD: ${{ secrets.MAC_APP_CSC_KEY_PASSWORD }}
        working-directory: js
        run: make electron.mac

      - name: Upload the Archive
        uses: actions/upload-artifact@v4
        with:
          path: js/web/dist/Berty.dmg
          name: berty-yolo-${{github.sha}}.unsigned-dmg
          if-no-files-found: error
