Skip to content

Commit 75b66b1

Browse files
Fixed the publication of logos
1 parent 4890364 commit 75b66b1

File tree

12 files changed

+150
-105
lines changed

12 files changed

+150
-105
lines changed

app/Brands/Brand.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,36 @@ public function getFilename(): string
3131
return Str::of(static::class)->basename()->snake()->toString();
3232
}
3333

34-
public function isDetected(): bool
34+
public function isDetectedName(): bool
35+
{
36+
return $this->isDetectedComposer(['name'])
37+
|| $this->isDetectedNode(['name']);
38+
}
39+
40+
public function isDetectedDependencies(): bool
3541
{
3642
return $this->isDetectedComposer()
3743
|| $this->isDetectedNode();
3844
}
3945

40-
protected function isDetectedComposer(): bool
46+
protected function isDetectedComposer(?array $names = null): bool
4147
{
42-
return $this->search($this->filesystem->getComposer(), [
43-
'name',
48+
$names ??= [
4449
'require',
4550
'require-dev',
46-
]);
51+
];
52+
53+
return $this->search($this->filesystem->getComposer(), $names);
4754
}
4855

49-
protected function isDetectedNode(): bool
56+
protected function isDetectedNode(?array $names = null): bool
5057
{
51-
return $this->search($this->filesystem->getNode(), [
52-
'name',
58+
$names ??= [
5359
'dependencies',
5460
'devDependencies',
55-
]);
61+
];
62+
63+
return $this->search($this->filesystem->getNode(), $names);
5664
}
5765

5866
protected function search(array $dependencies, array $sections): bool

app/Commands/Command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
use DragonCode\IconifyIde\Services\ConsoleOutput;
88
use LaravelZero\Framework\Commands\Command as BaseCommand;
9+
use Override;
910
use Symfony\Component\Console\Input\InputInterface;
1011

