Skip to content

Commit bff37ca

Browse files
author
Gaspar Chilingarov
committed
Add autoupdate functionality to check correct channel once a day
1 parent 875d062 commit bff37ca

File tree

4 files changed

+187
-20
lines changed

4 files changed

+187
-20
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ cd ~/.vim/bundle/vim-ide-elixir
109109
./update.sh
110110
```
111111

112+
## Autoupdate
113+
114+
By default Vim-Elixir-IDE is setup to check once a day updates from production branch.
115+
116+
You can control update check period by changing `g:vimide_update_check_period` time in seconds.
117+
E.g. for a once-a-week check:
118+
119+
```vim
120+
let g:vimide_update_check_period = 7*24*60*60
121+
```
122+
123+
Also you can control which update channel it will be checking. There are 3
124+
possible channels available:
125+
126+
* `production` will be released once in 6-8 weeks, most stable version.
127+
* `beta` will be released every 1-3 weeks, some things may break.
128+
* `dev` follows master branch of repository, fetches updates as soon as they
129+
available. Things may break more often.
130+
131+
Set update channel by setting in your `.vimrc`
132+
133+
```vim
134+
let g:vimide_update_channel = 'beta'
135+
```
136+
112137
## Editing
113138

114139
Vim-Elixir-IDE tries to smart guess your tabulation settings and set it for the

autoload/vimide.vim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"
44

55
let s:VIMIDE_BOOT_FINISHED = 0
6+
let s:UPDATE_CHECK_FINISHED = 0
67

78
let s:rootPath = fnamemodify(resolve(expand('<sfile>:p')), ':h:h')
89
let s:bundlePath = s:rootPath . "/bundle"
@@ -16,6 +17,9 @@ function! vimide#getBundlePath() " {{{
1617
endfunction " }}}
1718

1819
function! vimide#setDefaults() " {{{
20+
call s:setGlobal('g:vimide_update_check_period', 24*60*60)
21+
call s:setGlobal('g:vimide_update_channel', '') " valid values are production/beta
22+
1923
call s:setGlobal('g:vimide_global_enable', 0)
2024
call s:setGlobal('g:vimide_manage_indents', 1)
2125
call s:setGlobal('g:vimide_manage_search', 1)
@@ -68,6 +72,12 @@ function! vimide#init() " {{{
6872
endfunction " }}}
6973

7074
function! vimide#boot(setGlobal) " {{{
75+
" run update check just once on editor start
76+
if !s:UPDATE_CHECK_FINISHED
77+
call s:checkUpdates()
78+
let s:UPDATE_CHECK_FINISHED = 1
79+
endif
80+
7181
let bootVarName = (a:setGlobal ? "s:VIMIDE_BOOT_FINISHED" : "b:VIMIDE_BOOT_FINISHED")
7282
let bootFinished = s:VIMIDE_BOOT_FINISHED || (exists(bootVarName) && execute("echo ".bootVarName) == 1)
7383

@@ -577,3 +587,43 @@ function! s:setGlobal(name, default) " {{{
577587
endif
578588
endif
579589
endfunction " }}}
590+
591+
function! s:checkUpdates() " {{{
592+
" check now much times we run already
593+
let currentTs = strftime("%s")
594+
595+
let tsFileName = vimide#getRootPath() . "/.last_update_ts"
596+
597+
let runUpdateCheck = 0
598+
if !file_readable(tsFileName)
599+
let runUpdateCheck = 1
600+
else
601+
let lastUpdateTs = join(readfile(tsFileName), "")
602+
if lastUpdateTs + g:vimide_update_check_period < currentTs
603+
runUpdateCheck = 1
604+
endif
605+
endif
606+
607+
if !runUpdateCheck | return | endif
608+
609+
call writefile([currentTs], tsFileName, "w")
610+
611+
let checkOutput = system(vimide#getRootPath() . "/check_update.sh " . g:vimide_update_channel)
612+
613+
if checkOutput !~ '^update,' | return | endif
614+
615+
let matches = split(checkOutput, ',')
616+
617+
let msg = "Update available for Vim-IDE-Elixir, version " . matches[3] . "\n" .
618+
\ "Please cd " . vimide#getRootPath() . " and run \n".
619+
\ "./update.sh " . g:vimide_update_channel . " "
620+
621+
let answer = confirm(msg, "&Run now,Run &manually later", -999)
622+
623+
if (answer != -999) | return | endif
624+
625+
let updateOutput = system(vimide#getRootPath() . "/update.sh " . g:vimide_update_channel)
626+
627+
echo updateOutput
628+
call confirm("Restart Vim for updated version")
629+
endfunction " }}}

check_update.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
3+
case $1 in
4+
beta) CHANNEL="beta" ;;
5+
dev) CHANNEL="dev" ;;
6+
*) CHANNEL="production"
7+
esac
8+
9+
CWD=`dirname $(readlink -f $0)`
10+
cd $CWD
11+
12+
if [ -f "$CWD/.release_channel" ]; then
13+
CHANNEL=`cat "$CWD/.release_channel"`
14+
fi
15+
16+
case $CHANNEL in
17+
production) PATTERN='"tag_name": *".*[0-9]"' ;;
18+
beta) PATTERN='"tag_name": *".*[0-9]-beta"' ;;
19+
dev) PATTERN="dev";;
20+
*) echo "wrong channel name $CHANNEL"
21+
exit 5
22+
esac
23+
24+
if [ "$PATTERN" = "dev" ]; then
25+
DT=`date -I -d'6 months ago'`
26+
RELEASES=`curl -s -q "https://api.github.com/repos/gasparch/vim-ide-elixir/commits?since=$DT&sha=master"`
27+
LAST_RELEASE=`echo "$RELEASES" | egrep '"sha":' | head -n 1 | sed -e 's#^.*"\([0-9a-f]*\)".*$#\1#'`
28+
LAST_VERSION=$LAST_RELEASE
29+
else
30+
RELEASES=`curl -s -q "https://api.github.com/repos/gasparch/vim-ide-elixir/releases" | egrep '"url":.*vim-ide-elixir|"tag_name":'`
31+
LAST_RELEASE=`echo "$RELEASES" | egrep -B1 "$PATTERN" | grep '"url"' | sed -e 's#^.*\/\([0-9]*\).*$#\1#' | sort -n | tail -n1`
32+
33+
if [ -z "$LAST_RELEASE" ]; then
34+
echo "no releases in channel $CHANNEL"
35+
exit 5
36+
fi
37+
38+
LAST_VERSION=`echo "$RELEASES" | egrep -A1 "url.*releases/$LAST_RELEASE" | grep '"tag_name"' | cut -d: -f2 | sed -e 's/,$//' -e 's/ //g' -e 's/"//g'`
39+
fi
40+
41+
CURRENT_RELEASE=`[ -f $CWD/.current_release ] && cat $CWD/.current_release`
42+
43+
if [ ! -z "$CURRENT_RELEASE" -a "$CURRENT_RELEASE" = "$LAST_RELEASE" ]; then
44+
echo "noupdate,$CURRENT_RELEASE"
45+
else
46+
echo "update,$CURRENT_RELEASE,$LAST_RELEASE,$LAST_VERSION"
47+
fi
48+

update.sh

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,72 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

3-
CWD=$(dirname $(readlink -f $0))
3+
CWD=`dirname $(readlink -f $0)`
44
cd $CWD
55

6-
git pull
6+
case $1 in
7+
dev)
8+
CHANNEL="dev"
9+
;;
10+
beta)
11+
CHANNEL="beta"
12+
;;
13+
*)
14+
CHANNEL="production"
15+
esac
716

8-
SUBMODULE_LIST=`git submodule status | awk '{print $2}'`
17+
if [ "$CHANNEL" != "dev" ]; then
18+
VERSIONS=`$CWD/check_update.sh $CHANNEL`
19+
case "$VERSIONS" in
20+
noupdate,*)
21+
echo "no updates available"
22+
exit 1;;
23+
update,*)
24+
RELEASE=`echo "$VERSIONS" | cut -d',' -f3`
25+
VERSION=`echo "$VERSIONS" | cut -d',' -f4`
26+
echo "updating to $VERSION"
27+
esac
28+
fi
929

10-
HAS_REPOS_W_CHANGED_URLS=""
1130

12-
for SUBMOD in $SUBMODULE_LIST; do
13-
cd $CWD/$SUBMOD
14-
REPO_URL=`git remote -v | grep fetch | awk '{print $2}'`
15-
cd $CWD
16-
MANIFEST_URL=`egrep -A1 "path = $SUBMOD\$" .gitmodules | grep url | awk '{print $3}'`
17-
if [ "$MANIFEST_URL" != "$REPO_URL" ]; then
18-
echo "changed repo URL $SUBMOD $REPO_URL $MANIFEST_URL"
19-
HAS_REPOS_W_CHANGED_URLS="YES"
20-
rm -rf $CWD/$SUBMOD
21-
rm -rf $CWD/.git/modules/$SUBMOD
31+
git_fetch_all() {
32+
git fetch --all
33+
}
34+
35+
git_checkout_version() {
36+
if [ ! -z "$VERSION" ]; then
37+
git checkout $VERSION
38+
else
39+
git checkout master
40+
git pull
2241
fi
23-
done
42+
}
2443

25-
cd $CWD
26-
git submodule sync
27-
git submodule update --init --recursive --depth 100 -j 3
28-
git submodule update --remote --merge
44+
git_update_submodules() {
45+
SUBMODULE_LIST=`git submodule status | awk '{print $2}'`
46+
47+
HAS_REPOS_W_CHANGED_URLS=""
48+
49+
for SUBMOD in $SUBMODULE_LIST; do
50+
cd $CWD/$SUBMOD
51+
REPO_URL=`git remote -v | grep fetch | awk '{print $2}'`
52+
cd $CWD
53+
MANIFEST_URL=`egrep -A1 "path = $SUBMOD\$" .gitmodules | grep url | awk '{print $3}'`
54+
if [ "$MANIFEST_URL" != "$REPO_URL" ]; then
55+
echo "changed repo URL $SUBMOD $REPO_URL $MANIFEST_URL"
56+
HAS_REPOS_W_CHANGED_URLS="YES"
57+
rm -rf $CWD/$SUBMOD
58+
rm -rf $CWD/.git/modules/$SUBMOD
59+
fi
60+
done
61+
62+
cd $CWD
63+
git submodule sync > /dev/null &&
64+
git submodule update --init --recursive --depth 100 -j 3 &&
65+
git submodule update --remote --merge
66+
}
67+
68+
update_release_file() {
69+
echo "$RELEASE" > "$CWD/.current_release"
70+
}
71+
72+
git_fetch_all && git_checkout_version && git_update_submodules && update_release_file

0 commit comments

Comments
 (0)