/
functions
180 lines (158 loc) · 4.85 KB
/
functions
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
#!/bin/sh
# TODO: Rework this into a 1.0.0 module API
internal_vcs_initialize()
{
if [[ ! -w $sm_path ]]
then
archives_path="$HOME/.sm/archives"
scm_path="$HOME/.sm/scm"
src_path="$HOME/.sm/src"
fi
[[ -d "${archives_path}" ]] || mkdir -p $archives_path
[[ -d "${scm_path}" ]] || mkdir -p $scm_path
[[ -d "${src_path}" ]] || mkdir -p $src_path
}
fetch_uri()
{
trace_filter vcs || set -o xtrace
fetch_error_initialize "$@"
typeset vcs_initial_pwd _uri _target _args
vcs_initial_pwd="$PWD"
_uri="${1:-}"
_target="${2:-}"
_args=()
(( $# )) && shift || __sm.log.fail "uri must be specified as the first parameter."
(( $# )) && shift || __sm.log.fail "target path must be specified as the second parameter."
builtin cd "$initial_pwd"
unset scm_type
_args=("${_uri}" "${_target}")
fetch_uri_parse_args "$@"
if [[ -z "${scm_type}" ]]
then
if ! fetch_uri_detect_scm_type "${_uri}" || [[ -n "${__vcs_failed}" ]]
then
fetch_error unknown_scm "uri=${_uri}"
return $?
fi
fi
__sm.log.debug vcs "type: ${scm_type}, params: ${_args[@]}."
case "${scm_type}" in
(path|archive|github|git|svn|hg|fossil|file)
__sm.includes include internal/vcs/handlers/${scm_type}
"${scm_type}_get" "${_args[@]}"
;;
(*)
fetch_error unknown_scm "uri=${_uri}"
;;
esac
builtin cd "$vcs_initial_pwd"
}
# Example:
#
# $ ./local --debug=vcs --call=api/vcs/fetch_uris /root ~/tmp/test_uris --fallback-uri /root/test,mpapis/mpapis_test
#
# DEBUG vcs: type: path, params: /root /home/mpapis/tmp/test_uris.
# DEBUG vcs: Copying from '/root' to '/home/mpapis/tmp/test_uris' failed.
# DEBUG vcs: Unknown SCM type (not found) for url '/root/test'.
# DEBUG vcs: type: github, params: mpapis/mpapis_test /home/mpapis/tmp/test_uris.
# DEBUG vcs: github -> https://github.com/mpapis/mpapis_test/tarball/master
# DEBUG vcs: github tarball archive name /home/mpapis/.sm/archives/mpapis_mpapis_test_master.tar.gz.
# DEBUG vcs: master does not match checksum #; removing and downloading.
#
fetch_uris()
{
typeset _uris _target _args _uri
_uris=( ${=1} )
_target="${2:-}"
_args=()
(( $# )) && shift || __sm.log.fail "uri must be specified as the first parameter."
(( $# )) && shift || __sm.log.fail "target path must be specified as the second parameter."
fetch_uri_parse_args "$@"
for _uri in ${_uris[@]}
do
fetch_uri "${_uri}" "${_target}" --on-fail __sm.log.debug ${_args[@]} || true
case "${__vcs_failed}" in
(unknown_scm|copy_failed|download_failed|empty_archive|remote_branch_not_found|cannot_checkout_branch|pull_failed|cannot_clone|extracting_archive_failed)
# in this case continue with next uri
continue
;;
("")
# actually no __sm.log.error spawned, return ok
return 0
;;
(*) # (unknown_archive_format|cannot_remove|all_uris_failed)
# those are not download related
return 1
esac
done
fetch_error all_uris_failed
}
#
# # VCS (SCM) Module
#
fetch_uri_parse_args()
{
typeset -g fetch_error_handler branch remote revision scm_type checksum
typeset -ga _uris
typeset _token _allowed_arg
while (( $# > 0 ))
do
_token="$1" && shift
for _allowed_arg in branch remote revision scm_type checksum
do
case "${_token}" in
(*${_allowed_arg})
eval "${_allowed_arg}=\"${1}\""
(( $# )) && shift || __sm.log.fail "${_allowed_arg} must be specified after '${_token}'."
continue 2
;;
esac
done
case "${_token}" in
(--on-fail)
fetch_error_handler=$1
(( $# )) && shift || __sm.log.fail "fetch __sm.log.error handler function must be specified after '--on-fail'."
continue
;;
(--fallback-uri)
_uris+=( ${1//,/ } )
(( $# )) && shift || __sm.log.fail "comma separated lit of uris must be specified after '--fallback-uri'."
continue
;;
esac
_args+=("${_token}")
done
}
fetch_update()
{
typeset _uri _target
_target="${1:-}"
(( $# )) && shift || __sm.log.fail "target path must be specified as the second parameter."
if [[ -d "${_target}" ]] && [[ -s "${_target}/.uri" ]]
then
_uri=$( __sm.vcs.read_uri "${_target}/.uri" )
if [[ -n "${_uri}" ]]
then
fetch_uri "${_uri}" "${_target}" "$@"
return $?
fi
fi
return 1
}
cli_fetch()
{
typeset _uri _target _args
_uri="${1:-}"
_target="${2:-}"
_args=()
(( $# )) && shift || __sm.log.error "uri must be specified as the first parameter."
(( $# )) && shift || __sm.log.error "target path must be specified as the second parameter."
fetch_uri "${_uri}" "${_target}" --on-fail __sm.log.error "$@"
}
cli_fetch_update()
{
typeset _target
_target="${1:-}"
(( $# )) && shift || __sm.log.error "target path must be specified as the second parameter."
fetch_update "${_target}" --on-fail __sm.log.error "$@"
}