name: Development Client

on:
  workflow_dispatch: {}
  # This task will fail due to migration to `expo-modules-core`.
  # We temporary disable it.
  # pull_request:
  #   paths:
  #     - .github/workflows/development-client.yml
  #     - packages/expo-dev-*/**
  # push:
  #   branches: [main]
  #   paths:
  #     - .github/workflows/development-client.yml
  #     - packages/expo-dev-*/**

concurrency:
  group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  android:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        ndk-version: [21.4.7075529]
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
          ndk: 'true'
          ndk-version: ${{ matrix.ndk-version }}
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: Init new expo app
        working-directory: ../
        run: yarn create expo-app ./development-client-android-test --yes
      - name: Make Yarn resolve expo-dev-client dependencies locally
        working-directory: ../development-client-android-test
        run: |
          node -e " \
            const pkg = require('./package.json'); \
            pkg.resolutions = { \
              ...pkg.resolutions, \
              'expo-dev-menu': 'file:../expo/packages/expo-dev-menu', \
              'expo-dev-menu-interface': 'file:../expo/packages/expo-dev-menu-interface', \
              'expo-dev-launcher': 'file:../expo/packages/expo-dev-launcher', \
              'expo-updates-interface': 'file:../expo/packages/expo-updates-interface', \
              'expo-updates': 'file:../expo/packages/expo-updates', \
              'expo-manifests': 'file:../expo/packages/expo-manifests', \
              'expo-json-utils': 'file:../expo/packages/expo-json-utils', \
            }; \
            fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2) + '\n');"
      - name: Add dependencies
        working-directory: ../development-client-android-test
        run: yarn add file:../expo/packages/expo-dev-client
      - name: Setup app.config.json
        working-directory: ../development-client-android-test
        run: echo "{\"name\":\"development-client-android-test\",\"plugins\":[\"expo-dev-client\"],\"android\":{\"package\":\"com.devclient.test\"},\"ios\":{\"bundleIdentifier\":\"com.devclient.test\"}}" > app.config.json
      - name: Prebuild Android
        working-directory: ../development-client-android-test
        run: yarn expo prebuild --platform android
      - name: Bump `build tools`
        working-directory: ../development-client-android-test
        run: sed -i -e 's/buildToolsVersion\ =\ \"29\..\..\"/buildToolsVersion\ = \"30\.0\.3\"/' ./android/build.gradle
      - name: Bump `android build tools`
        working-directory: ../development-client-android-test
        run: sed -i -e 's/com\.android\.tools\.build:gradle:3\..\../com\.android\.tools\.build:gradle:3\.5\.4/' ./android/build.gradle
      - name: 🏗️ Build debug version
        env:
          ANDROID_NDK_HOME: /usr/local/lib/android/sdk/ndk/${{ matrix.ndk-version }}/
        working-directory: ../development-client-android-test/android
        run: ./gradlew assembleDebug

  ios:
    runs-on: macos-13
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: Init new expo app
        working-directory: ../
        run: yarn create expo-app ./development-client-ios-test --yes
      - name: Make Yarn resolve expo-dev-client dependencies locally
        working-directory: ../development-client-ios-test
        run: |
          node -e " \
            const pkg = require('./package.json'); \
            pkg.resolutions = { \
              ...pkg.resolutions, \
              'expo-dev-menu': 'file:../expo/packages/expo-dev-menu', \
              'expo-dev-menu-interface': 'file:../expo/packages/expo-dev-menu-interface', \
              'expo-dev-launcher': 'file:../expo/packages/expo-dev-launcher', \
              'expo-updates-interface': 'file:../expo/packages/expo-updates-interface', \
              'expo-updates': 'file:../expo/packages/expo-updates', \
              'expo-manifests': 'file:../expo/packages/expo-manifests', \
              'expo-json-utils': 'file:../expo/packages/expo-json-utils', \
            }; \
            fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2) + '\n');"
      - name: Add dependencies
        working-directory: ../development-client-ios-test
        run: yarn add file:../expo/packages/expo-dev-client
      - name: Setup app.config.json
        working-directory: ../development-client-ios-test
        run: echo "{\"name\":\"development-client-ios-test\",\"plugins\":[\"expo-dev-client\"],\"android\":{\"package\":\"com.devclient.test\"},\"ios\":{\"bundleIdentifier\":\"com.devclient.test\"}}" > app.config.json
      - name: Prebuild iOS
        working-directory: ../development-client-ios-test
        run: yarn expo prebuild --platform ios
      - name: 🏗️ Build debug version
        working-directory: ../development-client-ios-test
        run: xcodebuild -workspace ios/developmentclientiostest.xcworkspace -scheme developmentclientiostest -configuration debug -sdk iphonesimulator -arch x86_64
