-
Notifications
You must be signed in to change notification settings - Fork 1
/
unify
executable file
·81 lines (66 loc) · 2.3 KB
/
unify
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
#! /usr/bin/env php
<?php
declare(strict_types=1);
\error_reporting(-1);
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/functions.php';
use Symfony\Component\Finder\Finder;
//******************************************************************************
// Update files
/**
* We take certain whitelisted files, and do token replacement on them.
*
* For these files, there are tokens which start/end with `@@`. This script will
* review those files, look at the tokens, and try to find a file in the
* `templates/` directory with an appropriate file name.
*
* The contents of that file are read, and injected into the whitelisted files,
* replacing the token. The results are written to a new file in the correct
* location in the repository's file system.
*/
$__files__ = [
'_php_cs.dist.tmpl',
'Makefile.tmpl',
'phpunit.xml.dist.tmpl',
'psalm.xml.tmpl',
'README.md.tmpl',
];
foreach ($__files__ as $__f__) {
$finder = Finder::create()
->files()
->name($__f__)
->depth('< 3')
->in(dirname(__DIR__))
->in(dirname(__DIR__) . '/.github')
->notPath('vendor')
;
foreach ($finder as $file) {
$template = file_get_contents($file->getRealPath());
$writeTo = sprintf(
'%s/%s',
dirname(
dirname($file->getRealPath())
),
preg_replace('/^_/', '.', strip_tmpl($__f__))
);
echo sprintf('Updating %s', $writeTo) . PHP_EOL;
$contents = preg_replace_callback('/@@([^@]+)@@/S', function(array $matches) use (&$__f__): string {
[$tag, $directive] = $matches;
$variables = explode(';', $directive);
$file = array_shift($variables);
$tmpl = trim(
file_get_contents(
sprintf('%s/templates/%s-%s.tmpl', __DIR__, get_tmpl($__f__), $file)
)
);
foreach ($variables as $var) {
[$k, $v] = explode('=', $var);
$tmpl = str_replace(sprintf('@@%s@@', strtoupper($k)), $v, $tmpl);
}
return $tmpl;
}, $template, -1) . PHP_EOL;
// Write the file back to disk
file_put_contents($writeTo, $contents);
}
unset($finder);
}