Implement file preservation system for output directory cleaning
- Add sophisticated file preservation functionality to _clean_output_dir() - Support .qsgen3_preserve file with shell glob patterns for selective preservation - Preserve shared articles and important files during site regeneration - Maintain backward compatibility (no preserve file = complete cleaning) - Add comprehensive documentation in how-it-works.md - Include example preserve file (.qsgen3_preserve.example) - Remove legacy images directory and update documentation - Fix workflow documentation formatting issues
This commit is contained in:
105
bin/qsgen3
105
bin/qsgen3
@ -599,9 +599,102 @@ _minify_output_directory() {
|
||||
# --- Core Functions ---
|
||||
_clean_output_dir() {
|
||||
_log INFO "Cleaning output directory: ${QSG_CONFIG[paths_output_dir]}"
|
||||
# rm -rf "${QSG_CONFIG[paths_output_dir]}"/* # Be careful with this!
|
||||
# For now, just ensure it exists
|
||||
mkdir -p "${QSG_CONFIG[paths_output_dir]}"
|
||||
|
||||
local output_dir="${QSG_CONFIG[paths_output_dir]}"
|
||||
local preserve_file="$PROJECT_ROOT/.qsgen3_preserve"
|
||||
|
||||
# If output directory doesn't exist, just create it
|
||||
if [[ ! -d "$output_dir" ]]; then
|
||||
mkdir -p "$output_dir"
|
||||
if [[ $? -eq 0 ]]; then
|
||||
_log DEBUG "Created output directory: $output_dir"
|
||||
else
|
||||
_log ERROR "Failed to create output directory: $output_dir"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Check if preserve file exists
|
||||
if [[ -f "$preserve_file" ]]; then
|
||||
_log INFO "Found preserve file: $preserve_file"
|
||||
_log DEBUG "Performing selective cleaning with file preservation"
|
||||
|
||||
# Create temporary directory to store preserved files
|
||||
local temp_preserve_dir=$(mktemp -d)
|
||||
local files_preserved=0
|
||||
|
||||
# Read preserve patterns and backup matching files
|
||||
while IFS= read -r pattern || [[ -n "$pattern" ]]; do
|
||||
# Skip empty lines and comments
|
||||
[[ -z "$pattern" || "$pattern" == \#* ]] && continue
|
||||
|
||||
# Remove leading/trailing whitespace
|
||||
pattern=$(echo "$pattern" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
||||
[[ -z "$pattern" ]] && continue
|
||||
|
||||
_log DEBUG "Processing preserve pattern: $pattern"
|
||||
|
||||
# Find files matching the pattern in output directory
|
||||
while IFS= read -r -d '' file; do
|
||||
if [[ -f "$file" ]]; then
|
||||
# Get relative path from output directory
|
||||
local rel_path="${file#$output_dir/}"
|
||||
local preserve_path="$temp_preserve_dir/$rel_path"
|
||||
|
||||
# Create directory structure in temp location
|
||||
mkdir -p "$(dirname "$preserve_path")"
|
||||
|
||||
# Copy file to preserve location
|
||||
if cp "$file" "$preserve_path"; then
|
||||
files_preserved=$((files_preserved + 1))
|
||||
_log DEBUG "Preserved file: $rel_path"
|
||||
else
|
||||
_log WARNING "Failed to preserve file: $rel_path"
|
||||
fi
|
||||
fi
|
||||
done < <(find "$output_dir" -name "$pattern" -type f -print0 2>/dev/null)
|
||||
|
||||
done < "$preserve_file"
|
||||
|
||||
# Remove the output directory
|
||||
_log DEBUG "Removing output directory contents (preserving $files_preserved files)"
|
||||
rm -rf "$output_dir"
|
||||
|
||||
# Recreate output directory
|
||||
mkdir -p "$output_dir"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_log ERROR "Failed to recreate output directory: $output_dir"
|
||||
rm -rf "$temp_preserve_dir"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Restore preserved files
|
||||
if [[ $files_preserved -gt 0 ]]; then
|
||||
_log DEBUG "Restoring $files_preserved preserved files"
|
||||
if [[ -d "$temp_preserve_dir" ]]; then
|
||||
# Copy preserved files back, maintaining directory structure
|
||||
(cd "$temp_preserve_dir" && find . -type f -exec cp --parents {} "$output_dir/" \; 2>/dev/null)
|
||||
_log INFO "Restored $files_preserved preserved files"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Clean up temporary directory
|
||||
rm -rf "$temp_preserve_dir"
|
||||
|
||||
else
|
||||
# No preserve file - do complete cleaning as before
|
||||
_log DEBUG "No preserve file found - performing complete cleaning"
|
||||
rm -rf "$output_dir"
|
||||
mkdir -p "$output_dir"
|
||||
if [[ $? -ne 0 ]]; then
|
||||
_log ERROR "Failed to recreate output directory: $output_dir"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
_log DEBUG "Output directory cleaning completed successfully"
|
||||
return 0
|
||||
}
|
||||
|
||||
_copy_static_files() {
|
||||
@ -1168,9 +1261,9 @@ _generate_rss_feed() {
|
||||
continue
|
||||
fi
|
||||
|
||||
local title=$(echo "$frontmatter" | grep -m1 -iE '^title:' | sed -E 's/^title:[[:space:]]*//i; s/^["\x27](.*)["\x27]$/\1/')
|
||||
local date_iso=$(echo "$frontmatter" | grep -m1 -iE '^date:' | sed -E 's/^date:[[:space:]]*//i; s/^["\x27](.*)["\x27]$/\1/')
|
||||
local summary=$(echo "$frontmatter" | grep -m1 -iE '^summary:' | sed -E 's/^summary:[[:space:]]*//i; s/^["\x27](.*)["\x27]$/\1/')
|
||||
local title=$(echo "$frontmatter" | grep -m1 -iE '^title:' | sed -E 's/^title:[[:space:]]*//i; s/^[\"\x27](.*)[\"\x27]$/\1/')
|
||||
local date_iso=$(echo "$frontmatter" | grep -m1 -iE '^date:' | sed -E 's/^date:[[:space:]]*//i; s/^[\"\x27](.*)[\"\x27]$/\1/')
|
||||
local summary=$(echo "$frontmatter" | grep -m1 -iE '^summary:' | sed -E 's/^summary:[[:space:]]*//i; s/^[\"\x27](.*)[\"\x27]$/\1/')
|
||||
local draft=$(echo "$frontmatter" | grep -m1 -iE '^draft:' | sed -E 's/^draft:[[:space:]]*//i' | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
if [[ "$draft" == "true" && "${QSG_CONFIG[build_options_process_drafts]}" != "true" ]]; then
|
||||
|
Reference in New Issue
Block a user