dev/endpoints/add-fields.sh

83 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
#
# example:
#
# add-fields.sh klimatologia4/web/modules/custom/src/Nodes.php string manager
#
#
C="\e[0;36m"
R="\e[0;31m"
G="\e[0;32m"
B="\e[0;34m"
W="\e[0;97m"
E="\e[00m"
# Check for 3 parameters
[ $# -ne 3 ] && echo 'Parameter missing' && exit
# Check Target file
DST_FILE="${1}"
[ ! -f ${DSTS_FILE} ] && echo 'File ${DSTS_FILE} does not exist' && exit
# Field name
NAME="${3}"
# Field source files
TYPE="${2}"
FIELD_DIR="/root/dev/endpoints/endpoint_get_fields/"
ls -1 $FIELD_DIR | grep -E "${TYPE}-(extractor|response|splitter)" > /dev/null
[ "$?" -ne 0 ] && echo -e "File for field ${R}${TYPE}${E} not found"
#
# Iterate source files
#
for SRC_FILE in `ls -1 $FIELD_DIR | grep -E "^${TYPE}-(extractor|response|splitter)"`; do
COMMENT=$(echo ${SRC_FILE} | awk -F- '{print "// AUTO_ADD_CODE_BELLOW_"$2}')
NEW_CODE=$(cat ${FIELD_DIR}/${SRC_FILE} | sed -e "s/FFFFF/${NAME}/g")
COMMENT_LINE=$(grep -n "$COMMENT" "$DST_FILE" | cut -d ":" -f 1)
INSERT_LINE=$((${COMMENT_LINE} + 1))
echo -e " 📦 ${W}SOURCE :${E} ${SRC_FILE}"
echo -e " 📦 ${W}DESTGINATION :${E} $(basename ${DST_FILE})"
echo -e " 🏁 ${W}AFTER COMMENT :${E} ${COMMENT}"
echo -e " 🏁 ${W}INSERT LINE :${E} ${INSERT_LINE}"
echo -e " 🧩 ${W}CODE :${E} ${NEW_CODE}"
awk -v insert="${INSERT_LINE}" -v text="${NEW_CODE}" 'NR == insert {print text} {print}' "$DST_FILE" > tmpfile && mv tmpfile "$DST_FILE"
echo
done
exit
comment="// FIELDS " ${}
exit
# new_code=$(cat << 'EOF'
# <?php
# // Your new code here
# $new_variable = 'New value';
# ?>
# EOF
# )
# Find the line number of the comment
comment_line=$(grep -n "$comment" "$DST_FILE" | cut -d ":" -f 1)
if [[ -n "$comment_line" ]]; then
# Calculate the line number to insert the new code
insert_line=$((comment_line + 1))
# Insert the new code into the PHP file
awk -v insert="$insert_line" -v text="$NEW_CODE" 'NR == insert {print text} {print}' "$DST_FILES" > tmpfile && mv tmpfile "$DST_FILE"
echo "New code added successfully!"
else
echo "Comment not found!"
fi