From 355588c7f92ff55e199ac3f5ce4484a9ec45c308 Mon Sep 17 00:00:00 2001 From: Aditya Gupta Date: Thu, 25 Jun 2026 21:16:28 +0530 Subject: [PATCH] refactor: Added new download_multiple_files_parallel helper function - plugin.sh uses this to download manifests concurrently --- lib/common.sh | 32 +++++++++++++++++++++++++++++++- lib/plugins.sh | 26 +++++++++++++++++++------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/lib/common.sh b/lib/common.sh index 27e76c8..90b47ee 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -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 diff --git a/lib/plugins.sh b/lib/plugins.sh index 61fe9f2..e3c9a3a 100644 --- a/lib/plugins.sh +++ b/lib/plugins.sh @@ -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