workup/nova-command-runner

Laravel Nova 工具,用于运行 Artisan 和 bash (shell) 命令。

v4.2.5.001 2023-09-14 13:53 UTC

This package is auto-updated.

Last update: 2024-09-14 15:58:11 UTC


README

Laravel Nova 工具,用于运行 Artisan & Shell 命令

Latest Version on Packagist Total Downloads License

screenshot of the command runner tool

描述

Nova 工具允许您直接从 nova 运行 artisan 和 bash 命令。

这是由 guratr 开发的原始包 Nova Command Runner 的扩展版本。

要求

  • php: >=8.0
  • laravel/nova: ^4.0

功能

  • ⌨️ 运行预定义的 artisan 和 shell 命令
  • 📝 运行自定义 artisan 和 shell 命令
  • 📄 在运行命令时使用变量
  • 🙋‍♂️ 在运行命令时提示用户指定可选标志
  • 📄 使用选择框或提示用户输入变量的预定义值。
  • 📖 跟踪命令运行历史
  • 💾 不需要数据库更改。一切均由单个配置文件管理。
  • 🕔 排队长时间运行的命令
  • ✅ 支持命令进度条
  • 📱 响应式
  • 🕶️ 暗黑模式

安装

您可以通过 composer 在使用 Nova 的 Laravel 应用中安装 nova 工具。

composer require stepanenko3/nova-command-runner

接下来,您必须将工具与 Nova 注册。通常在 NovaServiceProvidertools 方法中完成此操作。

// in app/Providers/NovaServiceProvder.php

// ...

public function tools()
{
    return [
        // ...
        new \Workup\Nova\CommandRunner\CommandRunnerTool,
    ];
}

发布配置文件

php artisan vendor:publish --provider="Workup\Nova\CommandRunner\ToolServiceProvider"

将您的命令添加到 config/nova-command-runner.php

用法

点击您的 Nova 应用中的“Command Runner”菜单项以查看工具。

配置

所有配置均由位于 config/nova-command-runner.php 的单个配置文件管理。

添加命令

需要易于访问的所有命令应在配置文件中的 commands 数组中定义。

命令选项

  • run : 要运行的命令 (例如 route:cache)
  • type : 按钮类 (primary, secondary, success, danger, warning, info, light, dark, link)
  • group: 组名 (可选)
  • variables : 命令中使用的变量数组 (可选)
  • command_type : 命令类型。(artisan 或 bash。默认 artisan)
  • flags : 命令的可选标志数组 (可选)
  • output_size: 将给定命令的输出裁剪到指定的行数
  • timeout: 更新给定排队命令的超时限制

示例

'commands' => [

    // Basic command
    'Clear Cache' => [
        'run' => 'cache:clear', 
        'type' => 'danger', 
        'group' => 'Cache',
    ],
     
    // Bash command
    'Disk Usage' => [
        'run' => 'df -h', 
        'type' => 'danger', 
        'group' => 'Statistics',
        'command_type' => 'bash'
    ],

    // Command with variable   
    'Clear Cache' => [
        'run' => 'cache:forget {cache key}', 
        'type' => 'danger', 
        'group' => 'Cache'
    ],

    // Command with advanced variable customization
    'Clear Cache' => [
        'run' => 'cache:forget {cache key}', 
        'type' => 'danger', 
        'group' => 'Cache',
        'variables' => [
            [
                'label' =>  'cache key' // This needs to match with variable defined in the command,
                'field' => 'select' // Allowed values (text,number,tel,select,date,email,password),
                'options' => [
                    'blog-cache' => 'Clear Blog Cache', 
                    'app-cache' => 'Clear Application Cache'
                ],
                'placeholder' => 'Select An Option'
            ]
        ]
    ],

    // Command with flags
    'Run Migrations' => [
        'run' => 'migrate --force', 
        'type' => 'danger', 
        'group' => 'Migration',
    ],

    // Command with optional flags
    'Run Migrations' => [
        'run' => 'migrate', 
        'type' => 'danger', 
        'group' => 'Migration',
        'flags' => [ 
            // These optional flags will be prompted as a checkbox for the user
            // And will be appended to the command if the user checks the checkbox

            '--force' => 'Force running in production' 
        ]
    ],

    // Command with help text
    'Run Migrations' => [
        'run' => 'migrate --force', 
        'type' => 'danger', 
        'group' => 'Migration',

        // You can also add html for help text.
        'help' => 'This is a destructive operation. Proceed only if you really know what you are doing.'
    ],

    // Queueing commands
    'Clear Cache' => [
        'run' => 'cache:clear --should-queue',
        'type' => 'danger',
        'group' => 'Cache',
    ],

    // Queueing commands on custom queue and connection
    'Clear Cache' => [
        'run' => 'cache:clear --should-queue --cr-queue=high --cr-connection=database',
        'type' => 'danger',
        'group' => 'Cache',
    ],
],

带有进度条的命令

创建带有进度条的 Artisan 命令。例如

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Workup\Nova\CommandRunner\Console\HasNovaProgressBar;

class LongCommand extends Command
{
    use HasNovaProgressBar;

    protected $signature = 'command:long';

    public function handle()
    {
        $bar = $this->createProgressBar(10);

        foreach (range(1, 10) as $i) {
            sleep(1);

            $bar->advance();
        }

        $bar->finish();

        return Command::SUCCESS;
    }
}

将您的命令添加到配置命令列表中

'commands' => [
    ...

    // Queueing long-running commands with custom timeout limit and output size
    'Long Command' => [ 
        'run' => 'command:long --should-queue',  // '--should-queue' is a required option to display the progressbar

        'type' => 'primary', 
        'group' => 'Application', 
        'timeout' => 120, // Updates the timeout limit for the given queued command
        'output_size' => 3, // The number of last lines to be displayed in the output
    ],
]

其他自定义

// Limit the command run history to latest 10 runs
'history'  => 10,

// Tool name displayed in the navigation menu
'navigation_label' => 'Command Runner',

// Any additional info to display on the tool page. Can contain string and html.
'help' => '',

// Groups whose commands should not be running simultaneously, ['*'] to apply this globally to all commands
'unique_command_groups' => [],

// Allow running of custom artisan and bash(shell) commands
'custom_commands' => ['artisan','bash'],

// Allow running of custom artisan commands only(disable custom bash(shell) commands)
'custom_commands' => ['artisan'],

// Allow running of custom bash(shell) commands only(disable custom artisan commands)
'custom_commands' => ['bash'],

// Disable running of custom commands.
'custom_commands' => [],

'without_overlapping' => [
    // Blocks running commands simultaneously under the given groups. Use '*' for block all groups
    'groups' => [],

    // Blocks running commands simultaneously. Use '*' for block all groups
    'commands' => [],
],

屏幕截图

screenshot of the command runner tool

screenshot of the command runner tool

screenshot of the command runner tool

screenshot of the command runner tool

screenshot of the command runner tool

screenshot of the command runner tool

screenshot of the command runner tool

致谢

贡献

感谢您考虑为此包做出贡献!请创建一个带有您贡献的拉取请求,并详细说明您提议的更改。

许可

此包是开源软件,受 MIT 许可证 许可。