Skip to content

Conversation

@People-Sea
Copy link
Member

@People-Sea People-Sea commented Sep 12, 2025

Related mineadmin/MineAdmin#697

Summary by CodeRabbit

  • 新功能

    • 创建插件时自动从路径生成命名空间与名称(无需单独输入名称)。
    • 为后端/混合插件自动生成安装/卸载脚本、ConfigProvider 与视图脚本;必要时创建 web 目录。
    • mine.json 中新增 config 条目并改善自动加载映射。
  • 改进

    • 路径格式严格校验(必须为 organization/plugin-name),不符合将报错并中止。
    • mine.json 采用更友好的 JSON 编码(保留斜杠与 Unicode,带缩进)。
  • 修复

    • 卸载脚本的类名更正,避免与安装脚本混淆。

… JSON output; fix class name in UninstallScript.stub
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Sep 12, 2025
@coderabbitai
Copy link

coderabbitai bot commented Sep 12, 2025

Walkthrough

调整 CreateCommand 对插件路径的解析与 mine.json 生成:路径必须为 org/plugin 格式,命名空间从路径派生;移除对单独 name 参数的依赖并修改相关方法签名;为 Backend/Mix 插件新增 ConfigProvider 与 view 脚本生成;卸载脚本模板类名改为 UninstallScript

Changes

