1+ #! /bin/bash
2+
3+ # Colors for output
4+ RED=' \033[0;31m'
5+ GREEN=' \033[0;32m'
6+ YELLOW=' \033[1;33m'
7+ BLUE=' \033[0;34m'
8+ NC=' \033[0m' # No Color
9+
10+ # Script to update README.md with latest release information
11+ # Similar to TOC generation, this uses markers to define update sections
12+
13+ README_FILE=" README.md"
14+ TEMP_FILE=" README.tmp"
15+
16+ # Function to check if we're in a git repository
17+ check_git_repo () {
18+ if ! git rev-parse --git-dir > /dev/null 2>&1 ; then
19+ echo -e " ${RED} Error: Not in a git repository${NC} "
20+ exit 1
21+ fi
22+ }
23+
24+ # Function to get latest release info from GitHub API
25+ get_latest_release () {
26+ local repo_url=$( git config --get remote.origin.url)
27+ local repo_path=" "
28+
29+ # Extract owner/repo from different URL formats
30+ if [[ $repo_url == * " github.com" * ]]; then
31+ if [[ $repo_url == git@* ]]; then
32+ # SSH format: git@github.com:user/repo.git
33+ repo_path=$( echo " $repo_url " | sed ' s/git@github.com://' | sed ' s/\.git$//' )
34+ else
35+ # HTTPS format: https://github.com/user/repo.git
36+ repo_path=$( echo " $repo_url " | sed ' s|.*github.com/||' | sed ' s/\.git$//' )
37+ fi
38+ else
39+ echo -e " ${RED} Error: Not a GitHub repository${NC} "
40+ exit 1
41+ fi
42+
43+ echo " $repo_path "
44+ }
45+
46+ # Function to fetch release data from GitHub API
47+ fetch_release_data () {
48+ local repo_path=" $1 "
49+ local api_url=" https://api.github.com/repos/${repo_path} /releases/latest"
50+
51+ echo -e " ${BLUE} Fetching latest release from: $api_url ${NC} "
52+
53+ # Use curl to fetch release data
54+ local response=$( curl -s " $api_url " )
55+
56+ # Check if we got a valid response
57+ if echo " $response " | grep -q ' "message": "Not Found"' ; then
58+ echo -e " ${YELLOW} Warning: No releases found or repository not public${NC} "
59+ return 1
60+ fi
61+
62+ echo " $response "
63+ }
64+
65+ # Function to extract download URLs for different platforms
66+ extract_download_urls () {
67+ local release_data=" $1 "
68+ local version=$( echo " $release_data " | grep ' "tag_name"' | head -1 | cut -d' "' -f4)
69+ local release_notes=$( echo " $release_data " | grep ' "body"' | head -1 | cut -d' "' -f4 | sed ' s/\\n/\n/g' | sed ' s/\\r//g' )
70+
71+ echo -e " ${GREEN} Found release: $version ${NC} "
72+
73+ # Extract asset download URLs
74+ local assets=$( echo " $release_data " | grep ' "browser_download_url"' | cut -d' "' -f4)
75+
76+ # Generate markdown for different platforms
77+ local download_section=" "
78+ download_section+=" \n## 📦 Installation\n\n"
79+ download_section+=" ### Download Pre-built Binaries\n\n"
80+ download_section+=" **Latest Release: \` $version \` **\n\n"
81+
82+ # Platform-specific downloads
83+ if echo " $assets " | grep -q " linux.*x86_64" ; then
84+ local linux_x64_url=$( echo " $assets " | grep " linux.*x86_64" | head -1)
85+ download_section+=" - **Linux x86_64**: [Download]($linux_x64_url )\n"
86+ fi
87+
88+ if echo " $assets " | grep -q " linux.*aarch64" ; then
89+ local linux_arm64_url=$( echo " $assets " | grep " linux.*aarch64" | head -1)
90+ download_section+=" - **Linux ARM64**: [Download]($linux_arm64_url )\n"
91+ fi
92+
93+ if echo " $assets " | grep -q " windows.*x86_64" ; then
94+ local windows_x64_url=$( echo " $assets " | grep " windows.*x86_64" | head -1)
95+ download_section+=" - **Windows x86_64**: [Download]($windows_x64_url )\n"
96+ fi
97+
98+ if echo " $assets " | grep -q " macos.*x86_64" ; then
99+ local macos_x64_url=$( echo " $assets " | grep " macos.*x86_64" | head -1)
100+ download_section+=" - **macOS Intel**: [Download]($macos_x64_url )\n"
101+ fi
102+
103+ if echo " $assets " | grep -q " macos.*aarch64" ; then
104+ local macos_arm64_url=$( echo " $assets " | grep " macos.*aarch64" | head -1)
105+ download_section+=" - **macOS Apple Silicon**: [Download]($macos_arm64_url )\n"
106+ fi
107+
108+ download_section+=" \n### Installation Instructions\n\n"
109+ download_section+=" 1. Download the appropriate binary for your platform\n"
110+ download_section+=" 2. Rename the downloaded file to \` ci\` (Linux/macOS) or \` ci.exe\` (Windows)\n"
111+ download_section+=" 3. Move the binary to your PATH: \` mv ci /usr/local/bin/\` (Linux/macOS)\n"
112+ download_section+=" 4. Make it executable: \` chmod +x /usr/local/bin/ci\` (Linux/macOS)\n\n"
113+
114+ # Add what's new section if release notes exist
115+ if [[ -n " $release_notes " && " $release_notes " != " null" ]]; then
116+ download_section+=" ### What's New in $version \n\n"
117+ download_section+=" $release_notes \n\n"
118+ fi
119+
120+ echo -e " $download_section "
121+ }
122+
123+ # Function to update README between markers
124+ update_readme () {
125+ local new_content=" $1 "
126+ local start_marker=" <!-- RELEASE_INFO_START -->"
127+ local end_marker=" <!-- RELEASE_INFO_END -->"
128+
129+ if [ ! -f " $README_FILE " ]; then
130+ echo -e " ${RED} Error: $README_FILE not found${NC} "
131+ exit 1
132+ fi
133+
134+ # Check if markers exist
135+ if ! grep -q " $start_marker " " $README_FILE " || ! grep -q " $end_marker " " $README_FILE " ; then
136+ echo -e " ${YELLOW} Warning: Release info markers not found in $README_FILE ${NC} "
137+ echo -e " ${YELLOW} Please add the following markers where you want the release info:${NC} "
138+ echo -e " ${BLUE} $start_marker ${NC} "
139+ echo -e " ${BLUE} $end_marker ${NC} "
140+ exit 1
141+ fi
142+
143+ # Create temp file with updated content
144+ awk -v start=" $start_marker " -v end=" $end_marker " -v content=" $new_content " '
145+ BEGIN { in_section = 0 }
146+ $0 ~ start {
147+ print $0
148+ print content
149+ in_section = 1
150+ next
151+ }
152+ $0 ~ end {
153+ print $0
154+ in_section = 0
155+ next
156+ }
157+ !in_section { print $0 }
158+ ' " $README_FILE " > " $TEMP_FILE "
159+
160+ # Replace original file
161+ mv " $TEMP_FILE " " $README_FILE "
162+
163+ echo -e " ${GREEN} ✓ README.md updated successfully${NC} "
164+ }
165+
166+ # Main execution
167+ main () {
168+ echo -e " ${BLUE} === README Release Info Updater ===${NC} "
169+
170+ # Check if we're in a git repo
171+ check_git_repo
172+
173+ # Get repository information
174+ local repo_path=$( get_latest_release)
175+ echo -e " ${BLUE} Repository: $repo_path ${NC} "
176+
177+ # Fetch release data
178+ local release_data=$( fetch_release_data " $repo_path " )
179+ if [[ $? -ne 0 ]]; then
180+ exit 1
181+ fi
182+
183+ # Extract and format download information
184+ local download_content=$( extract_download_urls " $release_data " )
185+
186+ # Update README
187+ update_readme " $download_content "
188+
189+ echo -e " ${GREEN} Release info update completed!${NC} "
190+ }
191+
192+ # Parse command line arguments
193+ case " ${1:- } " in
194+ -h|--help)
195+ echo " Usage: $0 [--help]"
196+ echo " Updates README.md with latest release information from GitHub"
197+ echo " "
198+ echo " The script looks for these markers in README.md:"
199+ echo " <!-- RELEASE_INFO_START -->"
200+ echo " <!-- RELEASE_INFO_END -->"
201+ echo " "
202+ echo " All content between these markers will be replaced with"
203+ echo " automatically generated release information."
204+ exit 0
205+ ;;
206+ * )
207+ main " $@ "
208+ ;;
209+ esac
0 commit comments