Skip to content

Instantly share code, notes, and snippets.

@deekayen
Last active October 29, 2021 22:08
Show Gist options
  • Save deekayen/14d48f1fadec49039a79 to your computer and use it in GitHub Desktop.
Save deekayen/14d48f1fadec49039a79 to your computer and use it in GitHub Desktop.
Automate conversion of Drupal modules to Backdrop with bash utilities.
adbd68fd1cc1b47bc11567b0acc003a56153b2c2
#!/usr/bin/env bash
# Bash 4 required.
# brew install bash
USAGE=$(cat <<EOF_USAGE
USAGE: ${SCRIPT} <options>
The following options are supported:
--help : Displays this help message.
--branch [branch to deploy] : Expects something like 7.x-1.x.
--project [git project name] : The repository name on git.drupal.org.
example: dropback.sh --project remove_generator --branch 7.x-1.x
EOF_USAGE
)
NUMARGS="$#"
# Print usage message and exit
print_usage () {
echo "${USAGE}" >&2
}
assign_value () {
case ${1} in
--*)
print_usage
exit 4
;;
*)
echo ${1}
;;
esac
}
while [ "${1}" != "" ]; do
case ${1} in
--branch)
BRANCH=$(assign_value ${2})
shift 2
;;
--project)
PROJECT=$(assign_value ${2})
shift 2
;;
--help|--*)
print_usage
exit 0
;;
esac
done
if [ ${NUMARGS} -le 1 ]; then
print_usage
exit 2
fi
echo "Cloning from git.drupal.org..."
git clone --quiet --branch ${BRANCH} https://git.drupal.org/project/${PROJECT}.git
cd ${PROJECT}
echo "Checking out new branch 1.x-1.x..."
git checkout --quiet -b 1.x-1.x
if [ ! -f "LICENSE.txt" ]; then
echo "Adding the latest recommended LICENSE.txt file from Backdrop Ops..."
wget --quiet https://raw.githubusercontent.com/backdrop-ops/contrib/master/examples/LICENSE.txt
fi
echo "Converting module information core version..."
find ./ -name *.info -type f | xargs sed -i '' 's/core = 7.x/backdrop = 1.x/g'
echo "Removing references to files[] in module information..."
find ./ -name *.info -type f | xargs sed -i '' '/^files\[\]/d'
echo "Adding module type to module information..."
find ./ -name *.info -type f | xargs grep -L "type = module" | xargs sed -i '' '$a\
type = module
'
echo "Updating lowercase references from drupal to backdrop..."
find ./ -type f | xargs grep -rl "drupal" | xargs sed -i '' 's/drupal/backdrop/g'
echo "Updating ucfirst references from Drupal to Backdrop..."
find ./ -type f | xargs grep -rl "Drupal" | xargs sed -i '' 's/Drupal/Backdrop/g'
echo "Updating uppercase references from DRUPAL to BACKDROP..."
find ./ -type f | xargs grep -rl "DRUPAL" | xargs sed -i '' 's/DRUPAL/BACKDROP/g'
echo "Replacing variable_get()s..."
find ./ -type f | xargs grep -rl "variable_get(" | xargs sed -i '' "s/variable_get(/config_get('${PROJECT}.settings', /g"
echo "Replacing variable_set()s..."
find ./ -type f | xargs grep -rl "variable_set(" | xargs sed -i '' "s/variable_set(/config_set('${PROJECT}.settings', /g"
SYSTEM_FORMS=$(grep -r "system_settings_form(" * | wc -l | xargs)
echo "Found ${SYSTEM_FORMS} system forms..."
HUMAN_PROJECT=${PROJECT//_/ }
HOOK_CONFIG_INFO=$(cat <<EOF_USAGE
/**
* Implements hook_config_info();
*/
function ${PROJECT,,}_config_info() {
\$prefixes = array();
\$prefixes['${PROJECT,,}.settings'] = array(
'group' => t('Configuration'),
'label' => t('${HUMAN_PROJECT^} settings'),
);
return \$prefixes;
}
EOF_USAGE
)
if [[ ${SYSTEM_FORMS} -gt 0 ]]; then
echo "Adding hook_config_info() to *.module..."
# One revision of this looked for any .module file then filled the
# hook information using the filename, but it's difficult then to decide
# how to create settings files since the variable_set/get replacement
# considers the entire project, not by submodule.
find . -name ${PROJECT}.module -type f | xargs grep -L "_config_info()" | while read -r MODULE_FILENAME ; do
echo "${HOOK_CONFIG_INFO}" >> ${MODULE_FILENAME}
sed -i '' '$a\' ${MODULE_FILENAME}
if [[ ! -d config ]]; then
mkdir config
fi
# Generate stub default settings.
SETTINGS_FILE="config/${MODULE_FILENAME:2:(-7)}.settings"
if [[ ! -f ${SETTINGS_FILE} ]]; then
grep -rh "config_get('${PROJECT}" * | while read -r CONFIG_GET ; do
# Only add vars prefixed with the project name.
FOUND_VAR=$(echo "${CONFIG_GET}" | sed -n 's/.*config_get.*,[[:space:]+][[:punct:]]\([[:alnum:]_]*\)[[:punct:]],.*/\1/p')
if [[ ${FOUND_VAR} =~ ^${PROJECT} ]]; then
echo " \"${FOUND_VAR}\": \"\"," >> ${SETTINGS_FILE}
fi
done
# Sort the variables and remove duplicates.
sort ${SETTINGS_FILE} | uniq > ${SETTINGS_FILE}.json
rm ${SETTINGS_FILE}
# Remove last comma for valid json.
sed -i '' '$s/,$//' ${SETTINGS_FILE}.json
# Add starting json bracket.
sed -i '' '1s/^/{\
/' ${SETTINGS_FILE}.json
# Add closing json bracket.
echo "}" >> ${SETTINGS_FILE}.json
fi
done
fi
echo "Manual cleanup checklist:"
echo " - Remove default values from the third argument of config_get()."
echo " - Review default settings stub file."
echo " - Convert system_settings_form() invocations to use form submit hooks."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment