create-profile.sh: copy more stuff from the current profile to the new one
authorAntonio Ospite <ao2@ao2.it>
Mon, 12 Mar 2018 09:59:09 +0000 (10:59 +0100)
committerAntonio Ospite <ao2@ao2.it>
Fri, 16 Mar 2018 12:30:04 +0000 (13:30 +0100)
The "drupal generate:profile" command only provides a stub
implementation for the .install and .profile files which are only fine
when writing a new installation profile from scratch.

When creating a profile form an installed one, it is appropriate to
preserve as much as possible of the original profile, so copy code and
data from the currently installed profile to the newly created one,
adjusting them to the new profile name.

libexec/create-profile.sh

index 84ab022..62a5b01 100755 (executable)
@@ -70,6 +70,11 @@ PROFILE_DEST_DIR="${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}"
 
 pushd "$WEB_ROOT"
 
+CURRENT_PROFILE="$($DRUSH php-eval "echo drupal_get_profile();")"
+CURRENT_PROFILE_PATH="$($DRUSH php-eval "echo drupal_get_path('profile', '${CURRENT_PROFILE}');")"
+
+[ -d "$CURRENT_PROFILE_PATH" ] || { echo "Aborting, the current profile path is not valid." 1>&2; exit 1; }
+
 # The list of modules and themes could also be obtained by exporting the
 # configuration first and then looking at: config/install/core.extension.yml
 # like this:
@@ -95,6 +100,28 @@ $DRUPAL_CONSOLE generate:profile \
   --profile-path="$PROJECT_ROOT" \
   --no-interaction
 
+# Copy private code and data from the currently installed profile
+pushd "$CURRENT_PROFILE_PATH"
+
+find . -maxdepth 1 -type d ! -name "tests" -printf "%P\n" |
+  while read -r dir;
+  do
+    [ -d "$dir" ] && cp -r "$dir" "${PROFILE_DEST_DIR}/"
+  done
+
+find . -maxdepth 1 -type f ! -name "*.info.yml" -printf "%P\n" |
+  while read -r original_file;
+  do
+    DESTINATION_FILE="${PROFILE_DEST_DIR}/${original_file/#${CURRENT_PROFILE}/${PROFILE_MACHINE_NAME}}"
+    cp "${original_file}" "$DESTINATION_FILE"
+    if file "$original_file" | grep -q PHP;
+    then
+      sed -i "s/^function ${CURRENT_PROFILE}_/function ${PROFILE_MACHINE_NAME}_/g" "$DESTINATION_FILE"
+    fi
+  done
+
+popd
+
 # Basically do what's suggested in the "Configuration" section here:
 # https://www.drupal.org/docs/8/creating-distributions/how-to-write-a-drupal-8-installation-profile
 $DRUPAL_CONSOLE config:export --directory="${PROFILE_DEST_DIR}/config/install" --remove-uuid --remove-config-hash
@@ -114,4 +141,12 @@ then
   $DRUSH default-content-export-references --folder="${PROFILE_DEST_DIR}/content" node
 fi
 
+# The code copied over from the current profile may refer to the old name.
+# Warn the user about that.
+if [ "$PROFILE_MACHINE_NAME" != "$CURRENT_PROFILE" ];
+then
+  echo "The newly created profile has a name different from the current profile."
+  echo "Check that the code of the created profile refers to the new profile name."
+fi
+
 popd