Skip to content

Commit

Permalink
[#20] Rename project config in post-create-project script
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuapease committed May 28, 2024
1 parent 7ba2dbe commit ab46c36
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ public static function error(string $message): int|false
return fwrite(STDERR, self::ANSI_RED . $message . self::ANSI_CLOSE . PHP_EOL);
}

public static function replaceText(string $subject, string $pattern, string $replacement): string
public static function replaceFileText(string $filePath, string $pattern, string $replacement): void
{
return preg_replace($pattern, $replacement, $subject);
$fileContent = file_get_contents($filePath);
$fileContent = preg_replace($pattern, $replacement, $fileContent);
file_put_contents($filePath, $fileContent);
}

public static function kebabCase(string $string)
Expand Down
59 changes: 59 additions & 0 deletions composer-scripts/post-create-project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use craft\helpers\Console;

require_once 'ScriptHelpers.php';
require_once 'vendor/autoload.php';

$cwd = getcwd();

/**
* Prompt the user for input
*/
$projectName = Console::prompt('What is the name of your project (Example: My Client Name)? ', [
'required' => true,
]);

Console::output("Great! We'll use the name: $projectName");

$suggestedProjectSlug = ScriptHelpers::kebabCase($projectName);

$projectSlugPrompt = Console::prompt("Customize the project slug? This controls the DDEV URL, etc.", [
'default' => $suggestedProjectSlug,
]);

$projectSlug = !empty(trim($projectSlugPrompt)) ? ScriptHelpers::kebabCase($projectSlugPrompt) : $suggestedProjectSlug;

ScriptHelpers::success("Great! We'll use $projectSlug");

/**
* Update DDEV config
*/

ScriptHelpers::replaceFileText(
filePath: "$cwd/.ddev/config.yaml",
pattern: "/name:\s+craft-starter/",
replacement: "name: $projectSlug",
);

/**
* Update project config
*/

ScriptHelpers::replaceFileText(
filePath: "$cwd/config/project/project.yaml",
pattern: "/Viget Craft Starter/",
replacement: "$projectName",
);

ScriptHelpers::replaceFileText(
filePath: "$cwd/config/project/siteGroups/805d8826-faed-4186-9b88-f509eb9b07e6.yaml",
pattern: "/Viget Craft Starter/",
replacement: "$projectName",
);

ScriptHelpers::replaceFileText(
filePath: "$cwd/config/project/sites/default--35b563a0-4662-40b9-b885-a8450a2868d9.yaml",
pattern: "/Viget Craft Starter/",
replacement: "$projectName",
);
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"scripts": {
"post-create-project-cmd": [
"@php -r \"file_exists('.env') || copy('.env.example.dev', '.env');\"",
"@php install-scripts/rename-ddev.php",
"@php composer-scripts/post-create-project.php",
"echo 'Cleaning composer.json'",
"@composer config --unset scripts.post-create-project-cmd",
"@composer config --unset name",
Expand Down
23 changes: 0 additions & 23 deletions install-scripts/rename-ddev.php

This file was deleted.

0 comments on commit ab46c36

Please sign in to comment.