Skip to content

Commit a21e44c

Browse files
committed
Make plugin selection in CLI easier
1 parent 98a1530 commit a21e44c

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

modules/system/console/PluginList.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php namespace System\Console;
22

3-
use Symfony\Component\Console\Helper\Table;
4-
use Symfony\Component\Console\Helper\TableSeparator;
53
use System\Models\PluginVersion;
64
use Winter\Storm\Console\Command;
75

@@ -43,7 +41,7 @@ public function handle()
4341
}
4442

4543
$rows = [];
46-
foreach ($allPlugins as $plugin) {
44+
foreach ($allPlugins->sortBy('code') as $plugin) {
4745
$rows[] = [
4846
$plugin->code,
4947
$plugin->version,

modules/system/console/traits/HasPluginArgument.php

+55-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
use InvalidArgumentException;
44
use System\Classes\PluginBase;
55
use System\Classes\PluginManager;
6+
use Throwable;
7+
use Winter\Storm\Support\Arr;
8+
9+
use function Laravel\Prompts\search;
10+
use function Laravel\Prompts\select;
611

712
/**
813
* Console Command Trait that provides autocompletion for the "plugin" argument
@@ -22,6 +27,8 @@ trait HasPluginArgument
2227
*/
2328
// protected $validatePluginInput = true;
2429

30+
protected ?PluginBase $plugin = null;
31+
2532
/**
2633
* Return available plugins for autocompletion of the "plugin" argument
2734
*/
@@ -45,7 +52,33 @@ public function suggestPluginValues()
4552
}
4653
}
4754

48-
return $plugins;
55+
return Arr::sort($plugins);
56+
}
57+
58+
/**
59+
* Prompt for which provider or tag to publish.
60+
*/
61+
protected function promptForPlugin(): ?string
62+
{
63+
$choices = $this->suggestPluginValues();
64+
65+
$choice = windows_os()
66+
? select(
67+
"Which plugin would you like to select?",
68+
$choices,
69+
scroll: 15,
70+
)
71+
: search(
72+
label: "Which plugin would you like to select?",
73+
placeholder: 'Search...',
74+
options: fn ($search) => array_values(array_filter(
75+
$choices,
76+
fn ($choice) => str_contains(strtolower($choice), strtolower($search))
77+
)),
78+
scroll: 15,
79+
);
80+
81+
return $choice;
4982
}
5083

5184
/**
@@ -55,7 +88,7 @@ public function suggestPluginValues()
5588
public function getPluginIdentifier($identifier = null): string
5689
{
5790
$pluginManager = PluginManager::instance();
58-
$pluginName = $identifier ?? $this->argument('plugin');
91+
$pluginName = $identifier ?? $this->argument('plugin') ?? $this->promptForPlugin();
5992
$pluginName = $pluginManager->normalizeIdentifier($pluginName);
6093

6194
if (
@@ -76,4 +109,24 @@ public function getPlugin($identifier = null): ?PluginBase
76109
{
77110
return PluginManager::instance()->findByIdentifier($this->getPluginIdentifier($identifier));
78111
}
112+
113+
/**
114+
* Validates that the provided plugin can be accessed
115+
*
116+
* @throws InvalidArgumentException if it cannot be
117+
*/
118+
public function validateProvidedPlugin()
119+
{
120+
if (!$this->plugin) {
121+
try {
122+
$this->plugin = $this->getPlugin();
123+
} catch (Throwable $e) {
124+
throw new InvalidArgumentException(sprintf(
125+
'Provided plugin "%s" failed to load: %s',
126+
$this->argument('plugin'),
127+
$e->getMessage()
128+
));
129+
}
130+
}
131+
}
79132
}

0 commit comments

Comments
 (0)