Skip to content

Commit 3670f6f

Browse files
committed
Illuminate: wildcard/all for plugins.
1 parent f18822d commit 3670f6f

File tree

6 files changed

+146
-125
lines changed

6 files changed

+146
-125
lines changed

docs/Illuminator.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ Note: Instead of meta modifications (doc blocks, annotations) like the Annotator
77
Make sure to backup/commit your changes before running it.
88

99
Each task has its own scope defined, based on path or filename.
10-
If that doesnt match, it will be skipped.
10+
If that doesn't match, it will be skipped.
11+
12+
Use `-p PluginName` to annotate inside a plugin.
13+
14+
Tip: Use `*` wildcard to refer to a group of plugins.
15+
E.g. `-p SomePrefix/*` which are all inside your own `plugins/` directory.
16+
You can also use `all` for all app plugins.
17+
18+
For more than one plugin the command will not run into `vendor/` plugins, to avoid accidental
19+
modification there.
1120

1221
### Available tasks
1322

@@ -52,7 +61,7 @@ $query->orderDesc(Post::FIELD_PUBLISH_DATE);
5261
This allows for less typing as autocomplete finds it immediately - and for usage display (IDE => rightclick => get usage).
5362
That also means refactoring on those is much easier this way (via IDE usually a clean one-modification-refactor across the whole project).
5463

55-
Note: For PHP 7.1+ it will also add the visibility flag `public` if you don't configure it otherwise.
64+
Note: Since PHP 7.1+ it will also add the visibility flag `public` if you don't configure it otherwise.
5665

5766
This task will not clean out removed or renamed fields.
5867
You should quickly check for usage of this constant if unused it can be safely removed.

src/Command/AnnotateCommand.php

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
namespace IdeHelper\Command;
44

5-
use Cake\Command\Command;
65
use Cake\Console\Arguments;
76
use Cake\Console\ConsoleIo;
87
use Cake\Console\ConsoleOptionParser;
98
use Cake\Core\Configure;
109
use IdeHelper\Annotator\AbstractAnnotator;
1110
use IdeHelper\Console\Io;
12-
use IdeHelper\Utility\App;
13-
use IdeHelper\Utility\AppPath;
14-
use IdeHelper\Utility\Plugin;
15-
use IdeHelper\Utility\PluginPath;
1611

1712
abstract class AnnotateCommand extends Command {
1813

@@ -40,16 +35,6 @@ abstract class AnnotateCommand extends Command {
4035
*/
4136
protected array $_instantiatedAnnotators = [];
4237

43-
/**
44-
* @var \Cake\Console\Arguments
45-
*/
46-
protected Arguments $args;
47-
48-
/**
49-
* @var \Cake\Console\ConsoleIo
50-
*/
51-
protected ConsoleIo $io;
52-
5338
/**
5439
* @return void
5540
*/
@@ -73,9 +58,6 @@ public function initialize(): void {
7358
* @return int|null|void The exit code or null for success
7459
*/
7560
public function execute(Arguments $args, ConsoleIo $io) {
76-
$this->args = $args;
77-
$this->io = $io;
78-
7961
parent::execute($args, $io);
8062

8163
if ($args->getOption('ci')) {
@@ -194,84 +176,4 @@ protected function _annotatorMadeChanges(): bool {
194176
return AbstractAnnotator::$output !== false;
195177
}
196178

197-
/**
198-
* @param string|null $type
199-
* @return array<string>
200-
*/
201-
protected function getPaths(?string $type = null): array {
202-
$plugin = (string)$this->args->getOption('plugin') ?: null;
203-
if (!$plugin) {
204-
if (!$type) {
205-
return [ROOT . DS];
206-
}
207-
208-
if ($type === 'classes') {
209-
return [ROOT . DS . APP_DIR . DS];
210-
}
211-
212-
return $type === 'templates' ? App::path('templates') : AppPath::get($type);
213-
}
214-
215-
$plugins = $this->getPlugins($plugin);
216-
217-
$paths = [];
218-
foreach ($plugins as $plugin) {
219-
if (!$type) {
220-
$pluginPaths = [Plugin::path($plugin)];
221-
} else {
222-
if ($type === 'classes') {
223-
$pluginPaths = [PluginPath::classPath($plugin)];
224-
} else {
225-
$pluginPaths = $type === 'templates' ? App::path('templates', $plugin) : AppPath::get($type, $plugin);
226-
}
227-
}
228-
229-
foreach ($pluginPaths as $pluginPath) {
230-
$paths[] = $pluginPath;
231-
}
232-
}
233-
234-
return $paths;
235-
}
236-
237-
/**
238-
* @param string $plugin
239-
*
240-
* @return array<string>
241-
*/
242-
protected function getPlugins(string $plugin): array {
243-
if ($plugin !== 'all' && !str_contains($plugin, '*')) {
244-
return [Plugin::path($plugin) => $plugin];
245-
}
246-
247-
$loaded = Plugin::loaded();
248-
$plugins = [];
249-
foreach ($loaded as $name) {
250-
$path = Plugin::path($name);
251-
$rootPath = str_replace(ROOT . DS, '', $path);
252-
if (str_starts_with($rootPath, 'vendor' . DS)) {
253-
continue;
254-
}
255-
256-
$plugins[$path] = $name;
257-
}
258-
259-
if ($plugin === 'all') {
260-
return $plugins;
261-
}
262-
263-
return $this->filterPlugins($plugins, $plugin);
264-
}
265-
266-
/**
267-
* @param array<string> $plugins
268-
* @param string $pattern
269-
* @return array<string>
270-
*/
271-
protected function filterPlugins(array $plugins, string $pattern): array {
272-
return array_filter($plugins, function($plugin) use ($pattern) {
273-
return fnmatch($pattern, $plugin);
274-
});
275-
}
276-
277179
}

src/Command/Command.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace IdeHelper\Command;
4+
5+
use Cake\Command\Command as CoreCommand;
6+
use Cake\Console\Arguments;
7+
use Cake\Console\ConsoleIo;
8+
use IdeHelper\Utility\App;
9+
use IdeHelper\Utility\AppPath;
10+
use IdeHelper\Utility\Plugin;
11+
use IdeHelper\Utility\PluginPath;
12+
13+
abstract class Command extends CoreCommand {
14+
15+
/**
16+
* @var \Cake\Console\Arguments
17+
*/
18+
protected Arguments $args;
19+
20+
/**
21+
* @var \Cake\Console\ConsoleIo
22+
*/
23+
protected ConsoleIo $io;
24+
25+
/**
26+
* @param \Cake\Console\Arguments $args The command arguments.
27+
* @param \Cake\Console\ConsoleIo $io The console io
28+
* @throws \Cake\Console\Exception\StopException
29+
* @return int|null|void The exit code or null for success
30+
*/
31+
public function execute(Arguments $args, ConsoleIo $io) {
32+
$this->args = $args;
33+
$this->io = $io;
34+
35+
parent::execute($args, $io);
36+
}
37+
38+
/**
39+
* @param string|null $type
40+
* @return array<string>
41+
*/
42+
protected function getPaths(?string $type = null): array {
43+
$plugin = (string)$this->args->getOption('plugin') ?: null;
44+
if (!$plugin) {
45+
if (!$type) {
46+
return [ROOT . DS];
47+
}
48+
49+
if ($type === 'classes') {
50+
return [ROOT . DS . APP_DIR . DS];
51+
}
52+
53+
return $type === 'templates' ? App::path('templates') : AppPath::get($type);
54+
}
55+
56+
$plugins = $this->getPlugins($plugin);
57+
58+
$paths = [];
59+
foreach ($plugins as $plugin) {
60+
if (!$type) {
61+
$pluginPaths = [Plugin::path($plugin)];
62+
} else {
63+
if ($type === 'classes') {
64+
$pluginPaths = [PluginPath::classPath($plugin)];
65+
} else {
66+
$pluginPaths = $type === 'templates' ? App::path('templates', $plugin) : AppPath::get($type, $plugin);
67+
}
68+
}
69+
70+
foreach ($pluginPaths as $pluginPath) {
71+
$paths[] = $pluginPath;
72+
}
73+
}
74+
75+
return $paths;
76+
}
77+
78+
/**
79+
* @param string $plugin
80+
*
81+
* @return array<string>
82+
*/
83+
protected function getPlugins(string $plugin): array {
84+
if ($plugin !== 'all' && !str_contains($plugin, '*')) {
85+
return [Plugin::path($plugin) => $plugin];
86+
}
87+
88+
$loaded = Plugin::loaded();
89+
$plugins = [];
90+
foreach ($loaded as $name) {
91+
$path = Plugin::path($name);
92+
$rootPath = str_replace(ROOT . DS, '', $path);
93+
if (str_starts_with($rootPath, 'vendor' . DS)) {
94+
continue;
95+
}
96+
97+
$plugins[$path] = $name;
98+
}
99+
100+
if ($plugin === 'all') {
101+
return $plugins;
102+
}
103+
104+
return $this->filterPlugins($plugins, $plugin);
105+
}
106+
107+
/**
108+
* @param array<string> $plugins
109+
* @param string $pattern
110+
* @return array<string>
111+
*/
112+
protected function filterPlugins(array $plugins, string $pattern): array {
113+
return array_filter($plugins, function($plugin) use ($pattern) {
114+
return fnmatch($pattern, $plugin);
115+
});
116+
}
117+
118+
}

src/Command/IlluminateCommand.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace IdeHelper\Command;
44

5-
use Cake\Command\Command;
65
use Cake\Console\Arguments;
76
use Cake\Console\ConsoleIo;
87
use Cake\Console\ConsoleOptionParser;
9-
use Cake\Core\Plugin;
108
use IdeHelper\Console\Io;
119
use IdeHelper\Illuminator\Illuminator;
1210
use IdeHelper\Illuminator\TaskCollection;
13-
use InvalidArgumentException;
1411

1512
class IlluminateCommand extends Command {
1613

@@ -19,11 +16,6 @@ class IlluminateCommand extends Command {
1916
*/
2017
public const CODE_CHANGES = 2;
2118

22-
/**
23-
* @var \Cake\Console\ConsoleIo
24-
*/
25-
protected ConsoleIo $io;
26-
2719
/**
2820
* @return string
2921
*/
@@ -42,26 +34,26 @@ public static function getDescription(): string {
4234
* @return int The exit code or null for success
4335
*/
4436
public function execute(Arguments $args, ConsoleIo $io): int {
45-
$this->io = $io;
46-
4737
parent::execute($args, $io);
4838

49-
$path = $args->getArgument('path');
50-
if (!$path) {
51-
$path = ($args->getOption('plugin') ? 'src' : APP_DIR) . DS;
52-
}
39+
$paths = $this->getPaths();
5340

54-
$root = ROOT . DS;
55-
if ($args->getOption('plugin')) {
56-
$root = Plugin::path((string)$args->getOption('plugin'));
41+
$pathElement = $args->getArgument('path');
42+
if (!$pathElement) {
43+
$pathElement = ($args->getOption('plugin') ? 'src' : APP_DIR) . DS;
5744
}
58-
$path = $root . $path;
59-
if (!is_dir($path)) {
60-
throw new InvalidArgumentException('Path does not exist: ' . $path);
45+
46+
$filesChanged = 0;
47+
foreach ($paths as $path) {
48+
$path .= $pathElement;
49+
if (!is_dir($path)) {
50+
continue;
51+
}
52+
53+
$illuminator = $this->getIlluminator($args);
54+
$filesChanged += $illuminator->illuminate($path, (string)$args->getOption('filter') ?: null);
6155
}
6256

63-
$illuminator = $this->getIlluminator($args);
64-
$filesChanged = $illuminator->illuminate($path, (string)$args->getOption('filter') ?: null);
6557
if (!$filesChanged) {
6658
return static::CODE_SUCCESS;
6759
}
@@ -88,7 +80,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
8880
$subcommandParser = [
8981
'plugin' => [
9082
'short' => 'p',
91-
'help' => 'The plugin to run. Defaults to the application otherwise.',
83+
'help' => 'The plugin(s) to run. Defaults to the application otherwise. Supports wildcard `*` for partial match, `all` for all app plugins.',
9284
'default' => null,
9385
],
9486
'dry-run' => [

src/View/Helper/DocBlockHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function buildTableAnnotations(
139139
array $associationInfo,
140140
array $behaviors,
141141
string $entity,
142-
string $namespace,
142+
string $namespace
143143
): array {
144144
$annotations = [];
145145
foreach ($associations as $type => $assocs) {

tests/TestCase/View/Helper/DocBlockHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* DocBlockHelper Test
1414
*/
15-
#[\PHPUnit\Framework\Attributes\CoversClass(\IdeHelper\View\Helper\DocBlockHelper::class)]
15+
#[\PHPUnit\Framework\Attributes\CoversClass(DocBlockHelper::class)]
1616
class DocBlockHelperTest extends TestCase {
1717

1818
/**

0 commit comments

Comments
 (0)