This is a restructure of the script I wrote the other day called “Bash Automation Script to Clone and Bulk Modify Files for Commit”
In this version of the script I am using it for something of an impact analysis to help identify all potential filenames that match a potential Jira ID so I can write a patch for work that I’m doing that will effectively reverse the changes.
This is helpful for situations where the final artifact you may have is located and generated based off a codebase you may not have access to.
Sequence is check the code out, build it, then search for the files/directories that match the pattern you’re looking for, then sync their directory structure to the target directory, then copy the file/directory to the target.
#!/bin/bash
SCRIPT_DIRECTORY=$(pwd)
# Target SVN Path to Checkout and Modify
SVN_CHECKOUT_URL="https://svn.company.com/path/to/checkout"
# Target Checkout Directory
SVN_CHECKOUT_DIRECTORY="/c/Code/Dynamic_Checkout"
# Filenames or directories we want to find
declare -a PATTERN_TO_TARGET_LIST
PATTERN_TO_TARGET_LIST+=("JIRA-ID-1")
PATTERN_TO_TARGET_LIST+=("JIRA-ID-2")
function find_matching_files()
{
#Spaces in a String will mess up a for loop - https://askubuntu.com/questions/344407/how-to-read-complete-line-in-for-loop-with-spaces
IFS=$'\n'
for PATTERN_TO_TARGET in "${PATTERN_TO_TARGET_LIST[@]}"
do
echo "[INFO] Looking for $PATTERN_TO_TARGET ..."
FILE_OR_DIRECTORY_LIST=$(find "$SVN_CHECKOUT_DIRECTORY" -iname "*$PATTERN_TO_TARGET*" )
for FILE_OR_DIRECTORY in $FILE_OR_DIRECTORY_LIST
do
echo "[INFO] Found $FILE_OR_DIRECTORY - Copying to Script Directory..."
# Get Preceding Directory of the File or Directory
# Input Example - /my/directory/to/target
# Target Output - /my/directory/to/
DIRECTORY_OF_FILE_OR_DIRECTORY=$(performConditionalActionOnFile "GET_JUST_THE_PRECEDING_DIRECTORY" "$FILE_OR_DIRECTORY")
# Get Directory without the SVN_CHECKOUT_DIRECTORY
# so we can create the directory structure from the origin
# Input Example - /c/Code/Dynamic_Checkout/SearchResult/FileResult
# Target to Remove - /c/Code/Dynamic_Checkout/
# Target Output - /SearchResult/FileResult
DIRECTORY_OF_FILE_WITHOUT_UNDESIRED_PATH_PREFIX=$(misc_findReplace "$SVN_CHECKOUT_DIRECTORY" "" "$DIRECTORY_OF_FILE_OR_DIRECTORY")
# Target Directory to copy the FILE_OR_DIRECTORY to
# Ideally we are copying:
# FROM /c/Code/Dynamic_Checkout/SearchResult/FileResult
# TO /c/ThisScriptPathDirectory/SearchResult/FileResult
TARGET_DIRECTORY_FOR_COPY="$SCRIPT_DIRECTORY/$DIRECTORY_OF_FILE_WITHOUT_UNDESIRED_PATH_PREFIX"
mkdir -p "$TARGET_DIRECTORY_FOR_COPY"
cp -R "$FILE_OR_DIRECTORY" "$TARGET_DIRECTORY_FOR_COPY"
done
done
#Turn it off after - https://askubuntu.com/questions/344407/how-to-read-complete-line-in-for-loop-with-spaces
unset IFS
}
function performConditionalActionOnFile()
{
STRING_MODE="$1"
FILE_NAME=$(basename -- "$2")
FILE_EXTENSION="${FILE_NAME##*.}"
FILE_NAME="${FILE_NAME%.*}"
ARCHIVE_DIRECTORY=$(dirname "$2")
ARCHIVE_OUTPUT_DIRECTORY="$ARCHIVE_DIRECTORY/$FILE_NAME"
if [ "$STRING_MODE" == "GET_JUST_THE_FILENAME_WITH_EXTENSION" ]; then
echo "$FILE_NAME.$FILE_EXTENSION"
fi
if [ "$STRING_MODE" == "GET_JUST_THE_FILENAME_NO_EXTENSION" ]; then
echo "$FILE_NAME"
fi
if [ "$STRING_MODE" == "GET_JUST_THE_PRECEDING_DIRECTORY" ]; then
echo "$ARCHIVE_DIRECTORY"
fi
}
function misc_findReplace()
{
VARIABLE_FIND="$1"
VARIABLE_REPLACE="$2"
VARIABLE_STRING="$3"
echo "$VARIABLE_STRING" | sed --expression "s|${VARIABLE_FIND}|${VARIABLE_REPLACE}|g"
}
function build()
{
cd "$SVN_CHECKOUT_DIRECTORY"
chmod 777 ./gradlew
./gradlew assemble --no-daemon --console=plain
}
function checkout()
{
# rm -Rf "$SVN_CHECKOUT_DIRECTORY"
mkdir -p "$SVN_CHECKOUT_DIRECTORY"
svn checkout "$SVN_CHECKOUT_URL" "$SVN_CHECKOUT_DIRECTORY"
}
function main()
{
checkout
build
find_matching_files
}
main