1112
abstract class Command extends BaseCommand
1213
{
1314
protected ConsoleOutput $info;
1415

15-
#[\Override]
16+
#[Override]
1617
protected function configurePrompts(InputInterface $input): void
1718
{
1819
parent::configurePrompts($input);

app/Commands/DefaultCommand.php

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,73 @@
44

55
namespace DragonCode\IconifyIde\Commands;
66

7-
use DragonCode\Support\Facades\Filesystem\Directory;
8-
use Illuminate\Support\Str;
7+
use DirectoryIterator;
8+
use DragonCode\IconifyIde\Services\Filesystem;
99
use InvalidArgumentException;
1010

11-
use function array_unshift;
12-
use function count;
11+
use function config;
1312
use function file_exists;
1413
use function filled;
14+
use function in_array;
1515
use function is_dir;
1616
use function realpath;
1717

1818
class DefaultCommand extends Command
1919
{
2020
protected $signature = 'default'
21-
. ' {--all : Publishing files in all projects of the current directory}'
22-
. ' {--path= : Indicates the way to the search directory}';
21+
. ' {--all : Publishing files in all projects of the current directory}'
22+
. ' {--path= : Indicates the way to the search directory}';
2323

2424
protected $description = 'Publishes icons to improve project display in IDE';
2525

2626
public function handle(): void
2727
{
28-
$count = count($directories = $this->getDirectories());
28+
if ($this->hasAll()) {
29+
$this->info->title('Recursive search for projects ...');
2930

30-
$this->process($directories, $count);
31-
}
31+
$this->processWithStatus($this->getPath());
32+
$this->processMany($this->getPath(), new Filesystem($this->getPath()));
3233

33-
protected function process(array $directories, int $count): void
34-
{
35-
$count === 1
36-
? $this->processOnce($directories[0])
37-
: $this->processMany($directories);
34+
return;
35+
}
36+
37+
$this->processOnce($this->getPath());
3838
}
3939

40-
protected function processOnce(string $directory): void
40+
protected function processOnce(string $path, bool $silent = false): int
4141
{
42-
$this->call(PublishCommand::class, ['--path' => $directory]);
42+
return $silent
43+
? $this->callSilent(PublishCommand::class, ['--path' => $path])
44+
: $this->call(PublishCommand::class, ['--path' => $path]);
4345
}
4446

45-
protected function processMany(array $directories): void
47+
protected function processMany(string $path, Filesystem $files): void
4648
{
47-
$this->info->title('Recursive search for projects ...');
49+
foreach ($files->directory($path) as $directory) {
50+
if ($this->skip($directory)) {
51+
continue;
52+
}
4853

49-
foreach ($directories as $directory) {
50-
$status = $this->callSilent(PublishCommand::class, ['--path' => $directory]);
54+
$this->processWithStatus($directory->getRealPath());
5155

52-
$this->info->status($directory, $status);
56+
if (! $directory->isDot()) {
57+
$this->processMany($directory->getRealPath(), new Filesystem($directory->getRealPath()));
58+
}
5359
}
5460
}
5561

56-
protected function getDirectories(): array
62+
protected function processWithStatus(string $path): void
5763
{
58-
if (! $this->hasAll()) {
59-
return [$this->getPath()];
60-
}
61-
62-
$paths = Directory::allPaths($this->getPath(), static function (string $path) {
63-
return Str::doesntContain($path, ['.git', 'vendor', 'node_modules', 'tests']);
64-
}, recursive: true);
64+
$this->info->status($path, $this->processOnce($path, true));
65+
}
6566

66-
array_unshift($paths, realpath($this->getPath()));
67+
protected function skip(DirectoryIterator $directory): bool
68+
{
69+
if (! $directory->isDir() || $directory->isDot()) {
70+
return true;
71+
}
6772

68-
return $paths;
73+
return (bool) (in_array($directory->getBasename(), config('data.exclude'), true));
6974
}
7075

7176
protected function getPath(): string
@@ -77,11 +82,6 @@ protected function getPath(): string
7782
return '.';
7883
}
7984

80-
protected function hasAll(): bool
81-
{
82-
return $this->option('all');
83-
}
84-
8585
protected function validatePath(string $path): string
8686
{
8787
if (! file_exists($path)) {
@@ -92,6 +92,11 @@ protected function validatePath(string $path): string
9292
throw new InvalidArgumentException("The specified path is not a directory ($path).");
9393
}
9494

95-
return $path;
95+
return realpath($path);
96+
}
97+
98+
protected function hasAll(): bool
99+
{
100+
return $this->option('all');
96101
}
97102
}

app/Commands/PublishCommand.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace DragonCode\IconifyIde\Commands;
66

7+
use Closure;
8+
use DragonCode\IconifyIde\Brands\Brand;
79
use DragonCode\IconifyIde\Ide\Ide;
10+
use DragonCode\IconifyIde\Services\Filesystem;
811
use DragonCode\IconifyIde\Services\Publisher;
912
use Symfony\Component\Console\Attribute\AsCommand;
1013

@@ -19,16 +22,18 @@ class PublishCommand extends Command
1922
public const SKIPPED = 1;
2023

2124
protected $signature = 'publish'
22-
. ' {--path= : Indicates the way to the search directory}';
25+
. ' {--path= : Indicates the way to the search directory}';
2326

2427
protected $description = 'Publishes icons to improve project display in IDE';
2528

29+
protected ?Filesystem $files = null;
30+
2631
protected bool $isPublished = false;
2732

28-
public function handle(Publisher $publisher): int
33+
public function handle(): int
2934
{
30-
foreach ($this->ide() as $class) {
31-
$ide = $this->initializeIde($class);
35+
foreach ($this->ide() as $ideClass) {
36+
$ide = $this->initializeIde($ideClass);
3237

3338
$this->info->title($ide->getName());
3439

@@ -38,33 +43,45 @@ public function handle(Publisher $publisher): int
3843
continue;
3944
}
4045

41-
$published = false;
46+
if ($this->detect($ide, fn (Brand $brand) => $brand->isDetectedName())) {
47+
continue;
48+
}
4249

43-
foreach ($ide->getBrands() as $brand) {
44-
if ($published) {
45-
$this->info->skipped($brand);
50+
$this->detect($ide, fn (Brand $brand) => $brand->isDetectedDependencies());
51+
}
4652

47-
continue;
48-
}
53+
return $this->isPublished
54+
? static::PUBLISHED
55+
: static::SKIPPED;
56+
}
4957

50-
if (! $brand->isDetected()) {
51-
$this->info->notFound($brand);
58+
protected function detect(Ide $ide, Closure $when, bool $isPublished = false): bool
59+
{
60+
foreach ($ide->getBrands() as $brandClass) {
61+
$brand = $this->initializeBrand($brandClass);
5262

53-
continue;
54-
}
63+
if ($isPublished) {
64+
$this->info->skipped($brand);
5565

56-
$publisher->publish($ide, $brand, $this->getPath());
57-
$this->info->published($brand);
66+
continue;
67+
}
5868

59-
$published = true;
69+
if (! $when($brand)) {
70+
$this->info->notFound($brand);
6071

61-
$this->isPublished = true;
72+
continue;
6273
}
74+
75+
(new Publisher($this->getFilesystem()))->publish($ide, $brand);
76+
77+
$this->info->published($brand);
78+
79+
$isPublished = true;
80+
81+
$this->isPublished = true;
6382
}
6483

65-
return $this->isPublished
66-
? static::PUBLISHED
67-
: static::SKIPPED;
84+
return $isPublished;
6885
}
6986

7087
protected function hasIde(Ide $ide): bool
@@ -74,7 +91,12 @@ protected function hasIde(Ide $ide): bool
7491

7592
protected function initializeIde(string $ide): Ide
7693
{
77-
return $this->app->make($ide);
94+
return new $ide($this->getFilesystem());
95+
}
96+
97+
protected function initializeBrand(string $brand): Brand
98+
{
99+
return new $brand($this->getFilesystem());
78100
}
79101

80102
protected function ide(): array
@@ -90,4 +112,9 @@ protected function getPath(): string
90112

91113
return realpath('.');
92114
}
115+
116+
protected function getFilesystem(): Filesystem
117+
{
118+
return $this->files ??= new Filesystem($this->getPath());
119+
}
93120
}

app/Helpers/Init.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/Ide/Ide.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace DragonCode\IconifyIde\Ide;
66

7-
use DragonCode\IconifyIde\Brands\Brand;
87
use DragonCode\IconifyIde\Contracts\Named;
9-
use DragonCode\IconifyIde\Helpers\Init;
108
use Illuminate\Support\Str;
119

1210
use function config;
@@ -32,11 +30,8 @@ public function getFilename(): string
3230
return $this->filename;
3331
}
3432

