# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>
# Contributor: Christoph Reiter <reiter.christoph@gmail.com>

_realname=ruff
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.14.0
pkgrel=1
pkgdesc="An extremely fast Python linter, written in Rust (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://astral.sh/ruff'
msys2_repository_url='https://github.com/astral-sh/ruff'
msys2_changelog_url='https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md'
license=('spdx:MIT')
msys2_references=(
  'archlinux: ruff'
  'anitya: 370829'
  'gentoo: dev-util/ruff'
  'purl: pkg:pypi/ruff'
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-maturin"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  'git'
)
options=('!strip')
source=("${_realname}-${pkgver}.tar.gz::${msys2_repository_url}/releases/download/${pkgver}/source.tar.gz")
sha256sums=('877d7e3b9082ac7007e8ebd307ecb9ed3e55bccdc44944570251c7cec495fc7c')

prepare() {
  cd "${_realname}-${pkgver}"

  cargo fetch --locked --config='net.git-fetch-with-cli=true'
}

build() {
  cd "${_realname}-${pkgver}"

  export _PYTHON_HOST_PLATFORM=$(python -c "import sysconfig, sys; sys.stdout.write(sysconfig.get_platform())")
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  maturin build --release --strip --frozen --all-features
}

check() {
  cd "${_realname}-${pkgver}"

  export WINAPI_NO_BUNDLED_LIBRARIES=1
  cargo test --release --frozen --all-features
}

package_ruff() {
  install -Dm755 "${_realname}-${pkgver}/target/release/ruff" "${pkgdir}${MINGW_PREFIX}/bin/ruff"
  install -Dm644 "${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  local _complete="${_realname}-${pkgver}/target/release/ruff generate-shell-completion"
  $_complete bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/${_realname}"
  $_complete fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/${_realname}.fish"
  $_complete zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_${_realname}"
}

package_python-ruff() {
  pkgdesc+=" (Python module)"
  depends=("${MINGW_PACKAGE_PREFIX}-python" "${MINGW_PACKAGE_PREFIX}-ruff")

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" "${_realname}-${pkgver}"/target/wheels/*.whl

  # split ruff binary
  rm -rf "${pkgdir}${MINGW_PREFIX}/bin/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
