version: 2.1
jobs:
  node:
    working_directory: ~/react-native-firebaseui-auth
    docker:
      - image: circleci/node:lts
    steps:
      - checkout

      - restore_cache:
          key: yarn-v1-{{ checksum "example/yarn.lock" }}-{{ arch }}

      - restore_cache:
          key: node-v1-{{ checksum "example/package.json" }}-{{ arch }}

      - run:
          name: install dependences
          command: |
            cd example
            yarn install

      - save_cache:
          key: yarn-v1-{{ checksum "example/yarn.lock" }}-{{ arch }}
          paths:
            - ~/.cache/yarn

      - save_cache:
          key: node-v1-{{ checksum "example/package.json" }}-{{ arch }}
          paths:
            - example/node_modules

      - run:
          name: run tests
          command: |
            mkdir -p test-results/jest
            cd example
            yarn test
          environment:
            JEST_JUNIT_OUTPUT: ./test-results/jest/junit.xml

      - store_test_results:
          path: ./example/test-results

      - persist_to_workspace:
          root: ~/react-native-firebaseui-auth
          paths:
            - example/node_modules

  android:
    working_directory: ~/react-native-firebaseui-auth/example/android
    docker:
      - image: circleci/android:api-30-node
    steps:
      - checkout:
          path: ~/react-native-firebaseui-auth

      - attach_workspace:
          at: ~/react-native-firebaseui-auth

      # - restore_cache:
      #     key: bundle-v1-{{ checksum "Gemfile.lock" }}-{{ arch }}

      # - run: bundle install

      # - save_cache:
      #     key: bundle-v1-{{ checksum "Gemfile.lock" }}-{{ arch }}
      #     paths:
      #       - vendor/bundle

      # - run:
      #     name: fastlane tests
      #     command: |
      #       mkdir -p test-results/fastlane
      #       bundle exec fastlane test
      #       mv fastlane/report.xml test-results/fastlane

      - restore_cache:
          key: jars-cache-{{ .Branch }}-{{ checksum "./build.gradle" }}-{{ checksum  "./app/build.gradle" }}

      - run:
          name: make gradlew executable
          command: |
            echo 'export TERM=xterm' >> $BASH_ENV && sudo chmod +x gradlew

      - run:
          name: accept all licenses on error
          command: |
            yes | $ANDROID_HOME/tools/bin/sdkmanager --licenses || if [ $? -ne '141' ]; then exit $?; fi;  #Captures SIGPIPE 141 error but still allow repeating "y" to accept all licenses

      - run:
          name: build debug
          command: |
            cd app
            mv ./gs ./google-services.json
            cd ../..
            npx jetify
            cd android
            ./gradlew assembleDebug

      - store_artifacts:
          path: ./app/build/outputs/apk/debug/app-debug.apk
          destination: app-debug.apk

  ios:
    macos:
      xcode: "9.0"
    working_directory: ~/react-native-firebaseui-auth/example/ios

    # use a --login shell so our "set Ruby version" command gets picked up for later steps
    shell: /bin/bash --login -o pipefail

    steps:
      - checkout

      - run:
          name: set Ruby version
          command:  echo "ruby-2.4" > ~/.ruby-version

      - restore_cache:
          key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}

      - restore_cache:
          key: node-v1-{{ checksum "package.json" }}-{{ arch }}

      # not using a workspace here as Node and Yarn versions
      # differ between our macOS executor image and the Docker containers above
      - run: yarn install

      - save_cache:
          key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}
          paths:
            - ~/.cache/yarn

      - save_cache:
          key: node-v1-{{ checksum "package.json" }}-{{ arch }}
          paths:
            - node_modules

      - restore_cache:
          key: bundle-v1-{{ checksum "ios/Gemfile.lock" }}-{{ arch }}

      - run:
          command: bundle install
          working_directory: ios

      - save_cache:
          key: bundle-v1-{{ checksum "ios/Gemfile.lock" }}-{{ arch }}
          paths:
            - vendor/bundle

      - run:
          command: bundle exec fastlane test
          working_directory: ios

      - run:
          name: set up test results
          working_directory: ios
          when: always
          command: |
            mkdir -p test-results/fastlane test-results/xcode
            mv fastlane/report.xml test-results/fastlane
            mv fastlane/test_output/report.junit test-results/xcode/junit.xml

      - store_test_results:
          path: ios/test-results

      - store_artifacts:
          path: ios/test-results

workflows:
  version: 2
  node-android-ios:
    jobs:
      - node
      - android:
          requires:
            - node
      # - ios:
      #     requires:
      #       - node
