# Maintainer: Maxim Baz <archlinux at maximbaz dot com>
# Maintainer: Daniel M. Capella <polyzen@archlinux.org>
# Contributor: James Zhu <jameszhu@berkeley.edu>

pkgname=python-black
pkgver=24.3.0
pkgrel=3
pkgdesc='Uncompromising Python code formatter'
arch=('any')
url=https://github.com/psf/black
license=('MIT')
depends=('python' 'python-click' 'python-mypy_extensions' 'python-packaging'
         'python-pathspec' 'python-platformdirs')
makedepends=('python-build' 'python-hatch-fancy-pypi-readme' 'python-hatch-vcs'
             'python-hatchling' 'python-installer')
checkdepends=('ipython' 'python-aiohttp' 'python-parameterized' 'python-pytest'
              'python-typed-ast' 'python-tokenize-rt')
optdepends=('ipython: for Jupyter notebook support'
            'python-tokenize-rt: for Jupyter notebook support'
            'python-aiohttp: for the blackd HTTP server'
            'python-colorama: for colored diffs')
source=("https://files.pythonhosted.org/packages/source/b/black/black-$pkgver.tar.gz")
b2sums=('9a853a0943ba081ecb7a4c22913b060607ac47fb90b70a9645358d681f3eab55707ad6fe9ad9928ba0d39a8adf5dfde0168598a8abcc24b823b407287cefaf82')

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

check() {
  cd "black-$pkgver"
  python -m venv --system-site-packages test-env
  test-env/bin/python -m installer dist/*.whl
  # https://github.com/psf/black/issues/3251#issuecomment-1236413890
  ulimit -n 2048
  PATH="$PWD/test-env/bin:$PATH" test-env/bin/python -m pytest
}

package() {
  cd "black-$pkgver"
  python -m installer --destdir="$pkgdir" dist/*.whl

  # Symlink license file
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
  install -d "$pkgdir"/usr/share/licenses/$pkgname
  ln -s "$site_packages"/black-$pkgver.dist-info/licenses/LICENSE \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE

  # # Completions
  # install -vd \
  #   "$pkgdir/usr/share/"{bash-completion/completions,fish/vendor_completions.d,zsh/site-functions}
  # _BLACK_COMPLETE=bash_source test-env/bin/black > \
  #   "$pkgdir/usr/share/bash-completion/completions/black"
  # _BLACK_COMPLETE=fish_source test-env/bin/black > \
  #   "$pkgdir/usr/share/fish/vendor_completions.d/black.fish"
  # _BLACK_COMPLETE=zsh_source test-env/bin/black > \
  #   "$pkgdir/usr/share/zsh/site-functions/_black"
  # _BLACKD_COMPLETE=bash_source test-env/bin/blackd > \
  #   "$pkgdir/usr/share/bash-completion/completions/blackd"
  # _BLACKD_COMPLETE=fish_source test-env/bin/blackd > \
  #   "$pkgdir/usr/share/fish/vendor_completions.d/blackd.fish"
  # _BLACKD_COMPLETE=zsh_source test-env/bin/blackd > \
  #   "$pkgdir/usr/share/zsh/site-functions/_blackd"

  # Vim plugin
  install -Dm644 -t "$pkgdir/usr/share/vim/vimfiles/plugin" plugin/black.vim
  install -Dm644 -t "$pkgdir/usr/share/vim/vimfiles/autoload" autoload/black.vim
}

# vim:set ts=2 sw=2 et:
