Skip to content

Commit 4980647

Browse files
Added recursive folder check
1 parent 5ca991b commit 4980647

File tree

8 files changed

+301
-110
lines changed

8 files changed

+301
-110
lines changed

app/Commands/Command.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\IconifyIde\Commands;
6+
7+
use DragonCode\IconifyIde\Services\ConsoleOutput;
8+
use LaravelZero\Framework\Commands\Command as BaseCommand;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
11+
abstract class Command extends BaseCommand
12+
{
13+
protected ConsoleOutput $info;
14+
15+
#[\Override]
16+
protected function configurePrompts(InputInterface $input): void
17+
{
18+
parent::configurePrompts($input);
19+
20+
$this->info = new ConsoleOutput($this->components);
21+
}
22+
}

app/Commands/DefaultCommand.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\IconifyIde\Commands;
6+
7+
use DragonCode\Support\Facades\Filesystem\Directory;
8+
use Illuminate\Support\Str;
9+
use InvalidArgumentException;
10+
11+
use function array_unshift;
12+
use function count;
13+
use function file_exists;
14+
use function filled;
15+
use function is_dir;
16+
use function realpath;
17+
18+
class DefaultCommand extends Command
19+
{
20+
protected $signature = 'default'
21+
. ' {--all : Publishing files in all projects of the current directory}'
22+
. ' {--path= : Indicates the way to the search directory}';
23+
24+
protected $description = 'Publishes icons to improve project display in IDE';
25+
26+
public function handle(): void
27+
{
28+
$count = count($directories = $this->getDirectories());
29+
30+
$this->process($directories, $count);
31+
}
32+
33+
protected function process(array $directories, int $count): void
34+
{
35+
$count === 1
36+
? $this->processOnce($directories[0])
37+
: $this->processMany($directories);
38+
}
39+
40+
protected function processOnce(string $directory): void
41+
{
42+
$this->call(PublishCommand::class, ['--path' => $directory]);
43+
}
44+
45+
protected function processMany(array $directories): void
46+
{
47+
$this->info->title('Recursive search for projects ...');
48+
49+
foreach ($directories as $directory) {
50+
$status = $this->callSilent(PublishCommand::class, ['--path' => $directory]);
51+
52+
$this->info->status($directory, $status);
53+
}
54+
}
55+
56+
protected function getDirectories(): array
57+
{
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);
65+
66+
array_unshift($paths, realpath($this->getPath()));
67+
68+
return $paths;
69+
}
70+
71+
protected function getPath(): string
72+
{
73+
if (filled($path = $this->option('path'))) {
74+
return $this->validatePath($path);
75+
}
76+
77+
return '.';
78+
}
79+
80+
protected function hasAll(): bool
81+
{
82+
return $this->option('all');
83+
}
84+
85+
protected function validatePath(string $path): string
86+
{
87+
if (! file_exists($path)) {
88+
throw new InvalidArgumentException("The directory does not exist ($path).");
89+
}
90+
91+
if (! is_dir($path)) {
92+
throw new InvalidArgumentException("The specified path is not a directory ($path).");
93+
}
94+
95+
return $path;
96+
}
97+
}

app/Commands/PublishCommand.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,36 @@
44

55
namespace DragonCode\IconifyIde\Commands;
66

7-
use DragonCode\IconifyIde\Contracts\Named;
87
use DragonCode\IconifyIde\Ide\Ide;
98
use DragonCode\IconifyIde\Services\Publisher;
10-
use LaravelZero\Framework\Commands\Command;
119
use Symfony\Component\Console\Attribute\AsCommand;
1210

1311
use function config;
1412
use function is_dir;
13+
use function realpath;
1514

