name: Generate Directory READMEs on: push: branches: [main, master] paths: - "Found/**" - "Made/**" workflow_dispatch: permissions: contents: write jobs: generate-readmes: runs-on: local steps: - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Generate READMEs run: | #!/bin/bash set -euo pipefail IMAGE_EXTS=("jpg" "jpeg" "png" "gif" "webp" "bmp" "svg") count_images() { local dir="$1" count=0 for ext in "${IMAGE_EXTS[@]}"; do local n n=$(find "$dir" -maxdepth 1 -type f -iname "*.${ext}" 2>/dev/null | wc -l) count=$((count + n)) done echo "$count" } get_images() { local dir="$1" for ext in "${IMAGE_EXTS[@]}"; do find "$dir" -maxdepth 1 -type f -iname "*.${ext}" 2>/dev/null done | sort } get_subdirs() { find "$1" -maxdepth 1 -mindepth 1 -type d 2>/dev/null | sort } generate_readme() { local dir="$1" readme="${1}/README.md" local dir_name rel_path dir_name=$(basename "$dir") rel_path="${dir#./}" [ "$rel_path" = "." ] && return local content="" content+="# ${dir_name}\n\n" local parent parent_name parent=$(dirname "$dir") parent_name=$(basename "$parent") if [ "$parent" != "." ]; then content+="[\u2b05\ufe0f Back to ${parent_name}](../README.md)\n\n" fi local subdirs subdirs=$(get_subdirs "$dir") if [ -n "$subdirs" ]; then content+="## Folders\n\n" while IFS= read -r subdir; do local sub_name sub_count sub_name=$(basename "$subdir") sub_count=$(count_images "$subdir") content+="- [\ud83d\udcc2 ${sub_name}](./${sub_name}/) (${sub_count} images)\n" done <<< "$subdirs" content+="\n" fi local images images=$(get_images "$dir") if [ -n "$images" ]; then local img_count img_count=$(echo "$images" | wc -l) content+="## Images (${img_count})\n\n" content+="| | | |\n|---|---|---|\n" local col=0 while IFS= read -r img; do local img_name img_name=$(basename "$img") [ $col -eq 0 ] && content+="| " content+="[![${img_name}](./${img_name})](./${img_name}) " col=$((col + 1)) if [ $col -eq 3 ]; then content+="|\n" col=0 fi done <<< "$images" if [ $col -ne 0 ]; then while [ $col -lt 3 ]; do content+="| " col=$((col + 1)) done content+="|\n" fi content+="\n" fi echo -e "$content" > "$readme" echo "Generated: ${readme}" } for base_dir in Found Made; do if [ -d "$base_dir" ]; then generate_readme "./${base_dir}" while IFS= read -r dir; do generate_readme "$dir" done < <(find "./${base_dir}" -type d | sort) fi done - name: Commit and push changes run: | git config user.name "ACTIONEER[BOT]" git config user.email "actioneer@users.noreply.git.krzak.org" if git diff --quiet -- '*.md'; then echo "No changes to commit" exit 0 fi git add '**/README.md' git commit -m "generate READMEs" git push