Skip to content

Commit

Permalink
restored repurpose script
Browse files Browse the repository at this point in the history
  • Loading branch information
klaxalk committed Nov 2, 2023
1 parent 35a91ea commit d5afb77
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions repurpose_package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env bash

if [ $# -le 1 ]; then
echo "usage: ./repurpose_package.sh <original_package_name> <new_package_name> [--dry-run]"
if [ $1 == "--help" ]; then
echo "
Removes the .git directory, initializes a new one,
replaces all occurences of the original package name with the new one in all files withing all subfolders,
replaces all occurences of the original package name with the new one wthinin all filenames."
fi
exit 1
fi

ORIG_NAME=$1
NEW_NAME=$2

if [ "$3" == "--dry-run" ]; then
echo "Dry run mode on - nothing will be changed"
fi

# Remove .git and initialize a new repository

echo "** CREATING NEW GIT REPOSITORY **"

# Ask for confirmation
echo History of the old repository will be deleted.
read -p "Are you sure? " -r

if [[ $REPLY =~ ^[Yy]$ ]]
then

if [ "$3" != "--dry-run" ]; then
rm -rf .git
git init
fi

fi

# Replace occurences within files
readarray -d '' within_files < <(grep -rl "$ORIG_NAME" --null)

echo "** REPLACING OCCURENCES WITHIN FILES **"

if [[ -z "${within_files[@]}" ]]; then

echo "No matches found for \"$1\" within files!"

else

# Ask for confirmation
echo These files will be modified:
for file in "${within_files[@]}"
do
echo " - $file"
done
read -p "Are you sure? " -r

if [[ $REPLY =~ ^[Yy]$ ]]
then

for file in "${within_files[@]}"
do
echo " - Replacing \"$1\" with \"$2\" in file \"$file\"."
if [ "$3" != "--dry-run" ]; then
sed -i "s/$ORIG_NAME/$NEW_NAME/g" "$file"
fi
done

fi

fi

# Replace occurences in file names
readarray -d '' files < <(find -name "*$ORIG_NAME*" -print0)

echo "** REPLACING OCCURENCES WITHIN FILE NAMES **"

if [[ -z "${files[@]}" ]]; then

echo "No matches found for \"$1\" in file names!"

else

# Ask for confirmation
echo These files will be modified:
for file in "${files[@]}"
do
echo " - $file"
done
read -p "Are you sure? " -r

if [[ $REPLY =~ ^[Yy]$ ]]
then


for file in "${files[@]}"
do
new_file=${file//$ORIG_NAME/$NEW_NAME}
echo " - Renaming file \"$file\" to \"$new_file\"."
if [ "$3" != "--dry-run" ]; then
mv "$file" "$new_file"
fi
done

fi

fi

if [ "$3" == "--dry-run" ]; then
echo "Dry run mode on - nothing was changed"
fi

0 comments on commit d5afb77

Please sign in to comment.