From 275df33d75da038c9bf5e1ec8700c59f8017c512 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Mon, 18 Jul 2022 22:42:04 +0800 Subject: [PATCH] refactor: scanning of the jsBundleRule for plugins (#2249) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.0 #### What this PR does / why we need it: 重构 JsBundle 获取方式,增加对 js entry 的文件校验如果不存在,则启用插件时不生成反向代理 APIs #### Which issue(s) this PR fixes: Fixes # #### Special notes for your reviewer: /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../run/halo/app/core/extension/Plugin.java | 5 +- .../reconciler/PluginReconciler.java | 32 +++--------- .../resources/JsBundleRuleProvider.java | 51 +++++++++++++++---- .../ReverseProxyRouterFunctionFactory.java | 11 +--- .../reconciler/PluginReconcilerTest.java | 4 +- ...ReverseProxyRouterFunctionFactoryTest.java | 5 +- 6 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/main/java/run/halo/app/core/extension/Plugin.java b/src/main/java/run/halo/app/core/extension/Plugin.java index 67803f3d5..f829c62b5 100644 --- a/src/main/java/run/halo/app/core/extension/Plugin.java +++ b/src/main/java/run/halo/app/core/extension/Plugin.java @@ -44,7 +44,7 @@ public class Plugin extends AbstractExtension { @JsonIgnore public PluginStatus statusNonNull() { if (this.status == null) { - return new PluginStatus(); + this.status = new PluginStatus(); } return status; } @@ -87,7 +87,8 @@ public class Plugin extends AbstractExtension { @NonNull @JsonIgnore public List extensionLocationsNonNull() { - return Objects.requireNonNullElseGet(extensionLocations, List::of); + this.extensionLocations = Objects.requireNonNullElseGet(extensionLocations, List::of); + return this.extensionLocations; } } diff --git a/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java b/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java index d02254392..b1ea9c963 100644 --- a/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java +++ b/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java @@ -10,14 +10,12 @@ import lombok.extern.slf4j.Slf4j; import org.pf4j.PluginRuntimeException; import org.pf4j.PluginState; import org.pf4j.PluginWrapper; -import org.pf4j.RuntimeMode; import run.halo.app.core.extension.Plugin; import run.halo.app.extension.ExtensionClient; import run.halo.app.extension.controller.Reconciler; import run.halo.app.infra.utils.JsonUtils; import run.halo.app.plugin.HaloPluginManager; import run.halo.app.plugin.PluginStartingError; -import run.halo.app.plugin.YamlPluginFinder; import run.halo.app.plugin.resources.JsBundleRuleProvider; import run.halo.app.plugin.resources.ReverseProxyRouterFunctionFactory; @@ -83,8 +81,6 @@ public class PluginReconciler implements Reconciler { return; } - ensureSpecUpToDateWhenDevelopmentMode(pluginWrapper, plugin); - if (!Objects.equals(pluginStatus.getPhase(), pluginWrapper.getPluginState())) { // Set to the correct state pluginStatus.setPhase(pluginWrapper.getPluginState()); @@ -134,12 +130,14 @@ public class PluginReconciler implements Reconciler { Plugin.PluginStatus status = plugin.statusNonNull(); // TODO Check whether the JS bundle rule exists. If it does not exist, do not populate // populate stylesheet path - String jsBundleRoute = ReverseProxyRouterFunctionFactory.buildRoutePath(pluginName, - jsBundleRule.jsRule(pluginName)); - String cssBundleRoute = ReverseProxyRouterFunctionFactory.buildRoutePath(pluginName, - jsBundleRule.cssRule(pluginName)); - status.setEntry(jsBundleRoute); - status.setStylesheet(cssBundleRoute); + jsBundleRule.jsRule(pluginName) + .map(jsRule -> ReverseProxyRouterFunctionFactory.buildRoutePath(pluginName, jsRule)) + .ifPresent(status::setEntry); + + jsBundleRule.cssRule(pluginName) + .map(cssRule -> ReverseProxyRouterFunctionFactory.buildRoutePath(pluginName, cssRule)) + .ifPresent(status::setStylesheet); + status.setLastStartTime(Instant.now()); } @@ -170,18 +168,4 @@ public class PluginReconciler implements Reconciler { throw new PluginRuntimeException(startingError.getMessage()); } } - - private void ensureSpecUpToDateWhenDevelopmentMode(PluginWrapper pluginWrapper, - Plugin oldPlugin) { - if (RuntimeMode.DEPLOYMENT.equals(pluginWrapper.getRuntimeMode())) { - return; - } - YamlPluginFinder yamlPluginFinder = new YamlPluginFinder(); - Plugin pluginFromPath = yamlPluginFinder.find(pluginWrapper.getPluginPath()); - // ensure plugin spec is up to date - Plugin.PluginSpec pluginSpec = JsonUtils.deepCopy(pluginFromPath.getSpec()); - if (!Objects.equals(oldPlugin.getSpec(), pluginSpec)) { - oldPlugin.setSpec(pluginSpec); - } - } } diff --git a/src/main/java/run/halo/app/plugin/resources/JsBundleRuleProvider.java b/src/main/java/run/halo/app/plugin/resources/JsBundleRuleProvider.java index a99a5917f..3bfba00f3 100644 --- a/src/main/java/run/halo/app/plugin/resources/JsBundleRuleProvider.java +++ b/src/main/java/run/halo/app/plugin/resources/JsBundleRuleProvider.java @@ -1,7 +1,13 @@ package run.halo.app.plugin.resources; +import java.util.Optional; +import org.pf4j.PluginWrapper; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.lang.NonNull; import org.springframework.stereotype.Component; -import run.halo.app.core.extension.ReverseProxy; +import run.halo.app.core.extension.ReverseProxy.FileReverseProxyProvider; +import run.halo.app.core.extension.ReverseProxy.ReverseProxyRule; +import run.halo.app.plugin.HaloPluginManager; /** * TODO Optimize code to support user customize js bundle rules. @@ -11,6 +17,20 @@ import run.halo.app.core.extension.ReverseProxy; */ @Component public class JsBundleRuleProvider { + private static final String JS_LOCATION = "/admin/main.js"; + private static final String CSS_LOCATION = "/admin/style.css"; + + private static final FileReverseProxyProvider JS_FILE_PROXY = + new FileReverseProxyProvider("admin", "main.js"); + + private static final FileReverseProxyProvider CSS_FILE_PROXY = + new FileReverseProxyProvider("admin", "style.css"); + + private final HaloPluginManager haloPluginManager; + + public JsBundleRuleProvider(HaloPluginManager haloPluginManager) { + this.haloPluginManager = haloPluginManager; + } /** * Gets plugin js bundle rule. @@ -18,10 +38,11 @@ public class JsBundleRuleProvider { * @param pluginName plugin name * @return a js bundle rule */ - public ReverseProxy.ReverseProxyRule jsRule(String pluginName) { - ReverseProxy.FileReverseProxyProvider - file = new ReverseProxy.FileReverseProxyProvider("admin", "main.js"); - return new ReverseProxy.ReverseProxyRule("/admin/main.js", file); + public Optional jsRule(String pluginName) { + return Optional.of(JS_LOCATION) + .filter(path -> createResourceLoader(pluginName) + .getResource(path).exists()) + .map(path -> new ReverseProxyRule(path, JS_FILE_PROXY)); } /** @@ -30,9 +51,21 @@ public class JsBundleRuleProvider { * @param pluginName plugin name * @return a stylesheet bundle rule */ - public ReverseProxy.ReverseProxyRule cssRule(String pluginName) { - ReverseProxy.FileReverseProxyProvider - file = new ReverseProxy.FileReverseProxyProvider("admin", "style.css"); - return new ReverseProxy.ReverseProxyRule("/admin/style.css", file); + public Optional cssRule(String pluginName) { + return Optional.of(CSS_LOCATION) + .filter(path -> createResourceLoader(pluginName) + .getResource(path) + .exists()) + .map(path -> new ReverseProxyRule(path, CSS_FILE_PROXY)); } + + @NonNull + private DefaultResourceLoader createResourceLoader(String pluginName) { + PluginWrapper plugin = haloPluginManager.getPlugin(pluginName); + if (plugin == null) { + return new DefaultResourceLoader(); + } + return new DefaultResourceLoader(plugin.getPluginClassLoader()); + } + } diff --git a/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java b/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java index 568835e9b..4327e7a3c 100644 --- a/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java +++ b/src/main/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactory.java @@ -114,15 +114,8 @@ public class ReverseProxyRouterFunctionFactory { private List getJsBundleRules(String pluginId) { List rules = new ArrayList<>(2); - ReverseProxyRule jsRule = jsBundleRuleProvider.jsRule(pluginId); - if (jsRule != null) { - rules.add(jsRule); - } - - ReverseProxyRule cssRule = jsBundleRuleProvider.cssRule(pluginId); - if (cssRule != null) { - rules.add(cssRule); - } + jsBundleRuleProvider.jsRule(pluginId).ifPresent(rules::add); + jsBundleRuleProvider.cssRule(pluginId).ifPresent(rules::add); return rules; } diff --git a/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java b/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java index d5a7dbc08..8a8cea01c 100644 --- a/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java +++ b/src/test/java/run/halo/app/core/extension/reconciler/PluginReconcilerTest.java @@ -19,7 +19,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.pf4j.PluginState; import org.pf4j.PluginWrapper; -import org.pf4j.RuntimeMode; import run.halo.app.core.extension.Plugin; import run.halo.app.extension.ExtensionClient; import run.halo.app.extension.controller.Reconciler; @@ -50,11 +49,10 @@ class PluginReconcilerTest { @BeforeEach void setUp() { - JsBundleRuleProvider jsBundleRule = new JsBundleRuleProvider(); + JsBundleRuleProvider jsBundleRule = new JsBundleRuleProvider(haloPluginManager); pluginReconciler = new PluginReconciler(extensionClient, haloPluginManager, jsBundleRule); when(haloPluginManager.getPlugin(any())).thenReturn(pluginWrapper); - when(pluginWrapper.getRuntimeMode()).thenReturn(RuntimeMode.DEPLOYMENT); when(haloPluginManager.getUnresolvedPlugins()).thenReturn(List.of()); } diff --git a/src/test/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactoryTest.java b/src/test/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactoryTest.java index 4234d4bac..65c134a78 100644 --- a/src/test/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactoryTest.java +++ b/src/test/java/run/halo/app/plugin/resources/ReverseProxyRouterFunctionFactoryTest.java @@ -17,6 +17,7 @@ import org.springframework.web.reactive.function.server.ServerResponse; import run.halo.app.core.extension.ReverseProxy; import run.halo.app.extension.ExtensionClient; import run.halo.app.extension.Metadata; +import run.halo.app.plugin.HaloPluginManager; import run.halo.app.plugin.PluginApplicationContext; import run.halo.app.plugin.PluginConst; @@ -34,12 +35,14 @@ class ReverseProxyRouterFunctionFactoryTest { @Mock private PluginApplicationContext pluginApplicationContext; + @Mock + private HaloPluginManager haloPluginManager; private ReverseProxyRouterFunctionFactory reverseProxyRouterFunctionFactory; @BeforeEach void setUp() { - JsBundleRuleProvider jsBundleRuleProvider = new JsBundleRuleProvider(); + JsBundleRuleProvider jsBundleRuleProvider = new JsBundleRuleProvider(haloPluginManager); reverseProxyRouterFunctionFactory = new ReverseProxyRouterFunctionFactory(extensionClient, jsBundleRuleProvider);