-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs_scripts.php
447 lines (409 loc) · 10.4 KB
/
funcs_scripts.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
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
<?php
use Panlatent\CronExpressionDescriptor\ExpressionDescriptor;
use SilverStripe\SupportedModules\MetaData;
// These functions in scripts can be used in scripts
/**
* Check that a directory exists relative to the root of the module being processed
*
* Example usage:
* check_dir_exists('src')
*/
function check_dir_exists($dirname) {
global $MODULE_DIR;
$path = "$MODULE_DIR/$dirname";
if (!is_dir($path)) {
info("Directory $path does not exist, though this should be OK");
return false;
}
return true;
}
/**
* Check that a file exists relative to the root of the module being processed
*
* Example usage:
* check_file_exists('composer.json')
*/
function check_file_exists($filename)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (!file_exists($path)) {
info("File $path does not exist, though this should be OK");
return false;
}
return true;
}
/**
* Read a file relative to the root of the module being processed
*
* Example usage:
* read_file('composer.json')
*/
function read_file($filename)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (!file_exists($path)) {
error("File $path does not exist");
}
return file_get_contents($path);
}
/**
* Write a file relative to the root of the module being processed even if it already exists
*
* Example usage:
* write_file_even_if_exists('.github/workflows/ci.yml')
*/
function write_file_even_if_exists($filename, $content)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
write_file($path, $content);
}
/**
* Write a file relative to the root of the module being processed only if it doesn't already exist
*
* Example usage:
* write_file_if_not_exist('LICENSE')
*/
function write_file_if_not_exist($filename, $content)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (!file_exists($path)) {
write_file($path, $content);
}
}
/**
* Delete a file relative to the root of the module being processed if it exists
*
* Example usage:
* delete_file_if_exists('.travis.yml')
*/
function delete_file_if_exists($filename)
{
global $MODULE_DIR;
$path = "$MODULE_DIR/$filename";
if (file_exists($path)) {
unlink($path);
info("Deleted $path");
}
}
/**
* Rename a file relative to the root of the module being processed if it exists
*
* Example usage:
* rename_file_if_exists('oldfilename.md', 'newfilename.md')
*/
function rename_file_if_exists($oldFilename, $newFilename)
{
global $MODULE_DIR;
$oldPath = "$MODULE_DIR/$oldFilename";
$newPath = "$MODULE_DIR/$newFilename";
if (file_exists($oldPath)) {
$contents = read_file($oldFilename);
write_file($newPath, $contents);
delete_file_if_exists($oldFilename);
}
}
/**
* Determine if the module being processed is a recipe, including silverstripe-installer
*
* Example usage:
* module_is_recipe()
*/
function module_is_recipe()
{
if (!check_file_exists('composer.json')) {
return false;
}
$contents = read_file('composer.json');
$json = json_decode($contents);
if (is_null($json)) {
$lastError = json_last_error();
error("Could not parse from composer.json - last error was $lastError");
}
return ($json->type ?? '') === 'silverstripe-recipe';
}
/**
* Determine if the repository being processed is an actual silverstripe module e.g. silverstripe-admin, not gha-*
*
* Example usage:
* is_module()
*/
function is_module()
{
if (!check_file_exists('composer.json')) {
return false;
}
if (is_gha_repository()) {
return false;
}
$contents = read_file('composer.json');
$json = json_decode($contents);
if (is_null($json)) {
$lastError = json_last_error();
error("Could not parse from composer.json - last error was $lastError");
}
// config isn't technically a Silverstripe CMS module, but we treat it like one.
if (($json->name ?? '') === 'silverstripe/config') {
return true;
}
$moduleTypes = [
'silverstripe-vendormodule',
'silverstripe-module',
'silverstripe-recipe',
'silverstripe-theme',
];
return in_array($json->type ?? '', $moduleTypes);
}
/**
* Determine if the module being processed is a composer plugin
*
* Example usage:
* is_composer_plugin()
*/
function is_composer_plugin()
{
if (!check_file_exists('composer.json')) {
return false;
}
$contents = read_file('composer.json');
$json = json_decode($contents);
if (is_null($json)) {
$lastError = json_last_error();
error("Could not parse from composer.json - last error was $lastError");
}
return ($json->type ?? '') === 'composer-plugin';
}
/**
* Determine if the module being processed is a theme
*
* Example usage:
* is_theme()
*/
function is_theme()
{
if (!check_file_exists('composer.json')) {
return false;
}
$contents = read_file('composer.json');
$json = json_decode($contents);
if (is_null($json)) {
$lastError = json_last_error();
error("Could not parse from composer.json - last error was $lastError");
}
return $json->type === 'silverstripe-theme';
}
/**
* Determine if the module being processed is a meta repository
*/
function is_meta_repo()
{
$moduleName = module_name();
return $moduleName === '.github';
}
/**
* Determine if the module being processed is a source of documentation
*
* Example usage:
* is_docs()
*/
function is_docs()
{
$moduleName = module_name();
return $moduleName === 'developer-docs' || $moduleName === 'silverstripe-userhelp-content';
}
/**
* Determine if the module being processed is commercially supported
*
* Example usage:
* is_supported()
*/
function is_supported()
{
global $GITHUB_REF;
return in_array(
$GITHUB_REF,
array_column(
MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_SUPPORTED],
'github'
)
);
}
/**
* Determine if the module being processed is a gha-* repository e.g. gha-ci
* aka "WORKFLOW"
*
* Example usage:
* is_gha_repository()
*/
function is_gha_repository()
{
global $GITHUB_REF;
return in_array(
$GITHUB_REF,
array_column(
MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_WORKFLOW],
'github'
)
);
}
/**
* Determine if the module being processed is "TOOLING"
*
* Example usage:
* is_tooling()
*/
function is_tooling()
{
global $GITHUB_REF;
return in_array(
$GITHUB_REF,
array_column(
MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_TOOLING],
'github'
)
);
}
/**
* Determine if the module being processed is "MISC"
*
* Example usage:
* is_misc()
*/
function is_misc()
{
global $GITHUB_REF;
return in_array(
$GITHUB_REF,
array_column(
MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_MISC],
'github'
)
);
}
/**
* Determine if the module being processed has a wildcard major version mapping
* in silverstripe/supported-modules repositories.json
*
* Example usage:
* has_wildcard_major_version_mapping()
*/
function has_wildcard_major_version_mapping()
{
global $GITHUB_REF;
$repoData = MetaData::getMetaDataForRepository($GITHUB_REF);
return array_key_exists('*', $repoData['majorVersionMapping']);
}
/**
* Return the module name without the account e.g. silverstripe/silverstripe-admin with return silverstripe-admin
*
* Example usage:
* if (module_name() === 'silverstripe-admin) {
* // logic
* }
*/
function module_name()
{
global $GITHUB_REF;
$parts = explode('/', $GITHUB_REF);
return end($parts);
}
/**
* Determine if the module being processed is one of the modules in a list
*
* Example usage:
* module_is_one_of(['silverstripe-mfa', 'silverstripe-totp'])
*/
function module_is_one_of($repos)
{
global $GITHUB_REF;
if (!is_array($repos)) {
error('repos is not an array');
}
foreach ($repos as $repo) {
if (!is_string($repo)) {
error('repo is not a string');
}
if (strpos($GITHUB_REF, "/$repo") !== false) {
return true;
}
}
return false;
}
/**
* Return the github account of the module being processed
*
* Example usage:
* module_account()
*/
function module_account()
{
global $GITHUB_REF;
return explode('/', $GITHUB_REF)[0];
}
/**
* Output an info message to the console
*
* Example usage:
* info('This is a mildly interesting message')
*/
function info($message)
{
// using writeln with <info> instead of ->info() so that it only takes up one line instead of five
io()->writeln("<info>$message</>");
}
/**
* Output a warning message to the console
*
* Example usage:
* warning('This is something you might want to pay attention to')
*/
function warning($message)
{
io()->warning($message);
}
/**
* Converts a cron expression to a human readable string
* Says UTC because that's what GitHub Actions uses
*
* Example usage:
* human_cron('5 4 * * 0')
* => 'At 4:05 AM UTC, only on Sunday'
*/
function human_cron(string $cron): string
{
$str = (new ExpressionDescriptor($cron))->getDescription();
$str = preg_replace('#0([1-9]):#', '$1:', $str);
$str = preg_replace('# (AM|PM),#', ' $1 UTC,', $str);
return $str;
}
/**
* Creates a predicatable random int between 0 and $max based on the module name and file name script
* is called with to be used with the % mod operator.
* $offset variable will offset both the min (0) and $max. e.g. $offset of 1 with a max of 27 will return an int
* between 1 and 28
* Note that this will return the exact same value every time it is called for a given filename in a given module
*/
function predictable_random_int($scriptName, $max, $offset = 0): int
{
$chars = str_split(module_name() . $scriptName);
$codes = array_map(fn($c) => ord($c), $chars);
$sum = array_sum($codes);
$remainder = $sum % ($max + 1);
return $remainder + $offset;
}
/**
* Determine if the current branch is either 1 or 1.2 numeric style
* Can also be pulls/<number>/... style
*/
function current_branch_name_is_numeric_style()
{
global $MODULE_DIR;
$currentBranch = cmd('git rev-parse --abbrev-ref HEAD', $MODULE_DIR);
if (preg_match('#^(pulls/)?([0-9]+)(\.[0-9]+)?(/|$)#', $currentBranch)) {
return true;
}
return false;
}