tasks/python.tasks.ps1

$PythonPoetryProject = ""
$PythonPublishUser = ""
$PythonPublishPassword = ""
$PythonPackageRepoUrl = ""      # e.g. https://pkgs.dev.azure.com/myOrg/Project/_packaging/azure-python-feed/pypi/upload


task InstallPythonPoetry {
    $poetry = Get-Command poetry -ErrorAction SilentlyContinue
    if (!$poetry) {
        Write-Build White "Installing Poetry"
        Invoke-WebRequest -Uri https://install.python-poetry.org/ -OutFile get-poetry.py
        python get-poetry.py --yes
        Remove-Item get-poetry.py
    }
    else {
        Write-Build Green "Poetry already installed"
    }
}

task RunFlake8 -If { $PythonPoetryProject -ne "" } InitialisePythonPoetry,{
    Write-Build White "Running flake8"
    exec { & poetry run flake8 src -v }
}

task InitialisePythonPoetry -If { $PythonPoetryProject -ne "" } InstallPythonPoetry,{

    if (!(Test-Path (Join-Path $PythonPoetryProject "pyproject.toml"))) {
        throw "pyproject.toml not found in $PythonPoetryProject"
    }

    $env:POETRY_VIRTUALENVS_IN_PROJECT = "true"     # review whether we want to keep this?
    exec { & poetry install --with dev,test }
}

task RunPyTests -If { $PythonPoetryProject -ne "" } InitialisePythonPoetry,{
    exec { & poetry run pytest --cov=src --cov-report=xml --cov-report=term-missing --junitxml=./pytest-test-results.xml }
}

task BuildPythonPackages -If { $PythonPoetryProject -ne "" } GitVersion,InitialisePythonPoetry,{
    exec { & poetry version $GitVersion.MajorMinorPatch }
    exec { & poetry build }
}

task PublishPythonPackages -If { $PythonPoetryProject -ne "" } InitialisePythonPoetry,{
    exec { & poetry config repositories.ci-python-feed $PythonPackageRepoUrl }
    exec { & poetry publish -u $PythonPublishUser -p $PythonPublishPassword -r ci-python-feed }
}