From 9ac8e7e78306c0c547e18982723b840d8d75c18c Mon Sep 17 00:00:00 2001
From: Stanislav Zhuk <stasadev@gmail.com>
Date: Thu, 16 Jan 2025 16:24:09 +0200
Subject: [PATCH 1/7] Issue #3500131: Fix launch script counter

---
 project_template/launch-drupal-cms.sh | 30 +++++++++++++++++++--------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/project_template/launch-drupal-cms.sh b/project_template/launch-drupal-cms.sh
index 8e0cfe69b..0c0dfc6c3 100755
--- a/project_template/launch-drupal-cms.sh
+++ b/project_template/launch-drupal-cms.sh
@@ -12,20 +12,32 @@
 # Abort this entire script if any one command fails.
 set -e
 
-if ! command -v ddev >/dev/null; then
+if ! command -v ddev >/dev/null 2>&1; then
   echo "DDEV needs to be installed. Visit https://ddev.com/get-started for instructions."
   exit 1
 fi
 
-NAME=$(basename $PWD)
-# If there are any other DDEV projects in this system with this name, add a numeric suffix.
-declare -i n=$(ddev list | grep --count "$NAME")
-if [ $n > 0 ]; then
-  NAME=$NAME-$(expr $n + 1)
+# If there is no existing project yet.
+if ! ddev describe >/dev/null 2>&1; then
+  # If there is any other DDEV project in this system with this name, add a numeric suffix.
+  NAME="$(basename $PWD)"
+  SUFFIX=1
+  # Loop to increment the suffix until the name is unique.
+  while ddev describe "${NAME}" >/dev/null 2>&1; do
+    NAME="$(basename $PWD)-${SUFFIX}"
+    SUFFIX=$((SUFFIX + 1))
+  done
+  # Configure a new project.
+  ddev config --project-type=drupal11 --docroot=web --php-version=8.3 --ddev-version-constraint=">=1.24.0" --project-name="$NAME"
 fi
-
-# Configure DDEV if not already done.
-test -d .ddev || ddev config --project-type=drupal11 --docroot=web --php-version=8.3 --ddev-version-constraint=">=1.24.0" --project-name="$NAME"
+PROJECT_ROOT="$(ddev describe -j | docker run -i --rm ddev/ddev-utilities jq -r .raw.approot)"
+CURRENT_DIRECTORY="$(pwd)"
+cd "${PROJECT_ROOT}"
+if [ "$(pwd)" != "${CURRENT_DIRECTORY}" ]; then
+  echo "You're trying to set up a Drupal CMS inside an existing project in $(pwd), refusing to do it."
+  exit 1
+fi
+echo "Running script in $(pwd)"
 # Start your engines.
 ddev start
 # Install dependencies if not already done.
-- 
GitLab


From ff061c359682dc0a6aa711b51f7e60e5d2f4eb4d Mon Sep 17 00:00:00 2001
From: Stanislav Zhuk <stasadev@gmail.com>
Date: Thu, 16 Jan 2025 16:51:24 +0200
Subject: [PATCH 2/7] Run jq from docker only if docker command is available

---
 project_template/launch-drupal-cms.sh | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/project_template/launch-drupal-cms.sh b/project_template/launch-drupal-cms.sh
index 0c0dfc6c3..75915fa44 100755
--- a/project_template/launch-drupal-cms.sh
+++ b/project_template/launch-drupal-cms.sh
@@ -30,7 +30,14 @@ if ! ddev describe >/dev/null 2>&1; then
   # Configure a new project.
   ddev config --project-type=drupal11 --docroot=web --php-version=8.3 --ddev-version-constraint=">=1.24.0" --project-name="$NAME"
 fi
-PROJECT_ROOT="$(ddev describe -j | docker run -i --rm ddev/ddev-utilities jq -r .raw.approot)"
+# Get the existing project location.
+if command -v jq >/dev/null 2>&1; then
+  PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | jq -r .raw.approot)"
+elif command -v docker >/dev/null 2>&1; then
+  PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | docker run -i --rm ddev/ddev-utilities jq -r .raw.approot)"
+else
+  PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | grep '"approot"' | sed 's/.*"approot":"\([^"]*\)".*/\1/')"
+fi
 CURRENT_DIRECTORY="$(pwd)"
 cd "${PROJECT_ROOT}"
 if [ "$(pwd)" != "${CURRENT_DIRECTORY}" ]; then
