-
Notifications
You must be signed in to change notification settings - Fork 1
187 lines (150 loc) · 7.18 KB
/
Copy pathgenerate-theme-gallery.yml
File metadata and controls
187 lines (150 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
name: Update Theme Gallery
concurrency:
group: theme-gallery
cancel-in-progress: false
on:
workflow_dispatch:
workflow_run:
workflows: ["Package Theme Previews"]
types:
- completed
branches:
- main
jobs:
update-readme:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags
- name: Check for new themes
id: check-themes
env:
GH_TOKEN: ${{ github.token }}
run: |
normalize_name() {
echo "$1" | sed "s/ /_/g; s/'//g; s/!//g"
}
# Get normalized list of theme directories
current_themes=$(find Themes -maxdepth 1 -mindepth 1 -type d | sed 's|Themes/||' | while IFS= read -r name; do normalize_name "$name"; done | sort)
echo "::debug::Current themes (normalized):"
echo "::debug::$current_themes"
# Get list of themes from release assets
release_themes=$(gh release view 1 --json assets -q '.assets[].name' 2>/dev/null | sed 's|\.7z$||' | sort)
echo "::debug::Themes in release:"
echo "::debug::$release_themes"
echo "$current_themes" > current_themes.txt
echo "$release_themes" > release_themes.txt
if ! diff current_themes.txt release_themes.txt > theme_diff.txt; then
echo "::notice::Differences detected between directory and release:"
cat theme_diff.txt
echo "theme_changed=1" >> "$GITHUB_OUTPUT"
else
echo "::notice::No differences detected"
echo "theme_changed=0" >> "$GITHUB_OUTPUT"
fi
rm current_themes.txt release_themes.txt theme_diff.txt
- name: Generate Theme Gallery
if: github.event_name == 'workflow_dispatch' || github.event_name == 'workflow_run' || steps.check-themes.outputs.theme_changed == '1'
id: generate-gallery
env:
GH_TOKEN: ${{ github.token }}
run: |
normalize_name() {
echo "$1" | sed "s/ /_/g; s/'//g; s/!//g"
}
# Fetch release asset list once
RELEASE_ASSETS=$(gh release view 1 --json assets -q '.assets[].name' 2>/dev/null || echo "")
# Create backup of current README
cp README.md README.md.bak
# Create temporary files
TEMP_FILE=$(mktemp)
GALLERY_CONTENT=$(mktemp)
# Generate the new gallery content
echo -e "## Theme Gallery\n" > "$GALLERY_CONTENT"
# Initialize arrays for the grid
declare -a themes=()
# Process each theme folder
for theme_dir in Themes/*/; do
if [ -d "$theme_dir" ] && [ "$theme_dir" != "PackedThemes/" ]; then
theme_name="${theme_dir#Themes/}"
theme_name="${theme_name%/}"
preview_path_gif="$theme_dir/preview.gif"
preview_path_png="$theme_dir/preview.png"
config_path="$theme_dir/config.json"
normalized_theme="$(normalize_name "$theme_name")"
if echo "$RELEASE_ASSETS" | grep -qF "${normalized_theme}.7z" && [ -f "$config_path" ] && { [ -f "$preview_path_gif" ] || [ -f "$preview_path_png" ]; }; then
# Extract author and description from config.json
author=$(jq -r '.author' "$config_path")
description=$(jq -r '.description' "$config_path")
# Determine which preview file to use
if [ -f "$preview_path_gif" ]; then
preview_path="$preview_path_gif"
else
preview_path="$preview_path_png"
fi
# Create properly encoded URLs
preview_path=$(echo "$preview_path" | sed 's|^Themes/||')
theme_name_encoded=$(echo "$theme_name" | sed -e 's/ /%20/g')
preview_url="https://raw.githubusercontent.com/$GITHUB_REPOSITORY/main/Themes/${theme_name_encoded}/preview.png"
preview_img="<img title=\"${theme_name}\" width=\"200px\" src=\"${preview_url}\" />"
download_url="https://github.com/$GITHUB_REPOSITORY/releases/download/1/${normalized_theme}.7z"
# Clean up preview URL (remove any double slashes except after https:)
preview_url=$(echo "$preview_url" | sed -e 's|//|/|g' -e 's|https:/|https://|')
# Create theme cell content
theme_cell="<td align=\"center\" valign=\"top\" width=\"33.33%\">
<br/>
<a href=\"$download_url\">
$preview_img<br/>
<b>$theme_name</b></a><br/>
<small><i>$author</i></small><br/>
<small>$description</small><br/>
</td>"
themes+=("$theme_cell")
fi
fi
done
# Output in grid format
count=0
total=${#themes[@]}
echo "<table align=\"center\">" >> "$GALLERY_CONTENT"
while [ $count -lt $total ]; do
echo "<tr>" >> "$GALLERY_CONTENT"
# Add up to 3 theme cells per row
for i in {0..2}; do
if [ $((count + i)) -lt $total ]; then
echo "${themes[$((count + i))]}" >> "$GALLERY_CONTENT"
fi
done
echo "</tr>" >> "$GALLERY_CONTENT"
count=$((count + 3))
done
echo "</table>" >> "$GALLERY_CONTENT"
# Extract content before Theme Gallery
awk '/^## Theme Gallery/{exit} {print}' README.md > "$TEMP_FILE"
# Add the new gallery content
cat "$GALLERY_CONTENT" >> "$TEMP_FILE"
# Add footer content
echo -e "\nThemes here are in .7z format, you can place them into your Themes folder and spruce will automatically unzip them." >> "$TEMP_FILE"
# Replace the original README
mv "$TEMP_FILE" README.md
# Compare if there were actual changes
if ! diff README.md README.md.bak > readme_diff.txt; then
echo "::notice::README content has changed"
cat readme_diff.txt
echo "readme_changed=1" >> "$GITHUB_OUTPUT"
else
echo "::notice::README content remains the same"
echo "readme_changed=0" >> "$GITHUB_OUTPUT"
fi
rm README.md.bak readme_diff.txt
- name: Commit and push changes
if: (github.event_name == 'workflow_dispatch' || steps.check-themes.outputs.theme_changed == '1') && steps.generate-gallery.outputs.readme_changed == '1'
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add README.md
git commit -m "Update theme gallery" || echo "No changes to commit"
git push || echo "No changes to push"