-
Notifications
You must be signed in to change notification settings - Fork 24
/
dsl
306 lines (273 loc) · 5.95 KB
/
dsl
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env bash
#
# # VCS (SCM) Module
#
# ## detect\_uri()
#
#
#
# Code Walkthrough
detect_uri()
{
local extensions_path=$1 _uri_path
shift
_uri_path=( $(rebuild_path ".uri" $extensions_path $@ ) )
if [[ -s "${_uri_path}" ]]
then
uri_path=${_uri_path}
return 0
else
return 1
fi
}
# ## scm\_identifier()
#
#
#
# Code Walkthrough
scm_identifier()
{
local _url=$1
variables_must_be_nonempty _url
printf "${_url//\//_}"
}
# ## directory\_get()
#
#
#
# Code Walkthrough
directory_get()
{
local initial_pwd="$PWD" _source=$1 _target=$2
variables_must_be_nonempty _source _target
rm -rf "${_target}" 2>&1 >/dev/null
if cp -r ${_source} "${_target}" >/dev/null 2>&1
then
echo "$(cd "${_source}"; pwd)" > "${_target}/.uri"
else
fail "There was an error copying '${_source}'"
fi
}
# ## git\_get()
#
#
#
# Code Walkthrough
git_get()
{
local initial_pwd="$PWD" _url=$1 _final_target=$2 branch=${3:-master} remote=${4:-origin}
variables_must_be_nonempty _url _final_target
local _target="${scm_path}/$( scm_identifier "${_url}" )"
_final_target=$(
builtin cd "${initial_pwd}"
mkdir -p "${_final_target}"
builtin cd "${_final_target}"
pwd
)
if [[ -d "$_target/.git" ]]
then
builtin cd "${_target}"
git checkout master -f -q 2>&1 >/dev/null
git reset --hard HEAD 2>&1 >/dev/null
git fetch ${remote} 2>&1 >/dev/null
if [[ -z "$(git branch | awk "/${branch}$/" 2>/dev/null)" ]]
then
if ! (
builtin cd "${repo_path}"
git checkout -b "${branch}" --track "${remote}/${branch}" 2>&1 >/dev/null
)
then
error "$remote $branch remote branch not found."
fi
elif [[ -z "$(git branch | awk "/\* $branch$/{print \$2}" 2>/dev/null)" ]]
then
if ! (
builtin cd "${repo_path}"
git checkout $branch >/dev/null 2>&1
)
then
error "Unable to checkout $branch."
fi
fi
if ! (
builtin cd "${repo_path}"
git pull --rebase origin $branch 2>&1 >/dev/null
)
then
fail "Git pull failed."
fi
else
mkdir -p "${_target}"
if git clone --depth 1 ${_url} "${_target}" 2>&1 >/dev/null
then
echo "${_url}" > "${_target}/.uri"
else
local _url_https="${_url/git:\/\//https:\/\/}"
if git clone --depth 1 ${_url_https} "${_target}" 2>&1 >/dev/null
then
echo "${_url_https}" > "${_target}/.uri"
else
fail "There was an error while cloning the repository from the url '${_url}'"
fi
fi
fi
rm -rf "${_final_target}" 2>&1 >/dev/null
if ! cp -frT "${_target}" "${_final_target}" 2>&1 >/dev/null
then
fail "There was an error copying '${_target}' -> '${_final_target}'"
fi
builtin cd "$initial_pwd"
}
hg_get()
{
local initial_pwd="$PWD" _url=$1 _target=$2
builtin cd "${_target}"
hg pull
}
svn_get()
{
local initial_pwd="$PWD" _url=$1 _target=$2
builtin cd "${_target}"
svn update
}
# ## is\_github\_url()
#
#
#
# Code Walkthrough
is_github_url()
{
#TODO: add validation that given repository exists on github
ematch "$1" "^[[:alnum:]_-]+\/[[:alnum:]_-]+$"
}
# ## github\_get()
#
#
#
# Code Walkthrough
github_get()
{
local _url="$1" _target="$2"
shift
shift
# use git for fetching github repo
git_get "git://github.com/${_url}.git" "${_target}" "$@"
# and overwrite repo uri
echo "${_url}" > "${_target}/.uri"
}
archive_get() {
local _url="$1" _final_target="$2"
local _archive="${archives_path}/${_url##*/}"
shift
shift
if [[ -f "${_url}" ]]
then
if ! copy_file --force "${_url}" to "${_archive}"
then
log "Copying from '${_url}' to '${_archive}' failed "
return 1
fi
else
if ! curl -L "${_url}" -o "${_archive}"
then
log "Downloading from '${_url}' to '${_archive}' failed "
return 1
fi
fi
rm -rf "${_final_target}" || true
mkdir -p "${_final_target}"
builtin cd "${_final_target}"
case "${_archive}" in
*.tar) # tar
tar xf "${_archive}" || error "There was an error while extracting the archive '${_archive}'"
;;
*.tar.gz|*.tgz) # gzip
tar zxf "${_archive}" || error "There was an error while extracting the archive '${_archive}'"
;;
*.tar.bz2|*.tbz2) # bzip
tar jxf "${_archive}" || error "There was an error while extracting the archive '${_archive}'"
;;
*.tar.xz) # LZMA
# TODO: if 'xz' command exists, use that, otherwise use tar J
tar Jxf "${_archive}" || error "There was an error while extracting the archive '${_archive}'"
;;
*.zip)
unzip "${_archive}" || error "There was an error while extracting the archive '${_archive}'"
;;
*)
error "Unknown archive format for ${_archive}"
;;
esac
if ! ls -d * &>/dev/null
then
error "Empty archive ${_archive}"
fi
}
is_archive_uri() {
case "$1" in
*.tar|*.tgz|*.txz|*.tbz2|*.tar.xz|*.tar.bz2|*.tar.gz|*.zip)
return 0
;;
esac
return 1
}
# ## fetch\_uri()
#
#
#
# Code Walkthrough
fetch_uri()
{
trap trace_filter RETURN
trace_filter vcs
[[ $# -ge 2 ]] || fail "Missing params _uri _target"
log "${log_prefix}Fetching $1"
unset scm_type
if [[ -d "$1" ]] # check for directory
then
scm_type='directory'
directory_get "$@"
return $?
elif is_archive_uri "$1" # check for packages
then
scm_type='package'
archive_get "$@"
return $?
elif is_github_url "$1" # check for pattern user/repository from github
then
scm_type='github'
github_get "$@"
return $?
fi
case "$1" in
(*git*)
scm_type='git'
git_get "$@"
;;
(*svn*)
scm_type='svn'
NIY "Subversion scm_update"
svn_get
;;
(*hg*)
scm_type='hg'
NIY "hg scm_update"
hg_get
;;
(*fossil*)
scm_type='fossil'
NIY "fossil scm_update"
;;
(*)
error "Unknownn SCM for url '${_url}'"
;;
esac
}
fetch()
{
fetch_uri "$@"
}
scm_help()
{
NIY "display help for scm"
}