-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision.php
128 lines (102 loc) · 3.53 KB
/
provision.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
<?php
/**
* Args may be passed when executing `wp eval-file`. E.g. `wp eval-file provision.php --skip-wordpress dry-run=1`.
*
* @link https://developer.wordpress.org/cli/commands/eval-file/
*/
if ( ! empty( $args ) ) {
$_args = [];
foreach ( $args as $k => $arg ) {
$pieces = explode( '=', $arg );
$_args[ $pieces[0] ] = $pieces[1];
}
$args = $_args;
}
define( 'NEW_FILENAME_PREFIX', $args['new-filename-prefix'] ?? '' );
define( 'NEW_FUNCTION_PREFIX', $args['new-function-prefix'] ?? '' );
define( 'NEW_CLASS_PREFIX', $args['new-class-prefix'] ?? '' );
define( 'NEW_NAMESPACE_PREFIX', $args['new-namespace-prefix'] ?? '' );
define( 'OLD_FILENAME_PREFIX', $args['old-filename-prefix'] ?? 'my-plugin' );
define( 'OLD_FUNCTION_PREFIX', $args['old-function-prefix'] ?? 'my_plugin' );
define( 'OLD_CLASS_PREFIX', $args['old-class-prefix'] ?? 'My_Plugin' );
define( 'OLD_NAMESPACE_PREFIX', $args['old-namespace-prefix'] ?? 'MyPlugin' );
$exclude = [ '.git', 'node_modules', 'vendor' ];
$directory = new RecursiveDirectoryIterator( __DIR__, RecursiveDirectoryIterator::SKIP_DOTS );
$iterator = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
$directory,
/**
* @param SplFileInfo $file
* @param mixed $key
* @param RecursiveCallbackFilterIterator $iterator
*
* @return bool True if you need to recurse or if the item is acceptable.
*/
function ( $file, $key, $iterator ) use ( $exclude ) {
// If is a directory and it's not supposed to be excluded we return true.
if ( ! $file->isFile() && ! in_array( $file->getFilename(), $exclude ) ) {
return true;
}
// If filename equals to index.php or the file is this current file we return false.
if ( $file->getFilename() == 'index.php'
|| $file->getFilename() == basename( __FILE__ ) ) {
return false;
}
// Finally, now returns true only if it's a file.
return $file->isFile();
} )
);
$replace_file_content = function () use ( $args, $iterator ) {
$files = [];
/**
* @var SplFileInfo $info
*/
foreach ( $iterator as $info ) {
$ext = $info->getExtension();
if ( $ext != 'php'
&& $ext != 'js'
&& $ext != 'json' ) {
continue;
}
$files[] = $info->getPathname();
}
foreach ( $files as $file ) {
if ( NEW_FILENAME_PREFIX ) {
file_put_contents( $file, str_replace( OLD_FILENAME_PREFIX, NEW_FILENAME_PREFIX, file_get_contents( $file ) ) );
}
if ( NEW_FUNCTION_PREFIX ) {
file_put_contents( $file, str_replace( OLD_FUNCTION_PREFIX, NEW_FUNCTION_PREFIX, file_get_contents( $file ) ) );
}
if ( NEW_CLASS_PREFIX ) {
file_put_contents( $file, str_replace( OLD_CLASS_PREFIX, NEW_CLASS_PREFIX, file_get_contents( $file ) ) );
}
if ( NEW_NAMESPACE_PREFIX ) {
file_put_contents( $file, str_replace( OLD_NAMESPACE_PREFIX, NEW_NAMESPACE_PREFIX, file_get_contents( $file ) ) );
}
}
};
$replace_file_content();
$replace_filename = function () use ( $args, $iterator ) {
$files = [];
/**
* @var SplFileInfo $info
*/
foreach ( $iterator as $info ) {
$files[] = $info->getPathname();
}
$renamed = [];
foreach ( $files as $file ) {
if ( strpos( $file, OLD_FILENAME_PREFIX ) !== false ) {
if ( empty( $args['dry-run'] ) ) {
rename( $file, str_replace( OLD_FILENAME_PREFIX, NEW_FILENAME_PREFIX, $file ) );
}
$renamed[] = "$file -> " . str_replace( OLD_FILENAME_PREFIX, NEW_FILENAME_PREFIX, $file );
}
}
if ( ! empty( $args['dry-run'] ) ) {
echo implode( "\n", $renamed ) . "\n";
}
};
if ( NEW_FILENAME_PREFIX ) {
$replace_filename();
}