@@ -38,6 +45,7 @@ if [ "$(pwd)" != "${CURRENT_DIRECTORY}" ]; then
   exit 1
 fi
 echo "Running script in $(pwd)"
+
 # Start your engines.
 ddev start
 # Install dependencies if not already done.
-- 
GitLab


From a0b0696ae267c7922ebfb37ba5ec62a73a8f8222 Mon Sep 17 00:00:00 2001
From: Stanislav Zhuk <stasadev@gmail.com>
Date: Thu, 16 Jan 2025 17:54:28 +0200
Subject: [PATCH 3/7] Fixing tests

---
 project_template/launch-drupal-cms.sh          |  3 ++-
 .../tests/src/Unit/LaunchScriptTest.php        | 18 +++++++++++++++---
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/project_template/launch-drupal-cms.sh b/project_template/launch-drupal-cms.sh
index 75915fa44..cd66c0faf 100755
--- a/project_template/launch-drupal-cms.sh
+++ b/project_template/launch-drupal-cms.sh
@@ -30,6 +30,7 @@ if ! ddev describe >/dev/null 2>&1; then
   # Configure a new project.
   ddev config --project-type=drupal11 --docroot=web --php-version=8.3 --ddev-version-constraint=">=1.24.0" --project-name="$NAME"
 fi
+
 # Get the existing project location.
 if command -v jq >/dev/null 2>&1; then
   PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | jq -r .raw.approot)"
@@ -38,13 +39,13 @@ elif command -v docker >/dev/null 2>&1; then
 else
   PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | grep '"approot"' | sed 's/.*"approot":"\([^"]*\)".*/\1/')"
 fi
+
 CURRENT_DIRECTORY="$(pwd)"
 cd "${PROJECT_ROOT}"
 if [ "$(pwd)" != "${CURRENT_DIRECTORY}" ]; then
   echo "You're trying to set up a Drupal CMS inside an existing project in $(pwd), refusing to do it."
   exit 1
 fi
-echo "Running script in $(pwd)"
 
 # Start your engines.
 ddev start
