This commit is contained in:
Dane Sabo 2025-02-19 22:54:53 -05:00
parent 238dab668a
commit b1b7898a5c

View File

@ -1,3 +1,4 @@
#!/bin/bash
# ------------------------------------------------------------------
# Author: ChatGPT
@ -7,16 +8,19 @@
# file in each non-hidden directory. The file is named "Folder Name - README.md".
#
# Each README includes:
# - A header with an author/date line.
# - An author/date header.
# - A header with the folder's name.
# - An alphabetically sorted list of immediate subdirectories, with links
# to that subdirectorys own README.
# - An alphabetically sorted list of immediate files (excluding the generated README).
# - A "Summary" section with a short description generated by an LLM (using llama3.2:latest).
#
# Hidden directories (or any directory under a hidden parent such as .git) are skipped.
#
# The script only updates a README if the "body" (everything from line 3 on)
# has changed. It also prints a progress indicator like "[x/y] Updated: {dir}".
# The script only updates a README if the static content (header, subdirectories and files)
# changes. The LLM summary output is excluded from the diff check.
#
# Progress is printed as "[x/y] Updated: {dir}" or "[x/y] No change: {dir}".
# ------------------------------------------------------------------
# Set the base directory (change "." if necessary)
@ -25,8 +29,8 @@ base_dir="."
# Build an array of directories to process.
dirs=()
while IFS= read -r -d '' dir; do
# Skip directories whose basename starts with a dot or whose path contains "/.git"
if [[ "$(basename "$dir")" == .* ]] || [[ "$dir" == *"/.git"* ]] || [[ "$dir" == *"/.obsidian"* ]] ; then
# Skip directories that are hidden or inside a hidden parent (like .git)
if [[ "$(basename "$dir")" == .* ]] || [[ "$dir" == *"/.git"* ]]; then
continue
fi
dirs+=( "$dir" )
@ -35,10 +39,10 @@ done < <(find "$base_dir" -type d -print0)
total=${#dirs[@]}
count=0
# Process each directory in the array.
for dir in "${dirs[@]}"; do
count=$((count+1))
# For the top-level directory, use the actual name (using $PWD); otherwise, use the directory's basename.
# For the top-level directory, use the actual name from $PWD; otherwise, use the directory's basename.
if [ "$dir" = "$base_dir" ]; then
base=$(basename "$PWD")
else
@ -48,16 +52,14 @@ for dir in "${dirs[@]}"; do
# Define the path for the README file: "Folder Name - README.md"
readme="$dir/${base} - README.md"
# Create a temporary file to hold the new content.
tmpfile=$(mktemp)
# Write the header into the temporary file.
# Create a temporary file to hold the new static content.
tmp_static=$(mktemp)
{
echo "Generated by ChatGPT on $(date '+%Y-%m-%d')"
echo ""
echo "# Table of Contents for ${base}"
echo ""
} > "$tmpfile"
} > "$tmp_static"
#########################
# Process Subdirectories
@ -71,15 +73,14 @@ for dir in "${dirs[@]}"; do
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type d ! -name '.*' -print0)
if [ ${#subdirs[@]} -gt 0 ]; then
# Sort subdirectory names alphabetically.
mapfile -t sorted_subdirs < <(printf "%s\n" "${subdirs[@]}" | sort)
echo "## Subdirectories" >> "$tmpfile"
echo "## Subdirectories" >> "$tmp_static"
for sub in "${sorted_subdirs[@]}"; do
# Link format: [[Subfolder/Subfolder - README]]
line="- [[${sub}/${sub} - README]]"
printf "%s\n" "$line" >> "$tmpfile"
printf "%s\n" "$line" >> "$tmp_static"
done
echo "" >> "$tmpfile"
echo "" >> "$tmp_static"
fi
#################
@ -94,28 +95,49 @@ for dir in "${dirs[@]}"; do
done < <(find "$dir" -mindepth 1 -maxdepth 1 -type f ! -path "$readme" -print0)
if [ ${#files[@]} -gt 0 ]; then
# Sort file names alphabetically.
mapfile -t sorted_files < <(printf "%s\n" "${files[@]}" | sort)
echo "## Files" >> "$tmpfile"
echo "## Files" >> "$tmp_static"
for f in "${sorted_files[@]}"; do
line="- [[${f}]]"
printf "%s\n" "$line" >> "$tmpfile"
printf "%s\n" "$line" >> "$tmp_static"
done
echo "" >> "$tmpfile"
echo "" >> "$tmp_static"
fi
# If the README already exists, compare its body (from line 3 onward)
# with the new content (ignoring the first two lines, which contain the date header).
# Determine if the static content has changed.
static_changed=1
if [ -f "$readme" ]; then
if diff -q <(tail -n +3 "$tmpfile") <(tail -n +3 "$readme") >/dev/null; then
rm "$tmpfile"
echo "[$count/$total] No change: $dir"
continue
# Extract the static portion from the existing README (everything before "## Summary")
existing_static=$(sed '/^## Summary/ q' "$readme")
new_static=$(cat "$tmp_static")
if [ "$existing_static" = "$new_static" ]; then
static_changed=0
fi
fi
# Update (or create) the README.
mv "$tmpfile" "$readme"
if [ $static_changed -eq 0 ]; then
echo "[$count/$total] No change: $dir"
rm "$tmp_static"
continue
fi
#################
# Query LLM for Summary
#################
prompt="Based on the following table of contents, provide a short summary description of what this directory is about:
$(cat "$tmp_static")"
# Query the model using the Ollama CLI with the llama3.2:latest model.
summary=$(ollama run llama3.2:latest --prompt "$prompt")
# Build the full new content: static portion + summary section.
{
cat "$tmp_static"
echo "## Summary"
echo "$summary"
echo ""
} > "$readme"
rm "$tmp_static"
echo "[$count/$total] Updated: $dir"
done