blob: 1af5738b98576f8d5a5f88fbd2c7e50cb1529341 (
plain)
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
|
# This script auto detects dependencies versions and downloads them.
# It can be used to update the PKGBUILD sources.
# It moves existing dependencies from $depspath to $targetdepspath
# and downloads non-existing ones in $targetdepspath
getsemverspec() {
local dep="$1"
case "$dep" in
*@npm:*@*)
echo "$dep" | gawk -F'@npm:' '{ print "npm:"$2 }'
;;
*)
echo "$dep" | gawk -F'@' '{ print $NF }'
;;
esac
}
getpackagename() {
local dep="$1"
local semverspec="$2"
local package="${dep::-${#semverspec}-1}"
echo "$package"
}
# move from $depspath or download deps to the $targetdepspath
# save info in sourcelist sha1sumslist noextractlist files
onlinebestmatch() {
local package="$1";
local semverspec="$2";
case "$semverspec" in
*/*)
local fullname="${package}-$(echo "$semverspec" | cut -d/ -f1 | gawk -F'github:' '{ print $2; }').zip"
if [ -f "$depspath/$fullname" ]; then
mv "$depspath/$fullname" "$targetdepspath/"
elif [ ! -f "$targetdepspath/$fullname" ]; then
echo "The $package [version = $semverspec] must be downloaded manually from github to continue."
exit -1;
fi
echo "$fullname"
;;
npm:*)
local alias_target="${semverspec#npm:}"
local alias_semverspec="$(getsemverspec "$alias_target")"
local alias_package="$(getpackagename "$alias_target" "$alias_semverspec")"
onlinebestmatch "$alias_package" "$alias_semverspec"
;;
*)
local json="$(npm view --json "$package@$semverspec")"
if [ "$(echo "$json" | jq -r '.version? // "INVALID"')" = "INVALID" ]; then
json="$(echo "$json" | jq '.[0]')"
fi
local version="$(echo "$json" | jq -r '.version' | head -n1)"
local latestversion
if [ "$semverspec" = "latest" ]; then
latestversion="$(echo "$json" | jq -r '.versions | .[]' | tail -n1)"
else
latestversion="$(echo "$json" | jq '.versions | .[]' | xargs semver -r "$semverspec" | tail -n1)"
fi
if [ "$version" != "$latestversion" ]; then
json="$(npm view --json "$package@$latestversion")"
fi
local url="$(echo $json | jq -r '.dist.tarball')";
local shasum="$(echo $json | jq -r '.dist.shasum')";
local name="${package/\//\#}-${latestversion}.tgz"
if ! grep -q "$shasum" "$targetdepspath/sha1sumslist"; then
if echo "$package" | grep -q "/"; then
echo "$name::$url" >> "$targetdepspath/sourcelist"
else
echo "$url" >> "$targetdepspath/sourcelist"
fi
echo "$shasum" >> "$targetdepspath/sha1sumslist"
echo "$name" >> "$targetdepspath/noextractlist"
if [ -f "$depspath/$name" ]; then
mv "$depspath/$name" "$targetdepspath/"
else
cd "$targetdepspath/"
wget -O "$name" --retry-on-host-error "$url"
fi
fi
echo "$name"
;;
esac
}
recursivedownloaddeps() {
local target="$1"
if [ -d "$tmpbuildpath/$target" ]; then
echo "Downloading dependency $target [cached]"
else
echo "Downloading dependency $target (recursively) ..."
mkdir -p "$tmpbuildpath/$target"
cd "$tmpbuildpath/$target"
if [ "${target: -4}" = ".zip" ]; then
unzip "$targetdepspath/${target}"
else
bsdtar xzf "$targetdepspath/${target}"
fi
folder="$(ls)"
chmod 755 "$folder"
find "$folder" -mindepth 1 -maxdepth 1 -print0 | xargs -0 mv -t .;
rm -r "$folder"
cat package.json | jq -r '.dependencies | to_entries? | map(.key + "@" + .value) | .[]' | while read dep; do
local semverspec="$(getsemverspec "$dep")"
local package="$(getpackagename "$dep" "$semverspec")"
if [ ! -d "$tmpbuildpath/${target}/node_modules/$package" ]; then
local subtarget="$(onlinebestmatch "$package" "$semverspec")"
recursivedownloaddeps "$subtarget"
fi
done
fi
}
downloaddeps() {
cd "$tmpbuildpath"
cat "$packagejsonpath" | jq -r '.dependencies, .devDependencies | to_entries? | map(.key + "@" + .value) | .[]' | sort -u | while read dep; do
local semverspec="$(getsemverspec "$dep")"
local package="$(getpackagename "$dep" "$semverspec")"
local target="$(onlinebestmatch "$package" "$semverspec")"
recursivedownloaddeps "$target"
done
}
createlist() {
local arrname="$1"
local filename="$2"
echo "${arrname}+=("
cat "$filename" | sed 's/^/ "/' | sed 's/$/"/'
echo " )"
}
download_main() {
depspath="$(pwd)"
rm -rf tmp-deps-build deps
mkdir tmp-deps-build deps
tmpbuildpath="$(realpath tmp-deps-build)"
targetdepspath="$depspath/deps"
downloaddeps
# sorting entries...
cd "$targetdepspath/"
cat sourcelist | gawk '{ n=n+1; print n, $0; }' > a
cat sha1sumslist | gawk '{ n=n+1; print n, $0; }' > b
cat noextractlist | gawk '{ n=n+1; print n, $0; }' > c
cat c | gawk '{ print $2, $1 }' | sort | gawk '{ n=n+1; print $2, n; }' | sort --numeric-sort | gawk '{ print $2 }' > order
paste order a | sort --numeric-sort | gawk '{ print $3 }' > aaa
paste order b | sort --numeric-sort | gawk '{ print $3 }' > bbb
paste order c | sort --numeric-sort | gawk '{ print $3 }' > ccc
createlist source aaa > threelists
createlist sha1sums bbb >> threelists
createlist noextract ccc >> threelists
}
if [ "$0" = "$BASH_SOURCE" ]; then
packagejsonpath="$(realpath "$1")"
if [ ! -f "$packagejsonpath" ]; then
echo "Usage: $0 <path of the main package.json file>"
exit -1
fi
download_main
fi
|