Plugin manager, misc fixes
* Fixed an error when trying to create nonexisting resource type * Updated the init-command to create plugins and scipts directories * Added a basic installer function
This commit is contained in:
		@@ -26,12 +26,14 @@ class InitCommand extends Command
 | 
			
		||||
 | 
			
		||||
        mkdir($configDir);
 | 
			
		||||
        mkdir($configDir."/plugins");
 | 
			
		||||
        mkdir($configDir."/scripts");
 | 
			
		||||
        file_put_contents($configDir."/spark.json", json_encode([
 | 
			
		||||
            'preload' => [
 | 
			
		||||
                './.spark/plugins/*'
 | 
			
		||||
                './.spark/plugins/*',
 | 
			
		||||
                './.spark/scripts/*'
 | 
			
		||||
            ],
 | 
			
		||||
            'scripts' => [
 | 
			
		||||
                'hello' => "echo 'Hello World\n'"
 | 
			
		||||
                'hello' => "echo 'Hello World'"
 | 
			
		||||
            ]
 | 
			
		||||
        ], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										79
									
								
								src/Commands/PluginsCommand.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								src/Commands/PluginsCommand.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Spark\Commands;
 | 
			
		||||
 | 
			
		||||
use Spark\SparkApplication;
 | 
			
		||||
use Symfony\Component\Console\Attribute\AsCommand;
 | 
			
		||||
use Symfony\Component\Console\Helper\Table;
 | 
			
		||||
use Symfony\Component\Console\Input\InputInterface;
 | 
			
		||||
use Symfony\Component\Console\Input\InputOption;
 | 
			
		||||
use Symfony\Component\Console\Output\OutputInterface;
 | 
			
		||||
 | 
			
		||||
#[AsCommand(name:'plugins', description:'Manage plugins from global repository')]
 | 
			
		||||
class PluginsCommand extends Command
 | 
			
		||||
{
 | 
			
		||||
    protected function configure()
 | 
			
		||||
    {
 | 
			
		||||
        $this->addOption("enable", null, InputOption::VALUE_REQUIRED, "Enable (symlink) a plugin");
 | 
			
		||||
        $this->addOption("disable", null, InputOption::VALUE_REQUIRED, "Disable (remove) a plugin");
 | 
			
		||||
        $this->addOption("force", "f", InputOption::VALUE_NONE, "Remove a plugin even if not a symlink");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function execute(InputInterface $input, OutputInterface $output)
 | 
			
		||||
    {
 | 
			
		||||
        $env = $this->getEnvironment();
 | 
			
		||||
        $app = $this->getApplication();
 | 
			
		||||
 | 
			
		||||
        $globalPlugins = getenv("SPARK_PLUGINS");
 | 
			
		||||
        $localPlugins = $env->getConfigDirectory()."/plugins";
 | 
			
		||||
 | 
			
		||||
        $globalPluginList = glob($globalPlugins."/*");
 | 
			
		||||
        $globalBasePluginList = array_map("basename", $globalPluginList);
 | 
			
		||||
        $localPluginList = array_map("basename", glob($localPlugins."/*"));
 | 
			
		||||
 | 
			
		||||
        if ($plugin = $input->getOption("enable")) {
 | 
			
		||||
            if (in_array($plugin, $localPluginList)) {
 | 
			
		||||
                $output->writeln("Plugin <info>{$plugin}</> already enabled!");
 | 
			
		||||
                return Command::SUCCESS;
 | 
			
		||||
            }
 | 
			
		||||
            if (!in_array($plugin, $globalBasePluginList)) {
 | 
			
		||||
                $output->writeln("<error>No such plugin {$plugin}</>");
 | 
			
		||||
                return Command::FAILURE;
 | 
			
		||||
            }
 | 
			
		||||
            symlink($globalPlugins."/".$plugin, $localPlugins."/".$plugin);
 | 
			
		||||
            $output->writeln("Enabled plugin <info>{$plugin}</>!");
 | 
			
		||||
            return Command::SUCCESS;
 | 
			
		||||
        } elseif ($plugin = $input->getOption("disable")) {
 | 
			
		||||
            $force = $input->getOption("force");
 | 
			
		||||
            if (!in_array($plugin, $localPluginList)) {
 | 
			
		||||
                $output->writeln("<error>Plugin {$plugin} not enabled!</>");
 | 
			
		||||
                return Command::FAILURE;
 | 
			
		||||
            }
 | 
			
		||||
            if (!is_link($localPlugins."/".$plugin) && !$force) {
 | 
			
		||||
                $output->writeln("<error>Plugin is not a symlink and --force not specified</>");
 | 
			
		||||
                return Command::FAILURE;
 | 
			
		||||
            } elseif (is_link($localPlugins."/".$plugin)) {
 | 
			
		||||
                unlink($localPlugins."/".$plugin);
 | 
			
		||||
                $output->writeln("Disabled plugin <info>{$plugin}</>!");
 | 
			
		||||
                return Command::SUCCESS;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($globalPluginList as $plugin) {
 | 
			
		||||
            if (!is_dir($plugin)) continue;
 | 
			
		||||
            if (!file_exists($plugin."/sparkplug.php")) continue;
 | 
			
		||||
            $head = (file($plugin."/sparkplug.php", FILE_IGNORE_NEW_LINES)[0])??null;
 | 
			
		||||
            if (str_contains($head, "//")) {
 | 
			
		||||
                $json = "{" . substr($head, strpos($head, "//") + 3) . "}";
 | 
			
		||||
                $info = json_decode($json)??object();
 | 
			
		||||
            } else {
 | 
			
		||||
                $info = object();
 | 
			
		||||
            }
 | 
			
		||||
            $installed = in_array(basename($plugin), $localPluginList);
 | 
			
		||||
            $badge = ($installed)?"<fg=green>\u{2714}</>":"<fg=gray>\u{27f3}</>";
 | 
			
		||||
            $output->writeln(sprintf(" %s <fg=#0ff>%-20s</>  %s", $badge, basename($plugin), $info->name??null));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return Command::SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user