# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=pdm-backend
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.4.6
pkgrel=1
pkgdesc="The build backend used by PDM that supports latest packaging standards (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://backend.pdm-project.org/'
msys2_repository_url='https://github.com/pdm-project/pdm-backend'
msys2_references=(
  'purl: pkg:pypi/pdm-backend'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-pyproject-metadata"
         "${MINGW_PACKAGE_PREFIX}-python-tomli-w")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname/-/_}-${pkgver}.tar.gz"
        "python-pdm-backend-2.3.1-devendor.patch")
sha256sums=('5dd9cd581a4f18d57ff506a5b3aad7c8df31e5949b6fd854bbc34c38107e4532'
            'c87e46b7903c770700a0325bde0420f74d123780e61d5350339775ecc971001a')
noextract=("${_realname/-/_}-${pkgver}.tar.gz")

prepare() {
  echo "Extracting ${_realname/-/_}-${pkgver}.tar.gz ..."
  tar -xzf ${_realname/-/_}-${pkgver}.tar.gz || true
  cd "${srcdir}/${_realname/-/_}-${pkgver}"
  # copied from https://gitlab.archlinux.org/archlinux/packaging/packages/python-pdm-backend/-/blob/main/python-pdm-backend-2.3.1-devendor.patch
  patch -p1 -i "${srcdir}"/python-pdm-backend-2.3.1-devendor.patch
  rm -rf src/pdm/backend/_vendor
}

build() {
  cp -r "${_realname/-/_}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m pytest || warning "Tests failed"
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
