dist: xenial
language: bash
git:
  depth: 1
  quiet: true

branches:
  # Don't build these branches
  except:
    # Used by bors
    - trying.tmp
    - staging.tmp

cache:
  directories:
    - $HOME/.cargo
before_cache:
  - cargo install cargo-cache --debug
  - find $HOME/.cargo/bin/ ! -type d -exec strip {} \;
  - cargo cache --autoclean

env:
  global:
    - RUST_BACKTRACE=1
    - secure: "OKulfkA5OGd/d1IhvBKzRkHQwMcWjzrzbimo7+5NhkUkWxndAzl+719TB3wWvIh1i2wXXrEXsyZkXM5FtRrHm55v1VKQ5ibjEvFg1w3NIg81iDyoLq186fLqywvxGkOAFPrsePPsBj5USd5xvhwwbrjO6L7/RK6Z8shBwOSc41s="

before_install:
  - export CARGO_TARGET_DIR="$TRAVIS_BUILD_DIR/target"
  - |
    case "$TRAVIS_OS_NAME" in
      linux ) HOST=x86_64-unknown-linux-gnu;;
      osx ) HOST=x86_64-apple-darwin;;
      windows ) HOST=x86_64-pc-windows-msvc;;
    esac
  - curl -sSL https://sh.rustup.rs | sh -s -- -y --default-host="$HOST" --default-toolchain=nightly --profile=minimal
  - export PATH="$HOME/.cargo/bin:$PATH"
install:
  - |
    if [[ -z ${INTEGRATION} ]]; then
      if ! rustup component add rustfmt; then
        TARGET=$(rustc -Vv | awk '/host/{print $2}')
        NIGHTLY=$(curl -s "https://rust-lang.github.io/rustup-components-history/${TARGET}/rustfmt")
        curl -sSL "https://static.rust-lang.org/dist/${NIGHTLY}/rustfmt-nightly-${TARGET}.tar.xz" | \
        tar -xJf - --strip-components=3 -C ~/.cargo/bin
        rm -rf ~/.cargo/bin/doc
      fi
      if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
        . $HOME/.nvm/nvm.sh
        nvm install stable
        nvm use stable
        npm install remark-cli remark-lint
      elif [[ "$TRAVIS_OS_NAME" == "windows" ]]; then
        choco install windows-sdk-10.1
      fi
    fi

# disabling the integration tests in forks should be done with
# if: fork = false
# but this is currently buggy travis-ci/travis-ci#9118
matrix:
  fast_finish: true
  include:
    # Builds that are executed for every PR
    - os: linux
    # i686 toolchain could run on x86_64 system.
    - os: linux
      env: HOST_TOOLCHAIN=i686-unknown-linux-gnu
      addons:
        apt:
          packages:
            - gcc-multilib
            - libssl-dev:i386     # openssl dev in Cargo.toml
      if: branch IN (auto, try)
    - os: windows
      env: CARGO_INCREMENTAL=0 OS_WINDOWS=true

    # Builds that are only executed when a PR is r+ed or a try build is started
    # We don't want to run these always because they go towards
    # the build limit within the Travis rust-lang account.
    # The jobs are approximately sorted by execution time
    - os: osx
      if: branch IN (auto, try)
    - env: INTEGRATION=rust-lang/rls
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-lang/cargo
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-lang/chalk
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=Geal/nom
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-lang/rustfmt
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=hyperium/hyper
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-itertools/itertools
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    # FIXME: rustc ICE on `serde_test_suite`
    # - env: INTEGRATION=serde-rs/serde
    #   if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-lang/stdarch
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-random/rand
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-lang/futures-rs
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=Marwes/combine
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-lang-nursery/failure
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=rust-lang/log
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
    - env: INTEGRATION=chronotope/chrono
      if: repo =~ /^rust-lang\/rust-clippy$/ AND branch IN (auto, try)
  allow_failures:
    - os: windows
      env: CARGO_INCREMENTAL=0 OS_WINDOWS=true

before_script:
  - |
    if [[ "$TRAVIS_BRANCH" == "auto" ]] || [[ "$TRAVIS_BRANCH" == "try" ]]; then
      PR=$(echo "$TRAVIS_COMMIT_MESSAGE" | grep -o "#[0-9]*" | head -1 | sed 's/^#//g')
      output=$(curl -H "Authorization: token $GITHUB_API_TOKEN" -s "https://api.github.com/repos/rust-lang/rust-clippy/pulls/$PR" | \
        python -c "import sys, json; print(json.load(sys.stdin)['body'])" | \
        grep "^changelog: " | \
        sed "s/changelog: //g")
      if [[ -z "$output" ]]; then
        echo "ERROR: PR body must contain 'changelog: ...'"
        exit 1
      elif [[ "$output" = "none" ]]; then
        echo "WARNING: changelog is 'none'"
      fi
    fi
  - |
    rm rust-toolchain
    ./setup-toolchain.sh
  - |
    SYSROOT=$(rustc --print sysroot)
    case "$TRAVIS_OS_NAME" in
      windows ) export PATH="${SYSROOT}/bin:${PATH}" ;;
      linux ) export LD_LIBRARY_PATH="${SYSROOT}/lib${LD_LIBRARY_PATH+:${LD_LIBRARY_PATH}}" ;;
      osx )
        # See <https://github.com/nteract/nteract/issues/1523#issuecomment-301623519>
        sudo mkdir -p /usr/local/lib
        sudo find "$SYSROOT/lib" -maxdepth 1 -name '*.dylib' -exec ln -s {} /usr/local/lib \;
        ;;
    esac

script:
  - |
    if [[ -n ${INTEGRATION} ]]; then
      cargo test --test integration --features integration && sleep 5
    else
      ./ci/base-tests.sh && sleep 5
    fi

after_success:
  - |
    if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
      set -e
      if [[ -z ${INTEGRATION} ]]; then
        ./.github/deploy.sh
      else
        echo "Not deploying, because we're in an integration test run"
      fi
      set +e
    fi