Skip to content

Instantly share code, notes, and snippets.

@YohannesTz
Created March 14, 2024 17:42
Show Gist options
  • Save YohannesTz/de88f90b4175fc69b0dd87fd78a92a15 to your computer and use it in GitHub Desktop.
Save YohannesTz/de88f90b4175fc69b0dd87fd78a92a15 to your computer and use it in GitHub Desktop.
a Simple script to resolve java files into their respective folder/package structure
# usage
chmod +x package_resolver.sh
./pacakge_resolver.sh /input/folder /output/folder
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <source_folder> <destination_folder>"
exit 1
fi
source_folder="$1"
destination_folder="$2"
# Check if the source folder exists
if [ ! -d "$source_folder" ]; then
echo "Source folder does not exist: $source_folder"
exit 1
fi
# Loop through each Java file in the source folder
find "$source_folder" -type f -name "*.java" | while read -r java_file; do
# Extract package declaration from Java file
package=$(grep -E '^package\s+([a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*);' "$java_file" | sed -E 's/^package\s+([a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*);/\1/' | tr -d ';')
# Replace dots with slashes for folder structure
package_folder="${package//./\/}"
# Create destination folder if it doesn't exist
mkdir -p "$destination_folder/$package_folder"
# Copy the Java file to the destination folder
cp "$java_file" "$destination_folder/$package_folder"
done
echo "Java files resolved and copied to their respective package structure."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment