mirror of https://github.com/halo-dev/halo
feat: add Halo version variable to theme model (#6677)
#### What type of PR is this? /kind feature /milestone 2.20.x /area theme #### What this PR does / why we need it: 主题支持通过 `${site.version}` 得到 Halo 版本号 #### Which issue(s) this PR fixes: Fixes #6676 #### Does this PR introduce a user-facing change? ```release-note 主题支持通过 `${site.version}` 得到 Halo 版本号 ```pull/6680/head
parent
07077f7d0c
commit
dcadd38843
|
@ -7,6 +7,7 @@ import org.springframework.web.server.ServerWebExchange;
|
|||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.infra.ExternalUrlSupplier;
|
||||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||
import run.halo.app.infra.SystemVersionSupplier;
|
||||
import run.halo.app.theme.finders.vo.SiteSettingVo;
|
||||
|
||||
/**
|
||||
|
@ -21,6 +22,7 @@ public class SiteSettingVariablesAcquirer implements ViewContextBasedVariablesAc
|
|||
|
||||
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||
private final ExternalUrlSupplier externalUrlSupplier;
|
||||
private final SystemVersionSupplier systemVersionSupplier;
|
||||
|
||||
@Override
|
||||
public Mono<Map<String, Object>> acquire(ServerWebExchange exchange) {
|
||||
|
@ -28,7 +30,8 @@ public class SiteSettingVariablesAcquirer implements ViewContextBasedVariablesAc
|
|||
.filter(configMap -> configMap.getData() != null)
|
||||
.map(configMap -> {
|
||||
SiteSettingVo siteSettingVo = SiteSettingVo.from(configMap)
|
||||
.withUrl(externalUrlSupplier.getURL(exchange.getRequest()));
|
||||
.withUrl(externalUrlSupplier.getURL(exchange.getRequest()))
|
||||
.withVersion(systemVersionSupplier.get().toString());
|
||||
return Map.of("site", siteSettingVo);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -25,6 +25,9 @@ public class SiteSettingVo {
|
|||
@With
|
||||
URL url;
|
||||
|
||||
@With
|
||||
String version;
|
||||
|
||||
String subtitle;
|
||||
|
||||
String logo;
|
||||
|
|
|
@ -6,6 +6,7 @@ import static org.mockito.Mockito.mock;
|
|||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.github.zafarkhaja.semver.Version;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
@ -20,6 +21,7 @@ import reactor.test.StepVerifier;
|
|||
import run.halo.app.extension.ConfigMap;
|
||||
import run.halo.app.infra.ExternalUrlSupplier;
|
||||
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||
import run.halo.app.infra.SystemVersionSupplier;
|
||||
import run.halo.app.theme.finders.vo.SiteSettingVo;
|
||||
|
||||
/**
|
||||
|
@ -32,6 +34,10 @@ import run.halo.app.theme.finders.vo.SiteSettingVo;
|
|||
public class SiteSettingVariablesAcquirerTest {
|
||||
@Mock
|
||||
private ExternalUrlSupplier externalUrlSupplier;
|
||||
|
||||
@Mock
|
||||
private SystemVersionSupplier systemVersionSupplier;
|
||||
|
||||
@Mock
|
||||
private SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||
|
||||
|
@ -45,6 +51,7 @@ public class SiteSettingVariablesAcquirerTest {
|
|||
|
||||
var url = new URL("https://halo.run");
|
||||
when(externalUrlSupplier.getURL(any())).thenReturn(url);
|
||||
when(systemVersionSupplier.get()).thenReturn(Version.parse("0.0.0-alpha.1"));
|
||||
when(environmentFetcher.getConfigMap()).thenReturn(Mono.just(configMap));
|
||||
|
||||
siteSettingVariablesAcquirer.acquire(mock(ServerWebExchange.class))
|
||||
|
@ -52,9 +59,13 @@ public class SiteSettingVariablesAcquirerTest {
|
|||
.consumeNextWith(result -> {
|
||||
assertThat(result).containsKey("site");
|
||||
assertThat(result.get("site")).isInstanceOf(SiteSettingVo.class);
|
||||
assertThat((SiteSettingVo) result.get("site"))
|
||||
var site = (SiteSettingVo) result.get("site");
|
||||
assertThat(site)
|
||||
.extracting(SiteSettingVo::getUrl)
|
||||
.isEqualTo(url);
|
||||
assertThat(site)
|
||||
.extracting(SiteSettingVo::getVersion)
|
||||
.isEqualTo("0.0.0-alpha.1");
|
||||
})
|
||||
.verifyComplete();
|
||||
verify(externalUrlSupplier).getURL(any());
|
||||
|
|
Loading…
Reference in New Issue