Cohort / File(s) Summary
CreateCommand 逻辑变更
src/AppStore/src/Command/CreateCommand.php
移除对单独 name 选项的读取与验证;新增严格路径格式校验(必须且仅含一个 /,支持反斜杠归一化);createNamespace(string $path):由路径拆分生成 Plugin\<Org>\<Ext>(移除 name 参数);createMineJson(string $path, PluginTypeEnum $pluginType):不再接收 name,mine.json 的 name 字段设为插件路径,PSR-4 键去掉前导反斜杠;为 Backend/Mix 类型调用 createInstallScript/createUninstallScript/createConfigProvider/createViewScript 并在 mine.json 添加 config 条目;确保生成 web 目录;json_encode 使用 `JSON_UNESCAPED_UNICODE
脚本模版命名修正
src/AppStore/src/Command/Stub/UninstallScript.stub
模板中类名由 InstallScript 重命名为 UninstallScript,其余实现不变。

Sequence Diagram(s)

sequenceDiagram
    participant CLI as CLI 用户
    participant Cmd as CreateCommand
    participant FS as 文件系统
    participant JSON as mine.json

    CLI->>Cmd: 调用 create(path, pluginType)
    Cmd->>Cmd: 规范化 path(\ 转 /),校验格式(必须含且仅含一个 /)
    Cmd->>Cmd: createNamespace(path) ⇒ Plugin\<Org>\<Ext>
    Cmd->>Cmd: createMineJson(path, pluginType)
    alt pluginType 是 Backend/Mix
        Cmd->>Cmd: createInstallScript(namespace, path)
        Cmd->>Cmd: createUninstallScript(namespace, path)
        Cmd->>Cmd: createConfigProvider(namespace, path)
        Cmd->>FS: createViewScript(namespace, path)(确保 web/ 存在)
        Cmd->>JSON: 在 mine.json 添加 "config": "Namespace\\ConfigProvider"
    end
    Cmd->>JSON: 写入 name=path、PSR-4 映射(无前导反斜杠),使用 pretty/unescaped JSON
    Cmd->>FS: 写入文件
    FS-->>Cmd: 写入成功
    Cmd-->>CLI: 返回完成
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • Optimize plugin namespace #139 — 与本次对 CreateCommand 中使用插件路径作为 mine.json "name" 字段及 PSR-4 映射修改有直接代码层关联。

Suggested labels

enhancement

Suggested reviewers

  • zds-s

Poem

我是小兔敲键忙,
斜杠分家路更明。
JSON 排版笑颜扬,
卸载换名也乖巧。
跳跃欢庆发新章,🐰✨


📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bf37e78 and 3179c74.

📒 Files selected for processing (1)
  • src/AppStore/src/Command/CreateCommand.php (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/AppStore/src/Command/CreateCommand.php
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Test on PHP8.2 Swoole-master
  • GitHub Check: Test on PHP8.1 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.1 Swoole-master
  • GitHub Check: Test on PHP8.1 Swoole-v5.1.2
  • GitHub Check: Test on PHP8.2 Swoole-v5.1.2
  • GitHub Check: Test on PHP8.2 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.3 Swoole-master
  • GitHub Check: Test on PHP8.3 Swoole-v5.1.2

Pre-merge checks (2 passed, 1 warning)

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 当前标题 "fix(AppStore/CreateCommand): correct namespace formatting and improve JSON output" 清晰且直接地反映了变更集中在修正命名空间格式(包括 PSR-4 映射)和改进 mine.json 输出(编码选项与字段)的两项主要改动,措辞简洁、聚焦核心修改且未包含噪音或模糊描述。变更集中还包含新增安装/卸载脚本与 ConfigProvider 的实现细节,但这些属于实现层面的补充,不影响标题对主要目的的准确覆盖。因而标题与提交内容的主要意图一致且可被团队快速识别。
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot bot added the bug Something isn't working label Sep 12, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/AppStore/src/Command/Stub/UninstallScript.stub (1)

5-5: 建议:为模板补充 strict_types 和 __invoke 返回类型

提升生成文件的类型约束与一致性;不改变运行时语义。

可参考:

 <?php
+declare(strict_types=1);

 namespace %namespace%;

-class UninstallScript {
+class UninstallScript {
-    public function __invoke(){
+    public function __invoke(): void {
         echo "Commands to be executed when uninstalling the plug-in";
     }
 }
src/AppStore/src/Command/CreateCommand.php (1)

109-111: 建议:显式处理 JSON 编码与写文件失败

为避免 mine.json 为空/损坏仍提示成功,建议启用 JSON_THROW_ON_ERROR 并检查写入结果。

可参考:

-        $output = Json::encode($output, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT);
-        file_put_contents($path . '/mine.json', $output);
-        $this->output->success(\sprintf('%s 创建成功', $path . '/mine.json'));
+        try {
+            $json = Json::encode(
+                $output,
+                \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR
+            );
+        } catch (\Throwable $e) {
+            $this->output->error(\sprintf('mine.json 生成失败(JSON 编码错误):%s', $e->getMessage()));
+            return;
+        }
+        $mineJsonPath = $path . '/mine.json';
+        if (false === @file_put_contents($mineJsonPath, $json)) {
+            $this->output->error(\sprintf('写入失败:%s', $mineJsonPath));
+            return;
+        }
+        $this->output->success(\sprintf('%s 创建成功', $mineJsonPath));
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 54cfe35 and add851d.

📒 Files selected for processing (2)
  • src/AppStore/src/Command/CreateCommand.php (2 hunks)
  • src/AppStore/src/Command/Stub/UninstallScript.stub (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Test on PHP8.1 Swoole-master
  • GitHub Check: Test on PHP8.2 Swoole-master
  • GitHub Check: Test on PHP8.1 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.2 Swoole-v5.1.2
  • GitHub Check: Test on PHP8.1 Swoole-v5.1.2
  • GitHub Check: Test on PHP8.2 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.3 Swoole-master
  • GitHub Check: Test on PHP8.3 Swoole-v5.1.2
🔇 Additional comments (2)
src/AppStore/src/Command/Stub/UninstallScript.stub (1)

5-5: 类名与模版文件名统一,修正合理

与 CreateCommand::createUninstallScript 的引用保持一致,避免生成类名不匹配的问题。

src/AppStore/src/Command/CreateCommand.php (1)

95-96: PSR-4 键去掉前导反斜杠是正确的

Composer 规范要求命名空间键不含前导 “\”,当前 $namespace . '\\' => 'src' 符合预期。

@People-Sea People-Sea requested a review from zds-s September 12, 2025 03:13
@zds-s
Copy link
Member

zds-s commented Sep 12, 2025

@People-Sea 生成的代码命名空间不对

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Sep 12, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/AppStore/src/Command/CreateCommand.php (1)

157-164: CLI 选项存在两处问题:无用的 name 参数与非法短选项

  • 本 PR 已移除对 --name 的使用,保留该选项会造成困惑。
  • Symfony Console 的短选项需为单字符,目前 'name'/'type'/'desc'/'author' 为非法值,可能导致运行时异常。
-            ->addOption('name', 'name', InputOption::VALUE_REQUIRED, 'Plug-in Name')
-            ->addOption('type', 'type', InputOption::VALUE_OPTIONAL, 'Plugin type, default mix optional mix,frond,backend')
-            ->addOption('description', 'desc', InputOption::VALUE_OPTIONAL, 'Plug-in Introduction')
-            ->addOption('author', 'author', InputOption::VALUE_OPTIONAL, 'Plugin Author Information');
+            // 移除未使用的 --name;修正短选项为单字符
+            ->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Plugin type (mix|frond|backend). Default: mix')
+            ->addOption('description', 'd', InputOption::VALUE_OPTIONAL, 'Plug-in Introduction')
+            ->addOption('author', 'a', InputOption::VALUE_OPTIONAL, 'Plugin Author Information');
🧹 Nitpick comments (4)
src/AppStore/src/Command/CreateCommand.php (4)

64-69: createNamespace 增强健壮性,避免分段异常

explode('/', $pluginPath) 假定一定是两段。为防止未来复用时传入不合规路径,建议显式校验段数并抛出异常。

-        [$orgName, $extName] = explode('/', $pluginPath);
+        $segments = explode('/', $pluginPath, 3);
+        if (count($segments) !== 2 || $segments[0] === '' || $segments[1] === '') {
+            throw new \InvalidArgumentException(sprintf('Invalid plugin path: %s', $pluginPath));
+        }
+        [$orgName, $extName] = $segments;

87-93: 前端-only 插件未创建 web 目录的差异行为

当前仅 Backend/Mix 分支会调用 createViewScript()(实际是确保 web 目录存在),Frond 分支不会。若前端插件同样需要 web 目录,建议同步处理以避免模板缺失。

-        if ($pluginType === PluginTypeEnum::Backend || $pluginType === PluginTypeEnum::Mix) {
+        if ($pluginType === PluginTypeEnum::Backend || $pluginType === PluginTypeEnum::Mix) {
             // ...
             $this->createViewScript($namespace, $path);
             // ...
         }
+        if ($pluginType === PluginTypeEnum::Frond) {
+            // 仅确保 web 目录
+            $this->createViewScript('N/A', $path);
+        }

另外,createViewScript() 目前只创建目录,建议重命名为 ensureWebDirectory() 以贴合语义。


51-53: 重复创建 web 目录,建议去重

$createDirectors 已包含 $pluginPath . '/web',而 createViewScript() 里再次 mkdir。建议去掉其一以减少分支差异与无效 I/O。

去掉初始化处的 web 目录创建:

-        $createDirectors = [
-            $pluginPath, $pluginPath . '/src', $pluginPath . '/Database', $pluginPath . '/Database/Migrations', $pluginPath . '/Database/Seeders', $pluginPath . '/web',
-        ];
+        $createDirectors = [
+            $pluginPath, $pluginPath . '/src', $pluginPath . '/Database', $pluginPath . '/Database/Migrations', $pluginPath . '/Database/Seeders',
+        ];

Also applies to: 166-169


112-121: 提示语语言风格统一(中/英混用)

同一命令中既有中文“创建成功”也有英文“Created Successfully”。建议统一语言,便于日志检索与用户理解。

Also applies to: 120-121

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between add851d and bf37e78.

📒 Files selected for processing (1)
  • src/AppStore/src/Command/CreateCommand.php (5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/AppStore/src/Command/CreateCommand.php (2)
src/AppStore/src/Command/AbstractCommand.php (1)
  • AbstractCommand (17-35)
src/AppStore/src/Plugin.php (1)
  • Plugin (29-434)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: Test on PHP8.1 Swoole-v5.1.2
  • GitHub Check: Test on PHP8.2 Swoole-master
  • GitHub Check: Test on PHP8.1 Swoole-master
  • GitHub Check: Test on PHP8.2 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.3 Swoole-master
  • GitHub Check: Test on PHP8.2 Swoole-v5.1.2
  • GitHub Check: Test on PHP8.3 Swoole-v5.1.2
  • GitHub Check: Test on PHP8.1 Swoole-v5.0.3
🔇 Additional comments (4)
src/AppStore/src/Command/CreateCommand.php (4)

60-62: 调用时传入绝对路径 OK

此处传入 $pluginPath(绝对路径)给 createMineJson() 合理;其内部通过去前缀获得相对路径使用场景一致。无进一步意见。


96-96: PSR-4 映射移除前导反斜杠是正确修复

Composer PSR-4 前缀不应以反斜杠开头,此变更可避免自动加载异常。LGTM。


110-113: JSON 输出选项改进 LGTM

开启 JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT 改善可读性与跨平台一致性。


51-58: 已验证:PHP 环境支持 0o 八进制字面量 — 无需变更
根目录 composer.json(及 src/Crontab/composer.json)将 php 平台约束设为 >=8.1,且当前 CLI 为 PHP 8.2.29;0o755/0o775 在 PHP 8.1+ 受支持。

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Sep 15, 2025
@zds-s zds-s merged commit 272aeda into mineadmin:3.0 Sep 15, 2025
11 checks passed
@People-Sea People-Sea deleted the fix/app-store/create-command-make-json branch September 15, 2025 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants