Skip to content

Commit c4050ff

Browse files
committed
fix(utils): refactor grab and finalize functions for improved artifact management and logging
1 parent b2d18b0 commit c4050ff

File tree

1 file changed

+139
-126
lines changed

1 file changed

+139
-126
lines changed

automation-tools/utils.sh

Lines changed: 139 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,7 @@ FINALIZE_VERSION=""
2929
FINALIZE_COMPONENT=""
3030
SPLIT="false" # When this is enalbed it will split the archive into multiple parts if it is larger than 95MB
3131

32-
parse_flags() {
33-
while [[ "$1" =~ ^-- ]]; do
34-
case "$1" in
35-
--force)
36-
FORCE=1
37-
;;
38-
--dry-run)
39-
DRY_RUN=1
40-
;;
41-
*)
42-
echo "Unknown option: $1"
43-
exit 1
44-
;;
45-
esac
46-
shift
47-
done
48-
# Return the remaining non-flag arguments
49-
echo "$@"
50-
}
32+
5133
parse_flags() {
5234
while [[ "$1" =~ ^-- ]]; do
5335
case "$1" in
@@ -86,12 +68,21 @@ grab() {
8668
log i "Grabbing type '$type' from URL: $url" "$logfile"
8769
mkdir -p "$component/artifacts"
8870

89-
# Early return for flatpak_id type (no download)
90-
if [[ "$type" == "flatpak_id" ]]; then
91-
log i "Type flatpak_id detected, skipping download." "$logfile"
92-
manage_flatpak_id "$component" "$url"
93-
return
94-
fi
71+
case "$type" in
72+
flatpak_id)
73+
log i "Type flatpak_id detected, skipping download." "$logfile"
74+
manage_flatpak_id "$component" "$url"
75+
return
76+
;;
77+
flatpak_artifacts)
78+
log i "Type flatpak_artifacts detected, handling flatpak artifacts from URL: $url" "$logfile"
79+
output=$(manage_flatpak_artifacts "$component" "$url" "$version")
80+
FINALIZE_PATH=$(echo "$output" | cut -d'|' -f1)
81+
FINALIZE_VERSION=$(echo "$output" | cut -d'|' -f2)
82+
FINALIZE_COMPONENT="$component"
83+
return
84+
;;
85+
esac
9586

9687
# --- Resolve Wildcards First ---
9788
log i "Resolving wildcards in URL..." "$logfile"
@@ -202,7 +193,6 @@ grab() {
202193
return
203194
fi
204195

205-
#FINALIZE_PATH=$(echo "$output" | cut -d'|' -f1)
206196
FINALIZE_PATH=$(echo "$output" | cut -d'|' -f1)
207197
FINALIZE_VERSION=$(echo "$output" | cut -d'|' -f2)
208198
FINALIZE_COMPONENT="$component"
@@ -277,7 +267,7 @@ manage_appimage() {
277267
log i "File already exists at destination." "$logfile"
278268
output="$final_appimage|$version"
279269
else
280-
cp "$file_path" "$final_appimage" || { log e "Failed to copy AppImage" "$logfile"; exit 1; }
270+
cp -f "$file_path" "$final_appimage" || { log e "Failed to copy AppImage" "$logfile"; exit 1; }
281271
fi
282272

283273
chmod +x "$final_appimage"
@@ -395,7 +385,6 @@ manage_flatpak_id() {
395385

396386
if [[ $? -eq 0 ]]; then
397387
log i "Skipping $flatpak_id because version is already up-to-date." "$logfile"
398-
echo "skip" > result.txt
399388
[[ "$was_installed" == "false" ]] && flatpak uninstall --user -y "$flatpak_id" || true
400389
exit 0
401390
fi
@@ -444,8 +433,44 @@ manage_flatpak_id() {
444433
fi
445434
}
446435

447-
finalize() {
436+
manage_flatpak_artifacts() {
437+
log d "Starting manage_flatpak_artifacts function" "$logfile"
438+
439+
local component="$1"
440+
local url="$2"
441+
local version="$3"
442+
443+
local filename
444+
filename=$(basename "$url")
445+
local extension="${filename##*.}"
446+
local output_path="$component/artifacts/$filename"
447+
448+
mkdir -p "$component/artifacts"
449+
450+
wget -qc "$url" -O "$output_path" || {
451+
log e "Failed to download Flatpak artifacts from $url" "$logfile"
452+
exit 1
453+
}
448454

455+
if [[ ! -s "$output_path" ]]; then
456+
log e "Downloaded file is empty or missing: $output_path" "$logfile"
457+
exit 1
458+
fi
459+
460+
# Tentativo di estrazione versione dal file se non fornita
461+
if [[ -z "$version" ]]; then
462+
version=$(version_check "metainfo" "$component" "$output_path" 2>/dev/null)
463+
if [[ -z "$version" ]]; then
464+
log w "Unable to extract version from $output_path, falling back to filename." "$logfile"
465+
version=$(basename "$url" | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n 1)
466+
[[ -z "$version" ]] && version="unknown"
467+
fi
468+
fi
469+
470+
echo "$output_path|$version"
471+
}
472+
473+
finalize() {
449474
log d "Starting finalize function" "$logfile"
450475

451476
if [[ -z "$FINALIZE_COMPONENT" || -z "$FINALIZE_PATH" || -z "$FINALIZE_VERSION" ]]; then
@@ -460,100 +485,82 @@ finalize() {
460485
local source_path="${2:-$FINALIZE_PATH}"
461486
local version="${3:-$FINALIZE_VERSION}"
462487
local max_size_mb=95
463-
464488
local artifact_dir="$component/artifacts"
465-
mkdir -p "$artifact_dir"
466-
467489
local artifact_base="$artifact_dir/$component"
468490
local temp_tar="$artifact_base.tar.gz"
469491
local tmpzip_dir="$artifact_dir/.tmpzip"
470492

471-
if [[ -f "$source_path" ]]; then
472-
log i "Source is a file, checking size..." "$logfile"
473-
474-
local artifact_size_mb=$(( $(stat -c%s "$source_path") / 1024 / 1024 ))
493+
mkdir -p "$artifact_dir"
475494

476-
if [[ "$artifact_size_mb" -gt "$max_size_mb" && "$SPLIT" == "true" ]]; then
477-
log i "File larger than ${max_size_mb}MB, preparing split ZIP archive." "$logfile"
495+
# Case 1: single file
496+
if [[ -f "$source_path" ]]; then
497+
log i "Source is a file: $source_path" "$logfile"
478498

479-
log i "Preparing temporary directory for zipping..." "$logfile"
480-
rm -rf "$tmpzip_dir"
481-
mkdir -p "$tmpzip_dir"
482-
483-
if [[ "$source_path" =~ \.tar\.(gz|bz2|xz)$ ]]; then
484-
log i "Extracting TAR archive before zipping..." "$logfile"
485-
tar -xf "$source_path" -C "$tmpzip_dir" || { log e "Failed to extract tar archive." "$logfile"; exit 1; }
486-
elif [[ "$source_path" =~ \.zip$ ]]; then
487-
log i "Extracting ZIP archive before re-zipping..." "$logfile"
488-
unzip -q "$source_path" -d "$tmpzip_dir" || { log e "Failed to extract zip archive." "$logfile"; exit 1; }
499+
# If AppImage we don't compress it
500+
if [[ "$source_path" == *.AppImage ]]; then
501+
log i "Detected AppImage file, skipping compression." "$logfile"
502+
if [[ "$(realpath "$source_path")" != "$(realpath "$artifact_base.AppImage")" ]]; then
503+
cp -f "$source_path" "$artifact_base.AppImage" || { log e "Failed to copy AppImage." "$logfile"; exit 1; }
489504
else
490-
log i "Copying file to temporary zipping folder..."
491-
cp "$source_path" "$tmpzip_dir/" || { log e "Failed to copy file." "$logfile"; exit 1; }
505+
log i "Source and destination are the same file, skipping copy." "$logfile"
492506
fi
507+
chmod +x "$artifact_base.AppImage"
508+
sha256sum "$artifact_base.AppImage" > "$artifact_base.AppImage.sha"
493509

494-
log i "Creating split ZIP archive..." "$logfile"
495-
(cd "$artifact_dir" && zip -r -s ${max_size_mb}m "${component}.zip" ".tmpzip") || { log e "Failed to create split zip archive." "$logfile"; exit 1; }
496-
497-
log i "Cleaning temporary zipping folder..." "$logfile"
498-
rm -rf "$tmpzip_dir"
499-
500-
log i "Moving split archive parts..." "$logfile"
501-
for part in "$artifact_dir"/${component}.zip "$artifact_dir"/${component}.z*; do
502-
[ -e "$part" ] || continue
503-
hash=($(sha256sum "$part"))
504-
echo "$hash" > "$artifact_dir/$(basename "$part").sha"
505-
done
506-
510+
# Otherwise, split if required
507511
else
508-
log i "File size is within limit or splitting is disabled, copying directly." "$logfile"
509-
cp "$source_path" "$artifact_base" || { log e "Failed to copy artifact file." "$logfile"; exit 1; }
510-
511-
hash=($(sha256sum "$artifact_base"))
512-
echo "$hash" > "$artifact_dir/$(basename "$artifact_base").sha"
512+
local artifact_size_mb=$(( $(stat -c%s "$source_path") / 1024 / 1024 ))
513+
514+
if [[ "$artifact_size_mb" -gt "$max_size_mb" && "$SPLIT" == "true" ]]; then
515+
log i "Large file detected and SPLIT=true. Creating split ZIP..." "$logfile"
516+
rm -rf "$tmpzip_dir"
517+
mkdir -p "$tmpzip_dir"
518+
cp -f "$source_path" "$tmpzip_dir/" || { log e "Failed to copy file to tmp." "$logfile"; exit 1; }
519+
(cd "$artifact_dir" && zip -r -s ${max_size_mb}m "${component}.zip" ".tmpzip") || { log e "ZIP split failed." "$logfile"; exit 1; }
520+
rm -rf "$tmpzip_dir"
521+
for part in "$artifact_dir"/${component}.zip "$artifact_dir"/${component}.z*; do
522+
[[ -f "$part" ]] && sha256sum "$part" > "$artifact_dir/$(basename "$part").sha"
523+
done
524+
else
525+
log i "Copying file as-is (no split)." "$logfile"
526+
cp -f "$source_path" "$artifact_base" || { log e "Copy failed." "$logfile"; exit 1; }
527+
sha256sum "$artifact_base" > "$artifact_base.sha"
528+
fi
513529
fi
514530

531+
# Case 2: Directory
515532
elif [[ -d "$source_path" ]]; then
516-
log i "Source is a directory, creating tar.gz to check size..." "$logfile"
517-
tar -czf "$temp_tar" -C "$source_path" . || { log e "Failed to create tar.gz archive." "$logfile"; exit 1; }
533+
log i "Source is a directory." "$logfile"
518534

519-
local artifact_size_mb=$(( $(stat -c%s "$temp_tar") / 1024 / 1024 ))
535+
tar -czf "$temp_tar" -C "$source_path" . || { log e "Tar creation failed." "$logfile"; exit 1; }
520536

521-
if [[ "$artifact_size_mb" -gt "$max_size_mb" ]]; then
522-
log i "Archive larger than ${max_size_mb}MB, preparing split ZIP archive." "$logfile"
537+
local artifact_size_mb=$(( $(stat -c%s "$temp_tar") / 1024 / 1024 ))
523538

539+
if [[ "$artifact_size_mb" -gt "$max_size_mb" && "$SPLIT" == "true" ]]; then
540+
log i "Directory archive is large and SPLIT=true. Creating split ZIP..." "$logfile"
524541
rm -f "$temp_tar"
525-
526-
log i "Preparing temporary directory for zipping..." "$logfile"
527542
rm -rf "$tmpzip_dir"
528-
cp -r "$source_path" "$tmpzip_dir" || { log e "Failed to copy directory." "$logfile"; exit 1; }
529-
530-
log i "Creating split ZIP archive..." "$logfile"
531-
(cd "$artifact_dir" && zip -r -s ${max_size_mb}m "${component}.zip" ".tmpzip") || { log e "Failed to create split zip archive." "$logfile"; exit 1; }
532-
533-
log i "Cleaning temporary zipping folder..." "$logfile"
543+
cp -rf "$source_path" "$tmpzip_dir" || { log e "Failed to copy directory." "$logfile"; exit 1; }
544+
(cd "$artifact_dir" && zip -r -s ${max_size_mb}m "${component}.zip" ".tmpzip") || { log e "Split zip failed." "$logfile"; exit 1; }
534545
rm -rf "$tmpzip_dir"
535-
536-
log i "Moving split archive parts..." "$logfile"
537546
for part in "$artifact_dir"/${component}.zip "$artifact_dir"/${component}.z*; do
538-
[ -e "$part" ] || continue
539-
hash=($(sha256sum "$part"))
540-
echo "$hash" > "$artifact_dir/$(basename "$part").sha"
547+
[[ -f "$part" ]] && sha256sum "$part" > "$artifact_dir/$(basename "$part").sha"
541548
done
542-
543549
else
544-
log i "Archive size is within limit, keeping tar.gz." "$logfile"
545-
hash=($(sha256sum "$temp_tar"))
546-
echo "$hash" > "$artifact_dir/$(basename "$temp_tar").sha"
550+
log i "Archive size OK or split disabled. Keeping tar.gz." "$logfile"
551+
sha256sum "$temp_tar" > "$temp_tar.sha"
547552
fi
548553

549554
else
550-
log e "Source path is neither a file nor a directory." "$logfile"
555+
log e "Source path is neither a file nor a directory: $source_path" "$logfile"
551556
exit 1
552557
fi
553558

554559
if [[ -n "$version" ]]; then
555560
echo "$version" > "$artifact_dir/version"
556561
fi
562+
563+
rm -rf "$artifact_dir/.tmp"
557564
}
558565

559566
write_components_version() {
@@ -593,44 +600,49 @@ version_check() {
593600
local current_version=""
594601
local version_file="$component/version"
595602

596-
if [[ "$check_type" == "manual" || "$check_type" == "link" || "$check_type" == "file" ]]; then
597-
if [[ "$source" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
598-
version="$source"
599-
fi
600-
fi
601-
602-
if [[ -z "$version" && "$source" =~ ^https?:// ]]; then
603-
version=$(basename "$source" | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n 1)
604-
fi
605-
606-
if [[ -z "$version" && "$check_type" == "metainfo" ]]; then
607-
local tempdir=""
608-
local metainfo_file=""
603+
case "$check_type" in
604+
manual|link|file)
605+
if [[ "$source" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
606+
version="$source"
607+
elif [[ "$source" =~ ^https?:// ]]; then
608+
version=$(basename "$source" | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n 1)
609+
fi
610+
;;
609611

610-
if [[ -f "$source" ]]; then
611-
tempdir=$(mktemp -d)
612+
metainfo)
613+
if [[ -f "$source" && "$source" =~ \.(metainfo|appdata)\.xml$ ]]; then
614+
version=$(xmlstarlet sel -t -v "/component/releases/release[1]/@version" "$source" 2>/dev/null | head -n 1)
615+
616+
elif [[ -d "$source" ]]; then
617+
local metainfo_file
618+
metainfo_file=$(find "$source" -type f \( -name "*.metainfo.xml" -o -name "*.appdata.xml" \) | head -n 1)
619+
if [[ -n "$metainfo_file" ]]; then
620+
version=$(xmlstarlet sel -t -v "/component/releases/release[1]/@version" "$metainfo_file" 2>/dev/null | head -n 1)
621+
fi
622+
623+
elif [[ -f "$source" && "$source" =~ \.tar\.(gz|xz|bz2)$ ]]; then
624+
local metainfo_path
625+
metainfo_path=$(tar -tf "$source" | grep -m1 -E '\.(metainfo|appdata)\.xml$')
626+
if [[ -n "$metainfo_path" ]]; then
627+
log d "Found metadata in archive: $metainfo_path" "$logfile"
628+
version=$(tar -xOf "$source" "$metainfo_path" 2>/dev/null | \
629+
xmlstarlet sel -t -v "/component/releases/release[1]/@version" 2>/dev/null | head -n 1)
630+
fi
631+
632+
elif [[ -f "$source" && "$source" =~ \.zip$ ]]; then
633+
version=$(unzip -p "$source" '*.metainfo.xml' '*.appdata.xml' 2>/dev/null | \
634+
xmlstarlet sel -t -v "/component/releases/release[1]/@version" 2>/dev/null | head -n 1)
612635

613-
if [[ "$source" =~ \.tar\.(gz|bz2|xz)$ ]]; then
614-
tar -xf "$source" -C "$tempdir" || { log e "Failed to extract archive." "$logfile"; rm -rf "$tempdir"; exit 1; }
615-
elif [[ "$source" =~ \.zip$ ]]; then
616-
unzip -q "$source" -d "$tempdir" || { log e "Failed to extract archive." "$logfile"; rm -rf "$tempdir"; exit 1; }
617636
else
618-
log e "Unsupported archive format for metainfo extraction." "$logfile"
619-
rm -rf "$tempdir"
620-
exit 1
637+
log w "Unsupported format for metainfo extraction: $source" "$logfile"
621638
fi
622-
623-
metainfo_file=$(find "$tempdir" -type f -name "*.metainfo.xml" | head -n 1)
624-
elif [[ -d "$source" ]]; then
625-
metainfo_file=$(find "$source" -type f -name "*.metainfo.xml" | head -n 1)
626-
fi
627-
628-
if [[ -n "$metainfo_file" ]]; then
629-
version=$(xmlstarlet sel -t -v "/component/releases/release[1]/@version" "$metainfo_file" 2>/dev/null | head -n 1)
630-
fi
631-
632-
[[ -n "$tempdir" ]] && rm -rf "$tempdir"
633-
fi
639+
;;
640+
641+
*)
642+
log e "Unknown version check type: $check_type" "$logfile"
643+
exit 1
644+
;;
645+
esac
634646

635647
if [[ -z "$version" ]]; then
636648
log e "Could not determine version for $component (source: \"$source\")" "$logfile"
@@ -642,7 +654,7 @@ version_check() {
642654
if [[ -f "$version_file" ]]; then
643655
current_version=$(cat "$version_file")
644656
if [[ "$current_version" == "$version" && "${FORCE:-0}" -ne 1 ]]; then
645-
echo "$version" # <-- AGGIUNTA per sicurezza
657+
echo "$version"
646658
return 0
647659
fi
648660
fi
@@ -651,3 +663,4 @@ version_check() {
651663
echo "$version"
652664
return 1
653665
}
666+

0 commit comments

Comments
 (0)