description: Builds the Android app at the given path with the given build types.  This should be run only after installing dependencies.  

parameters:
  project_path:
    description: The path to the root of the Android project you want to build, relative to the root of the repository. 
    type: string
    default: "./android"
  build_type:
    description: The build type to build.  This is normally either "debug" or "release" but you may have custom build types configured for your app. 
    type: string
    default: "debug"

steps:
  - run:
      name: Create cache checksum files
      command: |
        mkdir -p ~/.tmp/checksumfiles
        find .  -type f -name 'build.gradle' -not -path "*node_modules*" -exec cat {} + >> ~/.tmp/checksumfiles/build.gradle
        find .  -type f -name 'settings.gradle' -not -path "*node_modules*" -exec cat {} + >> ~/.tmp/checksumfiles/settings.gradle

  - restore_cache:
      keys:
        - gradle-wrapper-{{ arch }}-{{ checksum "<<parameters.project_path>>/gradle/wrapper/gradle-wrapper.properties" }}-{{ .Environment. CACHE_VERSION }}

  - restore_cache:
      keys:
        - gradle-home-cache-{{ arch }}-{{ checksum "~/.tmp/checksumfiles/build.gradle" }}-{{ checksum "~/.tmp/checksumfiles/settings.gradle" }}-{{ .Environment. CACHE_VERSION }}

  - restore_cache:
      name: Restoring Gradle Build caches
      keys:
        - gradle-build-cache-{{ .Revision }}

  - run:
      name: Dispersing Gradle Build caches for restoring
      command: |
        [ -d ~/gradle-build-caches ] &&
          [ -n "$(ls -A ~/gradle-build-caches)" ] &&
          rm -rf ~/.gradle/caches/build-cache-* &&
          mkdir -p ~/.gradle/caches/ &&
          mv ~/gradle-build-caches/* ~/.gradle/caches/ || true

  # download and cache dependencies and Gradle
  - run:
      name: Downloading Gradle Dependencies
      command: "cd <<parameters.project_path>> && ./gradlew --max-workers 2 downloadDependencies"

  - save_cache:
      name: Saving Gradle wrapper cache
      paths:
        - ~/.gradle/wrapper/
      key: gradle-wrapper-{{ arch }}-{{ checksum "<<parameters.project_path>>/gradle/wrapper/gradle-wrapper.properties" }}-{{ .Environment. CACHE_VERSION }}

  - save_cache:
      name: Saving Gradle home cache
      paths:
        - ~/.gradle/caches/
      key: gradle-home-cache-{{ arch }}-{{ checksum "~/.tmp/checksumfiles/build.gradle" }}-{{ checksum "~/.tmp/checksumfiles/settings.gradle" }}-{{ .Environment. CACHE_VERSION }}

  - run:
      name: Build Android APK
      command: "cd <<parameters.project_path>> && chmod +x gradlew && ./gradlew --build-cache --max-workers 2 --continue assemble<<parameters.build_type>> assembleAndroidTest -DtestBuildType=<<parameters.build_type>> --stacktrace"

  - run:
      name: Collecting Gradle Build caches for saving
      command: |
        mkdir -p ~/gradle-build-caches
        [ -d ~/.gradle/caches ] &&
          [ -n "$(ls -Ad ~/.gradle/caches/build-cache-* 2>/dev/null)" ] &&
          rm -rf ~/gradle-build-caches/* &&
          mv ~/.gradle/caches/build-cache-* ~/gradle-build-caches || true
      when: always

  - save_cache:
      name: Saving Gradle Build caches
      paths:
        - ~/gradle-build-caches
      key: gradle-debug-build-cache-{{ .Revision }}
      when: always
