Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add script to replace keyword placeholders with user input #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"name": "vendor-name/example-package",
"name": ":vendor_slug/:package_slug",
"type": "package",
"description": "An example package for Roots Acorn.",
"description": ":package_description",
"license": "MIT",
"authors": [
{
"name": "Brandon Nifong",
"email": "[email protected]"
"name": ":author_name",
"email": ":author_email"
}
],
"autoload": {
"psr-4": {
"VendorName\\ExamplePackage\\": "src/"
":vendor_name\\:class_name\\": "src/"
}
},
"require": {
"php": "^8.0"
"php": "^8.1"
},
"extra": {
"acorn": {
"providers": [
"VendorName\\ExamplePackage\\Providers\\ExampleServiceProvider"
":vendor_name\\:class_name\\Providers\\:class_nameServiceProvider"
],
"aliases": {
"Example": "VendorName\\ExamplePackage\\Facades\\Example"
":class_name": ":vendor_name\\:class_name\\Facades\\:class_name"
}
}
}
Expand Down
158 changes: 158 additions & 0 deletions configure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#!/usr/bin/env php
<?php

function ask(string $question, string $default = ''): string
{
$answer = readline($question.($default ? " ({$default})" : null).': ');

if (! $answer) {
return $default;
}

return $answer;
}

function confirm(string $question, bool $default = false): bool
{
$answer = ask($question.' ('.($default ? 'Y/n' : 'y/N').')');

if (! $answer) {
return $default;
}

return strtolower($answer) === 'y';
}

function writeln(string $line): void
{
echo $line.PHP_EOL;
}

function run(string $command): string
{
return trim((string) shell_exec($command));
}

function str_after(string $subject, string $search): string
{
$pos = strrpos($subject, $search);

if ($pos === false) {
return $subject;
}

return substr($subject, $pos + strlen($search));
}

function slugify(string $subject): string
{
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-'));
}

function title_case(string $subject): string
{
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject)));
}

function replace_in_file(string $file, array $replacements): void
{
$contents = file_get_contents($file);

file_put_contents(
$file,
str_replace(
array_keys($replacements),
array_values($replacements),
$contents
)
);
}

function determineSeparator(string $path): string
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}

function replaceForWindows(): array
{
return preg_split('/\\r\\n|\\r|\\n/', run('dir /S /B * | findstr /v /i .git\ | findstr /v /i vendor | findstr /v /i '.basename(__FILE__).' | findstr /r /i /M /F:/ ":author :vendor :package VendorName vendor_name vendor_slug [email protected]"'));
}

function replaceForAllOtherOSes(): array
{
return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|vendor_name|vendor_slug|[email protected]" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__)));
}

$gitName = run('git config user.name');
$authorName = ask('Author name', $gitName);

$gitEmail = run('git config user.email');
$authorEmail = ask('Author email', $gitEmail);
$authorUsername = ask('Author username', '[email protected]');

$vendorName = ask('Vendor name', 'Roots');
$vendorUsername = ask('Vendor username', 'roots');
$vendorSlug = slugify($vendorUsername);

$vendorNamespace = str_replace(' ', '', str_replace('-', '', ucwords($vendorName)));
$vendorNamespace = ask('Vendor namespace', $vendorNamespace);

$currentDirectory = getcwd();
$folderName = basename($currentDirectory);

$packageName = ask('Package name', $folderName);
$packageSlug = slugify($packageName);

$className = title_case($packageName);
$className = ask('Class name', $className);
$variableName = lcfirst($className);
$description = ask('Package description', "This is my package {$packageSlug}");

writeln('------');
writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})");
writeln("Vendor : {$vendorName} ({$vendorSlug})");
writeln("Package : {$packageSlug} <{$description}>");
writeln("Namespace : {$vendorNamespace}\\{$className}");
writeln("Class name : {$className}");
writeln('------');

writeln('This script will replace the above values in all relevant files in the project directory.');

if (! confirm('Modify files?', true)) {
exit(1);
}

$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes());

foreach ($files as $file) {
replace_in_file($file, [
':author_name' => $authorName,
':author_username' => $authorUsername,
':author_email' => $authorEmail,
':vendor_name' => $vendorNamespace,
':vendor_slug' => $vendorSlug,
'VendorName' => $vendorNamespace,
':package_name' => $packageName,
':package_slug' => $packageSlug,
'Example' => $className,
'ExamplePackage' => $className,
'example' => $packageSlug,
':package_description' => $description,
':class_name' => $className
]);

match (true) {
str_contains($file, determineSeparator('src/Example.php')) => rename($file, determineSeparator('./src/'.$className.'.php')),
str_contains($file, determineSeparator('src/Providers/ExampleServiceProvider.php')) => rename($file, determineSeparator('./src/providers/'.$className.'ServiceProvider.php')),
str_contains($file, determineSeparator('src/Console/ExampleCommand.php')) => rename($file, determineSeparator('./src/console/'.$className.'.php')),
str_contains($file, determineSeparator('src/Facades/Example.php')) => rename($file, determineSeparator('./src/Facades/'.$className.'.php')),
str_contains($file, determineSeparator('resources/views/example.blade.php')) => rename($file, determineSeparator('./resources/views/'.$packageSlug.'.blade.php')),
default => [],
};
}

rename(determineSeparator('config/example.php'), determineSeparator('./config/'.$packageSlug.'.php'));

confirm('Execute `composer install`?') && run('composer install');

confirm('Let this script delete itself?', true) && unlink(__FILE__);