Created
September 8, 2012 21:57
-
-
Save HighwayofLife/3680107 to your computer and use it in GitHub Desktop.
Rename/Duplicate PyroCMS Module Bash Script
This file contains 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
#!/bin/bash | |
## | |
# Script created by David Lewis ([email protected]) for PyroCMS | |
# This script will rename an existing (core) CMS module and duplicate it into the | |
# addons/shared_addons/modules directory, while renaming it and all file contents to | |
# match | |
# | |
# Usage: ./rename_module.sh "old_module" "new_module" | |
# | |
# TODO: | |
# 1. Ideally, this would work for all plugins/widgets/addons/modules, regardless | |
# of location. | |
# 2. Error-check input and ensure it comes in as lowercase alpha-numeric with | |
# underscores | |
# | |
# License: | |
# THE SOFTWARE IS PROVIDED TO YOU "AS IS," WITHOUT WARRANTY. THERE IS NO WARRANTY | |
# FOR THE SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT OF THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE QUALITY AND | |
# PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU | |
# ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. | |
# | |
## | |
# Current directory should be PyroCMS | |
PYROCMS_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
MODULES_DIR="$PYROCMS_PATH/system/cms/modules" | |
ADDON_MODULES_DIR="$PYROCMS_PATH/addons/shared_addons/modules" | |
OLD_MODULE="$1" | |
NEW_MODULE="$2" | |
if [[ -z "$1" || -z "$1" ]]; then | |
echo "Usage: ./rename_module [old-module] [new-module]" | |
exit 1 | |
fi | |
if [ ! -d "$MODULES_DIR" ]; then | |
echo "[ ERROR ] Not Found: Modules directory: ($MODULES_DIR)" | |
echo "[ ERROR ] Ensure this script is run from the PyroCMS root directory" | |
exit 1; | |
fi | |
if [ ! -d "$ADDON_MODULES_DIR" ]; then | |
echo "[ ERROR ] Not Found: Shared Addons Modules directory: ($ADDON_MODULES_DIR)" | |
echo "[ ERROR ] Ensure the $ADDON_MODULES_DIR directory exists" | |
exit 1; | |
fi | |
if [ ! -d "$MODULES_DIR/$OLD_MODULE" ]; then | |
echo "Existing module: '$OLD_MODULE' not found" | |
exit 1 | |
fi | |
echo "Duplicating $OLD_MODULE to '$ADDON_MODULES_DIR/$NEW_MODULE" | |
cp -a "$MODULES_DIR/$OLD_MODULE" "$ADDON_MODULES_DIR/$NEW_MODULE" | |
# Run this twice for renamed directories | |
# TODO: Rather than running multiple times, use while loop | |
echo "Rename the module files to match $NEW_MODULE" | |
find "$ADDON_MODULES_DIR/$NEW_MODULE" -name "*blog*" -exec rename "s/$OLD_MODULE/$NEW_MODULE/" {} \; | |
find "$ADDON_MODULES_DIR/$NEW_MODULE" -name "*blog*" -exec rename "s/$OLD_MODULE/$NEW_MODULE/" {} \; | |
LOWER_CASE="${NEW_MODULE,,}" | |
UPPER_CASE="${NEW_MODULE^^}" | |
TITLE_CASE="${NEW_MODULE[@]^}" | |
OLD_LOWER_CASE="${OLD_MODULE,,}" | |
OLD_UPPER_CASE="${OLD_MODULE^^}" | |
OLD_TITLE_CASE="${OLD_MODULE[@]^}" | |
echo "Perform find and replace (sed -i 's/$OLD_UPPER_CASE/$UPPER_CASE/')" | |
find "$ADDON_MODULES_DIR/$NEW_MODULE" -name "*blog*" -exec sed -i "s/$OLD_UPPER_CASE/$UPPER_CASE/" {} \; | |
echo "Perform find and replace (sed -i 's/$OLD_TITLE_CASE/$TITLE_CASE/')" | |
find "$ADDON_MODULES_DIR/$NEW_MODULE" -name "*blog*" -exec sed -i "s/$OLD_TITLE_CASE/$TITLE_CASE/" {} \; | |
echo "Perform find and replace (sed -i 's/$OLD_LOWER_CASE/$LOWER_CASE/')" | |
find "$ADDON_MODULES_DIR/$NEW_MODULE" -name "*blog*" -exec sed -i "s/$OLD_LOWER_CASE/$LOWER_CASE/" {} \; | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have created a fork of this for use on Mac OS X 10.7.5 with Bash 3.2.48(1)-release
Please see https://gist.github.com/3949038