forked from locomotivemtl/locomotive-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_boilerplate.php
120 lines (104 loc) · 3.81 KB
/
setup_boilerplate.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
<?php
require_once('vendor/autoload.php');
function validate_project_name($project_name)
{
if(!is_string($project_name)) {
return false;
}
if(!$project_name) {
return false;
}
return !!preg_match('/^[a-z]+$/', $project_name);
}
$climate = new League\CLImate\CLImate;
$climate->description('Boilerplate setup: Rename the boilerplate files and classes');
$climate->arguments->add([
'project_name' => [
'longPrefix' => 'name',
'description' => 'Project (module) name',
'defaultValue' => ''
],
'help' => [
'longPrefix' => 'help',
'description' => 'Prints a usage statement',
'noValue' => true
],
'quiet' => [
'prefix' => 'q',
'longPrefix' => 'quit',
'description' => 'Disable Output additional information on operations',
'noValue' => false
]
]);
if($climate->arguments->defined('help')) {
$climate->usage();
die();
}
$climate->underline()->out('Charcoal Boilerplate Module Setup');
$climate->arguments->parse();
$project_name = $climate->arguments->get('project_name');
$verbose = !!$climate->arguments->get('quiet');
$verbose = true;
if(!$project_name) {
$input = $climate->input('What is the name of the project?');
$project_name = strtolower($input->prompt());
}
if(!validate_project_name($project_name)) {
$climate->red()->out('Invalid project name. Operation aborted.');
die();
}
$climate->bold()->out(sprintf('Using "%s" as project name...', $project_name));
if(!function_exists('glob_recursive')) {
function glob_recursive($pattern, $flags = 0)
{
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
}
return $files;
}
}
$climate->out("\n".'Replacing file content...');
foreach(glob_recursive("www/*") as $filename) {
if(!is_dir($filename)) {
$file = file_get_contents($filename);
$num_replacement1 = 0;
$num_replacement2 = 0;
$content = preg_replace("/boilerplate/", $project_name, $file, -1, $num_replacement1);
$content = preg_replace("/Boilerplate/", ucfirst($project_name), $content, -1, $num_replacement2);
$num_replacements = ($num_replacement1+$num_replacement2);
if($num_replacements > 0) {
file_put_contents($filename, $content);
if($verbose) {
$climate->dim()->out(sprintf('%d occurence(s) of "boilerplate" has been changed to "%s" in file "%s"', $num_replacements, $project_name, $filename));
}
}
}
}
$climate->out("\n".'Renaming files and directories');
$boilerplate_files = glob_recursive("www/*boilerplate*");
$boilerplate_files = array_reverse($boilerplate_files);
foreach($boilerplate_files as $filename) {
$target_name = preg_replace("/boilerplate/", $project_name, basename($filename));
$target_name = dirname($filename).'/'.$target_name;
if($target_name != $filename) {
rename($filename, $target_name);
if($verbose) {
$climate->dim()->out(sprintf('%s has been renamed to %s', $filename, $target_name));
}
}
}
$boilerplate_files = glob_recursive("www/*Boilerplate*");
$boilerplate_files = array_reverse($boilerplate_files);
foreach($boilerplate_files as $filename) {
$climate->inline('.');
$target_name = preg_replace("/Boilerplate/", ucfirst($project_name), basename($filename));
$target_name = dirname($filename).'/'.$target_name;
if($target_name != $filename) {
rename($filename, $target_name);
if($verbose) {
$climate->dim()->out(sprintf('%s has been renamed to %s', $filename, $target_name));
}
}
}
$climate->green()->out("\n".'Success!');