# Maintainer: Orhun Parmaksız <orhun@archlinux.org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: MithicSpirit <rpc01234 at gmail dot com>
# Contributor: David Runge <dvzrv@archlinux.org>
# Contributor: Leonidas Spyropoulos <artafinde@archlinux.org>
# Contributor: Daniel M. Capella <polyzen@archlinux.org>
# Contributor: Guillaume Gauvrit <guillaume@gauvr.it>

pkgbase=uv
pkgname=("$pkgbase" "python-$pkgbase"{,-build})
pkgver=0.9.10
pkgrel=1
pkgdesc='An extremely fast Python package installer and resolver written in Rust'
arch=('x86_64' 'aarch64')
url="https://github.com/astral-sh/uv"
license=('MIT' 'Apache-2.0')
depends=(
  'bzip2'
  'gcc-libs'
  'glibc'
)
makedepends=(
  'cargo'
  'cmake'
  'git'
  'maturin'
  'python-installer'
  'xz'
)
options=('!lto')
source=("git+$url.git#tag=$pkgver")
sha256sums=('588637bfc9e66e8a51d64cb8601021fe090398a21be19d586d27a22efbbd6926')

prepare() {
  cd "$pkgbase"
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
  mkdir -p completions
}

# Note --frozen doesn't work here because cargo fetch didn't get everything
# maturin ends up trying to use so we make do with --locked ...
build() {
  cd "$pkgbase"
  local tripple="$(rustc -vV | sed -n 's/host: //p')"

  export RUSTFLAGS="-C lto=false"
  # Note: do not use --all-features as in enables a self-updater
  maturin build --locked --release --target "$tripple" --strip --compatibility linux
  maturin build --locked --release --target "$tripple" --strip --compatibility linux -m crates/uv-build/Cargo.toml
  local compgen="target/$tripple/release/uv --generate-shell-completion"
  $compgen bash > "completions/$pkgbase"
  $compgen elvish > "completions/$pkgbase.elv"
  $compgen fish > "completions/$pkgbase.fish"
  $compgen zsh > "completions/_$pkgbase"
}

check() {
  cd "$pkgbase"
  # The upstream cargo tests are unit tests against a matrix of Python versions
  # using vendored Python installs. Even collapsing the matrix to match our
  # system Python version and patching around the path issues to use it,
  # a majority of the unit tests are irrelevant.
  local tripple="$(rustc -vV | sed -n 's/host: //p')"
  local _target="target/$tripple/release/uv"
  $_target -V | grep -F "$pkgname $pkgver"
}

_package_common() {
  install -Dm0644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE-*
  install -Dm0644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md
}

package_uv() {
  depends=(
    gcc-libs
    glibc
    bzip2
  )

  cd "$pkgbase"
  _package_common
  local _target="target/$(rustc -vV | sed -n 's/host: //p')/release/uv"
  install -Dm0755 -t "$pkgdir/usr/bin/" "$_target"
  install -Dm0755 -t "$pkgdir/usr/bin/" "${_target}x"
  install -Dm 644 "completions/$pkgbase" -t "$pkgdir/usr/share/bash-completion/completions/"
  install -Dm 644 "completions/$pkgbase.elv" -t "$pkgdir/usr/share/elvish/lib/"
  install -Dm 644 "completions/$pkgbase.fish" -t "$pkgdir/usr/share/fish/vendor_completions.d/"
  install -Dm 644 "completions/_$pkgbase" -t "$pkgdir/usr/share/zsh/site-functions/"
}

package_python-uv() {
  pkgdesc+=' - Python wrapper'
  depends=(
    python
  )

  cd "$pkgbase"
  _package_common
  depends=(python "$pkgbase=$pkgver")
  python -m installer -d "$pkgdir" target/wheels/uv-$pkgver-*.whl
  rm -rf "$pkgdir/usr/bin"
}

package_python-uv-build() {
  pkgdesc+=' - Python build backend'
  depends=(
    bzip2
    gcc-libs
    glibc
    python
    xz
  )
  groups=(python-build-backend)

  cd "$pkgbase"
  _package_common
  python -m installer -d "$pkgdir" target/wheels/uv_build-$pkgver-*.whl
}

# vim: ts=2 sw=2 et:
