Skip to content

Commit 67548a1

Browse files
authored
Merge pull request #50 from spatie/bash-to-php
Bash to php
2 parents 2f2374a + b70db8a commit 67548a1

File tree

5 files changed

+142
-144
lines changed

5 files changed

+142
-144
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
This package can be used as to scaffold a framework agnostic package. Follow these steps to get started:
1010

1111
1. Press the "Use template" button at the top of this repo to create a new repo with the contents of this skeleton
12-
2. Run "./configure.sh" to run a script that will replace all placeholders throughout all the files
12+
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files
1313
3. Have fun creating your package.
1414
4. If you need help creating a package, consider picking up our <a href="https://laravelpackage.training">Laravel Package Training</a> video course.
1515
---

configure-skeleton.sh

Lines changed: 0 additions & 137 deletions
This file was deleted.

configure.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
function ask(string $question, string $default = ''): string {
5+
$answer = readline($question . ($default ? " ({$default})" : null) . ': ');
6+
7+
if (! $answer) {
8+
return $default;
9+
}
10+
11+
return $answer;
12+
}
13+
14+
function confirm(string $question, bool $default = false): bool {
15+
$answer = ask($question . ' (' . ($default ? 'Y/n' : 'y/N') . ')');
16+
17+
if (! $answer) {
18+
return $default;
19+
}
20+
21+
return strtolower($answer) === 'y';
22+
}
23+
24+
function writeln(string $line): void {
25+
echo $line . PHP_EOL;
26+
}
27+
28+
function run(string $command): string {
29+
return trim(shell_exec($command));
30+
}
31+
32+
function str_after(string $subject, string $search): string {
33+
$pos = strrpos($subject, $search);
34+
35+
if ($pos === false) {
36+
return $subject;
37+
}
38+
39+
return substr($subject, $pos + strlen($search));
40+
}
41+
42+
function slugify(string $subject): string {
43+
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $subject), '-'));
44+
}
45+
46+
function title_case(string $subject): string {
47+
return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $subject)));
48+
}
49+
50+
function replace_in_file(string $file, array $replacements): void {
51+
$contents = file_get_contents($file);
52+
53+
file_put_contents(
54+
$file,
55+
str_replace(
56+
array_keys($replacements),
57+
array_values($replacements),
58+
$contents
59+
)
60+
);
61+
}
62+
63+
$gitName = run('git config user.name');
64+
$authorName = ask('Author name', $gitName);
65+
66+
$gitEmail = run('git config user.email');
67+
$authorEmail = ask('Author email', $gitEmail);
68+
69+
$usernameGuess = explode(':', run('git config remote.origin.url'))[1];
70+
$usernameGuess = dirname($usernameGuess);
71+
$usernameGuess = basename($usernameGuess);
72+
$authorUsername = ask('Author username', $usernameGuess);
73+
74+
$vendorName = ask('Vendor name', $authorUsername);
75+
$vendorSlug = slugify($vendorName);
76+
$vendorNamespace = ucwords($vendorName);
77+
$vendorNamespace = ask('Vendor namespace', $vendorNamespace);
78+
79+
$currentDirectory = getcwd();
80+
$folderName = basename($currentDirectory);
81+
82+
$packageName = ask('Package name', $folderName);
83+
$packageSlug = slugify($packageName);
84+
85+
$className = title_case($packageName);
86+
$className = ask('Class name', $className);
87+
$description = ask('Package description', "This is my package {$packageSlug}");
88+
89+
writeln('------');
90+
writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})");
91+
writeln("Vendor : {$vendorName} ({$vendorSlug})");
92+
writeln("Package : {$packageSlug} <{$description}>");
93+
writeln("Namespace : {$vendorNamespace}\\{$className}");
94+
writeln("Class name : {$className}");
95+
writeln('------');
96+
97+
writeln('This script will replace the above values in all relevant files in the project directory.');
98+
99+
if (! confirm('Modify files?', true)) {
100+
exit(1);
101+
}
102+
103+
$files = explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|vendor_name|vendor_slug|[email protected]" --exclude-dir=vendor ./* ./.github/* | grep -v ' . basename(__FILE__)));
104+
105+
foreach ($files as $file) {
106+
replace_in_file($file, [
107+
':author_name' => $authorName,
108+
':author_username' => $authorUsername,
109+
'[email protected]' => $authorEmail,
110+
':vendor_name' => $vendorName,
111+
':vendor_slug' => $vendorSlug,
112+
'VendorName' => $vendorNamespace,
113+
':package_name' => $packageName,
114+
':package_slug' => $packageSlug,
115+
'Skeleton' => $className,
116+
':package_description' => $description,
117+
]);
118+
119+
match (true) {
120+
str_contains($file, 'src/Skeleton.php') => rename($file, './src/' . $className . '.php'),
121+
str_contains($file, 'src/SkeletonServiceProvider.php') => rename($file, './src/' . $className . 'ServiceProvider.php'),
122+
str_contains($file, 'src/SkeletonFacade.php') => rename($file, './src/' . $className . 'Facade.php'),
123+
str_contains($file, 'src/Commands/SkeletonCommand.php') => rename($file, './src/Commands/' . $className . 'Command.php'),
124+
default => [],
125+
};
126+
}
127+
128+
confirm('Execute `composer install` and run tests?') && run('composer install && composer test');
129+
130+
confirm('Let this script delete itself?', true) && unlink(__FILE__);

undo-configure.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
function run(string $command): string {
5+
return trim(shell_exec($command));
6+
}
7+
8+
run('git reset head --hard');
9+
run('git clean -f -d');
10+
run('rm -fr vendor');
11+
run('rm composer.lock');

undo_configure.sh

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)