-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrab.sh
More file actions
executable file
·392 lines (328 loc) · 9.87 KB
/
grab.sh
File metadata and controls
executable file
·392 lines (328 loc) · 9.87 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/bin/bash
LIBFILE_NAME="library.sh"
HOST_REGEX="[a-zA-Z0-9_\.]+"
ORG_REGEX="[a-zA-Z0-9_\-]+"
REPO_REGEX="[a-zA-Z0-9_\-]+"
PATH_REGEX="[a-zA-Z0-9_\.\-\/]*"
VERSION_REGEX="[a-zA-Z0-9\.\-]+"
FUNCTION_PATTERN='^[[:space:]]*([[:alnum:]_]+[[:space:]]*\(\)|function[[:space:]]+[[:alnum:]_]+)'
FULL_REGEX="($HOST_REGEX)\/($ORG_REGEX)\/($REPO_REGEX)($PATH_REGEX)?([@]$VERSION_REGEX)?"
#
# Converts a string of the format pointed bellow to an actual git url.
# <host>/<organization>/<repository>@<branch>
#
to_clone_url() {
echo "https://$1/$2/$3.git"
}
find_host() {
echo $1 | sed -E "s/$FULL_REGEX/\1/"
}
find_organization() {
echo $1 | sed -E "s/$FULL_REGEX/\2/"
}
find_repository() {
echo $1 | sed -E "s/$FULL_REGEX/\3/"
}
find_path() {
echo -n "."
echo $1 | sed -E "s/$FULL_REGEX/\4/"
}
find_version() {
local version=`echo $1 | awk -F "@" '{print $NF}'`
if [ "$version" == "$1" ]; then
echo "main"
elif [ -z "$version" ]; then
echo "main"
else
echo $version
fi
}
find_library_path() {
local path=$1
local filename=$LIBFILE_NAME;
if [ -f $path ]; then
filename=$(echo $path | awk -F "/" '{print $NF}')
path=$(dirname $path)
fi
pushd $path
if [ -f "$filename" ]; then
realpath $filename
fi
popd
}
find_default_branch() {
local clone_url=$1
local default_branch=$(git ls-remote --symref $clone_url HEAD 2>/dev/null | grep "^ref:" | cut -f2 | cut -d'/' -f3)
if [ -z "$default_branch" ]; then
# Fallback to master if we can't determine the default branch
echo "master"
else
echo "$default_branch"
fi
}
translate_as() {
local script=$1
local prefix=""
local separator=""
local source=`basename $script | cut -d "." -f1`
local dir=`dirname $script`
local target="$script"
local tmp_dir=$(mktemp -d /tmp/grab-alias.XXXXXXXX)
local tmp_source=$tmp_dir/source.sh
local tmp_target=$tmp_dir/target.sh
local needs_normalization=`grep -E $FUNCTION_PATTERN $script | grep "^__"`
#If an alias has been specified....
if [ "$2" == "as" ] && [ -n "$3" ]; then
prefix=$3
separator="::"
mkdir -p $dir/.alias/$source
target="$dir/.alias/${source}/$prefix.sh"
elif [ -n "$needs_normalization" ]; then
mkdir -p $dir/.normalized
target="$dir/.normalized/${source}.sh"
fi
cp $script $tmp_source
cp $script $tmp_target
# Get list of functions once, before any modifications
local functions=$(grep -E $FUNCTION_PATTERN $tmp_source | grep -v "::" | cut -d "{" -f1 | cut -d "(" -f1 | sed "s/^[ ]*function //g")
for func in $functions; do
# Only apply namespace if function doesn't already have it
if ! grep -q "\b$prefix$separator$func\b" $tmp_source; then
cat $tmp_source | sed "s/\b$func\b/$prefix$separator$func/g" > $tmp_target
cp $tmp_target $tmp_source
fi
local normalized=`echo $func | sed "s/^__//g"`
if [ "$normalized" != "$func" ]; then
cat $tmp_source | sed "s/$func/$normalized/g" > $tmp_target
cp $tmp_target $tmp_source
fi
done
cp $tmp_target $target
echo $target
}
# Utils
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
do_grab() {
# Command execution
local host=$(find_host $1)
local organization=$(find_organization $1)
local repository=$(find_repository $1)
local requested_version=$(find_version $1)
local path=$(find_path $1)
local clone_url=$(to_clone_url $host $organization $repository)
local shellib_home=${SHELLIB_HOME:=$HOME/.shellib}
# If no specific version was requested (defaults to "master"), detect the actual default branch
local version=$requested_version
if [ "$requested_version" == "master" ]; then
local actual_version=$(echo $1 | awk -F "@" '{print $NF}')
if [ "$actual_version" == "$1" ] || [ -z "$actual_version" ]; then
# No version was specified, detect the default branch
version=$(find_default_branch $clone_url)
fi
fi
mkdir -p $shellib_home/$host/$organization/$repository
pushd $shellib_home/$host/$organization/$repository
if [ -d $version ]; then
pushd $version
# Update default branches (master or main) with latest changes
local default_branch=$(find_default_branch $clone_url)
if [ "$default_branch" == "$version" ]; then
git pull --rebase origin $version > /dev/null 2> /dev/null
fi
translate_as $(find_library_path $path) $2 $3
popd
else
#Clone the repository
git clone -b $version --single-branch $clone_url $shellib_home/$host/$organization/$repository/$version > /dev/null 2> /dev/null
pushd $version
translate_as $(find_library_path $path) $2 $3
popd
fi
}
# Test functions
test_find_host() {
local host=$(find_host "github.com/owner/repo")
if [ "$host" = "github.com" ]; then
echo "✓ find_host works"
return 0
else
echo "✗ find_host failed, got: '$host'"
return 1
fi
}
test_find_organization() {
local org=$(find_organization "github.com/testorg/testrepo")
if [ "$org" = "testorg" ]; then
echo "✓ find_organization works"
return 0
else
echo "✗ find_organization failed, got: '$org'"
return 1
fi
}
test_find_repository() {
local repo=$(find_repository "github.com/testorg/testrepo")
if [ "$repo" = "testrepo" ]; then
echo "✓ find_repository works"
return 0
else
echo "✗ find_repository failed, got: '$repo'"
return 1
fi
}
test_find_path() {
local path=$(find_path "github.com/owner/repo/path/to/file")
if [ "$path" = "./path/to/file" ]; then
echo "✓ find_path works"
return 0
else
echo "✗ find_path failed, got: '$path'"
return 1
fi
}
test_find_version() {
local version1=$(find_version "github.com/owner/repo@v1.0")
if [ "$version1" = "v1.0" ]; then
echo "✓ find_version works with explicit version"
else
echo "✗ find_version failed with version, got: '$version1'"
return 1
fi
local version2=$(find_version "github.com/owner/repo")
if [ "$version2" = "main" ]; then
echo "✓ find_version defaults to main"
return 0
else
echo "✗ find_version failed default, got: '$version2'"
return 1
fi
}
test_to_clone_url() {
local url=$(to_clone_url "github.com" "owner" "repo")
if [ "$url" = "https://github.com/owner/repo.git" ]; then
echo "✓ to_clone_url works"
return 0
else
echo "✗ to_clone_url failed, got: '$url'"
return 1
fi
}
test_translate_as_simple() {
# Create a temporary test library
local temp_lib=$(mktemp)
cat > "$temp_lib" << 'EOF'
#!/bin/bash
simple_function() {
echo "test"
}
another_function() {
local var="value"
echo "$var"
}
EOF
# Test without alias
local result=$(translate_as "$temp_lib")
if [ -f "$result" ] && grep -q "simple_function" "$result"; then
echo "✓ translate_as works without alias"
else
echo "✗ translate_as failed without alias"
rm -f "$temp_lib"
return 1
fi
rm -f "$temp_lib"
return 0
}
test_translate_as_with_alias() {
# Create a temporary test library
local temp_lib=$(mktemp)
cat > "$temp_lib" << 'EOF'
#!/bin/bash
test_function() {
echo "test"
}
another_test() {
echo "another"
}
EOF
# Test with alias
local result=$(translate_as "$temp_lib" "as" "myprefix")
if [ -f "$result" ] && grep -q "myprefix::test_function" "$result"; then
echo "✓ translate_as works with alias"
else
echo "✗ translate_as failed with alias"
rm -f "$temp_lib"
return 1
fi
# Test that functions are properly namespaced
if grep -q "myprefix::another_test" "$result"; then
echo "✓ translate_as namespaces all functions"
else
echo "✗ translate_as failed to namespace all functions"
rm -f "$temp_lib"
return 1
fi
rm -f "$temp_lib"
return 0
}
test_translate_as_no_double_namespace() {
# Create a temporary test library with already namespaced functions
local temp_lib=$(mktemp)
cat > "$temp_lib" << 'EOF'
#!/bin/bash
simple_function() {
echo "test"
}
already_namespaced() {
echo "already"
}
EOF
# First pass - apply namespace
local first_result=$(translate_as "$temp_lib" "as" "test")
# Second pass - should not double-namespace
local second_result=$(translate_as "$first_result" "as" "test")
# Check that we don't have test::test:: anywhere
if ! grep -q "test::test::" "$second_result"; then
echo "✓ translate_as prevents double namespacing"
else
echo "✗ translate_as created double namespace"
rm -f "$temp_lib" "$first_result" "$second_result"
return 1
fi
rm -f "$temp_lib" "$first_result" "$second_result"
return 0
}
# Run all tests
run_tests() {
echo "Running tests for grab..."
local failed=0
test_find_host || failed=1
test_find_organization || failed=1
test_find_repository || failed=1
test_find_path || failed=1
test_find_version || failed=1
test_to_clone_url || failed=1
test_translate_as_simple || failed=1
test_translate_as_with_alias || failed=1
test_translate_as_no_double_namespace || failed=1
if [ $failed -eq 0 ]; then
echo "All tests passed!"
return 0
else
echo "Some tests failed!"
return 1
fi
}
# Handle command line arguments
if [ "$1" = "--test" ]; then
run_tests
exit $?
fi
#Only run grab if script is not sourced and not testing
if [ -n "$1" ] && [ "$1" != "--test" ]; then
do_grab $*
fi