Skip to content

Update Theme Gallery #134

Update Theme Gallery

Update Theme Gallery #134

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"