format_version: '11'
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
project_type: react-native
pipelines:
  main-trigger-pipeline:
    stages:
      - stage-trigger-run-main: {}
  pr-trigger-pipeline:
    stages:
      - stage-trigger-run-pr: {}
stages:
  stage-trigger-run-pr:
    workflows:
      - lint: {}
      - typescript: {}
      - docs: {}
      - unit-js: {}
      - unit-example-app: {}
      - unit-android: {}
      - unit-ios: {}
      - example-build-ios: {}
      - example-build-android: {}
      - e2e-android: {}
      - e2e-ios: {}
  stage-trigger-run-main:
    workflows:
      - lint: {}
      - typescript: {}
      - docs: {}
      - unit-js: {}
      - unit-example-app: {}
      - unit-android: {}
      - unit-ios: {}
      - example-build-ios: {}
      - example-build-android: {}
      - e2e-android: {}
      - e2e-ios: {}
      - deploy-firebase-android:
          run_if: '{{getenv "ENABLE_INTERNAL_RELEASES" | ne ""}}'
workflows:
  docs:
    steps:
      - yarn@0:
          inputs:
            - command: docs
          title: Generate TypeDoc API reference
      - yarn@0:
          inputs:
            - command: check:docs
          title: Check TypeDoc API reference coverage
      - deploy-to-bitrise-io@2:
          inputs:
            - deploy_path: docs/api-reference
            - notify_user_groups: none
          title: Deploy TypeDoc API reference
    before_run:
      - prep_all
    after_run:
      - complete_all
  complete_all:
    steps:
      - save-gradle-cache@1: {}
      # - save-npm-cache@1: {}
      # - save-cocoapods-cache@1: {}
  deploy-firebase-android:
    steps:
      - fastlane@3:
          inputs:
            - connection: 'off'
            - work_dir: ..
            - lane: increment_version app_id:$FIREBASE_APP_ID_ANDROID version_suffix:$VERSION_SUFFIX
          title: fastlane increment version
      - install-missing-android-tools@3:
          inputs:
            - gradlew_path: dev-app/android/gradlew
          title: Install missing Android SDK components for dev-app
      - yarn@0:
          inputs:
            - command: dev-app:build:android:release
            # - cache_local_deps: 'yes'
          title: Build app for Firebase deploy
      - fastlane@3:
          inputs:
            - connection: 'off'
            - work_dir: ..
            - lane: publish_to_firebase app_id:$FIREBASE_APP_ID_ANDROID firebase_token:$FIREBASE_TOKEN
          title: fastlane distribute app
    before_run:
      - prep_all
    after_run:
      - complete_all
  e2e-android:
    steps:
      - avd-manager@1:
          inputs:
            - emulator_id: TestingAVD
            - tag: default
            - abi: x86_64
            - api_level: '34'
      - yarn@0:
          inputs:
            - command: install --frozen-lockfile
            # - cache_local_deps: 'yes'
          title: yarn install --frozen-lockfile
      - install-missing-android-tools@3:
          inputs:
            - gradlew_path: dev-app/android/gradlew
          title: Install missing Android SDK components for dev-app
      - yarn@0:
          inputs:
            - command: e2e:build:android:release
            # - cache_local_deps: 'yes'
          title: build for maestro test
      - script@1:
          inputs:
            - content: |-
                adb install dev-app/android/app/build/outputs/apk/release/app-release.apk
          title: install app
      - script@1:
          inputs:
            - content: |-
                node e2e/clean.js
                curl -fsSL "https://get.maestro.mobile.dev" | bash
                ~/.maestro/bin/maestro test --debug-output "$BITRISE_DEPLOY_DIR/maestro" --flatten-debug-output -e APP_ID=com.dev.app.stripeterminalreactnative maestro/app.yml
          title: install maestro & run tests
      - deploy-to-bitrise-io@2:
          is_always_run: true
          inputs:
            - deploy_path: $BITRISE_DEPLOY_DIR/maestro
          title: Upload Maestro debug output
    before_run:
      - prep_all
    after_run:
      - complete_all
    meta:
      bitrise.io:
        stack: linux-docker-android-22.04
        machine_type_id: elite-xl
  e2e-ios:
    steps:
      - yarn@0:
          inputs:
            - command: e2e:build:ios:release
            # - cache_local_deps: 'yes'
          title: build for maestro test
      - xcode-start-simulator:
          inputs:
            - destination: platform=iOS Simulator,name=iPhone 17,OS=latest
            - wait_for_boot_timeout: 90
      - script@1:
          inputs:
            - content: |-
                node e2e/clean.js
                xcrun simctl install Booted dev-app/ios/build/Build/Products/Release-iphonesimulator/StripeTerminalReactNativeDevApp.app
                curl -fsSL "https://get.maestro.mobile.dev" | bash
                ~/.maestro/bin/maestro test --debug-output "$BITRISE_DEPLOY_DIR/maestro" --flatten-debug-output -e APP_ID=com.stripe.terminal.reactnative.dev.app maestro/app.yml
          title: install maestro & run tests
      - deploy-to-bitrise-io@2:
          is_always_run: true
          inputs:
            - deploy_path: $BITRISE_DEPLOY_DIR/maestro
          title: Upload Maestro debug output
    before_run:
      - prep_all
      - setup_cocoapods
    after_run:
      - complete_all
  example-build-android:
    steps:
      - avd-manager@1:
          inputs:
            - emulator_id: TestingAVD
            - tag: default
            - abi: x86_64
            - api_level: '34'
      - install-missing-android-tools@3:
          inputs:
            - gradlew_path: example-app/android/gradlew
          title: Install missing Android SDK components for example-app
      - yarn@0:
          inputs:
            - command: e2e:build:example:android
            # - cache_local_deps: 'yes'
          title: yarn e2e:build:example:android
    before_run:
      - prep_all
    after_run:
      - complete_all
    meta:
      bitrise.io:
        stack: linux-docker-android-22.04
        machine_type_id: elite-xl
  example-build-ios:
    steps:
      - script@1:
          inputs:
            - content: |-
                rbenv install --skip-existing 3.2.3
                rbenv local 3.2.3
                gem install cocoapods -v 1.16.2
          title: Set up CocoaPods
      - yarn@0:
          inputs:
            - command: e2e:build:example:ios
            # - cache_local_deps: 'yes'
          title: yarn e2e:build:example:ios
    before_run:
      - prep_all
    after_run:
      - complete_all
    meta:
      bitrise.io:
        stack: osx-xcode-26.5.x
        machine_type_id: g2.mac.large
  lint:
    steps:
      - yarn@0:
          inputs:
            - cache_local_deps: 'yes'
            - command: check:rn-error-codes
          title: yarn check:rn-error-codes
      - yarn@0:
          inputs:
            - cache_local_deps: 'yes'
            - command: check:rn-device-types
          title: yarn check:rn-device-types
      - yarn@0:
          inputs:
            # - cache_local_deps: 'yes'
            - command: lint
          title: yarn lint
      - yarn@0:
          inputs:
            - command: lint:native
          title: lint:native (SwiftLint + Android Null Check)
      - script@1:
          inputs:
            - content: node --test .github/scripts/pack-npm.test.cjs
          title: Publish helper tests
    before_run:
      - prep_all
    after_run:
      - complete_all
  prep_all:
    steps:
      - set-java-version@1:
          inputs:
            - set_java_version: '17'
      - git-clone@8.4.0: {}
      - change-workdir@1:
          inputs:
            - path: $WORKING_DIR
            - is_create_path: false
      # - restore-npm-cache@1: {}
      # - restore-cocoapods-cache@1: {}
      - restore-gradle-cache@1: {}
      - script@1:
          inputs:
            - content: |-
                asdf install nodejs 26.10.0
                asdf global nodejs 26.10.0
                npm install -g yarn
                echo 'export PATH="$PATH:~/project/node_modules/.bin:~/project/dev-app/node_modules/.bin"' >> $BASH_ENV
                source $BASH_ENV
                brew update >/dev/null
                brew bundle install >/dev/null
                touch .watchmanconfig
                npm install --global expo-cli
          title: Misc prep steps
      - yarn@0:
          inputs:
            # - cache_local_deps: 'yes'
            - workdir: example-app
            - command: install
          title: yarn install example-app
      - yarn@0:
          inputs:
            # - cache_local_deps: 'yes'
            - workdir: dev-app
            - command: install
          title: yarn install dev-app
      - yarn@0:
          inputs:
            # - cache_local_deps: 'yes'
            - command: install
          title: yarn install
      - script@1:
          inputs:
            - content: |-
                echo "Checking for diffs in yarn lockfile, if this fails please ensure all dependencies are up to date"
                git diff --exit-code
          title: Check for diffs in yarn lockfile
  setup_cocoapods:
    steps:
      - script@1:
          inputs:
            - content: |-
                set -e
                rbenv install --skip-existing 3.2.3
                rbenv local 3.2.3
                yarn pods
                echo "Checking for diffs in pod lockfile, if this fails please ensure all dependencies are up to date"
                # RN 0.85 prebuilt pods embed absolute paths in podspecs, making their
                # SPEC CHECKSUMS path-dependent. Ignore diffs in only those four pods.
                IGNORED_PODS="hermes-engine|ReactCodegen|React-Core-prebuilt|ReactNativeDependencies"
                UNEXPECTED_DIFFS=$(git diff -- dev-app/ios/Podfile.lock | grep '^[+-]  ' | grep -Ev "^[+-]  ($IGNORED_PODS):" || true)
                if [ -n "$UNEXPECTED_DIFFS" ]; then
                  echo "Unexpected diffs in Podfile.lock:"
                  echo "$UNEXPECTED_DIFFS"
                  exit 1
                fi
          title: Set up cocoapods
  typescript:
    steps:
      - yarn@0:
          inputs:
            # - cache_local_deps: 'yes'
            - command: typescript
          title: yarn typescript
      - yarn@0:
          inputs:
            - workdir: example-app
            # - cache_local_deps: 'yes'
            - command: typescript
          title: yarn typescript example-app
    before_run:
      - prep_all
    after_run:
      - complete_all
  unit-android:
    steps:
      - install-missing-android-tools@3:
          inputs:
            - gradlew_path: android/gradlew
          title: Install missing Android SDK components for library
      - yarn@0:
          inputs:
            - command: unit-test:android
            # - cache_local_deps: 'yes'
          title: yarn unit-test:android
    before_run:
      - prep_all
    after_run:
      - complete_all
    meta:
      bitrise.io:
        stack: linux-docker-android-22.04
        machine_type_id: elite-xl
  unit-js:
    steps:
      - yarn@0:
          inputs:
            - command: unit-test:js --coverage
            # - cache_local_deps: 'yes'
          title: yarn unit-test:js --coverage
    before_run:
      - prep_all
    after_run:
      - complete_all
  unit-example-app:
    steps:
      - yarn@0:
          inputs:
            - workdir: example-app
            - command: test
          title: yarn test (example-app)
    before_run:
      - prep_all
    after_run:
      - complete_all
  unit-ios:
    steps:
      - xcode-test@6:
          inputs:
            - project_path: dev-app/ios/StripeTerminalReactNativeDevApp.xcworkspace
            - scheme: UnitTests
            - destination: platform=iOS Simulator,name=iPhone 17,OS=latest
            # - cache_local_deps: 'yes'
          title: xcodebuild iOS Unit Tests
    before_run:
      - prep_all
      - setup_cocoapods
    after_run:
      - complete_all
meta:
  bitrise.io:
    stack: osx-xcode-26.5.x
    machine_type_id: g2.mac.large
app:
  envs:
    - opts:
        is_expand: false
      PLATFORM: all
    - opts:
        is_expand: false
      API_CA_URL_ANDROID: http://10.0.2.2:3003
    - opts:
        is_expand: false
      API_URL: http://127.0.0.1:3002
    - opts:
        is_expand: false
      API_URL_ANDROID: http://10.0.2.2:3002
trigger_map:
  - push_branch: '*'
    pipeline: main-trigger-pipeline
  - pull_request_source_branch: '*'
    pipeline: pr-trigger-pipeline
