Skip to content

Commit

Permalink
Wildcard for plugin option.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 22, 2024
1 parent 63e869a commit 9fd0a86
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/phpunit.phar
/.phpunit.result.cache
/.phpunit.cache/
/vendor/
/tmp/
/composer.lock
Expand Down
3 changes: 3 additions & 0 deletions docs/Annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ You get autocompletion on any `$this->Apples->...()` usage in your controllers t

Use `-p PluginName` to annotate inside a plugin. It will then use the plugin name as namespace.

Tip: Use `*` wildcard to refer to a group of plugins. Make sure to only touch internal plugins (in version control), however.
E.g. `-p SomePrefix/*` which are all inside your own `plugins/` directory - and not in `vendor/`.

### Primary model via $modelClass definition
When defining `$modelClass` it will be used instead:
```php
Expand Down
14 changes: 6 additions & 8 deletions src/Command/Annotate/CallbacksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use IdeHelper\Annotator\CallbackAnnotator;
use IdeHelper\Command\AnnotateCommand;

Expand Down Expand Up @@ -38,13 +37,12 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;

$path = $plugin ? Plugin::classPath($plugin) : ROOT . DS . APP_DIR . DS;

$folders = glob($path . '*', GLOB_ONLYDIR) ?: [];
foreach ($folders as $folder) {
$this->_callbacks($folder . DS);
$paths = $this->getPaths();
foreach ($paths as $path) {
$folders = glob($path . '*', GLOB_ONLYDIR) ?: [];
foreach ($folders as $folder) {
$this->_callbacks($folder . DS);
}
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
31 changes: 15 additions & 16 deletions src/Command/Annotate/ClassesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use IdeHelper\Annotator\ClassAnnotator;
use IdeHelper\Annotator\ClassAnnotatorTask\TestClassAnnotatorTask;
use IdeHelper\Annotator\ClassAnnotatorTaskCollection;
Expand Down Expand Up @@ -40,13 +39,12 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;

$path = $plugin ? Plugin::classPath($plugin) : ROOT . DS . APP_DIR . DS;

$folders = glob($path . '*', GLOB_ONLYDIR) ?: [];
foreach ($folders as $folder) {
$this->_classes($folder . DS);
$paths = $this->getPaths();
foreach ($paths as $path) {
$folders = glob($path . '*', GLOB_ONLYDIR) ?: [];
foreach ($folders as $folder) {
$this->_classes($folder . DS);
}
}

$collection = new ClassAnnotatorTaskCollection();
Expand All @@ -55,15 +53,16 @@ public function execute(Arguments $args, ConsoleIo $io): int {
return static::CODE_SUCCESS;
}

$path = $plugin ? Plugin::path($plugin) : ROOT . DS;
$path .= 'tests' . DS . 'TestCase' . DS;
if (!is_dir($path)) {
return static::CODE_SUCCESS;
}
foreach ($paths as $path) {
$path .= 'tests' . DS . 'TestCase' . DS;
if (!is_dir($path)) {
continue;
}

$folders = glob($path . '*', GLOB_ONLYDIR) ?: [];
foreach ($folders as $folder) {
$this->_classes($folder . DS);
$folders = glob($path . '*', GLOB_ONLYDIR) ?: [];
foreach ($folders as $folder) {
$this->_classes($folder . DS);
}
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
8 changes: 3 additions & 5 deletions src/Command/Annotate/CommandsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Core\App;
use IdeHelper\Annotator\CommandAnnotator;
use IdeHelper\Command\AnnotateCommand;

Expand All @@ -25,11 +24,10 @@ public static function getDescription(): string {
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;
$folders = App::classPath('Command', $plugin);
$paths = $this->getPaths('Command');

foreach ($folders as $folder) {
$this->_commands($folder);
foreach ($paths as $path) {
$this->_commands($path);
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
9 changes: 3 additions & 6 deletions src/Command/Annotate/ComponentsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Core\App;
use IdeHelper\Annotator\ComponentAnnotator;
use IdeHelper\Command\AnnotateCommand;

Expand All @@ -25,11 +24,9 @@ public static function getDescription(): string {
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;
$folders = App::classPath('Controller/Component', $plugin);

foreach ($folders as $folder) {
$this->_components($folder);
$paths = $this->getPaths('Controller/Component');
foreach ($paths as $path) {
$this->_components($path);
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
8 changes: 3 additions & 5 deletions src/Command/Annotate/ControllersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Core\App;
use Cake\Core\Configure;
use IdeHelper\Annotator\ControllerAnnotator;
use IdeHelper\Command\AnnotateCommand;
Expand All @@ -26,11 +25,10 @@ public static function getDescription(): string {
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;
$folders = App::classPath('Controller', $plugin);
$paths = $this->getPaths('Controller');

foreach ($folders as $folder) {
$this->_controllers($folder);
foreach ($paths as $path) {
$this->_controllers($path);
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
9 changes: 3 additions & 6 deletions src/Command/Annotate/HelpersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Core\App;
use IdeHelper\Annotator\HelperAnnotator;
use IdeHelper\Command\AnnotateCommand;

Expand All @@ -25,11 +24,9 @@ public static function getDescription(): string {
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;
$folders = App::classPath('View/Helper', $plugin);

foreach ($folders as $folder) {
$this->_helpers($folder);
$paths = $this->getPaths('View/Helper');
foreach ($paths as $path) {
$this->_helpers($path);
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
9 changes: 3 additions & 6 deletions src/Command/Annotate/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Core\App;
use IdeHelper\Annotator\ModelAnnotator;
use IdeHelper\Command\AnnotateCommand;

Expand All @@ -25,11 +24,9 @@ public static function getDescription(): string {
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;
$folders = App::classPath('Model/Table', $plugin);

foreach ($folders as $folder) {
$this->_models($folder);
$paths = $this->getPaths('Model/Table');
foreach ($paths as $path) {
$this->_models($path);
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
8 changes: 3 additions & 5 deletions src/Command/Annotate/TemplatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Core\App;
use IdeHelper\Annotator\TemplateAnnotator;
use IdeHelper\Command\AnnotateCommand;

Expand All @@ -25,11 +24,10 @@ public static function getDescription(): string {
public function execute(Arguments $args, ConsoleIo $io): int {
parent::execute($args, $io);

$plugin = (string)$args->getOption('plugin') ?: null;
$folders = App::path('templates', $plugin);
$paths = $this->getPaths('templates');

foreach ($folders as $folder) {
$this->_templates($folder);
foreach ($paths as $path) {
$this->_templates($path);
}

if ($args->getOption('ci') && $this->_annotatorMadeChanges()) {
Expand Down
69 changes: 67 additions & 2 deletions src/Command/AnnotateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Cake\Core\Configure;
use IdeHelper\Annotator\AbstractAnnotator;
use IdeHelper\Console\Io;
use IdeHelper\Utility\App;
use IdeHelper\Utility\AppPath;
use IdeHelper\Utility\Plugin;
use IdeHelper\Utility\PluginPath;

abstract class AnnotateCommand extends Command {

Expand Down Expand Up @@ -96,7 +100,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
],
'plugin' => [
'short' => 'p',
'help' => 'The plugin to run. Defaults to the application otherwise.',
'help' => 'The plugin(s) to run. Defaults to the application otherwise. Supports wildcard `*` for partial match.',
'default' => null,
],
'remove' => [
Expand Down Expand Up @@ -147,7 +151,7 @@ protected function _shouldSkip(string $fileName): bool {
return false;
}

return !(bool)preg_match('/' . preg_quote($filter, '/') . '/i', $fileName);
return !preg_match('/' . preg_quote($filter, '/') . '/i', $fileName);
}

/**
Expand Down Expand Up @@ -190,4 +194,65 @@ protected function _annotatorMadeChanges(): bool {
return AbstractAnnotator::$output !== false;
}

/**
* @param string|null $type
* @return array<string>
*/
protected function getPaths(?string $type = null): array {
$plugin = (string)$this->args->getOption('plugin') ?: null;
if (!$plugin) {
if (!$type) {
return [ROOT . DS . APP_DIR . DS];
}

return $type === 'templates' ? App::path('templates') : AppPath::get($type);
}

$plugins = $this->getPlugins($plugin);

$paths = [];
foreach ($plugins as $plugin) {
if (!$type) {
$pluginPaths = [PluginPath::classPath($plugin)];
} else {
$pluginPaths = $type === 'templates' ? App::path('templates', $plugin) : AppPath::get($type, $plugin);
}
foreach ($pluginPaths as $pluginPath) {
$paths[] = $pluginPath;
}
}

return $paths;
}

/**
* @param string $plugin
*
* @return array<string>
*/
protected function getPlugins(string $plugin): array {
if (!str_contains($plugin, '*')) {
return [$plugin];
}

$loaded = Plugin::loaded();
$plugins = [];
foreach ($loaded as $name) {
$plugins[Plugin::path($name)] = $name;
}

return $this->filterPlugins($plugins, $plugin);
}

/**
* @param array<string> $plugins
* @param string $pattern
* @return array<string>
*/
protected function filterPlugins(array $plugins, string $pattern): array {
return array_filter($plugins, function($plugin, $path) use ($pattern) {
return fnmatch($pattern, $plugin);
}, ARRAY_FILTER_USE_BOTH);
}

}

0 comments on commit 9fd0a86

Please sign in to comment.