35-
/**
36-
* @return Brand[]
37-
*/
3833
public function getBrands(): array
3934
{
40-
return Init::classes(config('data.brands'));
35+
return config('data.brands');
4136
}
4237
}

app/Services/Filesystem.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DragonCode\IconifyIde\Services;
66

7+
use DirectoryIterator;
78
use DragonCode\Support\Facades\Filesystem\Directory;
89

910
use function copy;
@@ -18,21 +19,33 @@ class Filesystem
1819
{
1920
protected array $registry = [];
2021

22+
public function __construct(
23+
protected readonly string $directory
24+
) {}
25+
26+
/**
27+
* @return DirectoryIterator|array<DirectoryIterator>
28+
*/
29+
public function directory(string $path): DirectoryIterator
30+
{
31+
return Directory::all($path);
32+
}
33+
2134
public function copy(string $source, string $target): void
2235
{
2336
Directory::ensureDirectory(dirname($target));
2437

25-
copy($source, $target);
38+
copy($source, $this->directory . '/' . $target);
2639
}
2740

2841
public function getComposer(): array
2942
{
30-
return $this->registry['composer'] ??= $this->load('composer.json');
43+
return $this->registry['composer'] ??= $this->load($this->directory . '/composer.json');
3144
}
3245

3346
public function getNode(): array
3447
{
35-
return $this->registry['node'] ??= $this->load('package.json');
48+
return $this->registry['node'] ??= $this->load($this->directory . '/package.json');
3649
}
3750

3851
protected function load(string $filename): array

0 commit comments

Comments
 (0)