diff --git a/.gitattributes b/.gitattributes index 30c2098..2e0b594 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,7 +1,6 @@ * text=auto /.github export-ignore -/bin export-ignore /tests export-ignore .gitattributes export-ignore .gitignore export-ignore diff --git a/bin/blade-icons-generate b/bin/blade-icons-generate new file mode 100755 index 0000000..0777467 --- /dev/null +++ b/bin/blade-icons-generate @@ -0,0 +1,25 @@ +#!/usr/bin/env php +setCode(function (InputInterface $input, OutputInterface $output) { + $output->writeln("Starting to generate icons..."); + + IconGenerator::create( + require getcwd().'/config/generation.php' + )->generate(); + + $output->writeln("Finished generating icons!"); + + return Command::SUCCESS; +})->run(); diff --git a/composer.json b/composer.json index 4091b06..33a2354 100644 --- a/composer.json +++ b/composer.json @@ -19,13 +19,18 @@ "illuminate/contracts": "^8.0", "illuminate/filesystem": "^8.0", "illuminate/support": "^8.0", - "illuminate/view": "^8.0" + "illuminate/view": "^8.0", + "symfony/console": "^5.3", + "symfony/finder": "^5.3" }, "require-dev": { "mockery/mockery": "^1.3", "orchestra/testbench": "^6.0", "phpunit/phpunit": "^9.0" }, + "bin": [ + "bin/blade-icons-generate" + ], "autoload": { "psr-4": { "BladeUI\\Icons\\": "src" diff --git a/src/Generation/IconGenerator.php b/src/Generation/IconGenerator.php new file mode 100644 index 0000000..6b190d2 --- /dev/null +++ b/src/Generation/IconGenerator.php @@ -0,0 +1,73 @@ +filesystem = new Filesystem(); + $this->sets = $sets; + } + + public static function create(array $config): self + { + return new self($config); + } + + public function generate(): void + { + foreach ($this->sets as $set) { + $destination = $this->getDestinationDirectory($set); + + foreach ($this->filesystem->files($set['source']) as $file) { + $filename = Str::of($file->getFilename()); + $filename = $this->applyPrefixes($set, $filename); + $pathname = $destination.$filename; + + $this->filesystem->copy($file->getRealPath(), $pathname); + + if (is_callable($set['after'] ?? null)) { + $set['after']($pathname, $set); + } + } + } + } + + private function getDestinationDirectory(array $set): string + { + $destination = Str::finish($set['destination'], DIRECTORY_SEPARATOR); + + if (! Arr::get($set, 'safe', false)) { + $this->filesystem->deleteDirectory($destination); + } + + $this->filesystem->ensureDirectoryExists($destination); + + return $destination; + } + + private function applyPrefixes($set, Stringable $filename): Stringable + { + if ($set['input-prefix'] ?? false) { + $filename = $filename->after($set['input-prefix']); + } + + if ($set['output-prefix'] ?? false) { + $filename = $filename->prepend($set['output-prefix']); + } + + return $filename; + } +}