-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: optional plugin dependencies not working correctly (#7094)
#### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: 修复可选插件依赖功能无法正常工作的问题 #### Special notes for your reviewer: 使用以下两个插件测试可选依赖: [测试插件集合.zip](https://github.com/user-attachments/files/17989250/default.zip) 使用以下测试用例进行测试: 测试用例1:plugin-feed 插件提供 RSS 扩展功能 - **前置条件:** 安装并启用 `plugin-feed` 插件。 - **操作步骤:** 访问 `http://localhost:8090/feed/rss.xml`。 - **期望结果:** 返回 `plugin-feed` 提供的 RSS 内容。 --- 测试用例 2: plugin-moments 扩展了 plugin-feed 的 RSS 功能(依赖于 plugin-feed) - **前置条件:** 安装并启用 `plugin-feed` 和 `plugin-moments` 插件。 - **操作步骤:** 访问 `http://localhost:8090/feed/moments/rss.xml`。 - **期望结果:** 返回 `plugin-moments` 提供的 RSS 内容。 --- 测试用例 3: plugin-feed 启用时安装 plugin-moments - **前置条件:** 启用 `plugin-feed` 插件。 - **操作步骤:** 1. 安装 `plugin-moments` 插件。 2. 访问 `http://localhost:8090/feed/moments/rss.xml`。 - **期望结果:** `plugin-moments` 提供的 RSS 路由可访问,并返回正确内容。 --- 测试用例 4: plugin-feed 未启用时安装 plugin-moments - **前置条件:** 未安装或未启用 `plugin-feed` 插件。 - **操作步骤:** 1. 安装并启用 `plugin-moments` 插件。 2. 访问 `http://localhost:8090/feed/moments/rss.xml`。 - **期望结果:** - `plugin-moments` 的 RSS 路由不可访问,返回 404。 - `plugin-moments` 的其他功能正常运行。 --- 测试用例 5: plugin-moments 启用后安装 plugin-feed - **前置条件:** 已安装并启用 `plugin-moments` 插件。 - **操作步骤:** 1. 安装并启用 `plugin-feed` 插件。 2. 访问 `http://localhost:8090/feed/moments/rss.xml`。 - **期望结果:** `plugin-moments` 提供的 RSS 路由可访问,并返回正确内容。 --- 测试用例 6: 停止 plugin-feed 后验证 RSS 路由 - **前置条件:** 已启用 `plugin-feed` 和 `plugin-moments` 插件。 - **操作步骤:** 1. 停止 `plugin-feed` 插件。 2. 访问 `http://localhost:8090/feed/moments/rss.xml`。 - **期望结果:** - `plugin-feed` 停止成功。 - `plugin-moments` 提供的 RSS 路由不可访问,返回 404。 --- 测试用例 7: 重启 Halo 后验证可选依赖插件的启动顺序 - **前置条件:** 已启用 `plugin-feed` 和 `plugin-moments` 插件。 - **操作步骤:** 1. 重启 Halo 服务。 2. 访问 `http://localhost:8090/feed/moments/rss.xml`。 - **期望结果:** - `plugin-moments` 提供的 RSS 路由**始终可访问**。 --- 测试用例 8: 必选依赖插件验证 - **场景 1: 安装 seo 插件时未安装应用市场** - **前置条件:** 未安装 `app-store-integration` 插件。 - **操作步骤:** 安装 `plugin-seo` 插件。 - **期望结果:** 提示需要先安装 `app-store-integration` 插件。 - **场景 2: 停止应用市场插件时 seo 插件仍启用** - **前置条件:** 已启用 `app-store-integration` 和 `plugin-seo` 插件。 - **操作步骤:** 停止 `app-store-integration` 插件。 - **期望结果:** 提示需要先停止 `plugin-seo` 插件。 #### Does this PR introduce a user-facing change? ```release-note 修复可选插件依赖功能无法正常工作的问题 ```
- Loading branch information
Showing
10 changed files
with
366 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
application/src/main/java/run/halo/app/plugin/OptionalDependentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package run.halo.app.plugin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.pf4j.PluginDependency; | ||
import org.pf4j.PluginDescriptor; | ||
import org.pf4j.util.DirectedGraph; | ||
|
||
/** | ||
* <p>Pass in a list of started plugin names to resolve dependency relationships, and return a | ||
* list of plugin names that depend on the specified plugin.</p> | ||
* <p>Do not consider the problem of circular dependency, because all the plugins that have been | ||
* started must not have this problem.</p> | ||
*/ | ||
public class OptionalDependentResolver { | ||
private final DirectedGraph<String> dependentsGraph; | ||
private final List<PluginDescriptor> plugins; | ||
|
||
public OptionalDependentResolver(List<PluginDescriptor> startedPlugins) { | ||
this.plugins = startedPlugins; | ||
this.dependentsGraph = new DirectedGraph<>(); | ||
resolve(); | ||
} | ||
|
||
private void resolve() { | ||
// populate graphs | ||
for (PluginDescriptor plugin : plugins) { | ||
addPlugin(plugin); | ||
} | ||
} | ||
|
||
public List<String> getOptionalDependents(String pluginId) { | ||
return new ArrayList<>(dependentsGraph.getNeighbors(pluginId)); | ||
} | ||
|
||
private void addPlugin(PluginDescriptor descriptor) { | ||
String pluginId = descriptor.getPluginId(); | ||
List<PluginDependency> dependencies = descriptor.getDependencies(); | ||
if (dependencies.isEmpty()) { | ||
dependentsGraph.addVertex(pluginId); | ||
} else { | ||
boolean edgeAdded = false; | ||
for (PluginDependency dependency : dependencies) { | ||
if (dependency.isOptional()) { | ||
edgeAdded = true; | ||
dependentsGraph.addEdge(dependency.getPluginId(), pluginId); | ||
} | ||
} | ||
|
||
// Register the plugin without dependencies, if all of its dependencies are optional. | ||
if (!edgeAdded) { | ||
dependentsGraph.addVertex(pluginId); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.