-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcommon.sh
115 lines (96 loc) · 1.72 KB
/
common.sh
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
function verbose()
{
if [ -n "$VERBOSE" ]; then
true
else
false
fi
}
function v()
{
if verbose; then
"$@"
fi
}
function this_file()
{
echo "${BASH_SOURCE[1]}"
}
function this_dir()
{
dirname "${BASH_SOURCE[1]}"
}
# banner FILE LABEL
# 'FILE' is used to determin type of banner
function banner()
{
local file="$1"
local label="$2"
case "$file" in
*.md)
;;
*.rb|*.sh|*.gitignore)
echo
echo "# $label -------------------"
echo
;;
*vimrc|*vimrc.*|*.vim)
echo
echo "\" $label -------------------"
echo
;;
*)
;;
esac
}
function append_to_file()
{
local file="$1"
local label="$2"
local destination="${VIM_DIR}/${file}"
mkdir -pv "$(dirname "$destination")"
v cyan "-- $file -> $destination"
(
banner "$file" "$label"
cat
) >> "$destination"
}
# copy files from directory into target
# will fail if directory doesn't exist
function copy_files()
{
local base="$1"
[ -d "$base" ] || die "directory $base not found"
_copy_files "$@"
}
# copy files from directory into target
# will ignore if directory doesn't exist
function _copy_files()
{
local base="$1"
if [ ! -d "$base" ]; then
return
fi
v blue $base
for f in $(cd "$base" && find . -type f); do
cat "$base/$f" | append_to_file "$f" "$base/$(basename $f)"
done
}
function load()
{
v yellow "$1"
if [ -f "$1/prompt.sh" ]; then
if ! eval "$(cat "$1/prompt.sh")"; then
return
fi
fi
if [ -d "$1/files" ]; then copy_files "$1/files"; fi
if [ -d "$1/plugins" ]; then load_all "$1/plugins/"*; fi
if [ -f "$1/install.sh" ]; then source "$1/install.sh"; fi
}
function load_all()
{
for d in "$@"; do
load "$d"
done
}