machine:
  xcode:
    version: 7.2
  node:
    version: 5.7.0
  java:
    version: oraclejdk8
  environment:
    GOPATH: $HOME/go-path
    PATH: $GOROOT/bin:$PATH
    # Limit memory usage of gradle
    _JAVA_OPTIONS: "-Xms512m -Xmx1024m"
    GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xmx1024m -XX:+HeapDumpOnOutOfMemoryError"'
    REACT_NATIVE_MAX_WORKERS: 1
    # Some node dependencies break without this
    CXX: g++-4.8
general:
  build_dir: shared
dependencies:
  pre:
    - sudo rm -rf $HOME/go* # Remove old go installation
    - sudo rm -rf /usr/local/go* # Remove old go installation
    - wget https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz -O $HOME/go.tar.gz
    - mkdir $HOME/go-path
    - (cd $HOME; tar -xzvf go.tar.gz)
    - sudo mv $HOME/go /usr/local
    - if ! $(grep -q "Revision=24.4.1" $ANDROID_HOME/tools/source.properties); then echo y | android update sdk -u -a -t "tools"; fi
    - if [ ! -e $ANDROID_HOME/build-tools/23.0.2 ]; then echo y | android update sdk -u -a -t "build-tools-23.0.2"; fi
     # Android Support Repository, revision 35 / Local Maven repository for Support Libraries
    - if [ ! -d "/usr/local/android-sdk-linux/extras/android/m2repository/com/android/support/design/24.1.0" ]; then echo y | android update sdk --no-ui --all --filter "extra-android-m2repository"; fi
    # Accept android licenses
    - mkdir /usr/local/android-sdk-linux/licenses
    - echo "8933bad161af4178b1185d1a37fbf41ea5269c55" > /usr/local/android-sdk-linux/licenses/android-sdk-license
    # Install Yarn
    - sudo apt-key adv --fetch-keys http://dl.yarnpkg.com/debian/pubkey.gpg
    - echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    - sudo apt-get update -qq
    - sudo apt-get install -y -qq yarn=0.17.9-1
  cache_directories:
    - /usr/local/android-sdk-linux/tools
    - /usr/local/android-sdk-linux/build-tools/23.0.2
    - /usr/local/android-sdk-linux/extras/android/m2repository
  override:
    # TODO(mm): use the emulator when we figure out a consistent way of getting this working
    # - emulator -avd circleci-android22 -no-audio -no-window:
    #    background: true
    #    parallel: true
    # Maybe we dont need this with yarn - rm -rf $HOME/client/react-native/node_modules # Clear node_modules cache, it appears to be getting out of sync
    - yarn install --pure-lockfile
    - yarn global add react-native-cli
    - mkdir -p $GOPATH/src/github.com/keybase
    - ln -s $HOME/client $GOPATH/src/github.com/keybase/client
    - yarn run rn-gobuild-android
test:
  override:
    # TODO(mm) uncomment these device tests (see todo above)
    # wait for it to have booted
    # - circle-android wait-for-boot
    # run tests  against the emulator.
    # - (cd android && ./gradlew connectedAndroidTest)
    # copy the build outputs to artifacts
    # - cp -r android/app/build/outputs $CIRCLE_ARTIFACTS
    # copy the test results to the test results directory.
    # - cp -r android/app/build/outputs/androidTest-results/* $CIRCLE_TEST_REPORTS
    # Build a debug version of the app
    - (cd react-native/android && ./gradlew assembleReleaseUnsigned)
    # Upload it to appetize
    # And grep so we only reveal the public url in the CI logs
    - PLATFORM=android PLATFORM_BUILD_PATH="react-native/android/app/build/outputs/apk/app-releaseUnsigned.apk" S3_URL="s3://kb-appbuilds/" BUILD_URL="https://s3-us-west-2.amazonaws.com/kb-appbuilds/" ./react-native/uploadApp.sh | grep -Eo "\"publicURL\":\"[^\"]*\"|APK_URL:.*$"
