@@ -18,7 +18,7 @@ print_message() {
1818check_dependencies () {
1919 local missing_deps=()
2020
21- for cmd in curl unzip grep cut; do
21+ for cmd in curl unzip grep cut jq ; do
2222 if ! command -v " $cmd " > /dev/null 2>&1 ; then
2323 missing_deps+=(" $cmd " )
2424 fi
@@ -53,18 +53,70 @@ cleanup() {
5353trap cleanup EXIT
5454trap ' print_message "Installation aborted." "${RED}"; exit 1' INT TERM
5555
56- # Get the latest release
57- print_message " Fetching latest release information..." " ${BLUE} "
58- LATEST_RELEASE_URL =$( curl -s https://api.github.com/repos/$REPO_OWNER /$REPO_NAME /releases/latest | grep " browser_download_url.*zip " | cut -d ' " ' -f 4 )
56+ # Retrieve all releases
57+ print_message " Fetching release information..." " ${BLUE} "
58+ ALL_RELEASES =$( curl -s " https://api.github.com/repos/$REPO_OWNER /$REPO_NAME /releases" )
5959
60- if [ -z " $LATEST_RELEASE_URL " ]; then
61- print_message " Error: Could not find the latest release. Please check your internet connection and try again." " ${RED} "
60+ # Check if we got a valid JSON response
61+ if ! echo " $ALL_RELEASES " | jq empty 2> /dev/null; then
62+ print_message " Error: Failed to fetch releases. GitHub API response is not valid JSON." " ${RED} "
63+ print_message " Response: ${ALL_RELEASES} " " ${RED} "
6264 exit 1
6365fi
6466
65- print_message " Downloading latest release from $LATEST_RELEASE_URL " " ${BLUE} "
66- if ! curl -sL " $LATEST_RELEASE_URL " -o " $TEMP_DIR /rules.zip" ; then
67- print_message " Error: Failed to download the release. Please check your internet connection and try again." " ${RED} "
67+ # Check if we have any releases
68+ RELEASE_COUNT=$( echo " $ALL_RELEASES " | jq ' length' )
69+ if [ " $RELEASE_COUNT " -eq 0 ]; then
70+ print_message " Error: No releases found for $REPO_OWNER /$REPO_NAME ." " ${RED} "
71+ exit 1
72+ fi
73+
74+ # Get the latest release that has assets
75+ print_message " Finding latest release with assets..." " ${BLUE} "
76+
77+ # Find the first release with a rules.zip asset
78+ DOWNLOAD_URL=" "
79+ for i in $( seq 0 $(( RELEASE_COUNT- 1 )) ) ; do
80+ ASSETS=$( echo " $ALL_RELEASES " | jq -r " .[$i ].assets" )
81+ ASSET_COUNT=$( echo " $ASSETS " | jq ' length' )
82+
83+ if [ " $ASSET_COUNT " -gt 0 ]; then
84+ for j in $( seq 0 $(( ASSET_COUNT- 1 )) ) ; do
85+ NAME=$( echo " $ASSETS " | jq -r " .[$j ].name" )
86+ if [ " $NAME " == " rules.zip" ]; then
87+ DOWNLOAD_URL=$( echo " $ASSETS " | jq -r " .[$j ].browser_download_url" )
88+ RELEASE_NAME=$( echo " $ALL_RELEASES " | jq -r " .[$i ].name" )
89+ RELEASE_TAG=$( echo " $ALL_RELEASES " | jq -r " .[$i ].tag_name" )
90+ break 2
91+ fi
92+ done
93+ fi
94+ done
95+
96+ if [ -z " $DOWNLOAD_URL " ]; then
97+ print_message " Error: No rules.zip asset found in any release." " ${RED} "
98+ exit 1
99+ fi
100+
101+ print_message " Found release: ${RELEASE_NAME:- Unnamed} (${RELEASE_TAG:- Untagged} )" " ${BLUE} "
102+ print_message " Downloading from: $DOWNLOAD_URL " " ${BLUE} "
103+
104+ HTTP_CODE=$( curl -s -L -w " %{http_code}" -o " $TEMP_DIR /rules.zip" " $DOWNLOAD_URL " )
105+
106+ # Check if the download succeeded
107+ if [ " $HTTP_CODE " != " 200" ]; then
108+ print_message " Error: Failed to download rules.zip (HTTP code: $HTTP_CODE ). Please check your internet connection and try again." " ${RED} "
109+ exit 1
110+ fi
111+
112+ # Check if the file is empty or contains "Not Found"
113+ if [ ! -s " $TEMP_DIR /rules.zip" ]; then
114+ print_message " Error: The downloaded file is empty." " ${RED} "
115+ exit 1
116+ fi
117+
118+ if grep -q " Not Found" " $TEMP_DIR /rules.zip" ; then
119+ print_message " Error: The downloaded file contains 'Not Found'. The asset may not exist." " ${RED} "
68120 exit 1
69121fi
70122
@@ -75,9 +127,10 @@ if ! unzip -q "$TEMP_DIR/rules.zip" -d "$TEMP_DIR"; then
75127 exit 1
76128fi
77129
78- # Verify that we have extracted files (check for MDC files instead of rules.json )
130+ # Verify that we have extracted files (check for MDC files)
79131if [ $( ls -1 " $TEMP_DIR " /* .mdc 2> /dev/null | wc -l) -eq 0 ]; then
80132 print_message " Error: Invalid release package. No rule files found." " ${RED} "
133+ ls -la " $TEMP_DIR " | head -n 20
81134 exit 1
82135fi
83136
0 commit comments