diff --git a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
index fec8b86ce..97c10d3a3 100644
--- a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
+++ b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
@@ -25,8 +25,19 @@ final class LaunchScriptTest extends UnitTestCase {
     $php_code = <<<END
 #!/usr/bin/env $php
 <?php
-if (\$argv[1] === "list") {
-  echo "Yes, there is a $project_name project";
+if (\$argv[1] === "describe") {
+  // Check if argv[2] is project_name, '-j'
+  \$arg2 = \$argv[2] ?? '';
+  if (\$arg2 === '-j') {
+    // Simulate JSON output for ddev describe -j
+    echo '{"raw":{"approot":"$working_dir"}}';
+  } elseif (\$arg2 === "$project_name") {
+    // Simulate ddev describe without arguments (normal describe behavior)
+    echo "Yes, there is a $project_name project";
+  } else {
+    // Simulate no project found
+    exit(1);
+  }
 }
 else {
   echo implode(" ", array_slice(\$argv, 1)) . "\n";
@@ -46,8 +57,9 @@ END;
 
     $this->assertSame(0, $launcher->getExitCode(), $launcher->getErrorOutput());
     $output = explode("\n", $launcher->getOutput());
+
     $this->assertStringStartsWith('config ', $output[0]);
-    $this->assertStringContainsString("--project-name=$project_name-2", $output[0]);
+    $this->assertStringContainsString("--project-name=$project_name-1", $output[0]);
   }
 
 }
-- 
GitLab


From 82ef7f679c612c5957af569ad7cfae7e6afdfaa1 Mon Sep 17 00:00:00 2001
From: Stanislav Zhuk <stasadev@gmail.com>
Date: Thu, 16 Jan 2025 18:01:52 +0200
Subject: [PATCH 4/7] Remove empty line

---
 .../drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php   | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
index 97c10d3a3..fa0ae84e1 100644
--- a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
+++ b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
@@ -26,7 +26,7 @@ final class LaunchScriptTest extends UnitTestCase {
 #!/usr/bin/env $php
 <?php
 if (\$argv[1] === "describe") {
-  // Check if argv[2] is project_name, '-j'
+  // Check if argv[2] is project_name, '-j', or else
   \$arg2 = \$argv[2] ?? '';
   if (\$arg2 === '-j') {
     // Simulate JSON output for ddev describe -j
@@ -57,7 +57,6 @@ END;
 
     $this->assertSame(0, $launcher->getExitCode(), $launcher->getErrorOutput());
     $output = explode("\n", $launcher->getOutput());
-
     $this->assertStringStartsWith('config ', $output[0]);
     $this->assertStringContainsString("--project-name=$project_name-1", $output[0]);
   }
-- 
GitLab


From 2028186ca82dedc954916f933928252fe16d15d9 Mon Sep 17 00:00:00 2001
From: Stanislav Zhuk <stasadev@gmail.com>
Date: Thu, 16 Jan 2025 18:31:26 +0200
Subject: [PATCH 5/7] Make comments clearer

---
 .../tests/src/Unit/LaunchScriptTest.php                     | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
index fa0ae84e1..089d03afc 100644
--- a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
+++ b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
@@ -29,13 +29,13 @@ if (\$argv[1] === "describe") {
   // Check if argv[2] is project_name, '-j', or else
   \$arg2 = \$argv[2] ?? '';
   if (\$arg2 === '-j') {
-    // Simulate JSON output for ddev describe -j
+    // Simulate JSON output for 'ddev describe -j'
     echo '{"raw":{"approot":"$working_dir"}}';
   } elseif (\$arg2 === "$project_name") {
-    // Simulate ddev describe without arguments (normal describe behavior)
+    // Simulate 'ddev describe project-name'
     echo "Yes, there is a $project_name project";
   } else {
-    // Simulate no project found
+    // Simulate 'ddev describe' for no project found
     exit(1);
   }
 }
-- 
GitLab


From 6e9eaa87128c7e8d2dd1ad369cf03c5216093293 Mon Sep 17 00:00:00 2001
From: Stanislav Zhuk <stasadev@gmail.com>
Date: Sat, 18 Jan 2025 10:58:59 +0200
Subject: [PATCH 6/7] Handle underscores and spaces

---
 project_template/launch-drupal-cms.sh         | 45 ++++++++++---------
 .../tests/src/Unit/LaunchScriptTest.php       | 26 +++++------
 2 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/project_template/launch-drupal-cms.sh b/project_template/launch-drupal-cms.sh
index cd66c0faf..18f87379f 100755
--- a/project_template/launch-drupal-cms.sh
+++ b/project_template/launch-drupal-cms.sh
@@ -12,38 +12,43 @@
 # Abort this entire script if any one command fails.
 set -e
 
+DDEV_VERSION_CONSTRAINT=">=1.24.0"
+
+# Check if DDEV is installed.
 if ! command -v ddev >/dev/null 2>&1; then
   echo "DDEV needs to be installed. Visit https://ddev.com/get-started for instructions."
   exit 1
 fi
 
+# Check if DDEV meets the required version constraint.
+if ! ddev debug match-constraint 2>&1 | grep -q match-constraint || ! ddev debug match-constraint "$DDEV_VERSION_CONSTRAINT" >/dev/null 2>&1; then
+  echo "Your DDEV version doesn't meet the constraint '$DDEV_VERSION_CONSTRAINT'. Please update to a DDEV version that meets this constraint."
+  exit 1
+fi
+
 # If there is no existing project yet.
 if ! ddev describe >/dev/null 2>&1; then
-  # If there is any other DDEV project in this system with this name, add a numeric suffix.
-  NAME="$(basename $PWD)"
-  SUFFIX=1
+  # Replace underscores and spaces with hyphens.
+  ORIGINAL_NAME="$(basename "$PWD" | sed 's/[_ ]/-/g')"
+  NAME=$ORIGINAL_NAME
   # Loop to increment the suffix until the name is unique.
-  while ddev describe "${NAME}" >/dev/null 2>&1; do
-    NAME="$(basename $PWD)-${SUFFIX}"
-    SUFFIX=$((SUFFIX + 1))
+  # Avoid using "while" to prevent an infinite loop.
+  for SUFFIX in $(seq 1 1000); do
+    if ! ddev describe "$NAME" >/dev/null 2>&1; then
+      break
+    fi
+    NAME="$ORIGINAL_NAME-$SUFFIX"
   done
-  # Configure a new project.
-  ddev config --project-type=drupal11 --docroot=web --php-version=8.3 --ddev-version-constraint=">=1.24.0" --project-name="$NAME"
+  # Configure the new DDEV project.
+  ddev config --project-type=drupal11 --docroot=web --php-version=8.3 --ddev-version-constraint="$DDEV_VERSION_CONSTRAINT" --project-name="$NAME"
 fi
 
-# Get the existing project location.
-if command -v jq >/dev/null 2>&1; then
-  PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | jq -r .raw.approot)"
-elif command -v docker >/dev/null 2>&1; then
-  PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | docker run -i --rm ddev/ddev-utilities jq -r .raw.approot)"
-else
-  PROJECT_ROOT="$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev describe -j | grep '"approot"' | sed 's/.*"approot":"\([^"]*\)".*/\1/')"
-fi
+CURRENT_DIR="$PWD"
+# Change directory to the project root.
+cd "$(ddev debug cd "" --get-approot)"
 
-CURRENT_DIRECTORY="$(pwd)"
-cd "${PROJECT_ROOT}"
-if [ "$(pwd)" != "${CURRENT_DIRECTORY}" ]; then
-  echo "You're trying to set up a Drupal CMS inside an existing project in $(pwd), refusing to do it."
+if [ "$PWD" != "$CURRENT_DIR" ]; then
+  echo "You're trying to set up a Drupal CMS inside an existing project in $PWD, refusing to do it."
   exit 1
 fi
 
diff --git a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
index 089d03afc..27db90184 100644
--- a/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
+++ b/project_template/web/profiles/drupal_cms_installer/tests/src/Unit/LaunchScriptTest.php
@@ -19,27 +19,21 @@ final class LaunchScriptTest extends UnitTestCase {
     }
 
     $working_dir = dirname(__FILE__, 7);
-    $project_name = basename($working_dir);
+    $project_name = str_replace(['_', ' '], '-', basename($working_dir));
 
     $php = PHP_BINARY;
     $php_code = <<<END
 #!/usr/bin/env $php
 <?php
-if (\$argv[1] === "describe") {
-  // Check if argv[2] is project_name, '-j', or else
-  \$arg2 = \$argv[2] ?? '';
-  if (\$arg2 === '-j') {
-    // Simulate JSON output for 'ddev describe -j'
-    echo '{"raw":{"approot":"$working_dir"}}';
-  } elseif (\$arg2 === "$project_name") {
-    // Simulate 'ddev describe project-name'
-    echo "Yes, there is a $project_name project";
-  } else {
-    // Simulate 'ddev describe' for no project found
-    exit(1);
-  }
-}
-else {
+\$arg2 = \$argv[2] ?? '';
+if (\$argv[1] === "describe" && \$arg2 === "$project_name") {
+  echo "Yes, there is a $project_name project";
+} elseif (\$argv[1] === "describe") {
+  // Simulate 'ddev describe' for no project found
+  exit(1);
+} elseif (\$argv[1] === "debug" && \$arg2 === "cd") {
+  echo "$working_dir";
+} else {
   echo implode(" ", array_slice(\$argv, 1)) . "\n";
 }
 END;
-- 
GitLab


From 1dc9cac9f919b2d37b764c60756c74f52ad8ad5e Mon Sep 17 00:00:00 2001
From: Stanislav Zhuk <stasadev@gmail.com>
Date: Sun, 19 Jan 2025 00:12:02 +0200
Subject: [PATCH 7/7] Disable env that can break `ddev debug cd`

---
 project_template/launch-drupal-cms.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/project_template/launch-drupal-cms.sh b/project_template/launch-drupal-cms.sh
index 18f87379f..1c3d5f989 100755
--- a/project_template/launch-drupal-cms.sh
+++ b/project_template/launch-drupal-cms.sh
@@ -45,7 +45,7 @@ fi
 
 CURRENT_DIR="$PWD"
 # Change directory to the project root.
-cd "$(ddev debug cd "" --get-approot)"
+cd "$(DDEV_VERBOSE=false DDEV_DEBUG=false ddev debug cd "" --get-approot)"
 
 if [ "$PWD" != "$CURRENT_DIR" ]; then
   echo "You're trying to set up a Drupal CMS inside an existing project in $PWD, refusing to do it."
-- 
GitLab