# This yaml shares the build bridge steps with ci and nightly.
name: Build flutter-rust-bridge
# 2023-11-23 18:00:00+00:00

on:
  workflow_call:

env:
  CARGO_EXPAND_VERSION: "1.0.95"
  FLUTTER_RUST_BRIDGE_VERSION: "1.80.1"
  RUST_VERSION: "1.75" # https://github.com/rustdesk/rustdesk/discussions/7503

jobs:
  generate_bridge:
    runs-on: ${{ matrix.job.os }}
    strategy:
      fail-fast: false
      matrix:
        job:
          # Default bridge for every platform still on Flutter 3.24.5 (generated with 3.22.3).
          - {
              target: x86_64-unknown-linux-gnu,
              os: ubuntu-22.04,
              extra-build-args: "",
              flutter-version: "3.22.3",
              artifact-name: "bridge-artifact",
            }
          # Dedicated bridge for the Windows arm64 build (Flutter 3.44); runs in parallel.
          - {
              target: x86_64-unknown-linux-gnu,
              os: ubuntu-22.04,
              extra-build-args: "",
              flutter-version: "3.44.0",
              artifact-name: "bridge-artifact-flutter-3.44",
            }
    steps:
      - name: Checkout source code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          submodules: recursive

      - name: Install prerequisites
        run: |
          sudo apt-get install ca-certificates -y
          sudo apt-get update -y
          sudo apt-get install -y \
            clang \
            cmake \
            curl \
            gcc \
            git \
            g++ \
            libclang-dev \
            libgtk-3-dev \
            llvm-dev \
            nasm \
            ninja-build \
            pkg-config \
            wget

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # v1
        with:
          toolchain: ${{ env.RUST_VERSION }}
          targets: ${{ matrix.job.target }}
          components: "rustfmt"

      - uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
        with:
          prefix-key: bridge-${{ matrix.job.os }}

      - name: Cache Bridge
        id: cache-bridge
        uses: actions/cache@6f8efc29b200d32929f49075959781ed54ec270c # v3
        with:
          path: /tmp/flutter_rust_bridge
          key: bridge-${{ matrix.job.flutter-version }}

      - name: Install flutter
        uses: subosito/flutter-action@1a449444c387b1966244ae4d4f8c696479add0b2 # v2
        with:
          channel: "stable"
          flutter-version: ${{ matrix.job.flutter-version }}
          cache: true

      - name: Install flutter rust bridge deps
        shell: bash
        run: |
          cargo install cargo-expand --version ${{ env.CARGO_EXPAND_VERSION }} --locked
          cargo install flutter_rust_bridge_codegen --version ${{ env.FLUTTER_RUST_BRIDGE_VERSION }} --features "uuid" --locked
          if [[ "${{ matrix.job.flutter-version }}" == "3.22.3" ]]; then
            # Default Flutter 3.22.3: extended_text 14 needs a newer Dart, so downgrade for resolution.
            sed -i -e 's/extended_text: 14.0.0/extended_text: 13.0.0/g' flutter/pubspec.yaml
          else
            # Flutter 3.44 bridge for Windows arm64: match that build's source/pubspec state so the
            # generated *.freezed.dart compiles against the same Flutter/freezed it resolves.
            bash .github/patches/apply_flutter_3.44_source_patches.sh
          fi
          pushd flutter && flutter pub get && popd

      - name: Run flutter rust bridge
        run: |
          ~/.cargo/bin/flutter_rust_bridge_codegen --rust-input ./src/flutter_ffi.rs --dart-output ./flutter/lib/generated_bridge.dart --c-output ./flutter/macos/Runner/bridge_generated.h
          cp ./flutter/macos/Runner/bridge_generated.h ./flutter/ios/Runner/bridge_generated.h

      - name: Upload Artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: ${{ matrix.job.artifact-name }}
          path: |
            ./src/bridge_generated.rs
            ./src/bridge_generated.io.rs
            ./flutter/lib/generated_bridge.dart
            ./flutter/lib/generated_bridge.freezed.dart
            ./flutter/macos/Runner/bridge_generated.h
            ./flutter/ios/Runner/bridge_generated.h
