summaryrefslogtreecommitdiffstats
path: root/update-plugins
blob: 42f1c7063b31f68c84f9f930cac8a2d6c0cb8ed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash

set -euo pipefail
IFS=$'\n\t'

readonly VIMHOME="$HOME/.vim"
readonly TMUXLINE_PATH="${HOME}/.tmux/tmuxline.conf"

function abspath {
    # convert relative path to absolute one
    # http://www.linuxquestions.org/questions/programming-9/bash-script-return-full-path-and-filename-680368/page3.html#post4309783
    if [[ -d "$1" ]]; then
        pushd "$1" >/dev/null
        pwd
        popd >/dev/null
    elif [[ -e "$1" ]]; then
        pushd "$(dirname $1)" >/dev/null
        echo "$(pwd)/$(basename $1)"
        popd >/dev/null
    else
        echo "$1 does not exist!" >&2
        return 127
    fi
}

function print_usage() {
    printf "Usage: ${BASH_SOURCE[0]} [-inh]

OPTIONS
    -i  Install only, don't update.
    -n  Don't install any package.
    -h  Show this help and exit.
"
}

function prepare() {
    local install_packages="$1"
    # TODO: report this upstream
    if [[ -e bundle/command-t/ruby/command-t/metadata.rb ]]; then
        pushd bundle/command-t
            git checkout ruby/command-t/metadata.rb ||:
        popd
    fi

    [[ "${install_packages}" == 0 ]] && return 0

    os_release="$(sed -n 's/^ID=["'"'"']\?\([^"'"'"']\+\).*/\1/p' /etc/os-release)"
    case "${os_release:-unknown}" in
        arch)
            function is_pkg_installed() {
                pacman -Qq "$1" >/dev/null 2>&1
            }
            function install_pkgs() {
                sudo pacman -S --noconfirm "$@"
            }
            ;;
        fedora)
            function is_pkg_installed() {
                rpm --quiet -q "$1"
            }
            function install_pkgs() {
                sudo dnf -y install "$@"
            }
            ;;
        rhel)
            function is_pkg_installed() {
                rpm --quiet -q "$1"
            }
            function install_pkgs() {
                sudo yum install -y "$@"
            }
            ;;
        *)
            echo "Unsupported os release: ${os_release}" >&2
            exit 1
            ;;
    esac
}

function fetch_submodules() {
    local update="$1"
    if [[ "${update}" == 1 ]]; then
        git submodule update --init --recursive
        git submodule foreach git checkout master
    else
        git submodule update --init --checkout --recursive
        git submodule foreach git checkout master
    fi
}

function install_packages() {
    local install_packages="$1"

    [[ "${install_packages}" == 0 ]] && return 0

    declare -a to_install=()

    if ! which ack >/dev/null 2>&1; then
        to_install+=( ack )
    fi
    if [[ "${os_release}" =~ fedora|rhel ]]; then
        if ! is_pkg_installed ruby-devel; then
            to_install+=( ruby-devel rubygems )
        fi
        if ! is_pkg_installed redhat-rpm-config; then
            to_install+=( redhat-rpm-config )
        fi
    fi
    if [[ "${os_release}" = arch ]] && ! is_pkg_installed ruby; then
        to_install+=( ruby )
    fi
    if [[ "${os_release}" = arch ]] && ! is_pkg_installed go-tools; then
        to_install+=( go-tools )
    fi
    if ! which ctags >/dev/null 2>&1; then
        to_install+=( ctags )
    fi

    if [[ "${#to_install[@]}" -gt 0 ]]; then
        install_pkgs "${to_install[@]}"
    fi
}

function fetch_plugins() {
    local update="$1"
    declare -a cmdargs
    if [[ "${update}" == 1 ]]; then
        cmdargs=( 'vim' '+PluginUpdate' '+qall' )
    else
        cmdargs=( 'vim' '+PluginInstall' '+qall' )
    fi
    if [[ -t 0 ]]; then
        eval "${cmdargs[@]}"
    else
        echo "Running in batch mode. Please run '${cmdargs[@]}' manually."
    fi
}

function build_plugins() {
    local update="$1"

    pushd bundle/command-t/ruby/command-t/ext/command-t
        ruby extconf.rb
        make
    popd

    pushd bundle/vimproc
        make
    popd

    if [[ -d bundle/YouCompleteMe ]]; then
        if ! [[ -e ~/wsp/ycm_build ]]; then
            mkdir -p ~/wsp/ycm_build
        fi
        pushd ~/wsp/ycm_build
            cmake -G "Unix Makefiles" -DUSE_SYSTEM_LIBCLANG=ON . \
                "${VIMHOME}/bundle/YouCompleteMe/third_party/ycmd/cpp"
            make ycm_support_libs
        popd
    fi

    declare -a cmdargs
    if [[ -d bundle/vim-go ]]; then
        if [[ "${update}" == 1 ]]; then
            cmdargs=( 'vim' '+GoUpdateBinaries' '+qall' )
        else
            cmdargs=( 'vim' '"+let g:go_get_update = 0"' '+GoInstallBinaries' '+qall' )
        fi
        if [[ -t 0 ]]; then
            set -x
            eval "${cmdargs[@]}"
            { set +x; } 2>/dev/null
        else
            echo "Running in batch mode. Please run '${cmdargs[@]}' manually."
        fi
    fi

    if [[ -d bundle/tmuxline.vim && ( "${update}" == 1 || ! -e "${TMUXLINE_PATH}" ) ]]; then
        cmdargs=( 'vim' "'+TmuxlineSnapshot ${TMUXLINE_PATH}'" '+qall' )
        if [[ -t 0 ]]; then
            set -x
            eval "${cmdargs[@]}"
            { set +x; } 2>/dev/null
        else
            echo "Running in batch mode. Please run '${cmdargs[@]}' manually."
        fi
    fi
}

src=`dirname $0`
if [[ "${src}" != '.' ]]; then
    cd `abspath "${src}"`
fi

update=1
install_packages=1

while getopts nhi opt; do
    case "${opt}" in
        i)
            update=0
            ;;
        n)
            install_packages=0
            ;;
        h)
            print_usage
            exit 0
            ;;
        *)
            print_usage
            exit 1
            ;;
    esac
done

prepare "${install_packages}"
fetch_submodules "${update}"
install_packages "${install_packages}"
fetch_plugins "${update}"
build_plugins "${update}"