Last active
July 20, 2024 21:13
-
-
Save OnceUponALoop/1dedfba8f75cf7beeea101121f8346bd to your computer and use it in GitHub Desktop.
This script searches for files with a specified pattern (e.g., *.puml) in a given directory, runs them through the PlantUML command to encode them, and saves the output in a specified directory.
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 | |
################################################################################ | |
# encode_puml.sh - A script to find and encode PlantUML files | |
# | |
# Author: Firas AlShafei | |
# Created: 2024.07.20 | |
# Last Modified: 2024.07.20 | |
# | |
# Description: | |
# This script searches for files with a specified pattern (e.g., *.puml) in a | |
# given directory, runs them through the PlantUML command to encode them, and | |
# saves the output in a specified directory. | |
# | |
# Usage: | |
# ./encode_puml.sh -p <PLANTUML_JAR> -s <SEARCH_PATH> -o <OUTPUT_DIR> -f <FILE_PATTERN> | |
# | |
# Options: | |
# -p <PLANTUML_JAR> Path to the PlantUML jar file | |
# -s <SEARCH_PATH> Path to search for files | |
# -o <OUTPUT_DIR> Directory to save the encoded files | |
# -f <FILE_PATTERN> File pattern to search for (e.g., "*.puml") | |
# | |
# Example: | |
# ./encode_puml.sh -p ./plantuml.jar -s diagrams/src -o diagrams/out -f '*.puml' | |
#######################d######################################################### | |
# Enable strict mode: | |
# -e: Exit immediately if a command exits with a non-zero status | |
# -u: Treat unset variables as an error | |
# -o pipefail: Prevent errors in a pipeline from being masked | |
set -euo pipefail | |
# Usage function to display help message | |
usage() { | |
cat <<EOF | |
Usage: $0 -p <PLANTUML_JAR> -s <SEARCH_PATH> -o <OUTPUT_DIR> -f <FILE_PATTERN> | |
-p <PLANTUML_JAR> Path to the PlantUML jar file | |
-s <SEARCH_PATH> Path to search for files | |
-o <OUTPUT_DIR> Directory to save the encoded files | |
-f <FILE_PATTERN> File pattern to search for (e.g., "*.puml") | |
EOF | |
exit 1 | |
} | |
# Function to log messages with a timestamp | |
log() { | |
local level="$1" | |
local message="$2" | |
echo "[$(date +'%Y-%m-%d %H:%M:%S')] [$level] $message" | |
} | |
# Function to check if Java is installed | |
check_java() { | |
if ! command -v java &> /dev/null; then | |
log "ERROR" "Java is not installed. Please install Java and try again." | |
exit 1 | |
fi | |
} | |
# Function to validate inputs | |
validate_inputs() { | |
if [[ -z "$PLANTUML_JAR_PATH" || -z "$SEARCH_PATH" || -z "$OUTPUT_DIR" || -z "$FILE_PATTERN" ]]; then | |
usage | |
fi | |
if [[ ! -f "$PLANTUML_JAR_PATH" ]]; then | |
log "ERROR" "PlantUML jar file not found at '$PLANTUML_JAR_PATH'" | |
exit 1 | |
fi | |
if [[ ! -d "$SEARCH_PATH" ]]; then | |
log "ERROR" "Search path '$SEARCH_PATH' does not exist" | |
exit 1 | |
fi | |
if ! mkdir -p "$OUTPUT_DIR"; then | |
log "ERROR" "Failed to create output directory '$OUTPUT_DIR'" | |
exit 1 | |
fi | |
check_java | |
} | |
# Initialize variables | |
PLANTUML_JAR_PATH="" | |
SEARCH_PATH="" | |
OUTPUT_DIR="" | |
FILE_PATTERN="" | |
# Parse command-line arguments | |
while getopts ":p:s:o:f:" opt; do | |
case $opt in | |
p) PLANTUML_JAR_PATH="$OPTARG" ;; | |
s) SEARCH_PATH="$OPTARG" ;; | |
o) OUTPUT_DIR="$OPTARG" ;; | |
f) FILE_PATTERN="$OPTARG" ;; | |
*) usage ;; # Display usage if an invalid option is passed | |
esac | |
done | |
# Validate inputs | |
validate_inputs | |
# Find all matching files and process them | |
while IFS= read -r -d '' file; do | |
# Get the filename without the path | |
filename=$(basename "$file") | |
# Get the filename without the extension | |
filename_no_ext="${filename%.*}" | |
# Define the output file path | |
output_file="$OUTPUT_DIR/$filename_no_ext.src.puml" | |
log "INFO" "Processing file '$file'..." | |
# Run PlantUML command and save the output | |
if java -jar "$PLANTUML_JAR_PATH" -o "$OUTPUT_DIR" -encodeurl "$file" > "$output_file"; then | |
log "INFO" "Successfully encoded '$file' to '$output_file'" | |
else | |
log "ERROR" "Failed to encode '$file'" | |
fi | |
done < <(find "$SEARCH_PATH" -type f -name "$FILE_PATTERN" -print0) | |
log "INFO" "Processing completed. Encoded files saved in '$OUTPUT_DIR'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment