refactor: Added new download_multiple_files_parallel helper function

- plugin.sh uses this to download manifests concurrently
This commit is contained in:
2026-06-25 21:16:28 +05:30
parent d108f14ce5
commit 355588c7f9
2 changed files with 50 additions and 8 deletions

View File

@@ -123,7 +123,37 @@ download_file() {
cp "$cache_file" "$dest"
}
# Helper to download multiple files in parallel using background jobs
download_multiple_files_parallel() {
# Usage: download_multiple_files_parallel url1 dest1 [url2 dest2 ...]
local pids=()
local urls=()
local exit_code=0
while [ $# -ge 2 ]; do
local url="$1"
local dest="$2"
shift 2
# Start download in background
mkdir -p "$(dirname "$dest")" 2>/dev/null || true
curl -fsSL "$url" -o "$dest" &
pids+=($!)
urls+=("$url")
done
# Wait for all background jobs to finish
for i in "${!pids[@]}"; do
if ! wait "${pids[$i]}"; then
log_warn "Failed to download from ${urls[$i]}"
exit_code=1
fi
done
return $exit_code
}
# Export functions and variables for subshells
export _LIB_COMMON_SOURCED=1
export RED GREEN YELLOW BLUE NC
export -f require_bash log_info log_success log_warn log_error confirm has_command make_temp_dir version_lt download_file
export -f require_bash log_info log_success log_warn log_error confirm has_command make_temp_dir version_lt download_file download_multiple_files_parallel

View File

@@ -60,18 +60,30 @@ declare -g -A PLUGIN_VERSIONS
EOF
if [ -f "$sources_file" ]; then
local dl_args=()
local temp_manifests=()
while IFS= read -r url || [ -n "$url" ]; do
# Skip empty lines and comments
[[ -z "$url" || "$url" == \#* ]] && continue
log_info "Fetching plugin manifest from $url..."
local manifest_json
if manifest_json=$(curl -fsSL "$url" 2>/dev/null); then
echo "$manifest_json" | parse_plugin_manifest >> "$cache_file"
else
log_warn "Failed to fetch manifest from $url"
fi
local temp_file
temp_file=$(mktemp --suffix=".json" 2>/dev/null || mktemp)
dl_args+=("$url" "$temp_file")
temp_manifests+=("$temp_file")
done < "$sources_file"
if [ ${#dl_args[@]} -gt 0 ]; then
log_info "Fetching ${#temp_manifests[@]} plugin manifests in parallel..."
download_multiple_files_parallel "${dl_args[@]}"
for temp_file in "${temp_manifests[@]}"; do
if [ -s "$temp_file" ]; then
cat "$temp_file" | parse_plugin_manifest >> "$cache_file"
fi
rm -f "$temp_file"
done
fi
fi
# Clear downloaded scripts to force lazy re-download of the updated versions