-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.sh
118 lines (95 loc) · 2.24 KB
/
lib.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
116
117
118
#!/bin/bash
# $1: source dir
# $2: target dir
copy_s() {
if [ -f $1 ]; then
echo "Info: $1 is a file"
mkdir -p ${2%/*}
cp -af $1 $2
elif [ -d $1 ]; then
echo "Info: $1 is a directory"
mkdir -p $2
cp -af $1/* $2/
else
echo "Warning: $1 not exist"
fi
}
# $1: url
# $2: target dir
dl_svn() {
tmp_dir=$(mktemp -d)
svn co -q $1 $tmp_dir/tmp
if [ $? == 0 ]; then
echo "Info: Svn download success: $1"
rm -rf $tmp_dir/tmp/.svn
mkdir -p $2
mv -f $tmp_dir/tmp/* $2/
else
echo "Error: Svn download fail: $1"
fi
rm -rf $tmp_dir
}
# $1: git url
# $2: target dir
# $3: branch
dl_git() {
tmp_dir=$(mktemp -d)
if [ -z $3 ]; then
git clone -q --depth=1 --single-branch $1 $tmp_dir/tmp
else
git clone -q --depth=1 --single-branch -b $3 $1 $tmp_dir/tmp
fi
if [ $? == 0 ]; then
echo "Info: Git download success: $1"
rm -rf $tmp_dir/tmp/.git
mkdir -p $2
mv -f $tmp_dir/tmp/* $2/
else
echo "Error: Git download fail: $1"
fi
rm -rf $tmp_dir
}
# $1: git url
# $2: target dir
# $3: sub dir
# $4: branch
dl_git_sub() {
tmp_dir=$(mktemp -d)
if [ -z $4 ]; then
git clone -q --depth=1 --single-branch $1 $tmp_dir/tmp
else
git clone -q --depth=1 --single-branch -b $4 $1 $tmp_dir/tmp
fi
if [ $? == 0 ]; then
echo "Info: Git-sub download success: $1/tree/$4/$3"
rm -rf $tmp_dir/tmp/.git
src_dir=$tmp_dir/tmp
if [ -n $3 ]; then
src_dir=$tmp_dir/tmp/$3
fi
if [ -e $src_dir ]; then
mkdir -p $2
mv -f $src_dir/* $2/
else
echo "Error: Git-sub copy fail not exist: $src_dir"
fi
else
echo "Error: Git-sub download fail: $1/tree/$4/$3"
fi
rm -rf $tmp_dir
}
# $1: url
# $2: target file
dl_curl() {
tmp_dir=$(mktemp -d)
curl -skL --retry 3 --connect-timeout 3 -o $tmp_dir/tmp $1
if [ $? == 0 ]; then
echo "Info: Curl Download success: $1"
rm -rf $tmp_dir/.git
mkdir -p ${2%/*}
mv -f $tmp_dir/tmp $2
else
echo "Error: Download fail: $1"
fi
rm -rf $tmp_dir
}