Skip to content

Commit

Permalink
add install script from coc.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcco committed Oct 27, 2018
1 parent 792bfce commit 6189997
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/install.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=@(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^@PowerShell.*EOF$')) & goto :EOF

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$repo = "iamcco/markdown-preview.nvim"
$file = "markdown-preview-win.zip"

$releases = "https://api.github.com/repos/$repo/releases"

Write-Host Determining latest release
if ($args[0]) { $tag = $args[0] } else { $tag = (Invoke-WebRequest $releases | ConvertFrom-Json)[0].tag_name }

$download = "https://github.com/$repo/releases/download/$tag/$file"
$name = $file.Split(".")[0]
$zip = "$name-$tag.zip"
$dir = "bin"

new-item -Name $dir -ItemType directory -Force

Write-Host Dowloading latest release
Invoke-WebRequest $download -Out $zip

Remove-Item $dir\* -Recurse -Force -ErrorAction SilentlyContinue

Write-Host Extracting release files
Expand-Archive $zip -DestinationPath $dir -Force

Remove-Item $zip -Force
Write-Host markdown-preview install completed.
84 changes: 84 additions & 0 deletions app/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/sh

set -o nounset # error when referencing undefined variable
set -o errexit # exit when command fails

BOLD="$(tput bold 2>/dev/null || echo '')"
GREY="$(tput setaf 0 2>/dev/null || echo '')"
BLUE="$(tput setaf 4 2>/dev/null || echo '')"
RED="$(tput setaf 1 2>/dev/null || echo '')"
NO_COLOR="$(tput sgr0 2>/dev/null || echo '')"
YELLOW="$(tput setaf 3 2>/dev/null || echo '')"

error() {
printf "${RED} $@${NO_COLOR}\n" >&2
}

warn() {
printf "${YELLOW}! $@${NO_COLOR}\n"
}

info() {
printf "${BLUE} $@${NO_COLOR}\n"
}

fetch() {
local command
if hash curl 2>/dev/null; then
set +e
command="curl --fail -L $1"
curl --compressed --fail -L "$1"
rc=$?
set -e
else
if hash wget 2>/dev/null; then
set +e
command="wget -O- -q $1"
wget -O- -q "$1"
rc=$?
set -e
else
error "No HTTP download program (curl, wget) found…"
exit 1
fi
fi

if [ $rc -ne 0 ]; then
error "Command failed (exit code $rc): ${BLUE}${command}${NO_COLOR}"
exit $rc
fi
}

get_latest_release() {
fetch "https://api.github.com/repos/iamcco/markdown-preview.nvim/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/'
}

if [ $# -eq 0 ]; then
info "Fetching latest release."
tag=$(get_latest_release)
else
tag=$1
fi

download() {
mkdir -p bin
cd bin
url="https://github.com/iamcco/markdown-preview.nvim/releases/download/$tag/${1}"
info "Downloading binary from ${url}"
if fetch "${url}" | tar xzfv -; then
chmod a+x ${1%.tar.gz}
return
else
warn "Binary not available for now, please wait for a few minutes."
fi
}

arch=$(uname -sm)
case "${arch}" in
"Linux x86_64") download markdown-preview-linux.tar.gz ;;
"Linux i686") download markdown-preview-linux.tar.gz ;;
"Darwin x86_64") download markdown-preview-macos.tar.gz ;;
*) info "No pre-built binary available for ${arch}.";;
esac
53 changes: 53 additions & 0 deletions autoload/mkdp/util.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let s:mkdp_root_dir = expand('<sfile>:h:h:h')

" echo message
function! mkdp#util#echo_messages(hl, msgs)
Expand Down Expand Up @@ -60,3 +61,55 @@ function! mkdp#util#get_platform() abort
return 'linux'
endfunction

function! mkdp#util#open_terminal(opts) abort
if get(a:opts, 'position', 'bottom') ==# 'bottom'
let p = '5new'
else
let p = 'vnew'
endif
execute 'belowright '.p.' +setl\ buftype=nofile '
setl buftype=nofile
setl winfixheight
setl norelativenumber
setl nonumber
setl bufhidden=wipe
let cmd = get(a:opts, 'cmd', '')
let autoclose = get(a:opts, 'autoclose', 1)
if empty(cmd)
throw 'command required!'
endif
let cwd = get(a:opts, 'cwd', '')
if !empty(cwd) | execute 'lcd '.cwd | endif
let keepfocus = get(a:opts, 'keepfocus', 0)
let bufnr = bufnr('%')
let Callback = get(a:opts, 'Callback', v:null)
if has('nvim')
call termopen(cmd, {
\ 'on_exit': function('s:OnExit', [autoclose, bufnr, Callback]),
\})
else
call term_start(cmd, {
\ 'exit_cb': function('s:OnExit', [autoclose, bufnr, Callback]),
\ 'curwin': 1,
\})
endif
if keepfocus
wincmd p
endif
return bufnr
endfunction

function! s:markdown_preview_installed() abort
echo '[markdown-preview.nvim]: install cpmpleted'
endfunction

function! mkdp#util#install()
let cmd = (mkdp#util#get_platform() ==# 'win' ? 'install.cmd' : './install.sh')
call mkdp#util#open_terminal({
\ 'cmd': cmd,
\ 'cwd': s:mkdp_root_dir . '/app',
\ 'Callback': function('s:markdown_preview_installed')
\})
wincmd p
endfunction

0 comments on commit 6189997

Please sign in to comment.