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

_realname=tinymist
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# WARNING: Odd-numbered versions (e.g. 0.12.11) use not stable typst!
pkgver=0.14.4
pkgrel=1
pkgdesc="An integrated language service for Typst (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://myriad-dreamin.github.io/tinymist'
msys2_repository_url='https://github.com/Myriad-Dreamin/tinymist'
msys2_changelog_url='https://github.com/Myriad-Dreamin/tinymist/blob/main/editors/vscode/CHANGELOG.md'
msys2_references=(
  'archlinux: tinymist'
  'purl: pkg:cargo/tinymist'
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" 'git')
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('f0ff8daa615aaa199095d2c1f2dd8505d2cb2a5d83b852da7dd70c00245f72a2')

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

  rm rust-toolchain.toml
  cargo fetch --locked \
    --config='net.git-fetch-with-cli=true' \
    --target "${RUST_CHOST}"
}

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

  cargo build --release --frozen -p tinymist-cli
}

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

  cargo test --release --frozen -p tinymist-cli
}

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

  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path crates/tinymist-cli \
    --root "${pkgdir}${MINGW_PREFIX}"

  local _target="target/release/tinymist completion"
  install -Dm644 <($_target bash) "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/${_realname}"
  install -Dm644 <($_target fish) "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/${_realname}.fish"
  install -Dm644 <($_target zsh) "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_${_realname}"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