16-
#[AsCommand('iconify')]
15+
#[AsCommand('publish')]
1716
class PublishCommand extends Command
1817
{
19-
protected $signature = 'iconify'
20-
. ' {--all : Publishing files in all projects of the current directory. Maximum depth is 3}';
18+
public const PUBLISHED = 0;
19+
public const SKIPPED = 1;
20+
21+
protected $signature = 'publish'
22+
. ' {--path= : Indicates the way to the search directory}';
2123

2224
protected $description = 'Publishes icons to improve project display in IDE';
2325

24-
public function handle(Publisher $publisher): void
26+
protected bool $isPublished = false;
27+
28+
public function handle(Publisher $publisher): int
2529
{
2630
foreach ($this->ide() as $class) {
2731
$ide = $this->initializeIde($class);
2832

29-
$this->components->info($ide->getName());
33+
$this->info->title($ide->getName());
3034

3135
if (! $this->hasIde($ide)) {
32-
$this->infoDetails($ide, 'NOT FOUND', 'comment');
36+
$this->info->notFound($ide);
3337

3438
continue;
3539
}
@@ -38,29 +42,34 @@ public function handle(Publisher $publisher): void
3842

3943
foreach ($ide->getBrands() as $brand) {
4044
if ($published) {
41-
$this->infoDetails($brand, 'SKIPPED', 'comment');
45+
$this->info->skipped($brand);
4246

4347
continue;
4448
}
4549

4650
if (! $brand->isDetected()) {
47-
$this->infoDetails($brand, 'NOT FOUND', 'comment');
51+
$this->info->notFound($brand);
4852

4953
continue;
5054
}
5155

52-
$publisher->publish($ide, $brand);
53-
54-
$this->infoDetails($brand, 'PUBLISHED', 'info');
56+
$publisher->publish($ide, $brand, $this->getPath());
57+
$this->info->published($brand);
5558

5659
$published = true;
60+
61+
$this->isPublished = true;
5762
}
5863
}
64+
65+
return $this->isPublished
66+
? static::PUBLISHED
67+
: static::SKIPPED;
5968
}
6069

6170
protected function hasIde(Ide $ide): bool
6271
{
63-
return is_dir('./' . $ide->getDirectoryName());
72+
return is_dir($this->getPath() . '/' . $ide->getDirectoryName());
6473
}
6574

6675
protected function initializeIde(string $ide): Ide
@@ -73,8 +82,12 @@ protected function ide(): array
7382
return config('data.ide');
7483
}
7584

76-
protected function infoDetails(Named $instance, string $status, string $style): void
85+
protected function getPath(): string
7786
{
78-
$this->components->twoColumnDetail($instance->getName(), "<$style>$status</>");
87+
if ($this->hasOption('path')) {
88+
return $this->option('path');
89+
}
90+
91+
return realpath('.');
7992
}
8093
}

app/Services/ConsoleOutput.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\IconifyIde\Services;
6+
7+
use DragonCode\IconifyIde\Commands\PublishCommand;
8+
use DragonCode\IconifyIde\Contracts\Named;
9+
use Illuminate\Console\View\Components\Factory;
10+
11+
class ConsoleOutput
12+
{
13+
public function __construct(
14+
protected Factory $output
15+
) {}
16+
17+
public function title(string $title): void
18+
{
19+
$this->output->info($title);
20+
}
21+
22+
public function notFound(Named|string $title): void
23+
{
24+
$this->columns($title, 'NOT FOUND', 'comment');
25+
}
26+
27+
public function skipped(Named|string $title): void
28+
{
29+
$this->columns($title, 'SKIPPED', 'comment');
30+
}
31+
32+
public function published(Named|string $title): void
33+
{
34+
$this->columns($title, 'PUBLISHED', 'info');
35+
}
36+
37+
public function status(string $title, int $code): void
38+
{
39+
$status = match ($code) {
40+
PublishCommand::PUBLISHED => 'PUBLISHED',
41+
PublishCommand::SKIPPED => 'NOT FOUND',
42+
};
43+
44+
$style = match ($code) {
45+
PublishCommand::PUBLISHED => 'info',
46+
PublishCommand::SKIPPED => 'comment',
47+
};
48+
49+
$this->columns($title, $status, $style);
50+
}
51+
52+
protected function columns(Named|string $title, string $status, string $style): void
53+
{
54+
if ($title instanceof Named) {
55+
$title = $title->getName();
56+
}
57+
58+
$this->output->twoColumnDetail($title, "<$style>$status</>");
59+
}
60+
}

app/Services/Filesystem.php

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

55
namespace DragonCode\IconifyIde\Services;
66

7+
use DragonCode\Support\Facades\Filesystem\Directory;
8+
79
use function copy;
10+
use function dirname;
811
use function file_exists;
912
use function file_get_contents;
1013
use function json_decode;
@@ -17,6 +20,8 @@ class Filesystem
1720

1821
public function copy(string $source, string $target): void
1922
{
23+
Directory::ensureDirectory(dirname($target));
24+
2025
copy($source, $target);
2126
}
2227

app/Services/Publisher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public function __construct(
1313
protected Filesystem $files
1414
) {}
1515

16-
public function publish(Ide $ide, Brand $brand): void
16+
public function publish(Ide $ide, Brand $brand, string $path): void
1717
{
1818
$this->files->copy(
1919
$this->sourcePath($brand),
20-
$this->targetPath($ide)
20+
$this->targetPath($ide, $path)
2121
);
2222
}
2323

@@ -26,8 +26,8 @@ protected function sourcePath(Brand $brand): string
2626
return __DIR__ . '/../../resources/brands/' . $brand->getFilename() . '.svg';
2727
}
2828

29-
protected function targetPath(Ide $ide): string
29+
protected function targetPath(Ide $ide, string $path): string
3030
{
31-
return './' . $ide->getDirectoryName() . '/' . $ide->getFilename();
31+
return $path . '/' . $ide->getDirectoryName() . '/' . $ide->getFilename();
3232
}
3333
}

0 commit comments

Comments
 (0)