Files
nuwiki/scripts/download_bin.vim
T
gffranco 96d53dddf3
CI / cargo fmt --check (push) Successful in 33s
CI / cargo clippy (push) Successful in 36s
CI / cargo test (push) Successful in 46s
CI / editor keymaps (push) Successful in 1m49s
fix(install): guard download_bin.vim against 'compatible' mode (dein build)
The dein/vim-plug build hook runs `vim -e -s -c "source scripts/download_bin.vim"`,
which starts in 'compatible' mode. There, leading-backslash line
continuations aren't recognised, so the multi-line URL string raised
`E10: \ should be followed by /, ? or &`. The install actually succeeded
(binary downloaded), but in silent Ex mode any emitted error makes Vim
exit non-zero — so dein reported a build failure. :NuwikiInstall worked
because a normal session is 'nocompatible'.

Wrap the script in the standard `let s:cpo_save = &cpo | set cpo&vim` …
`let &cpo = s:cpo_save` guard so line continuations parse regardless of
how it's invoked. Verified the exact dein command now exits 0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 02:05:56 +00:00

102 lines
3.5 KiB
VimL

" scripts/download_bin.vim — install `nuwiki-ls` from a release tarball,
" falling back to `cargo build --release` on failure.
"
" Invoked by Dein / vim-plug build hooks:
" vim -e -s -c "source scripts/download_bin.vim" -c "q"
"
" That invocation runs in 'compatible' mode, where leading-backslash line
" continuations aren't recognised (they'd raise E10, and any error in silent
" Ex mode makes Vim exit non-zero — a spurious build failure). Reset
" 'cpoptions' to the Vim default for the duration so the script parses, and
" restore it at the end.
let s:cpo_save = &cpo
set cpo&vim
let s:plugin_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h:h')
let s:bin_dir = s:plugin_dir . '/bin'
let s:suffix = has('win32') ? '.exe' : ''
let s:bin = s:bin_dir . '/nuwiki-ls' . s:suffix
call mkdir(s:bin_dir, 'p')
function! s:target() abort
if has('win32')
return 'x86_64-pc-windows-msvc'
elseif has('mac')
if has('arm64')
return 'aarch64-apple-darwin'
endif
return 'x86_64-apple-darwin'
elseif has('linux')
let l:ldd = system('ldd --version 2>&1')
let l:musl = stridx(l:ldd, 'musl') >= 0
let l:uname_m = trim(system('uname -m'))
if l:uname_m ==# 'aarch64' || l:uname_m ==# 'arm64'
return l:musl ? 'aarch64-unknown-linux-musl' : 'aarch64-unknown-linux-gnu'
endif
return l:musl ? 'x86_64-unknown-linux-musl' : 'x86_64-unknown-linux-gnu'
endif
return ''
endfunction
function! s:build_from_source() abort
if !executable('cargo')
echohl ErrorMsg | echom 'nuwiki: cargo not found, cannot build from source' | echohl None
return 0
endif
echom 'nuwiki: cargo build --release …'
call system('cargo build --release -p nuwiki-ls --manifest-path ' . shellescape(s:plugin_dir . '/Cargo.toml'))
if v:shell_error != 0
echohl ErrorMsg | echom 'nuwiki: cargo build failed' | echohl None
return 0
endif
let l:built = s:plugin_dir . '/target/release/nuwiki-ls' . s:suffix
call system('cp ' . shellescape(l:built) . ' ' . shellescape(s:bin))
call system('chmod +x ' . shellescape(s:bin))
return executable(s:bin)
endfunction
function! s:download_release() abort
let l:target = s:target()
if empty(l:target)
return 0
endif
if !executable('curl') || !executable('tar')
return 0
endif
" Gitea serves the most recent release under the `latest` tag segment
" (/releases/download/latest/<asset>). NOT GitHub's
" /releases/latest/download/<asset> shape, which Gitea 404s.
let l:url = 'https://code.gfran.co/gffranco/nuwiki/releases/download/latest/nuwiki-ls-'
\ . l:target . '.tar.gz'
let l:archive = tempname() . '.tar.gz'
call system('curl -fsSL -o ' . shellescape(l:archive) . ' ' . shellescape(l:url))
if v:shell_error != 0
return 0
endif
let l:extract = tempname()
call mkdir(l:extract, 'p')
call system('tar -xzf ' . shellescape(l:archive) . ' -C ' . shellescape(l:extract))
if v:shell_error != 0
return 0
endif
call system('cp ' . shellescape(l:extract . '/nuwiki-ls' . s:suffix) . ' ' . shellescape(s:bin))
call system('chmod +x ' . shellescape(s:bin))
return executable(s:bin)
endfunction
if get(g:, 'nuwiki_build_from_source', 0)
if !s:build_from_source()
echohl ErrorMsg | echom 'nuwiki: source build failed' | echohl None
endif
elseif s:download_release()
echom 'nuwiki: installed ' . s:bin
else
echohl WarningMsg | echom 'nuwiki: download failed, falling back to source build' | echohl None
if !s:build_from_source()
echohl ErrorMsg | echom 'nuwiki: install failed' | echohl None
endif
endif
let &cpo = s:cpo_save