Skip to content

Commit b2a80ff

Browse files
Apply SVG file filter for Generator by Default (#209)
* Add ability for Generator to filter files This allows icon package devs to define a filter closure applied to the list of discovered files. If the upstream icon package starts to included non-SVG files in Icon set folders this can filter those out. * Fix code styling * Just filter SVG files by default instead of new option * Add tests to cover IconGenerator and config options * Fix code styling * Update IconGeneratorTest.php --------- Co-authored-by: mallardduck <mallardduck@users.noreply.github.com> Co-authored-by: Dries Vints <dries@vints.io>
1 parent 3e81fa0 commit b2a80ff

3 files changed

Lines changed: 179 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ composer.lock
22
vendor
33
phpunit.xml
44
.phpunit.result.cache
5+
tests/fixtures/tmp

src/Generation/IconGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Str;
1010
use Illuminate\Support\Stringable;
11+
use Symfony\Component\Finder\SplFileInfo;
1112

1213
final class IconGenerator
1314
{
@@ -30,8 +31,12 @@ public function generate(): void
3031
{
3132
foreach ($this->sets as $set) {
3233
$destination = $this->getDestinationDirectory($set);
34+
$files = array_filter(
35+
$this->filesystem->files($set['source']),
36+
fn (SplFileInfo $value) => str_ends_with($value->getFilename(), '.svg')
37+
);
3338

34-
foreach ($this->filesystem->files($set['source']) as $file) {
39+
foreach ($files as $file) {
3540
$filename = Str::of($file->getFilename());
3641
$filename = $this->applyPrefixes($set, $filename);
3742
$filename = $this->applySuffixes($set, $filename);

tests/IconGeneratorTest.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use BladeUI\Icons\Generation\IconGenerator;
8+
use Illuminate\Filesystem\Filesystem;
9+
use SplFileInfo;
10+
11+
class IconGeneratorTest extends TestCase
12+
{
13+
const RESULT_DIR = __DIR__.'/fixtures/tmp';
14+
15+
private function clearResultsDirectory(): void
16+
{
17+
$fs = new Filesystem();
18+
19+
if ($fs->isDirectory(static::RESULT_DIR)) {
20+
$fs->deleteDirectory(static::RESULT_DIR);
21+
}
22+
}
23+
24+
/** @test */
25+
public function it_can_generate_icon_sets()
26+
{
27+
IconGenerator::create([
28+
[
29+
'source' => __DIR__.'/resources/svg',
30+
'destination' => static::RESULT_DIR.'/primary',
31+
],
32+
[
33+
'source' => __DIR__.'/resources/svg/solid',
34+
'destination' => static::RESULT_DIR.'/solid',
35+
],
36+
])->generate();
37+
38+
$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
39+
$this->assertDirectoryExists(static::RESULT_DIR.'/solid');
40+
$this->assertFileDoesNotExist(static::RESULT_DIR.'/primary/invalid-extension.txt');
41+
$this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg');
42+
$this->assertFileExists(static::RESULT_DIR.'/solid/camera.svg');
43+
44+
$this->clearResultsDirectory();
45+
}
46+
47+
/** @test */
48+
public function it_will_clear_icons_before_generation_with_safe_mode_off()
49+
{
50+
// Run once to generate the "OLD" icons
51+
IconGenerator::create([
52+
[
53+
'source' => __DIR__.'/resources/svg',
54+
'destination' => static::RESULT_DIR.'/primary',
55+
],
56+
[
57+
'source' => __DIR__.'/resources/svg/solid',
58+
'destination' => static::RESULT_DIR.'/solid',
59+
],
60+
])->generate();
61+
62+
$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
63+
$this->assertDirectoryExists(static::RESULT_DIR.'/solid');
64+
$this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg');
65+
$this->assertFileExists(static::RESULT_DIR.'/solid/camera.svg');
66+
67+
// Manually insert an "OLD ICON" that's been "removed".
68+
file_put_contents(static::RESULT_DIR.'/primary/cold-beans.svg', 'COOL BEANS!');
69+
$this->assertFileExists(static::RESULT_DIR.'/primary/cold-beans.svg');
70+
71+
// Regenerate with Safe mode ON to verify "old" icons will be kept...
72+
IconGenerator::create([
73+
[
74+
'source' => __DIR__.'/resources/svg',
75+
'destination' => static::RESULT_DIR.'/primary',
76+
'safe' => true,
77+
],
78+
[
79+
'source' => __DIR__.'/resources/svg/solid',
80+
'destination' => static::RESULT_DIR.'/solid',
81+
'safe' => true,
82+
],
83+
])->generate();
84+
85+
$this->assertFileExists(static::RESULT_DIR.'/primary/cold-beans.svg');
86+
87+
// Regenerate the icons with safe feature enabled.
88+
IconGenerator::create([
89+
[
90+
'source' => __DIR__.'/resources/svg',
91+
'destination' => static::RESULT_DIR.'/primary',
92+
],
93+
[
94+
'source' => __DIR__.'/resources/svg/solid',
95+
'destination' => static::RESULT_DIR.'/solid',
96+
],
97+
])->generate();
98+
99+
$this->assertFileDoesNotExist(static::RESULT_DIR.'/primary/cold-beans.svg');
100+
101+
$this->clearResultsDirectory();
102+
}
103+
104+
/** @test */
105+
public function it_can_use_generator_hooks()
106+
{
107+
$comment = '<!-- ICONS TEST --->'.PHP_EOL;
108+
109+
IconGenerator::create([
110+
[
111+
'source' => __DIR__.'/resources/svg',
112+
'destination' => static::RESULT_DIR.'/primary',
113+
'after' => static function (
114+
string $tempFilepath,
115+
array $iconSet,
116+
SplFileInfo $file
117+
) use ($comment) {
118+
$fileContents = file_get_contents($tempFilepath);
119+
file_put_contents($tempFilepath, $comment.$fileContents);
120+
},
121+
],
122+
])->generate();
123+
124+
$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
125+
$this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg');
126+
127+
$iconContent = file_get_contents(static::RESULT_DIR.'/primary/camera.svg');
128+
$this->assertStringStartsWith($comment, $iconContent);
129+
130+
$this->clearResultsDirectory();
131+
}
132+
133+
/** @test */
134+
public function it_will_remove_and_apply_prefixes()
135+
{
136+
IconGenerator::create([
137+
[
138+
'source' => __DIR__.'/resources/svg',
139+
'destination' => static::RESULT_DIR.'/primary',
140+
'input-prefix' => 'zondicon-',
141+
'output-prefix' => 'blade-',
142+
],
143+
])->generate();
144+
145+
$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
146+
$this->assertFileExists(static::RESULT_DIR.'/primary/blade-flag.svg');
147+
$this->assertFileExists(static::RESULT_DIR.'/primary/blade-camera.svg');
148+
$this->assertFileExists(static::RESULT_DIR.'/primary/blade-foo-camera.svg');
149+
150+
$this->clearResultsDirectory();
151+
}
152+
153+
/** @test */
154+
public function it_will_remove_and_apply_suffixes()
155+
{
156+
IconGenerator::create([
157+
[
158+
'source' => __DIR__.'/resources/svg',
159+
'destination' => static::RESULT_DIR.'/primary',
160+
'input-suffix' => '-camera',
161+
'output-suffix' => '-wonky',
162+
],
163+
])->generate();
164+
165+
$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
166+
$this->assertFileExists(static::RESULT_DIR.'/primary/zondicon-flag-wonky.svg');
167+
$this->assertFileExists(static::RESULT_DIR.'/primary/camera-wonky.svg');
168+
$this->assertFileExists(static::RESULT_DIR.'/primary/foo-wonky.svg');
169+
170+
$this->clearResultsDirectory();
171+
}
172+
}

0 commit comments

Comments
 (0)