diff --git a/project_template/launch-drupal-cms.sh b/project_template/launch-drupal-cms.sh index 8e0cfe69be827911dbe8f9cf0de9af122cdb9cec..1c3d5f98959cd3fd0bccef8cbe48bffe6bb32a87 100755 --- a/project_template/launch-drupal-cms.sh +++ b/project_template/launch-drupal-cms.sh @@ -12,20 +12,46 @@ # Abort this entire script if any one command fails. set -e -if ! command -v ddev >/dev/null; then +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 -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) +# 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 + # 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. + # 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 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 + +CURRENT_DIR="$PWD" +# Change directory to the project root. +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." + exit 1 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" # Start your engines. ddev start # Install dependencies if not already done. 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 fec8b86ce36e47b591d7f32da400bfa217e37521..27db90184ed0234c8274c63f12bb6df6fa6e17de 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,16 +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] === "list") { +\$arg2 = \$argv[2] ?? ''; +if (\$argv[1] === "describe" && \$arg2 === "$project_name") { echo "Yes, there is a $project_name project"; -} -else { +} 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; @@ -47,7 +52,7 @@ 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]); } }