aboutsummaryrefslogtreecommitdiff
path: root/vimplug.sh
blob: 1fb0755b84b4c9107cae519dd98437436d1b720c (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash

#This script installs vim plugins using git submodules.
#
#
#MIT License
#
#Copyright (c) 2018 Tucker Evans
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.


PLUG_PATH="vim/.vim/pack/$USER/start/"
JOBS=4

#
# helper funcitons
#

set_git() {
	if [[ -n GIT_DIR ]] 
	then
		GIT_DIR = "$HOME/dotfiles"
	fi
}

git_commit() {
	[[ -n GIT_DIR ]] || return 1
	[[ -n COMMIT_MSG ]] || return 1

	return git -C GIT_DIR commit -m"$COMMIT_MSG"
}

#
# subcommand functions
#

cmd_version () {
	cat <<-_EOF
	plug version 0.0.1
	_EOF
}


cmd_usage () {
	cat <<-_EOF
	Usage:
		$PROGRAM install [-odcn] git-repo
		$PROGRAM update [-jc]
		$PROGRAM remove [-odc] plugin-name
		
	More information can be found in the plug(1) man page.
	_EOF
}

cmd_install() {
	while getopts ":ocd:" opt;
	do
		case opt in
			o)
				PLUG_PATH="vim/.vim/pack/$USER/opt/"
				;;
			c)
				COMMIT_MSG="Installed "
				;;
			d)
				GIT_DIR=$OPTARG
				;;
			\?)
				echo "Invalid option: -$OPTARG" >&2
				return
				;;
			:)
				echo "Option -$OPTARG requerys an argument"
				;;
		esac
	done
	[[ -n $GIT_DIR ]] && PLUG_PATH="$GIT_DIR$PLUG_PATH"
	
	LINK=${!OPTIND}
	PLUG_NAME=${LINK##*/}
	FILENAME=${FILENAME%.git}
	FILENAME="$PLUG_PATH$FILENAME"
	echo "Installing $PLUG_NAME..."

	set_git

	if git submodule add $LINK $FILENAME 1> .plug/$(date -Is).log 
	then
		echo "Installed $PLUG_NAME"
		if [[ -n $COMMIT_MSG ]] 
		then
			echo "Commiting Changes..."
			COMMIT_MSG="$COMMIT_MSG$PLUGNAME"
			if git add .gitmodules $FILENAME 1> .plug/$(date -Is).log && \
			   git_commit  1> .plug/$(date -Is).log
			then
				echo "Changes Commited"
			else 
				echo "Error committing changes"
				return 1
			fi
		fi
	else 
		exit 1
	fi 

	echo "Installed $PLUG_NAME"
	return 0
}

cmd_update() {
	while getopts ":jc" opt;
	do
		case opt in
			c)
				COMMIT_MSG="Updated Submodules"
				;;
			j)
				JOBS=$OPTARG
				;;
			\?)
				echo "Invalid option: -$OPTARG" >&2
				return
				;;
			:)
				echo "Option -$OPTARG requerys an argument"
				;;
		esac
	done

	echo "Updating modules..."

	if git submodule update --remote --merge -j $JOBS 1> .plug/$(date -Is).log
	then 
		echo "Modules updated"
			if [[ -n $COMMIT_MSG ]] 
			then
				echo "Commiting Changes..."
				if git_commit  1> .plug/$(date -Is).log
				then
					echo "Changes Commited"
				else 
					echo "Error committing changes"
					return 1
				fi
			fi
	else 
		exit 1
	fi

	echo "Updated modules"
	return 0
}

cmd_remove() {
	while getopts ":ocd:" opt;
	do
		case opt in
			o)
				PLUG_PATH="vim/.vim/pack/$USER/opt/"
				;;
			c)
				COMMIT_MSG="Removed "
				;;
			d)
				GIT_DIR=$OPTARG
				;;
			\?)
				echo "Invalid option: -$OPTARG" >&2
				return
				;;
			:)
				echo "Option -$OPTARG requerys an argument"
				;;
		esac
	done
	
	NAME="${!OPTIND}"
	FILENAME="$PLUG_PATH$NAME"

	if git submodule deinit -f $FILENAME &&\
		git rm -f $FILENAME && \
		rm -rfv ".git/modules/$FILENAME"
	then
		
		if [[ -n $COMMIT_MSG ]] 
		then
			echo "Commiting Changes..."
			COMMIT_MSG="$COMMIT_MSG$PLUGNAME"
			if git_commit  1> .plug/$(date -Is).log
			then
				echo "Changes Commited"
			else 
				echo "Error committing changes"
				return 1
			fi
		fi
	else 
		echo "Error removing $NAME"
		exit 1
	fi

	echo "$NAME removed"
	return 0
}

cmd_list() {

echo "t"
}

case "$1" in 
	install|add)
		shift
		cmd_install "$@"
		;;
	help|--help)
		shift
		cmd_usage "$@"
		;;
	version|--version|-v)
		shift
		cmd_version "$@"
		;;
	ls|list|show)
		shift
		cmd_list "$@"
		;;
	update)
		shift
		cmd_update "$@"
		;;
	remove|delete)
		shift
		cmd_remove "$@"
esac
exit 0