Merge tag 'v0.1.2' into debian/master
[drupal-init-tools.git] / libexec / create-profile.sh
index 96020b3..57e3731 100755 (executable)
@@ -20,7 +20,7 @@ set -e
 
 usage() {
   cat <<EOF
-usage: drin $(basename $0 .sh) [-h|--help] <title> <machine_name>
+usage: drin $(basename "$0" .sh) [-h|--help] <title> <machine_name>
 
 Create an installation profile from the installed project.
 
@@ -47,7 +47,7 @@ do
 done
 
 
-[ "x$1" = "x" -o  "x$2" = "x" ] && { usage 1>&2; exit 1; }
+{ [ "x$1" = "x" ] || [ "x$2" = "x" ]; } && { usage 1>&2; exit 1; }
 
 PROFILE_TITLE="$1"
 PROFILE_MACHINE_NAME="$2"
@@ -64,10 +64,17 @@ DRUSH="${PROJECT_ROOT}/vendor/bin/drush"
 [ -x "$DRUSH" ] || { echo "Aborting, '$DRUSH' not available." 1>&2; exit 1; }
 [ -x "$DRUPAL_CONSOLE" ] || { echo "Aborting, '$DRUPAL_CONSOLE' not available." 1>&2; exit 1; }
 
-[ -d "${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}" ] && { echo "Aborting, ${PROJECT_ROOT}/${PROFILE_MACHINE_NAME} already exists." 1>&2; exit 1; }
+PROFILE_DEST_DIR="${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}"
+
+[ -d "${PROFILE_DEST_DIR}" ] && { echo "Aborting, '${PROFILE_DEST_DIR}' already exists." 1>&2; exit 1; }
 
 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:
@@ -76,41 +83,73 @@ pushd "$WEB_ROOT"
 #
 # or
 #
-#  $DRUPAL_CONSOLE yaml:get:value "$PWD/$PROFILE_MACHINE_NAME/config/install/core.extension.yml" dependencies
+#  $DRUPAL_CONSOLE yaml:get:value "${PROFILE_DEST_DIR}/config/install/core.extension.yml" dependencies
 #
 # However getting them before exporting the configuration and generating the
 # profile is cleaner.
 #
-ENABLED_MODULES="$($DRUSH pm-list --type=module --status=enabled --pipe | tr '\n' ',')"
-ENABLED_THEMES="$($DRUSH pm-list --type=theme --status=enabled --pipe | tr '\n' ',')"
+ENABLED_MODULES="$($DRUSH pm-list --type=module --field=name --status=enabled --pipe | tr '\n' ',' | sed 's/,$//')"
+ENABLED_THEMES="$($DRUSH pm-list --type=theme --field=name --status=enabled --pipe | tr '\n' ',' | sed 's/,$//')"
 
-rm -rf "${WEB_ROOT}/profiles/$PROFILE_MACHINE_NAME"
 $DRUPAL_CONSOLE generate:profile \
   --profile="$PROFILE_TITLE" \
   --machine-name="$PROFILE_MACHINE_NAME" \
   --description="Drupal installation profile for $PROFILE_TITLE" \
-  --dependencies=$ENABLED_MODULES \
-  --themes=$ENABLED_THEMES \
+  --dependencies="$ENABLED_MODULES" \
+  --themes="$ENABLED_THEMES" \
+  --profile-path="$PROJECT_ROOT" \
   --no-interaction
-cp -a "${WEB_ROOT}/profiles/${PROFILE_MACHINE_NAME}" "$PROJECT_ROOT"
+
+# 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 \
+        -e "s/^function ${CURRENT_PROFILE}_/function ${PROFILE_MACHINE_NAME}_/g" \
+        -e "s/\$form\['#submit'\]\[\] = '${CURRENT_PROFILE}_/\$form\['#submit'\]\[\] = '${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="${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}/config/install" --remove-uuid --remove-config-hash
-rm "${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}/config/install/core.extension.yml"
+$DRUPAL_CONSOLE config:export --directory="${PROFILE_DEST_DIR}/config/install" --remove-uuid --remove-config-hash
+rm "${PROFILE_DEST_DIR}/config/install/core.extension.yml"
 
-# The reference to the core version could be removed, but this is not strictly necessary
-#find "${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}/config/install" -type f -exec sed -i -e '/^_core: {  }/d' {} \;
+# The reference to the core version could be removed, even though it is not strictly necessary.
+find "${PROFILE_DEST_DIR}/config/install" -type f -exec sed -i -e '/^_core: {  }/d' {} \;
 
 # Since the profile generated by `$DRUPAL_CONSOLE generate:profile` calls in
 # the standard profile, some duplicated config files could be removed in the
 # new profile, but that's not strictly necessary either.
-#fdupes -f -1 "${WEB_ROOT}/core/profiles/standard/config/install/" "${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}/config/install/" | xargs rm
+#fdupes -f -1 "${WEB_ROOT}/core/profiles/standard/config/install/" "${PROFILE_DEST_DIR}/config/install/" | xargs rm
 
 # Export the default content if the default_content module is there
-if echo $ENABLED_MODULES | grep -q default_content;
+if $DRUSH pm-list --type=module --field=name --status=enabled --pipe | grep -q "^default_content$";
+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
-  $DRUSH default-content-export-references --folder="${PROJECT_ROOT}/${PROFILE_MACHINE_NAME}/content" node
+  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