Skip to content

Commit

Permalink
Apply SVG file filter for Generator by Default (#209)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Dries Vints <[email protected]>
  • Loading branch information
3 people authored Feb 15, 2023
1 parent 3e81fa0 commit b2a80ff
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ composer.lock
vendor
phpunit.xml
.phpunit.result.cache
tests/fixtures/tmp
7 changes: 6 additions & 1 deletion src/Generation/IconGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use Symfony\Component\Finder\SplFileInfo;

final class IconGenerator
{
Expand All @@ -30,8 +31,12 @@ public function generate(): void
{
foreach ($this->sets as $set) {
$destination = $this->getDestinationDirectory($set);
$files = array_filter(
$this->filesystem->files($set['source']),
fn (SplFileInfo $value) => str_ends_with($value->getFilename(), '.svg')
);

foreach ($this->filesystem->files($set['source']) as $file) {
foreach ($files as $file) {
$filename = Str::of($file->getFilename());
$filename = $this->applyPrefixes($set, $filename);
$filename = $this->applySuffixes($set, $filename);
Expand Down
172 changes: 172 additions & 0 deletions tests/IconGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

declare(strict_types=1);

namespace Tests;

use BladeUI\Icons\Generation\IconGenerator;
use Illuminate\Filesystem\Filesystem;
use SplFileInfo;

class IconGeneratorTest extends TestCase
{
const RESULT_DIR = __DIR__.'/fixtures/tmp';

private function clearResultsDirectory(): void
{
$fs = new Filesystem();

if ($fs->isDirectory(static::RESULT_DIR)) {
$fs->deleteDirectory(static::RESULT_DIR);
}
}

/** @test */
public function it_can_generate_icon_sets()
{
IconGenerator::create([
[
'source' => __DIR__.'/resources/svg',
'destination' => static::RESULT_DIR.'/primary',
],
[
'source' => __DIR__.'/resources/svg/solid',
'destination' => static::RESULT_DIR.'/solid',
],
])->generate();

$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
$this->assertDirectoryExists(static::RESULT_DIR.'/solid');
$this->assertFileDoesNotExist(static::RESULT_DIR.'/primary/invalid-extension.txt');
$this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg');
$this->assertFileExists(static::RESULT_DIR.'/solid/camera.svg');

$this->clearResultsDirectory();
}

/** @test */
public function it_will_clear_icons_before_generation_with_safe_mode_off()
{
// Run once to generate the "OLD" icons
IconGenerator::create([
[
'source' => __DIR__.'/resources/svg',
'destination' => static::RESULT_DIR.'/primary',
],
[
'source' => __DIR__.'/resources/svg/solid',
'destination' => static::RESULT_DIR.'/solid',
],
])->generate();

$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
$this->assertDirectoryExists(static::RESULT_DIR.'/solid');
$this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg');
$this->assertFileExists(static::RESULT_DIR.'/solid/camera.svg');

// Manually insert an "OLD ICON" that's been "removed".
file_put_contents(static::RESULT_DIR.'/primary/cold-beans.svg', 'COOL BEANS!');
$this->assertFileExists(static::RESULT_DIR.'/primary/cold-beans.svg');

// Regenerate with Safe mode ON to verify "old" icons will be kept...
IconGenerator::create([
[
'source' => __DIR__.'/resources/svg',
'destination' => static::RESULT_DIR.'/primary',
'safe' => true,
],
[
'source' => __DIR__.'/resources/svg/solid',
'destination' => static::RESULT_DIR.'/solid',
'safe' => true,
],
])->generate();

$this->assertFileExists(static::RESULT_DIR.'/primary/cold-beans.svg');

// Regenerate the icons with safe feature enabled.
IconGenerator::create([
[
'source' => __DIR__.'/resources/svg',
'destination' => static::RESULT_DIR.'/primary',
],
[
'source' => __DIR__.'/resources/svg/solid',
'destination' => static::RESULT_DIR.'/solid',
],
])->generate();

$this->assertFileDoesNotExist(static::RESULT_DIR.'/primary/cold-beans.svg');

$this->clearResultsDirectory();
}

/** @test */
public function it_can_use_generator_hooks()
{
$comment = '<!-- ICONS TEST --->'.PHP_EOL;

IconGenerator::create([
[
'source' => __DIR__.'/resources/svg',
'destination' => static::RESULT_DIR.'/primary',
'after' => static function (
string $tempFilepath,
array $iconSet,
SplFileInfo $file
) use ($comment) {
$fileContents = file_get_contents($tempFilepath);
file_put_contents($tempFilepath, $comment.$fileContents);
},
],
])->generate();

$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
$this->assertFileExists(static::RESULT_DIR.'/primary/camera.svg');

$iconContent = file_get_contents(static::RESULT_DIR.'/primary/camera.svg');
$this->assertStringStartsWith($comment, $iconContent);

$this->clearResultsDirectory();
}

/** @test */
public function it_will_remove_and_apply_prefixes()
{
IconGenerator::create([
[
'source' => __DIR__.'/resources/svg',
'destination' => static::RESULT_DIR.'/primary',
'input-prefix' => 'zondicon-',
'output-prefix' => 'blade-',
],
])->generate();

$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
$this->assertFileExists(static::RESULT_DIR.'/primary/blade-flag.svg');
$this->assertFileExists(static::RESULT_DIR.'/primary/blade-camera.svg');
$this->assertFileExists(static::RESULT_DIR.'/primary/blade-foo-camera.svg');

$this->clearResultsDirectory();
}

/** @test */
public function it_will_remove_and_apply_suffixes()
{
IconGenerator::create([
[
'source' => __DIR__.'/resources/svg',
'destination' => static::RESULT_DIR.'/primary',
'input-suffix' => '-camera',
'output-suffix' => '-wonky',
],
])->generate();

$this->assertDirectoryExists(static::RESULT_DIR.'/primary');
$this->assertFileExists(static::RESULT_DIR.'/primary/zondicon-flag-wonky.svg');
$this->assertFileExists(static::RESULT_DIR.'/primary/camera-wonky.svg');
$this->assertFileExists(static::RESULT_DIR.'/primary/foo-wonky.svg');

$this->clearResultsDirectory();
}
}

0 comments on commit b2a80ff

Please sign in to comment.