-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.php
155 lines (134 loc) · 5.19 KB
/
build.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
<?php
/*
BUILD SCRIPT
Takes the all the icons from the heroicons/optimized directory,
modifies them to add the check for attributes and saves them as PHP files
*/
@mkdir(__DIR__ . '/snippets/solid', recursive: true);
@mkdir(__DIR__ . '/snippets/outline', recursive: true);
@mkdir(__DIR__ . '/snippets/mini', recursive: true);
@mkdir(__DIR__ . '/snippets/micro', recursive: true);
@mkdir(__DIR__ . '/assets', recursive: true);
$spritesheet = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">';
/**
* Generate snippets
*/
foreach (new DirectoryIterator(__DIR__ . '/heroicons/optimized/16/solid') as $file) {
if ($file->getType() !== 'file') {
continue;
}
$original = file_get_contents($file->getPathname());
$icon = str_replace('<svg', '<svg class="<?= $class ?? \'\' ?>"', $original);
$icon = str_replace('aria-hidden="true" data-slot="icon"', '<?= $attributes ?? \'aria-hidden="true"\' ?>', $icon);
$name = __DIR__ . '/snippets/micro/' . str_replace('svg', 'php', $file->getFilename());
file_put_contents($name, $icon);
// Also generate sprite sheet from micro icons
$id = str_replace('.svg', '', $file->getFilename());
$symbol = preg_replace('/<svg[^>]*>/', "<symbol id=\"$id\" viewBox=\"0 0 16 16\" fill=\"none\">", $original);
$symbol = str_replace('</svg>', '</symbol>', $symbol);
$symbol = str_replace('<path', '<path fill="currentColor"', $symbol);
$spritesheet .= $symbol;
}
$spritesheet .= '</svg>';
foreach (new DirectoryIterator(__DIR__ . '/heroicons/optimized/20/solid') as $file) {
if ($file->getType() !== 'file') {
continue;
}
$icon = file_get_contents($file->getPathname());
$icon = str_replace('<svg', '<svg class="<?= $class ?? \'\' ?>"', $icon);
$icon = str_replace('aria-hidden="true" data-slot="icon"', '<?= $attributes ?? \'aria-hidden="true"\' ?>', $icon);
$name = __DIR__ . '/snippets/mini/' . str_replace('svg', 'php', $file->getFilename());
file_put_contents($name, $icon);
}
foreach (new DirectoryIterator(__DIR__ . '/heroicons/optimized/24/solid') as $file) {
if ($file->getType() !== 'file') {
continue;
}
$icon = file_get_contents($file->getPathname());
$icon = str_replace('<svg', '<svg class="<?= $class ?? \'\' ?>"', $icon);
$name = __DIR__ . '/snippets/solid/' . str_replace('svg', 'php', $file->getFilename());
$icon = str_replace('aria-hidden="true" data-slot="icon"', '<?= $attributes ?? \'aria-hidden="true"\' ?>', $icon);
file_put_contents($name, $icon);
}
foreach (new DirectoryIterator(__DIR__ . '/heroicons/optimized/24/outline') as $file) {
if ($file->getType() !== 'file') {
continue;
}
$icon = file_get_contents($file->getPathname());
$icon = str_replace('<svg', '<svg class="<?= $class ?? \'\' ?>"', $icon);
$icon = str_replace('stroke-width="1.5"', 'stroke-width="<?= $strokeWidth ?? 1.5 ?>"', $icon);
$icon = str_replace('aria-hidden="true" data-slot="icon"', '<?= $attributes ?? \'aria-hidden="true"\' ?>', $icon);
$name = __DIR__ . '/snippets/outline/' . str_replace('svg', 'php', $file->getFilename());
file_put_contents($name, $icon);
}
/**
* Generate plugin index.php
*/
$snippets = [];
foreach(new DirectoryIterator(__DIR__ . '/snippets/micro') as $file) {
if ($file->getType() !== 'file') { continue; }
$baseName = $file->getBasename('.php');
$snippetName = "heroicons/micro/$baseName";
$loadPath = "/snippets/micro/{$baseName}.php";
$snippets []= compact('snippetName', 'loadPath');
}
foreach(new DirectoryIterator(__DIR__ . '/snippets/mini') as $file) {
if ($file->getType() !== 'file') { continue; }
$baseName = $file->getBasename('.php');
$snippetName = "heroicons/mini/$baseName";
$loadPath = "/snippets/mini/{$baseName}.php";
$snippets []= compact('snippetName', 'loadPath');
}
foreach(new DirectoryIterator(__DIR__ . '/snippets/solid') as $file) {
if ($file->getType() !== 'file') { continue; }
$baseName = $file->getBasename('.php');
$snippetName = "heroicons/solid/$baseName";
$loadPath = "/snippets/solid/{$baseName}.php";
$snippets []= compact('snippetName', 'loadPath');
}
foreach(new DirectoryIterator(__DIR__ . '/snippets/outline') as $file) {
if ($file->getType() !== 'file') { continue; }
$baseName = $file->getBasename('.php');
$snippetName = "heroicons/outline/{$baseName}";
$loadPath = "/snippets/outline/{$baseName}.php";
$snippets []= compact('snippetName', 'loadPath');
}
$snippetsJoined = implode("\n", array_map(function($snippet) {
return " '{$snippet['snippetName']}' => __DIR__ . '{$snippet['loadPath']}',";
}, $snippets));
file_put_contents('index.php', <<<PHP
<?php
use Kirby\Cms\App;
use Kirby\Toolkit\F;
class Heroicons
{
public static function folder(): string
{
\$asset = kirby()->plugins()['adamkiss/heroicons']->asset('spritesheet.svg');
if (! F::exists(\$asset->mediaRoot())) {
F::copy(
__DIR__ . '/assets/spritesheet.svg',
\$asset->mediaRoot(),
true // to overwrite the symlink that might exist at the location
);
}
return pathinfo(
\$asset->mediaRoot(),
PATHINFO_DIRNAME
);
}
public static function sprite() : string
{
return 'spritesheet.svg';
}
}
App::plugin('adamkiss/heroicons', [
'snippets' => [
{$snippetsJoined}
],
]);
PHP);
/**
* Asset sprite for kirby-icon-field
*/
file_put_contents(__DIR__ . '/assets/spritesheet.svg', $spritesheet);