-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.php
346 lines (306 loc) · 8.85 KB
/
setup.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* SETUP BOILERPLATE
*
* 1. Edit the setup.json file with details for your new module.
* 2. run 'php setup.php'
* 3. run 'php build.php'
*
* All keys in setup.json are transformed to keys according to the following pattern
* {{BPREPLACE[JSONKEY]}}
*
* [JSONKEY] is replaced by an uppercase version of the key.
*
* This script will self destruct when done.
* Track with git to be able to reverse changes.
*
* Avabile replacement keys are:
* {{BPREPLACENAME}}
* {{BPREPLACEDESCRIPTION}}
* {{BPREPLACESLUG}}
* {{BPREPLACESLUGCAMELCASE}}
* {{BPREPLACENAMESPACE}}
* {{BPREPLACECAPSCONSTANT}}
* {{BPREPLACEAUTHOR}}
* {{BPREPLACEAUTHOREMAIL}}
* {{BPREPLACEGITHUB}}
*
*/
class Setup
{
private static $config;
private static $folders = ['source'];
private static $searchableFileTypes = ['php', 'scss', 'js'];
public function __construct()
{
self::info("Starting setup");
if (self::$config = self::getConfig()) {
//Traverse directories and make replacements
$filesToReplace = self::getFilesList();
if (is_array($filesToReplace) && !empty($filesToReplace)) {
foreach ($filesToReplace as $file) {
self::updateFile($file);
}
} else {
self::err("No files found to replace");
}
//Manually replacement targets
self::updateFile(self::getBasePath() . 'package.json');
self::updateFile(self::getBasePath() . 'composer.json');
self::updateFile(self::getBasePath() . 'webpack.config.js');
self::updateFile(self::getBasePath() . 'README-boilerplate.md');
self::updateFile(self::getBasePath() . 'modularity-boilerplate.php');
self::updateFile(self::getBasePath() . 'Public.php');
//Remove readme.md & replace with template
self::removeFile(self::getBasePath() . 'README.md');
self::moveFile(
self::getBasePath() . 'README-boilerplate.md',
self::getBasePath() . 'README.md'
);
//Rename main file
self::moveFile(
self::getBasePath() . 'modularity-boilerplate.php',
self::getBasePath() . 'modularity-' . self::$config->slug . '.php'
);
//Rename module files
self::moveFile(
self::getBasePath() . 'source/php/Module/Boilerplate.php',
self::getBasePath() . 'source/php/Module/' . self::$config->slugcamelcase . '.php'
);
self::moveFile(
self::getBasePath() . 'source/php/Module/views/boilerplate.blade.php',
self::getBasePath() . 'source/php/Module/views/' . self::$config->slug . '.blade.php'
);
//Rename asset source files
self::moveFile(
self::getBasePath() . 'source/js/modularity-boilerplate.js',
self::getBasePath() . 'source/js/modularity-' . self::$config->slug . '.js'
);
self::moveFile(
self::getBasePath() . 'source/sass/modularity-boilerplate.scss',
self::getBasePath() . 'source/sass/modularity-' . self::$config->slug . '.scss'
);
//Remove me
if (self::remove()) {
self::log("All done! Now go and make something beautiful!", 'info');
}
} else {
self::log("Exiting setup");
}
}
/**
* Get files lists
*
* @return array
*/
private static function getFilesList()
{
$files = [];
if (empty(self::$folders) || !is_array(self::$folders)) {
self::err("No folders to scan for replacements.");
return $files;
}
foreach (self::$folders as $folder) {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
self::getBasePath() . $folder,
\RecursiveDirectoryIterator::SKIP_DOTS
),
);
foreach ($iterator as $info) {
if (!$info->isFile()) {
continue;
}
if ($info->isFile() && $fileType = $info->getExtension()) {
if (!in_array($fileType, self::$searchableFileTypes)) {
continue;
}
}
$files[] = $info->getPathname();
}
}
return $files;
}
/**
* Replace contents of composer json
*
* @return void
*/
private static function updateFile($filename)
{
foreach (self::$config as $key => $value) {
$result = self::makeReplacements(
"{{BPREPLACE" . strtoupper($key) . "}}",
$value,
$filename
);
if (!$result) {
self::err("Failed to replace " . $filename);
break;
}
}
if ($result) {
self::log("Updated " . $filename);
}
}
/**
* Move a file
*
* @return bool
*/
private static function moveFile($from, $to)
{
if (rename($from, $to)) {
self::log("Moved file " . $from . " to " . $to);
return true;
}
self::err("Could not move file " . $from . " to " . $to);
return false;
}
/**
* Replace contents of file
*
* @param string $from
* @param string $to
* @param string $filename
* @return bool
*/
private static function makeReplacements($from, $to, $filename)
{
if (!file_exists($filename)) {
return false;
}
$content = str_replace(
$from,
$to,
file_get_contents($filename)
);
if (!file_put_contents($filename, $content)) {
return false;
}
return true;
}
/**
* Get config
*
* @return bool | string
*/
private static function getConfig()
{
if (file_exists(self::getConfigPath())) {
self::log("Found config file in " . self::getConfigPath());
return (object) json_decode(
file_get_contents(self::getConfigPath()),
true
);
} else {
self::log("Could not find config file in " . self::getConfigPath());
}
return false;
}
/**
* Get configuration file path
*
* @return string
*/
private static function getConfigPath()
{
return self::getBasePath() . 'setup.json';
}
/**
* Get base path
*
* @return string
*/
private static function getBasePath()
{
return rtrim(__DIR__, '/') . "/";
}
/**
* Log message to user
*
* @param string $message The message
* @param string $type Should not be changed
*/
private static function log($message, $type = null, $trimPath = true)
{
//Trim path in log msg, to make them readable
if ($trimPath === true) {
$message = str_replace(__DIR__, '', $message);
}
//Set type
if (is_null($type)) {
$type = __FUNCTION__;
}
//Print
echo self::getColor($type) . "[" . strtoupper($type) . "] " . $message . "\n";
}
/**
* Get colorcoding for messages
*
* @param string $type
*/
private static function getColor($type)
{
if ($type == 'info') {
return "\033[0;95m";
}
if ($type == 'log') {
return "\033[0;32m";
}
if ($type == 'err') {
return "\033[0;31m";
}
}
/**
* Invoke log, but with error prefix.
*
* @param string $message
*/
private static function err($message)
{
self::log($message, __FUNCTION__);
}
/**
* Invoke log, but with info prefix.
*
* @param string $message
*/
private static function info($message)
{
self::log($message, __FUNCTION__);
}
/**
* Remove a file
*
* @return bool
*/
private static function removeFile($file)
{
if (!unlink($file)) {
self::err("Failed to remove " . $file);
return false;
}
self::log("Removed file " . $file);
return true;
}
/**
* Remove this setup script
*/
private static function remove()
{
$files = [
self::getBasePath() . "setup.json",
self::getBasePath() . "setup.php"
];
if (!empty($files) && is_array($files)) {
foreach ($files as $file) {
self::removeFile($file);
}
return true;
}
self::err("Nothing to delete.");
return false;
}
}
new Setup();