Skip to content

Commit 3915479

Browse files
committed
ytoxt
1 parent b57d451 commit 3915479

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/lib/owner.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ update_owner() {
123123
echo "Creating $owner array..."
124124
find "$BKG_INDEX_DIR/$owner" -type f -name '*.json' ! -name '.*' -print0 | xargs -0 jq -cs '[.] | add' >"$BKG_INDEX_DIR/$owner/.json.tmp"
125125
jq -cs '{ ("package"): . }' "$BKG_INDEX_DIR/$owner/.json.tmp" >"$BKG_INDEX_DIR/$owner/.json"
126-
ytox "$BKG_INDEX_DIR/$owner/.json"
126+
ytoxt "$BKG_INDEX_DIR/$owner/.json"
127127
mv -f "$BKG_INDEX_DIR/$owner/.json.tmp" "$BKG_INDEX_DIR/$owner/.json"
128128

129129
echo "Creating $owner repo arrays..."
130130
parallel "jq -c --arg repo {} '[.[] | select(.repo == \$repo)]' \"$BKG_INDEX_DIR/$owner/.json\" > \"$BKG_INDEX_DIR/$owner/{}/.json.tmp\"" <<<"$owner_repos"
131131
xargs -I {} bash -c "jq -cs '{ (\"package\"): . }' \"$BKG_INDEX_DIR/$owner/{}/.json.tmp\" > \"$BKG_INDEX_DIR/$owner/{}/.json\"" <<<"$owner_repos"
132-
xargs -I {} bash -c "ytox \"$BKG_INDEX_DIR/$owner/{}/.json\"" <<<"$owner_repos"
132+
xargs -I {} bash -c "ytoxt \"$BKG_INDEX_DIR/$owner/{}/.json\"" <<<"$owner_repos"
133133
xargs -I {} mv -f "$BKG_INDEX_DIR/$owner/{}/.json.tmp" "$BKG_INDEX_DIR/$owner/{}/.json" <<<"$owner_repos"
134134
fi
135135

src/lib/package.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,7 @@ update_package() {
230230
[[ ! -f "$json_file".abs || ! -s "$json_file".abs ]] || jq -c --arg newest "$version_newest_id" --arg latest "$latest_version" '.version |= map(if .id == ($newest | tonumber) then .newest = true else . end | if .id == ($latest | tonumber) then .latest = true else . end)' "$json_file".abs >"$json_file".rel
231231
[[ ! -f "$json_file".rel || ! -s "$json_file".rel ]] || mv "$json_file".rel "$json_file".abs
232232
[[ ! -f "$json_file".abs || ! -s "$json_file".abs ]] || mv "$json_file".abs "$json_file"
233-
234-
# if the json is over 50MB, remove oldest versions from the packages with the most versions
235-
while [ -f "$json_file" ] && [ "$(stat -c %s "$json_file")" -ge 50000000 ]; do
236-
jq -e 'map(.version | length > 0) | any' "$json_file" || break
237-
jq -c 'sort_by(.versions | tonumber) | reverse | map(select(.versions > 0)) | map(.version |= sort_by(.id | tonumber) | del(.version[0]))' "$json_file" >"$json_file".tmp
238-
mv "$json_file".tmp "$json_file"
239-
done
240-
241-
ytox "$json_file"
233+
ytoxt "$json_file"
242234
rm -rf "$BKG_INDEX_DIR/$owner/$repo/$package.d"
243235
echo "Refreshed $owner/$package"
244236
}

src/lib/util.sh

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,45 @@ get_membership() {
435435
}
436436

437437
ytox() {
438-
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml>$(yq -ox -I0 "$1" | sed 's/"/\\"/g')</xml>" >"${1%.*}.xml"
438+
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml>$(yq -ox -I0 "$1" | sed 's/"/\\"/g')</xml>" >"${1%.*}.xml" 2>/dev/null
439+
stat -c %s "${1%.*}.xml" || echo -1
440+
}
441+
442+
ytoxt() {
443+
# ytox + trim if the json or xml is over 50MB, remove oldest versions
444+
while [ -f "$1" ] && [[ "$(ytox "$1")" -ge 50000000 || "$(stat -c %s "$1")" -ge 50000000 ]]; do
445+
jq -e '
446+
if type == "array" then
447+
reduce .[] as $pkg (false; . or (($pkg.version // []) | length > 0))
448+
else
449+
(.version // []) | length > 0
450+
end
451+
' "$1" || break
452+
jq -c '
453+
def trim_versions:
454+
if (.version // []) | length > 0 then
455+
.version |= (
456+
sort_by(
457+
.id | (if type == "string" and test("^-?[0-9]+$") then tonumber else . end)
458+
)
459+
| del(.[0])
460+
)
461+
else
462+
.
463+
end;
464+
def version_count:
465+
(if has("raw_versions") then .raw_versions else (.versions // 0) end)
466+
| (if type == "string" then (if test("^-?[0-9]+$") then tonumber else 0 end) else . end);
467+
if type == "array" then
468+
sort_by(version_count) | reverse
469+
| map(select((.version // []) | length > 0))
470+
| map(trim_versions)
471+
else
472+
trim_versions
473+
end
474+
' "$1" >"$1".tmp
475+
mv "$1".tmp "$1"
476+
done
439477
}
440478

441479
ytoy() {

0 commit comments

Comments
 (0)