Skip to content

Instantly share code, notes, and snippets.

@ivanopcode
Created July 10, 2023 23:26
Show Gist options
  • Save ivanopcode/412bdf1a811baa3e05c3b282e77acd2a to your computer and use it in GitHub Desktop.
Save ivanopcode/412bdf1a811baa3e05c3b282e77acd2a to your computer and use it in GitHub Desktop.
xcgen_perdux_module script that generates named perdux module stub
#!/bin/zsh
# ---------------------------------------------------------------------------------
# Script Name: xcgen_perdux_module
# Version: 1.0.0+2023-06-10
# Author: Ivan Oparin <[email protected]>
#
# License:
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ---------------------------------------------------------------------------------
#!/bin/zsh
# Color codes
ERROR='\033[1;31m' # Bold Red
SUCCESS='\033[1;32m' # Bold Green
WARNING='\033[1;33m' # Bold Yellow
NOCOLOR='\033[0m' # No Color
# Check if argument is provided
if [ $# -ne 1 ]; then
echo -e "${ERROR}Usage: $0 name_to_replace${NOCOLOR}"
exit 1
fi
# The name to replace TEMPLATE_NAME
NAME=$1
# The source directory
SRC_DIR="./Autogen/Redux/Modules/Module_Template"
# Check if source directory exists
if [ ! -d "$SRC_DIR" ]; then
echo -e "${ERROR}Source directory $SRC_DIR does not exist.${NOCOLOR}"
exit 1
fi
# The temporary directory
TEMP_DIR="./Autogen/Redux/Modules/${NAME}"
# Copy the source directory to the temporary directory
cp -R "$SRC_DIR" "$TEMP_DIR" || { echo -e "${ERROR}Failed to copy $SRC_DIR to $TEMP_DIR.${NOCOLOR}"; exit 1; }
# The final directory to move to
REDUX_MODULES_DIR="./Unitea/Modules"
# Find all files in the temporary directory that have TEMPLATE_NAME in the filename
find "$TEMP_DIR" -type f -name "*TEMPLATE_NAME*" | while read -r FILE
do
# Rename the file replacing TEMPLATE_NAME with the name
mv "$FILE" "${FILE//TEMPLATE_NAME/$NAME}" || { echo -e "${ERROR}Failed to rename $FILE.${NOCOLOR}"; exit 1; }
done
# Find all text files in the temporary directory and replace TEMPLATE_NAME with the name
find "$TEMP_DIR" -type f -exec grep -Iq . {} \; -print | while read -r FILE
do
sed -i '' "s/TEMPLATE_NAME/$NAME/g" "$FILE" || { echo -e "${ERROR}Failed to replace TEMPLATE_NAME in $FILE.${NOCOLOR}"; exit 1; }
done
# Move the temporary directory to the final directory
mv "$TEMP_DIR" "$REDUX_MODULES_DIR" || { echo -e "${ERROR}Failed to move $TEMP_DIR to $REDUX_MODULES_DIR.${NOCOLOR}"; exit 1; }
# Check if the move was successful
if [ $? -eq 0 ]; then
echo -e "${SUCCESS}Moved the directory to $REDUX_MODULES_DIR${NOCOLOR}"
else
echo -e "${ERROR}Failed to move the directory${NOCOLOR}"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment