# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT

name: Release Actions

on:
  release:
    types: [published]

jobs:
  upload-release-assets:
    runs-on: ubuntu-latest
    steps:
    - name: Dump GitHub context
      env:
        GITHUB_CONTEXT: ${{ toJson(github) }}
      run: echo "$GITHUB_CONTEXT"
    - name: Translate Repo Name For Build Tools filename_prefix
      id: repo-name
      run: |
        echo ::set-output name=repo-name::$(
        echo ${{ github.repository }} |
        awk -F '\/' '{ print tolower($2) }' |
        tr '_' '-'
        )
    - name: Set up Python 3.6
      uses: actions/setup-python@v1
      with:
        python-version: 3.6
    - name: Versions
      run: |
        python3 --version
    - name: Checkout Current Repo
      uses: actions/checkout@v1
      with:
        submodules: true
    - name: Checkout tools repo
      uses: actions/checkout@v2
      with:
        repository: adafruit/actions-ci-circuitpython-libs
        path: actions-ci
    - name: Install deps
      run: |
        source actions-ci/install.sh
    - name: Build assets
      run: circuitpython-build-bundles --filename_prefix ${{ steps.repo-name.outputs.repo-name }} --library_location .
    - name: Upload Release Assets
      # the 'official' actions version does not yet support dynamically
      # supplying asset names to upload. @csexton's version chosen based on
      # discussion in the issue below, as its the simplest to implement and
      # allows for selecting files with a pattern.
      # https://github.com/actions/upload-release-asset/issues/4
      #uses: actions/upload-release-asset@v1.0.1
      uses: csexton/release-asset-action@master
      with:
        pattern: "bundles/*"
        github-token: ${{ secrets.GITHUB_TOKEN }}

  upload-pypi:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Check For setup.py
      id: need-pypi
      run: |
        echo ::set-output name=setup-py::$( find . -wholename './setup.py' )
    - name: Set up Python
      if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
      uses: actions/setup-python@v1
      with:
        python-version: '3.x'
    - name: Install dependencies
      if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
      run: |
        python -m pip install --upgrade pip
        pip install setuptools wheel twine
    - name: Build and publish
      if: contains(steps.need-pypi.outputs.setup-py, 'setup.py')
      env:
        TWINE_USERNAME: ${{ secrets.pypi_username }}
        TWINE_PASSWORD: ${{ secrets.pypi_password }}
      run: |
        python setup.py sdist
        # After the dist file is packaged, extract it, update the __version__
        # lines and repackage it.
        cd dist
        ZIP_FILE=`ls | sed -e "s/\.tar\.gz$//"`
        echo "ZIP FILE = ${ZIP_FILE}"
        tar xzf "${ZIP_FILE}.tar.gz"
        echo The latest release version is \"${{github.event.release.tag_name}}\".
        # Don't descend into ./.env, ./.eggs, or ./docs
        for file in $(find -not -path "./.*" -not -path "./docs*" -name "*.py"); do
            sed -i -e "s/0.0.0-auto.0/${{github.event.release.tag_name}}/" $file;
        done;
        tar czf "${ZIP_FILE}.tar.gz" "${ZIP_FILE}"
        rm -rf "${ZIP_FILE}"
        cd ..
        twine upload dist/*
