Skip to content

Commit

Permalink
Illuminate: wildcard/all for plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 22, 2024
1 parent f18822d commit 3670f6f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 125 deletions.
13 changes: 11 additions & 2 deletions docs/Illuminator.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ Note: Instead of meta modifications (doc blocks, annotations) like the Annotator
Make sure to backup/commit your changes before running it.

Each task has its own scope defined, based on path or filename.
If that doesnt match, it will be skipped.
If that doesn't match, it will be skipped.

Use `-p PluginName` to annotate inside a plugin.

Tip: Use `*` wildcard to refer to a group of plugins.
E.g. `-p SomePrefix/*` which are all inside your own `plugins/` directory.
You can also use `all` for all app plugins.

For more than one plugin the command will not run into `vendor/` plugins, to avoid accidental
modification there.

### Available tasks

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

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

This task will not clean out removed or renamed fields.
You should quickly check for usage of this constant if unused it can be safely removed.
Expand Down
98 changes: 0 additions & 98 deletions src/Command/AnnotateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

namespace IdeHelper\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
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 @@ -40,16 +35,6 @@ abstract class AnnotateCommand extends Command {
*/
protected array $_instantiatedAnnotators = [];

/**
* @var \Cake\Console\Arguments
*/
protected Arguments $args;

/**
* @var \Cake\Console\ConsoleIo
*/
protected ConsoleIo $io;

/**
* @return void
*/
Expand All @@ -73,9 +58,6 @@ public function initialize(): void {
* @return int|null|void The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io) {
$this->args = $args;
$this->io = $io;

parent::execute($args, $io);

if ($args->getOption('ci')) {
Expand Down Expand Up @@ -194,84 +176,4 @@ 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];
}

if ($type === 'classes') {
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 = [Plugin::path($plugin)];
} else {
if ($type === 'classes') {
$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 ($plugin !== 'all' && !str_contains($plugin, '*')) {
return [Plugin::path($plugin) => $plugin];
}

$loaded = Plugin::loaded();
$plugins = [];
foreach ($loaded as $name) {
$path = Plugin::path($name);
$rootPath = str_replace(ROOT . DS, '', $path);
if (str_starts_with($rootPath, 'vendor' . DS)) {
continue;
}

$plugins[$path] = $name;
}

if ($plugin === 'all') {
return $plugins;
}

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) use ($pattern) {
return fnmatch($pattern, $plugin);
});
}

}
118 changes: 118 additions & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace IdeHelper\Command;

use Cake\Command\Command as CoreCommand;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use IdeHelper\Utility\App;
use IdeHelper\Utility\AppPath;
use IdeHelper\Utility\Plugin;
use IdeHelper\Utility\PluginPath;

abstract class Command extends CoreCommand {

/**
* @var \Cake\Console\Arguments
*/
protected Arguments $args;

/**
* @var \Cake\Console\ConsoleIo
*/
protected ConsoleIo $io;

/**
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @throws \Cake\Console\Exception\StopException
* @return int|null|void The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io) {
$this->args = $args;
$this->io = $io;

parent::execute($args, $io);
}

/**
* @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];
}

if ($type === 'classes') {
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 = [Plugin::path($plugin)];
} else {
if ($type === 'classes') {
$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 ($plugin !== 'all' && !str_contains($plugin, '*')) {
return [Plugin::path($plugin) => $plugin];
}

$loaded = Plugin::loaded();
$plugins = [];
foreach ($loaded as $name) {
$path = Plugin::path($name);
$rootPath = str_replace(ROOT . DS, '', $path);
if (str_starts_with($rootPath, 'vendor' . DS)) {
continue;
}

$plugins[$path] = $name;
}

if ($plugin === 'all') {
return $plugins;
}

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) use ($pattern) {
return fnmatch($pattern, $plugin);
});
}

}
38 changes: 15 additions & 23 deletions src/Command/IlluminateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace IdeHelper\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Plugin;
use IdeHelper\Console\Io;
use IdeHelper\Illuminator\Illuminator;
use IdeHelper\Illuminator\TaskCollection;
use InvalidArgumentException;

class IlluminateCommand extends Command {

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

/**
* @var \Cake\Console\ConsoleIo
*/
protected ConsoleIo $io;

/**
* @return string
*/
Expand All @@ -42,26 +34,26 @@ public static function getDescription(): string {
* @return int The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io): int {
$this->io = $io;

parent::execute($args, $io);

$path = $args->getArgument('path');
if (!$path) {
$path = ($args->getOption('plugin') ? 'src' : APP_DIR) . DS;
}
$paths = $this->getPaths();

$root = ROOT . DS;
if ($args->getOption('plugin')) {
$root = Plugin::path((string)$args->getOption('plugin'));
$pathElement = $args->getArgument('path');
if (!$pathElement) {
$pathElement = ($args->getOption('plugin') ? 'src' : APP_DIR) . DS;
}
$path = $root . $path;
if (!is_dir($path)) {
throw new InvalidArgumentException('Path does not exist: ' . $path);

$filesChanged = 0;
foreach ($paths as $path) {
$path .= $pathElement;
if (!is_dir($path)) {
continue;
}

$illuminator = $this->getIlluminator($args);
$filesChanged += $illuminator->illuminate($path, (string)$args->getOption('filter') ?: null);
}

$illuminator = $this->getIlluminator($args);
$filesChanged = $illuminator->illuminate($path, (string)$args->getOption('filter') ?: null);
if (!$filesChanged) {
return static::CODE_SUCCESS;
}
Expand All @@ -88,7 +80,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
$subcommandParser = [
'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, `all` for all app plugins.',
'default' => null,
],
'dry-run' => [
Expand Down
2 changes: 1 addition & 1 deletion src/View/Helper/DocBlockHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function buildTableAnnotations(
array $associationInfo,
array $behaviors,
string $entity,
string $namespace,
string $namespace
): array {
$annotations = [];
foreach ($associations as $type => $assocs) {
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Helper/DocBlockHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* DocBlockHelper Test
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\IdeHelper\View\Helper\DocBlockHelper::class)]
#[\PHPUnit\Framework\Attributes\CoversClass(DocBlockHelper::class)]
class DocBlockHelperTest extends TestCase {

/**
Expand Down

0 comments on commit 3670f6f

Please sign in to comment.