-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.php
245 lines (187 loc) · 6.54 KB
/
configure.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env php
<?php
/**
* Configure the PHP Package interactively.
*
* phpcs:disable
*/
if ( ! defined( 'STDIN' ) ) {
die( 'Not in CLI mode.' );
}
if ( 0 === strpos( strtoupper( PHP_OS ), 'WIN' ) ) {
die( 'Not supported in Windows. 🪟' );
}
function ask( string $question, string $default = '' ): string {
$answer = readline(
$question . ( $default ? " [{$default}]" : '' ) . ': '
);
return $answer ?: $default;
}
function confirm( string $question, bool $default = false ): bool {
$answer = readline(
"{$question} (yes/no) [" . ( $default ? 'yes' : 'no' ) . ']: '
);
if ( ! $answer ) {
return $default;
}
return in_array( strtolower( trim( $answer ) ), [ 'y', 'yes', 'true', '1' ], true );
}
function writeln( string $line ): void {
echo $line . PHP_EOL;
}
function run( string $command, string $dir = null ): string {
$command = $dir ? "cd {$dir} && {$command}" : $command;
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( (string) preg_replace( '/[^A-Za-z0-9-]+/', '-', $subject ), '-' ) );
}
function title_case( string $subject ): string {
return ensure_capitalp( str_replace( ' ', '_', ucwords( str_replace( [ '-', '_' ], ' ', $subject ) ) ) );
}
function ensure_capitalp( string $text ): string {
return str_replace( 'Wordpress', 'WordPress', $text );
}
/**
* @param string $file
* @param array<string, string> $replacements
*/
function replace_in_file( string $file, array $replacements ): void {
$contents = file_get_contents( $file );
if ( empty( $contents ) ) {
return;
}
file_put_contents(
$file,
str_replace(
array_keys( $replacements ),
array_values( $replacements ),
$contents,
)
);
}
function remove_readme_paragraphs( string $file ): void {
$contents = file_get_contents( $file );
if ( empty( $contents ) ) {
return;
}
file_put_contents(
$file,
trim( preg_replace( '/<!--delete-->.*<!--\/delete-->/s', '', $contents ) ?: $contents ),
);
}
function determine_separator( string $path ): string {
return str_replace( '/', DIRECTORY_SEPARATOR, $path );
}
/**
* @return array<int, string>
*/
function list_all_files_for_replacement(): array {
return explode( PHP_EOL, run( 'grep -R -l ./ --exclude LICENSE --exclude .phpunit.result.cache --exclude-dir node_modules --exclude configure.php --exclude composer.lock --exclude-dir .git --exclude-dir .github --exclude-dir vendor --exclude-dir bin --exclude-dir webpack --exclude-dir modules --exclude-dir .phpcs' ) );
}
/**
* @param string|array<int, string> $paths
*/
function delete_files( string|array $paths ): void {
if ( ! is_array( $paths ) ) {
$paths = [ $paths ];
}
foreach ( $paths as $path ) {
$path = determine_separator( $path );
if ( is_dir( $path ) ) {
run( "rm -rf {$path}" );
} elseif ( file_exists( $path ) ) {
unlink( $path );
}
}
}
$current_dir = getcwd();
echo "\nWelcome friend to alleyinteractive/create-php-package! 😀\nLet's setup your PHP package 🚀\n\n";
if ( ! $current_dir ) {
die( 'Could not determine current directory.' );
}
$folder_name = ensure_capitalp( basename( $current_dir ) );
$package_name = ask( 'Package name?', str_replace( '_', ' ', title_case( $folder_name ) ) );
$package_name_slug = slugify( $package_name );
$git_name = run( 'git config user.name' );
$author_name = ask( 'Author name?', $git_name );
$git_email = run( 'git config user.email' );
$author_email = ask( 'Author email?', $git_email );
$username_guess = explode( ':', run( 'git config remote.origin.url' ) )[1] ?? '';
$username_guess = dirname( $username_guess );
$username_guess = basename( $username_guess );
$author_username = ask( 'Author username?', $username_guess );
$vendor_name = ask( 'Vendor name (usually the Github Organization)?', $username_guess );
$vendor_slug = slugify( $vendor_name );
$is_wordpress_package = confirm( 'Is this a WordPress package?', false );
$namespace = ask(
'Package namespace?',
$is_wordpress_package ? 'Alley\\WP\\' . title_case( $package_name ) : 'Alley\\' . title_case( $package_name ),
);
$class_name = ask( 'Base class name for package?', title_case( $package_name ) );
$description = ask( 'Package description?', "This is my PHP package {$package_name}" );
writeln( '------' );
writeln( "Author : {$author_name} ({$author_email})" );
writeln( "Vendor : {$vendor_name} ({$vendor_slug})" );
writeln( "Package : {$package_name} <{$package_name_slug}>" );
writeln( "Description : {$description}" );
writeln( "Namespace : {$namespace}" );
writeln( "Main Class : {$class_name}" );
writeln( '------' );
writeln( 'This script will replace the above values in all relevant files in the project directory.' );
if ( ! confirm( 'Modify files?', true ) ) {
exit( 1 );
}
$search_and_replace = [
'author_name' => $author_name,
'author_username' => $author_username,
'[email protected]' => $author_email,
'A skeleton PHP package geared for WordPress Development' => $description,
// Extra slashes are here for composer.json.
'Alley\\\Create_PHP_Package\\\\' => str_replace( '\\', '\\\\', $namespace ) . '\\\\',
'Alley\Create_PHP_Package' => $namespace,
'Example_Package' => $class_name,
'package_name' => $package_name,
'create-php-package' => $package_name_slug,
'Create PHP Package' => $package_name,
'CREATE_PHP_PACKAGE' => strtoupper( str_replace( '-', '_', $package_name ) ),
'Skeleton' => $class_name,
'vendor_name' => $vendor_name,
'alleyinteractive' => $vendor_slug,
];
foreach ( list_all_files_for_replacement() as $path ) {
echo "Updating $path...\n";
replace_in_file( $path, $search_and_replace );
if ( str_contains( $path, determine_separator( 'src/class-example-package.php' ) ) ) {
rename( $path, determine_separator( './src/class-' . str_replace( '_', '-', strtolower( $class_name ) ) . '.php' ) );
}
if ( str_contains( $path, 'README.md' ) ) {
remove_readme_paragraphs( $path );
}
}
echo "Done!\n\n";
if ( confirm( 'Do you want to run `composer install`?', true ) ) {
if ( file_exists( __DIR__ . '/composer.lock' ) ) {
echo run( 'composer update' );
} else {
echo run( 'composer install' );
}
echo "\n\n";
}
if ( confirm( 'Let this script delete itself?', true ) ) {
delete_files(
[
'Makefile',
__FILE__,
]
);
}
echo "\n\nWe're done! 🎉\n\n";
die( 0 );