name: Dart/Flutter
on:
  workflow_dispatch:
  push:
    branches:
      - main
    tags:
      - v*
  pull_request:
    branches:
      - main
env:
  working-directory: bindings/dart
jobs:
  test:
    timeout-minutes: 30
    defaults:
      run:
        working-directory: ${{ env.working-directory }}
    strategy:
      matrix:
        os:
          - blacksmith-4vcpu-ubuntu-2404
          - windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install Rust (stable)
        uses: dtolnay/rust-toolchain@stable
      - name: Setup Dart
        uses: dart-lang/setup-dart@v1
      - name: Install Flutter
        uses: subosito/flutter-action@v2
      - name: Get flutter deps
        run: flutter pub get
      - name: Build test binaries
        run: cargo build --package turso_dart --target-dir=rust/test_build
      - name: Run tests
        run: flutter test
  precompile:
    if: ${{ false && startsWith(github.ref, 'refs/tags/') }}
    timeout-minutes: 30
    defaults:
      run:
        working-directory: ${{ env.working-directory }}
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
    strategy:
      matrix:
        os:
          - blacksmith-4vcpu-ubuntu-2404
          - macOS-latest
          - windows-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install Rust (stable)
        uses: dtolnay/rust-toolchain@stable
      - name: Setup Dart
        uses: dart-lang/setup-dart@v1
      - name: Install Flutter
        uses: subosito/flutter-action@v2
      - name: Set up Android SDK
        if: (matrix.os == 'ubuntu-latest')
        uses: android-actions/setup-android@v2
      - name: Install specific NDK
        if: (matrix.os == 'ubuntu-latest')
        run: sdkmanager --install "ndk;25.1.8937393"
      - name: Get flutter deps
        run: flutter pub get
      - name: Precompile
        if: (matrix.os == 'macOS-latest') || (matrix.os == 'windows-latest')
        run: dart run build_tool precompile-binaries -v --manifest-dir=../../rust --repository=tursodatabase/turso
        working-directory: ${{ env.working-directory }}/cargokit/build_tool
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PRIVATE_KEY: ${{ secrets.CARGOKIT_PRIVATE_KEY }}
      - name: Precompile (with Android)
        if: (matrix.os == 'ubuntu-latest')
        run: dart run build_tool precompile-binaries -v --manifest-dir=../../rust --repository=tursodatabase/turso --android-sdk-location=/usr/local/lib/android/sdk --android-ndk-version=25.1.8937393 --android-min-sdk-version=23
        working-directory: ${{ env.working-directory }}/cargokit/build_tool
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PRIVATE_KEY: ${{ secrets.CARGOKIT_PRIVATE_KEY }}
  publish:
    if: ${{ false && startsWith(github.ref, 'refs/tags/') }}
    timeout-minutes: 30
    defaults:
      run:
        working-directory: ${{ env.working-directory }}
    runs-on: blacksmith-4vcpu-ubuntu-2404
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Publish turso_dart to pub.dev
        uses: k-paxian/dart-package-publisher@v.1.6.2
        with:
          accessToken: ${{ secrets.PUBDEV_ACCESS_TOKEN }}
          refreshToken: ${{ secrets.PUBDEV_REFRESH_TOKEN }}
          skipTests: true
          force: true
