# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=ty
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.0.7
_ruffver=f9afcc400ce56b1cc0dd1f20f3910afb0bb37d44
pkgrel=1
pkgdesc="An extremely fast Python type checker and language server, written in Rust (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/astral-sh/ty'
msys2_documentation_url='https://docs.astral.sh/ty'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-maturin"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             'git')
options=('!strip')
source=("git+${url}#tag=${pkgver}"
        "git+https://github.com/astral-sh/ruff#commit=${_ruffver}")
sha256sums=('7eaaf3ef1c0664d4f7041b2ec374bd88e5d8ea35f061c658912e0612897b17c7'
            '52cd08b804935653577da02988ab7e37990925917ad8d2ed656bc6ea7101f772')

prepare() {
  cd ${_realname}

  git submodule init
  git config submodule.ruff.url ../ruff
  git -c protocol.file.allow=always submodule update

  cargo fetch \
    --manifest-path ruff/crates/${_realname}/Cargo.toml \
    --locked \
    --target "${RUST_CHOST}" \
    --config='net.git-fetch-with-cli=true'
}

build() {
  local target_binary="ruff/target/${RUST_CHOST}/release/${_realname}"

  cd ${_realname}
  export _PYTHON_HOST_PLATFORM=$(python -c "import sysconfig, sys; sys.stdout.write(sysconfig.get_platform())")
  maturin build --locked --release --all-features --target "${RUST_CHOST}" --strip

  for completion in bash fish zsh; do
    $target_binary generate-shell-completion $completion > $completion-completions
  done
}

check() {
  cd ${_realname}

  cargo test \
    --manifest-path ruff/crates/${_realname}/Cargo.toml \
    --frozen \
    --release \
    --all-features
}

package() {
  cd ${_realname}

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

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
  install -Dm644 bash-completions "${pkgdir}${MINGW_PREFIX}"/share/bash-completion/completions/${_realname}
  install -Dm644 fish-completions "${pkgdir}${MINGW_PREFIX}"/share/fish/vendor_completions.d/${_realname}.fish
  install -Dm644 zsh-completions "${pkgdir}${MINGW_PREFIX}"/share/zsh/site-functions/_${_realname}
}
