feat: 发布智能体版

This commit is contained in:
augushong
2026-03-26 20:22:34 +08:00
parent 7ee9e102a5
commit 8cc08bcb8c
138 changed files with 7964 additions and 660 deletions

View File

@@ -2,20 +2,24 @@
namespace base\common\command\scheme;
use think\console\Command;
use app\common\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Input\Option;
use think\console\Input\Argument;
use think\console\Output;
use think\facade\Db;
use think\facade\Config;
use app\common\service\scheme\DbToSchemeService;
use app\common\service\scheme\SchemeToDbService;
use app\common\service\scheme\attribute\Table;
use ReflectionClass;
class Make extends Command
{
protected function configure()
{
parent::configure();
$this->setName('scheme:make')
->addOption('table', 't', Option::VALUE_REQUIRED, "The table name (without prefix)")
->addArgument('table', Argument::OPTIONAL, "The table name (without prefix)")
@@ -27,7 +31,7 @@ class Make extends Command
$table = $input->getOption('table') ?: $input->getArgument('table');
$service = new DbToSchemeService();
$compare = new SchemeToDbService();
$tables = [];
if ($table) {
$tables[] = $table;
@@ -39,7 +43,7 @@ class Make extends Command
$connection = Config::get('database.default', 'mysql');
$prefix = Config::get('database.connections.' . $connection . '.prefix', '');
$backupPrefix = Config::get('scheme.backup_prefix', 'backup');
foreach ($allTables as $t) {
if ($this->isBackupTable($t, $prefix, $backupPrefix)) {
continue;
@@ -50,7 +54,7 @@ class Make extends Command
if ($prefix && str_starts_with($t, $prefix)) {
$shortName = substr($t, strlen($prefix));
}
if (!in_array($shortName, $config) && !in_array($t, $config)) {
$tables[] = $shortName;
}
@@ -61,7 +65,7 @@ class Make extends Command
$output->writeln("Processing table: $t");
try {
$code = $service->generate($t);
// 提取类名以确定文件名
if (preg_match('/class\s+(\w+)/', $code, $matches)) {
$className = $matches[1];
@@ -78,17 +82,17 @@ class Make extends Command
}
}
}
// 确保目录存在
if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0755, true);
}
file_put_contents($path, $code);
$output->writeln("<info>Generated: $path</info>");
}
} catch (\Exception $e) {
$output->writeln("<error>Error processing $t: " . $e->getMessage() . "</error>");
$output->error("Error processing $t: " . $e->getMessage());
}
}
}

View File

@@ -2,23 +2,27 @@
namespace base\common\command\scheme;
use think\console\Command;
use app\common\console\Command;
use app\common\service\scheme\SchemeToDbService;
use think\console\Input;
use think\console\input\Option;
use think\console\Input\Option;
use think\console\Output;
use ReflectionClass;
use app\common\scheme\attribute\Table;
use think\facade\Config;
use think\facade\Db;
use app\common\service\scheme\SchemeToDbService;
use app\common\scheme\attribute\Table;
use ReflectionClass;
/**
* scheme:sync 命令基类
*/
class Sync extends Command
{
protected function configure()
{
parent::configure();
$this->setName('scheme:sync')
->addOption('skip-data', null, Option::VALUE_NONE, 'Skip data migration')
->addOption('force', null, Option::VALUE_NONE, 'Force execution without confirmation')
->setDescription('Synchronize Scheme classes to Database');
}
@@ -32,17 +36,20 @@ class Sync extends Command
$connection = Config::get('database.default', 'mysql');
$prefix = Config::get('database.connections.' . $connection . '.prefix', '');
$backupPrefix = Config::get('scheme.backup_prefix', 'backup');
if (!is_dir($schemeDir)) {
$output->writeln("<error>Scheme directory not found: $schemeDir</error>");
$error = "Scheme directory not found: $schemeDir";
$output->writeln("<error>$error</error>");
return;
}
$pendingSync = [];
$files = glob($schemeDir . '*.php');
foreach ($files as $file) {
require_once $file;
$className = 'app\\admin\\scheme\\' . basename($file, '.php');
if (class_exists($className)) {
$tableName = $this->getTableNameFromScheme($className);
if (empty($tableName)) {
@@ -64,30 +71,62 @@ class Sync extends Command
try {
$diffs = $service->diff($className);
} catch (\Throwable $e) {
$output->writeln("<error>Check failed for $className: " . $e->getMessage() . "</error>");
$output->error("Check failed for $className: " . $e->getMessage());
continue;
}
if (count($diffs) === 1 && str_starts_with($diffs[0], '无法读取数据库表结构')) {
$output->writeln("<error>Check failed for $className: {$diffs[0]}</error>");
$output->error("Check failed for $className: {$diffs[0]}");
continue;
}
if (empty($diffs)) {
$output->writeln("Skipping $className (no schema changes)");
continue;
}
$output->writeln("Syncing $className...");
try {
$backup = $service->sync($className, $skipData);
$output->writeln("<info>Success!</info>");
if ($backup) {
$output->writeln("Backup created: $backup");
}
} catch (\Exception $e) {
$output->writeln("<error>Failed: " . $e->getMessage() . "</error>");
$pendingSync[] = [
'className' => $className,
'tableName' => $fullTableName,
'diffs' => $diffs
];
}
}
if (empty($pendingSync)) {
$output->writeln('<info>未检测到 Scheme 变更。</info>');
return;
}
// 显示所有待同步的变更
$output->writeln('');
$output->writeln('<comment>========================================</comment>');
$output->writeln('<comment>即将执行以下 Scheme 变更:</comment>');
$output->writeln('<comment>========================================</comment>');
foreach ($pendingSync as $item) {
$output->writeln("<comment>表名: {$item['tableName']} ({$item['className']})</comment>");
foreach ($item['diffs'] as $line) {
$output->writeln(" $line");
}
$output->writeln('');
}
// 确认(全局 -ff 可跳过)
if (!$output->confirm($input, '确认要将这些变更应用到数据库吗?', true)) {
$output->comment('操作已取消。');
return;
}
// 执行同步
foreach ($pendingSync as $item) {
$output->writeln("正在同步 {$item['className']}...");
try {
$backup = $service->sync($item['className'], $skipData);
$output->writeln("<info>成功!</info>");
if ($backup) {
$output->writeln("已创建备份: $backup");
}
} catch (\Exception $e) {
$output->writeln("<error>失败: " . $e->getMessage() . "</error>");
}
}
}