Last active
October 15, 2024 20:31
-
-
Save 7enderhead/24a74f6aaed4f96290bfabd224297e91 to your computer and use it in GitHub Desktop.
Watch Path for File Changes and Run Tool on Certain Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Function to get the absolute path | |
get_absolute_path() { | |
local path="$1" | |
if [[ -z "$path" ]]; then | |
path="." | |
fi | |
echo "$(cd "$(dirname "$path")" && pwd)/$(basename "$path")" | |
} | |
# Function to process a single file | |
process_file() { | |
local file="$1" | |
if [[ "$file" =~ \.ypp\.md$ ]]; then | |
source_file="$(basename "$file")" | |
target_file="${source_file%.ypp.md}.md" | |
command="ypp $source_file -o $target_file" | |
echo -n "Running '$command'... " | |
TIMEFORMAT='%R seconds' | |
time_output=$(eval "time $command" 2>&1) | |
echo "done in ${time_output}." | |
fi | |
} | |
# Function to process existing files | |
process_existing_files() { | |
local dir="$1" | |
for file in "$dir"/*.ypp.md; do | |
if [ -f "$file" ]; then | |
process_file "$file" | |
fi | |
done | |
} | |
# Check if a parameter is provided | |
if [ $# -eq 0 ]; then | |
# No parameter provided, use current directory | |
target_path="$(pwd)" | |
else | |
# Parameter provided, get absolute path | |
target_path=$(get_absolute_path "$1") | |
fi | |
# Check if the path exists | |
if [ ! -e "$target_path" ]; then | |
echo "Error: Path does not exist: $target_path" | |
exit 1 | |
fi | |
# Use the path | |
echo "Working with path: $target_path" | |
# Process existing files | |
process_existing_files "$target_path" | |
# Watch for file changes | |
inotifywait --monitor --event close_write --format '%w%f' "$target_path" | while read file | |
do | |
process_file "$file" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment