do/commands/dump
2025-08-16 19:53:13 -04:00

115 lines
No EOL
4.1 KiB
Bash

# ----------------------------------------------------
# Dumps the content of files matching a pattern into a single text file.
# ----------------------------------------------------
function run_dump() {
# --- 1. Validate Input ---
local INPUT_PATTERN="$1"
shift
local exclude_patterns=("$@")
if [ -z "$INPUT_PATTERN" ];
then
echo "Error: No input pattern provided." >&2
echo "Usage: _do dump \"<path/to/folder/*.extension>\" [-x <exclude_pattern>]..." >&2
return 1
fi
# --- 2. Determine Paths and Names ---
local SEARCH_DIR
SEARCH_DIR=$(dirname "$INPUT_PATTERN")
local FILE_PATTERN
FILE_PATTERN=$(basename "$INPUT_PATTERN")
local OUTPUT_BASENAME
OUTPUT_BASENAME=$(basename "$SEARCH_DIR")
local OUTPUT_FILE
if [ "$OUTPUT_BASENAME" == "." ]; then
# If searching in the current dir, use the folder's name for the output file.
OUTPUT_BASENAME=$(basename "$(pwd)")
OUTPUT_FILE="${OUTPUT_BASENAME}-dump.txt"
else
OUTPUT_FILE="${OUTPUT_BASENAME}.txt"
fi
# --- 3. Process Files ---
> "$OUTPUT_FILE"
echo "Searching in '$SEARCH_DIR' for files matching '$FILE_PATTERN'..."
if [ ${#exclude_patterns[@]} -gt 0 ]; then
echo "Excluding user-defined patterns: ${exclude_patterns[*]}"
fi
echo "Automatically excluding: .git directory contents"
# Dynamically build the find command
local find_cmd=("find" "$SEARCH_DIR" "-type" "f" "-name" "$FILE_PATTERN")
# Add user-defined exclusions
for pattern in "${exclude_patterns[@]}"; do
if [[ "$pattern" == */ ]]; then
# For directories (pattern ends with /), use -path
local dir_pattern=${pattern%/} # remove trailing slash
find_cmd+=("-not" "-path" "*/$dir_pattern/*")
else
# For files, use -name
find_cmd+=("-not" "-name" "$pattern")
fi
done
# Add automatic exclusions for the output file and .git directory
find_cmd+=("-not" "-name" "$OUTPUT_FILE")
find_cmd+=("-not" "-path" "*/.git/*")
# Execute the find command
"${find_cmd[@]}" -print0 | while IFS= read -r -d '' file; do
echo "--- File: $file ---" >> "$OUTPUT_FILE"
cat "$file" >> "$OUTPUT_FILE"
echo -e "\n" >> "$OUTPUT_FILE"
done
# --- 4. Final Report ---
if [ ! -s "$OUTPUT_FILE" ]; then
echo "Warning: No files found matching the pattern. No dump file created."
rm "$OUTPUT_FILE"
return 0
fi
local FILE_SIZE
FILE_SIZE=$(du -h "$OUTPUT_FILE" | cut -f1 | xargs)
# --- WordPress URL Logic ---
local dump_url=""
# Silently check if WP-CLI is available and we're in a WordPress installation.
if setup_wp_cli &>/dev/null && "$WP_CLI_CMD" core is-installed --quiet 2>/dev/null; then
local wp_home
wp_home=$("$WP_CLI_CMD" option get home --skip-plugins --skip-themes 2>/dev/null)
# We need `realpath` for this to work reliably
if [ -n "$wp_home" ] && command -v realpath &>/dev/null; then
local wp_root_path
# Use `wp config path` to get the wp-config.php path, which is more reliable.
wp_root_path=$("$WP_CLI_CMD" config path --quiet 2>/dev/null)
# Only proceed if we found a valid wp-config.php path.
if [ -n "$wp_root_path" ] && [ -f "$wp_root_path" ]; then
wp_root_path=$(dirname "$wp_root_path")
local current_path
current_path=$(realpath ".")
# Get the path of the current directory relative to the WordPress root
local relative_path
relative_path=${current_path#"$wp_root_path"}
# Construct the final URL
# This ensures no double slashes and correctly handles the root directory case
dump_url="${wp_home%/}${relative_path}/${OUTPUT_FILE}"
fi
fi
fi
# --- End WordPress URL Logic ---
echo "Generated $OUTPUT_FILE ($FILE_SIZE)"
if [ -n "$dump_url" ]; then
echo "URL: $dump_url"
fi
}