name: Publish Python Code

on:
  push:
    branches:
      - "!*"
    tags:
      - "v*"

jobs:
  package:

    runs-on: ubuntu-latest
    env:
      OS: ubuntu-latest
      PYTHON: '3.10'

    steps:
    - name: Checkout the project
      uses: actions/checkout@v3

    - name: Setup Python
      uses: actions/setup-python@master
      with:
        python-version: ${{ env.PYTHON }}
        cache: 'pip'

    - name: Set env
      run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

    - name: fix release version
      run: |
        sed -i /^RELEASE_VERSION/d setup.py
        sed -i /RELEASE_VERSION$/d setup.py
        sed -i s/{RELEASE_VERSION}/${{ env.RELEASE_VERSION }}/ setup.py

    - name: Install packages required for publishing
      run: pip install twine wheel setuptools

    - name: Build the library
      run: python setup.py sdist

    - name: check the library
      run: twine check dist/*

    - name: Publish package
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        user: __token__
        password: ${{ secrets.PYPI_API_TOKEN }}
