# Maintainer: David Runge <dvzrv@archlinux.org>

# set to 0 to use vendored sources
_devendored=1
_name=pdm-backend
pkgname=python-pdm-backend
pkgver=2.4.6
pkgrel=1
pkgdesc="The build backend used by PDM that supports latest packaging standards"
arch=(any)
url="https://github.com/pdm-project/pdm-backend"
license=(MIT)
groups=(python-build-backend)
depends=(
  python
)
if (( _devendored == 1 )); then
  # NOTE: devendored from sources
  depends+=(
    python-packaging
    python-pyproject-metadata
    python-tomli-w
  )
fi
makedepends=(
  python-build
  python-installer
)
checkdepends=(
  git
  mercurial
  python-editables
  python-pytest
  python-setuptools
)
optdepends=(
  'python-setuptools: for setuptools support'
  'python-editables: for editables backend support'
)
source=(
  $_name-$pkgver.tar.gz::$url/archive/refs/tags/$pkgver.tar.gz
  $pkgname-2.4.3-devendor.patch
)
sha256sums=('0164657b2456e3bb76300e9d3f29e6deb1f00a8391920b84d327c9a7ea9c63c7'
            '2bc1924abc74c66ea3056ddefdcf7a7e9b76c4ca9f87a0bbddc1a5da73a99eaa')
b2sums=('2e2de500fa61f059d350388c73b9585c582f1bf4b6a45a2215e97a8c130873d2ffad3113fdf0db7ea16eb49066e09539d7db7cf3a55be4098180837c51efa8d0'
        'c65bd035b0caad6b18059de5fec99ee358dc2e5552de9349e9d2c1166f95d6bd9615a0087f0003ff828002eb0ea0a36461db148acb694a0ac6ec469235724aa6')

prepare() {
  if (( _devendored == 1 )); then
    patch -Np1 -d $_name-$pkgver -i ../$pkgname-2.4.3-devendor.patch
    rm -frv $_name-$pkgver/src/pdm/backend/_vendor
  fi
}

build() {
  cd $_name-$pkgver
  PDM_BUILD_SCM_VERSION="$pkgver" python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  local pytest_options=(
    -vv
  )
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $_name-$pkgver
  # install to temporary location, as importlib is used
  python -m installer --destdir=test_dir dist/*.whl
  export PYTHONPATH="$PWD/test_dir/$site_packages:$PYTHONPATH"

  # set default git config for test
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
  pytest "${pytest_options[@]}"
}

package() {
  cd $_name-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -vDm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm 644 README.md -t "$pkgdir/usr/share/doc/$pkgname/"
}
