-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-source-downloader.sh
More file actions
executable file
·140 lines (114 loc) · 3.38 KB
/
git-source-downloader.sh
File metadata and controls
executable file
·140 lines (114 loc) · 3.38 KB
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
#!/bin/bash
# Purpose:
# Given file that has a url for a git repository or a directory given
# a collection of those files, clone or fetch their contents into a
# root directory. Afterwards, if specified, build and install the
# repositories following commands specified in same file that lists
# the repository url.
set -e
build=false
dest=""
src=""
print_usage(){
printf 'usage: [--build] <source> <destination>\n'
printf '\n'
printf ' --build\n'
printf ' build and install after any updated sources are\n'
printf ' fetched.\n'
printf '\n'
printf ' <destination>\n'
printf ' parent directory for downloaded repositories.\n'
printf '\n'
printf ' <source>\n'
printf ' File containing source url to pull from. File name\n'
printf ' must end with ".source". If "--build" is specified,\n'
printf ' then build instructions will be read from File as\n'
printf ' well. If <source> is a directory, then all files in\n'
printf ' contained within ending with ".source" will be\n'
printf ' processed.\n'
printf '\n'
exit 1
}
check_args(){
for arg in "$@"; do
if echo "$arg" | grep -Eq "^--" ; then
case "$arg" in
"--build") build=true ;;
*)
echo "Unknown option: $arg"
print_usage
;;
esac
elif [ -z "$src" ] ; then
# FIXME determine if $arg is rel or abs path
# If not abs, prefix with $PWD
if ! case "$arg" in /*) true;; *) false;; esac; then
src="$PWD/"
fi
src="$src$arg"
elif [ -z "$dest" ] ; then
dest="$arg"
else
echo "Unexpected positional arguments after <destination>"
print_usage
fi
done
if [ -z "$src" ] || [ -z "$dest" ] ; then
print_usage
fi
}
git_fetch(){
local dir="$1"
local giturl="$2"
local success=false
if [ ! -d "$dir" ] ; then
if git clone --depth 1 "$giturl" "$dir" ; then
success=true
fi
else
pushd "$dir"
if git pull origin master ; then
success=true
fi
popd
fi
if $success; then return 0; else return 1; fi
}
process_source_file(){
local file="$1"
local dir="$2/"
local sourcePath="$dir$file"
echo "$dir"
if echo "$aFile" | grep -Eq "\.source"; then
name=$(echo "$file" | sed -s 's/\.source//')
url=$(head -n 1 "$sourcePath")
echo "Working on $name"
if git_fetch "$name" "$url" ; then
if $build ; then
pushd "$name"
while IFS='' read -r line || [[ -n "$line" ]] ; do
eval "$line"
done < <(tail -n +2 "$sourcePath")
popd
fi
fi
echo "Done with $name"
fi
}
###### START HERE ######
check_args "$@"
mkdir -p "$dest"
cd "$dest"
if [ -d "$src" ] ; then
for aFile in $(ls -1 "$src"); do
process_source_file "$aFile" "$src"
done
elif [ -f "$src" ] ; then
process_source_file "$src"
else
echo "<source> is neither a file or directory"
print_usage
fi
# if $build ; then
# if (( $(cat $updatedList | wc -l) > 0 )) ; then
# fi