-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
497 lines (436 loc) · 12.3 KB
/
Copy pathsetup.php
File metadata and controls
497 lines (436 loc) · 12.3 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* @file
* setup.php
*/
/**
* Initial setup before we can create a theme.
*
* @return bool|void
* FALSE on failure, void on success.
*/
function init() {
$theme_name = get_cli_option('theme-name');
$parent_theme = get_cli_option('parent-theme');
$skip_sb = get_cli_option('skip-sb');
if (!$theme_name) {
print 'No theme name provided' . PHP_EOL;
return FALSE;
}
if ($parent_theme && !is_valid_machine_name($parent_theme)) {
print 'Parent theme must be a valid Drupal machine name (lowercase letters, numbers, underscores, starting with a letter)' . PHP_EOL;
return FALSE;
}
$machine_name = str_replace(' ', '_', strtolower($theme_name));
$machine_name = sanitize_machine_name($machine_name);
$kebab_name = str_replace('_', '-', $machine_name);
$const_name = strtoupper($machine_name);
create_theme($theme_name, $machine_name, $kebab_name, $const_name, $parent_theme, $skip_sb);
}
/**
* Creates a new theme.
*
* @param string $theme_name
* The human readable name of the theme.
* @param string $machine_name
* The machine_name for Drupal.
* @param string $kebab_name
* The package.json name.
* @param string $const_name
* The const name for Drupal.
* @param string|bool $parent_theme
* The parent theme machine name or FALSE.
* @param bool $skip_sb
* Should Storybook files be skipped.
*
* @return bool|void
* FALSE on failure, void on success.
*/
function create_theme(string $theme_name, string $machine_name, string $kebab_name, string $const_name, $parent_theme = FALSE, $skip_sb = FALSE) {
$theme_dir = substr(getcwd(), 0, strpos(getcwd(), 'themes') + 6);
$theme_path = $theme_dir . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . $machine_name;
if (!is_dir(normalize_path($theme_path))) {
if (@mkdir(normalize_path($theme_path), 0755, TRUE) !== TRUE) {
print 'Custom theme could not be created' . PHP_EOL;
return FALSE;
}
}
if (copy_files(get_files_to_copy($skip_sb), $theme_path) !== TRUE) {
print 'Failed to copy files' . PHP_EOL;
return FALSE;
}
$alterations = set_alterations($theme_name, $machine_name, $kebab_name, $const_name, $parent_theme);
if (alter_files($theme_path, get_files_to_alter($skip_sb), $alterations) !== TRUE) {
print 'Failed to alter files' . PHP_EOL;
return FALSE;
}
if (rename_files(get_files_to_rename(), $theme_path, $machine_name) !== TRUE) {
print 'Failed to rename files' . PHP_EOL;
return FALSE;
}
print "$theme_name theme created! Happy theming!" . PHP_EOL;
}
/* **************************************************
* COPY FUNCTIONS
*/
/**
* The files that need to be copied into the new theme.
*
* @param bool $skip_sb
* Whether to skip Storybook files.
*
* @return array
* An array of files to copy.
*/
function get_files_to_copy($skip_sb = FALSE) {
$files = [
'config',
'libraries',
'src',
'ui/.storybook',
'ui/dist',
'ui/plop-templates',
'ui/src',
'ui/utils',
'ui/.babelrc',
'ui/.nvmrc',
'ui/.yarnrc.yml',
'ui/package.json',
'ui/plopfile.mjs',
'ui/README.md',
'ui/webpack.config.js',
'ui-core',
'.editorconfig',
'package.json',
's360_base_theme.breakpoints.yml',
's360_base_theme.info.yml',
's360_base_theme.libraries.yml',
's360_base_theme.style_options.yml',
];
if ($skip_sb) {
$files = array_filter($files, function ($file) {
return strpos($file, '.storybook') === FALSE;
});
}
return $files;
}
/**
* Copy every file inside a directory.
*
* @param string $src
* The directory inside the base theme.
* @param string $dest
* The directory inside the new theme.
*/
function _recursive_copy(string $src, string $dest) {
if (is_dir($src)) {
// Make the destination directory if not exist.
@mkdir($dest, 0755, TRUE);
$dir_handle = opendir($src);
while ($file = readdir($dir_handle)) {
if ($file != '.' && $file != '..') {
_recursive_copy($src . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file);
}
}
closedir($dir_handle);
}
else {
copy($src, $dest);
}
}
/**
* Copy the files.
*
* @param array $files
* An array of files to copy, including folders.
* @param string $theme_path
* The path for the new theme.
*
* @return bool
* TRUE on success.
*/
function copy_files(array $files, string $theme_path) {
foreach ($files as $file) {
$src = getcwd() . DIRECTORY_SEPARATOR . $file;
$file = explode(DIRECTORY_SEPARATOR, $file);
if (in_array('ui', $file)) {
$file = 'ui' . DIRECTORY_SEPARATOR . end($file);
}
else {
$file = end($file);
}
$dest = $theme_path . DIRECTORY_SEPARATOR . $file;
_recursive_copy($src, $dest);
}
return TRUE;
}
/* **************************************************
* ALTER FUNCTIONS
*/
/**
* The files that need to be altered with the new theme name.
*
* @param bool $skip_sb
* Whether to skip Storybook files.
*
* @return array
* An array of files to alter.
*/
function get_files_to_alter($skip_sb = FALSE) {
$files = [
'config',
'libraries',
'src',
'ui/.storybook',
'ui/package.json',
'ui/plop-templates',
'ui/src',
'ui-core/package.json',
's360_base_theme.breakpoints.yml',
's360_base_theme.info.yml',
's360_base_theme.libraries.yml',
];
if ($skip_sb) {
$files = array_filter($files, function ($file) {
return strpos($file, '.storybook') === FALSE;
});
}
return $files;
}
/**
* Alter the files.
*
* @param string $theme_path
* The path for the new theme.
* @param array $files
* An array of files to alter, including folders.
* @param array $alterations
* The associative array of alterations.
* @param bool $absolute
* If the file path is absolute or relative.
*
* @return bool
* TRUE on success, FALSE on failure.
*/
function alter_files(string $theme_path, array $files, array $alterations, bool $absolute = FALSE) {
foreach ($files as $file) {
if ($absolute === TRUE) {
$file_type = filetype(realpath($file));
$file_path = $file;
}
else {
$file_type = filetype($theme_path . DIRECTORY_SEPARATOR . $file);
$file_path = $theme_path . DIRECTORY_SEPARATOR . $file;
}
if ($file_type === 'dir') {
$files = scandir($file_path);
$files = array_splice($files, 2);
foreach ($files as $file) {
$alter_files = alter_files($theme_path, [$file_path . DIRECTORY_SEPARATOR . $file], $alterations, TRUE);
if ($alter_files !== TRUE) {
return FALSE;
}
}
}
elseif ($file_type === 'file') {
if (alter_file_str_replace($file_path, array_keys($alterations), $alterations) !== TRUE) {
return FALSE;
}
}
}
return TRUE;
}
/**
* Use the alterations array to alter a file.
*
* @param string $file_path
* The path of the file to alter.
* @param array $find
* The keys of the associative array of alterations.
* @param array $replace
* The associative array of alterations.
*
* @return bool
* TRUE on success.
*/
function alter_file_str_replace(string $file_path, array $find, array $replace) {
$file_path = normalize_path($file_path);
$file_contents = file_get_contents($file_path);
$file_contents = str_replace($find, $replace, $file_contents);
file_put_contents($file_path, $file_contents);
return TRUE;
}
/**
* Sets up the alterations array for string replacements.
*
* @param string $theme_name
* The human readable name of the theme.
* @param string $machine_name
* The machine_name for Drupal.
* @param string $kebab_name
* The package.json name.
* @param string $const_name
* The PHP const name of the theme.
* @param string|bool $parent_theme
* The parent theme machine name or FALSE.
*
* @return array
* An associative array of the original theme with it's new counterparts.
*/
function set_alterations(string $theme_name, string $machine_name, string $kebab_name, string $const_name, $parent_theme = FALSE) {
$base_theme_value = $parent_theme ?: 'false';
return [
'S360 Base Theme' => $theme_name,
's360_base_theme' => $machine_name,
's360-base-theme' => $kebab_name,
'S360_BASE_THEME' => $const_name,
'drupal_base_theme' => $base_theme_value,
];
}
/* **************************************************
* RENAME FUNCTIONS
*/
/**
* The files that need to be renamed with the new theme name.
*
* @return array
* An array of files to rename.
*/
function get_files_to_rename() {
return [
'libraries/s360_base_theme.block.libraries.yml',
'libraries/s360_base_theme.component.libraries.yml',
'libraries/s360_base_theme.field.libraries.yml',
'libraries/s360_base_theme.form.libraries.yml',
'libraries/s360_base_theme.media.libraries.yml',
'libraries/s360_base_theme.navigation.libraries.yml',
'libraries/s360_base_theme.node.libraries.yml',
'libraries/s360_base_theme.paragraph.libraries.yml',
'libraries/s360_base_theme.taxonomy.libraries.yml',
'libraries/s360_base_theme.views.libraries.yml',
'config/install/block.block.s360_base_theme_content.yml',
'config/install/block.block.s360_base_theme_messages.yml',
'config/install/block.block.s360_base_theme_tabs.yml',
's360_base_theme.breakpoints.yml',
's360_base_theme.info.yml',
's360_base_theme.libraries.yml',
's360_base_theme.style_options.yml',
];
}
/**
* Renames files.
*
* @param array $files_to_rename
* The files to rename.
* @param string $theme_path
* The path for the new theme.
* @param string $machine_name
* The machine_name for Drupal.
*
* @return bool
* TRUE on success.
*/
function rename_files(array $files_to_rename, string $theme_path, string $machine_name) {
foreach ($files_to_rename as $file_to_rename) {
$file_original_path = $theme_path . DIRECTORY_SEPARATOR . $file_to_rename;
$file_new_path = $theme_path . DIRECTORY_SEPARATOR . str_replace('s360_base_theme', $machine_name, $file_to_rename);
rename($file_original_path, normalize_path($file_new_path));
}
return TRUE;
}
/* **************************************************
* UTILITY FUNCTIONS
*/
/**
* Validates if a string is a valid Drupal machine name.
*
* @param string $name
* The name to validate.
*
* @return bool
* TRUE if valid, FALSE otherwise.
*/
function is_valid_machine_name(string $name) {
// Machine names can only contain lowercase letters, numbers and underscores.
// Machine names must always begin with a letter.
return preg_match('/^[a-z][a-z0-9_]*$/', $name) === 1;
}
/**
* Sanitizes a string to be a valid Drupal machine name.
*
* @param string $name
* The name to sanitize.
*
* @return string
* The sanitized machine name.
*/
function sanitize_machine_name(string $name) {
$search = [
// Machine names can only contain letters, numbers and underscores.
'/[^a-z0-9_]/',
// Machine names must always begin with a letter.
'/^[^a-z]+/',
];
return preg_replace($search, '', $name);
}
/**
* Normalizes a path for the current operating system.
*
* @param string $path
* The path to normalize.
*
* @return string
* The normalized path.
*/
function normalize_path(string $path) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$path = str_replace('/', '\\', strtolower($path));
}
else {
$path = str_replace('\\', '/', $path);
}
return trim($path);
}
/**
* Returns all the passed in CLI options.
*
* @return array|bool
* An associative array of all the CLI options, or FALSE on error.
*/
function get_cli_options() {
global $argv;
$options = [];
foreach ($argv as $key => $arg) {
if (strpos($arg, '=') !== FALSE) {
print 'Do not use equal signs in your options.' . PHP_EOL;
return FALSE;
}
switch ($arg) {
case '--theme-name':
$options['theme-name'] = $argv[$key + 1];
break;
case '--parent-theme':
$options['parent-theme'] = $argv[$key + 1];
break;
case '--skip-sb':
$options['skip-sb'] = TRUE;
break;
}
}
return $options;
}
/**
* Gets the value of a specific CLI option.
*
* @param string $option
* The CLI option name.
*
* @return string|bool
* The value of the CLI option, or FALSE if the option wasn't passed.
*/
function get_cli_option(string $option) {
$cli_options = get_cli_options();
return (!empty($cli_options[$option])) ? $cli_options[$option] : FALSE;
}
// Kickoff!
init();