sudo: required
dist: xenial
language: python
python: "3.7"

jobs:
  include:

    - stage: code quality
      name: "pylint & mypy"
      install:
        - make install
      script: make lint

    - stage: test
      name: "LBRY Unit Tests"
      install:
        - make install
      script:
        - cd lbry && HOME=/tmp coverage run -p --source=lbry -m unittest discover -vv tests.unit
      after_success:
        - coverage combine lbry/
        - bash <(curl -s https://codecov.io/bash)

    - name: "LBRY Integration Tests"
      install:
        - pip install coverage tox-travis
      script: cd lbry && tox
      after_success:
        - coverage combine lbry
        - bash <(curl -s https://codecov.io/bash)

    - &torba-tests
      name: "Torba Unit Tests"
      env: TESTTYPE=unit
      install:
        - pip install coverage tox-travis
      script: cd torba && tox
      after_success:
        - coverage combine torba/tests
        - bash <(curl -s https://codecov.io/bash)

    - <<: *torba-tests
      name: "Torba Integration Tests"
      env: TESTTYPE=integration

    - name: "Run Examples"
      install:
        - make install
      script:
        - cd lbry && HOME=/tmp coverage run -p --source=lbry scripts/generate_json_api.py
      after_success:
        - coverage combine lbry
        - bash <(curl -s https://codecov.io/bash)

    - &build
      stage: build
      name: "Linux"
      env: OS=linux
      install:
        - pip install pyinstaller awscli
        - cd torba && pip install -e . && cd ..
        - cd lbry
        - python scripts/set_build.py
        - pip install -e .
      script:
        - pyinstaller -F -n lbrynet lbry/extras/cli.py
        - cd dist
        - chmod +x lbrynet
        - zip -j lbrynet-${OS}.zip lbrynet
        - shasum -a 256 -b lbrynet-${OS}.zip
        - ./lbrynet --version
      after_success:
        - aws configure set aws_access_key_id $ARTIFACTS_KEY
        - aws configure set aws_secret_access_key $ARTIFACTS_SECRET
        - aws configure set region us-east-1
        - export S3_PATH="daemon/build-${TRAVIS_BUILD_NUMBER}_commit-${TRAVIS_COMMIT:0:7}_branch-${TRAVIS_BRANCH}$([ ! -z ${TRAVIS_TAG} ] && echo _tag-${TRAVIS_TAG})"
        - aws s3 cp lbrynet-${OS}.zip s3://build.lbry.io/${S3_PATH}/lbrynet-${OS}.zip
      deploy:
        provider: releases
        api_key: $GITHUB_OAUTH_TOKEN
        file: lbrynet-${OS}.zip
        skip_cleanup: true
        overwrite: true
        draft: true
        on:
          tags: true

    - <<: *build
      name: "Mac"
      os: osx
      osx_image: xcode8.3
      language: shell
      env: OS=mac
      before_install:
        - brew upgrade python || true
        - pip3 install --user --upgrade pip virtualenv
        - /Users/travis/Library/Python/3.7/bin/virtualenv $HOME/venv
        - source $HOME/venv/bin/activate
      before_cache:
        - brew cleanup

    - <<: *build
      name: "Windows"
      os: windows
      language: shell
      env:
        - OS=windows
        - PATH=/c/Python37:/c/Python37/Scripts:/C/Windows/System32/downlevel:$PATH
      before_install:
        - choco install python --x86
        - python -m pip install --upgrade pip
        - pip install pywin32
      script:
        - pyinstaller --additional-hooks-dir=scripts/. --icon=icons/lbry256.ico -F -n lbrynet lbry/extras/cli.py
        - cd dist
        - 7z a -tzip lbrynet-windows.zip lbrynet.exe
        - sha256sum -b lbrynet-windows.zip
        - ./lbrynet.exe --version
    - if: tag IS present
      stage: build
      name: "Wallet Server Docker Image - Tagged Release"
      script:
          - set -e
          - echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin
          - travis_retry docker build -t lbry/wallet-server:$TRAVIS_TAG -f lbry/scripts/Dockerfile.wallet_server .
          - docker push lbry/wallet-server:$TRAVIS_TAG

    - if: tag IS blank AND branch = master AND NOT type IN (pull_request)
      stage: build
      name: "Wallet Server Docker Image - Master"
      script:
          - set -e
          - echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin
          - travis_retry docker build -t lbry/wallet-server:master -f lbry/scripts/Dockerfile.wallet_server .
          - docker push lbry/wallet-server:master

cache:
  directories:
    - $HOME/venv
    - $HOME/.cache/pip
    - $HOME/Library/Caches/pip
    - $HOME/Library/Caches/Homebrew
    - $TRAVIS_BUILD_DIR/.tox
