Skip to content

Commit

Permalink
Fix incorrectly non-git folder opening and fix start panic when no th…
Browse files Browse the repository at this point in the history
…emes found (#1479)
  • Loading branch information
JohnNiang authored Sep 16, 2021
1 parent a046f3e commit 0059668
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package run.halo.app.core;

import org.jetbrains.annotations.NotNull;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -26,18 +25,18 @@ public class CommonResultControllerAdvice implements ResponseBodyAdvice<Object>

@Override
public boolean supports(MethodParameter returnType,
@NotNull Class<? extends HttpMessageConverter<?>> converterType) {
@NonNull Class<? extends HttpMessageConverter<?>> converterType) {
return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
}

@Override
@NonNull
public final Object beforeBodyWrite(@Nullable Object body,
@NotNull MethodParameter returnType,
@NotNull MediaType contentType,
@NotNull Class<? extends HttpMessageConverter<?>> converterType,
@NotNull ServerHttpRequest request,
@NotNull ServerHttpResponse response) {
@NonNull MethodParameter returnType,
@NonNull MediaType contentType,
@NonNull Class<? extends HttpMessageConverter<?>> converterType,
@NonNull ServerHttpRequest request,
@NonNull ServerHttpResponse response) {
MappingJacksonValue container = getOrCreateContainer(body);
// The contain body will never be null
beforeBodyWriteInternal(container, contentType, returnType, request, response);
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/run/halo/app/listener/StartedListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,9 @@ private void initThemes() {
// Create theme folder
Path themePath = themeService.getBasePath();

// Fix the problem that the project cannot start after moving to a new server
if (Files.notExists(themePath) || !isInstalled) {
if (themeService.fetchThemePropertyBy(HaloConst.DEFAULT_THEME_ID).isEmpty()) {
FileUtils.copyFolder(source, themePath);
log.debug("Copied theme folder from [{}] to [{}]", source, themePath);
} else {
log.debug("Skipped copying theme folder due to existence of theme folder");
log.info("Copied theme folder from [{}] to [{}]", source, themePath);
}
} catch (Exception e) {
if (e instanceof FileNotFoundException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ public ThemeProperty getActivatedThemeProperty() {

@Override
public Optional<ThemeProperty> fetchThemePropertyByThemeId(String themeId) {
if (StringUtils.equals(themeId, getActivatedThemeId())) {
return Optional.of(getActivatedThemeProperty());
}

return ThemePropertyScanner.INSTANCE.scan(getThemeRootPath(), null)
.stream()
.filter(property -> Objects.equals(themeId, property.getId()))
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import run.halo.app.exception.ForbiddenException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.exception.ServiceException;
import run.halo.app.exception.ThemeNotFoundException;
import run.halo.app.exception.ThemeNotSupportException;
import run.halo.app.exception.ThemePropertyMissingException;
import run.halo.app.exception.ThemeUpdateException;
import run.halo.app.handler.theme.config.ThemeConfigResolver;
import run.halo.app.handler.theme.config.support.Group;
import run.halo.app.handler.theme.config.support.Option;
import run.halo.app.handler.theme.config.support.ThemeProperty;
import run.halo.app.model.support.HaloConst;
import run.halo.app.model.support.ThemeFile;
Expand Down Expand Up @@ -131,7 +133,7 @@ public List<String> listCustomTemplates(@NonNull String themeId, @NonNull String
Path themePath = Paths.get(themeProperty.getThemePath());
try (Stream<Path> pathStream = Files.list(themePath)) {
return pathStream.filter(path ->
StringUtils.startsWithIgnoreCase(path.getFileName().toString(), prefix))
StringUtils.startsWithIgnoreCase(path.getFileName().toString(), prefix))
.map(path -> {
// Remove prefix
final var customTemplate = StringUtils
Expand Down Expand Up @@ -315,13 +317,17 @@ public String getActivatedThemeId() {
@Override
@NonNull
public ThemeProperty getActivatedTheme() {
return fetchActivatedTheme().orElseThrow();
return themeRepository.getActivatedThemeProperty();
}

@Override
@NonNull
public Optional<ThemeProperty> fetchActivatedTheme() {
return Optional.of(themeRepository.getActivatedThemeProperty());
try {
return Optional.of(themeRepository.getActivatedThemeProperty());
} catch (ThemeNotFoundException tne) {
return Optional.empty();
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/run/halo/app/theme/GitThemeUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public ThemeProperty merge(ThemeProperty oldThemeProperty, ThemeProperty newThem

final var oldThemePath = Paths.get(oldThemeProperty.getThemePath());
// open old git repo
try (final var oldGit = Git.open(oldThemePath.toFile())) {
try (final var oldGit = Git.init().setDirectory(oldThemePath.toFile()).call()) {
// 0. commit old repo
commitAutomatically(oldGit);

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/run/halo/app/utils/footnotes/Footnote.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.ast.ReferencingNode;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;
import org.springframework.lang.NonNull;
import run.halo.app.utils.footnotes.internal.FootnoteRepository;

/**
Expand All @@ -30,7 +30,7 @@ public void setReferenceOrdinal(int referenceOrdinal) {

protected int referenceOrdinal;

@NotNull
@NonNull
@Override
public BasedSequence getReference() {
return text;
Expand Down Expand Up @@ -80,14 +80,14 @@ public void setFootnoteBlock(FootnoteBlock footnoteBlock) {
this.footnoteBlock = footnoteBlock;
}

@NotNull
@NonNull
@Override
public BasedSequence[] getSegments() {
return new BasedSequence[] {openingMarker, text, closingMarker};
}

@Override
public void getAstExtra(@NotNull StringBuilder out) {
public void getAstExtra(@NonNull StringBuilder out) {
out.append(" ordinal: ")
.append(footnoteBlock != null ? footnoteBlock.getFootnoteOrdinal() : 0).append(" ");
delimitedSegmentSpanChars(out, openingMarker, text, closingMarker, "text");
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/run/halo/app/utils/footnotes/FootnoteBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.vladsch.flexmark.util.sequence.BasedSequence;
import com.vladsch.flexmark.util.sequence.SequenceUtils;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.utils.footnotes.internal.FootnoteRepository;

/**
Expand Down Expand Up @@ -43,7 +43,7 @@ public void setFootnoteReferences(int footnoteReferences) {

@Nullable
@Override
public Footnote getReferencingNode(@NotNull Node node) {
public Footnote getReferencingNode(@NonNull Node node) {
return node instanceof Footnote ? (Footnote) node : null;
}

Expand Down Expand Up @@ -74,15 +74,15 @@ public void setFootnoteOrdinal(int footnoteOrdinal) {
}

@Override
public void getAstExtra(@NotNull StringBuilder out) {
public void getAstExtra(@NonNull StringBuilder out) {
out.append(" ordinal: ").append(footnoteOrdinal).append(" ");
segmentSpan(out, openingMarker, "open");
segmentSpan(out, text, "text");
segmentSpan(out, closingMarker, "close");
segmentSpan(out, footnote, "footnote");
}

@NotNull
@NonNull
@Override
public BasedSequence[] getSegments() {
return new BasedSequence[] {openingMarker, text, closingMarker, footnote};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.format.options.ElementPlacement;
import com.vladsch.flexmark.util.format.options.ElementPlacementSort;
import org.jetbrains.annotations.NotNull;
import org.springframework.lang.NonNull;
import run.halo.app.utils.footnotes.internal.FootnoteBlockParser;
import run.halo.app.utils.footnotes.internal.FootnoteLinkRefProcessor;
import run.halo.app.utils.footnotes.internal.FootnoteNodeFormatter;
Expand Down Expand Up @@ -63,8 +63,8 @@ public void extend(Formatter.Builder formatterBuilder) {
}

@Override
public void extend(@NotNull HtmlRenderer.Builder htmlRendererBuilder,
@NotNull String rendererType) {
public void extend(@NonNull HtmlRenderer.Builder htmlRendererBuilder,
@NonNull String rendererType) {
if (htmlRendererBuilder.isRendererType("HTML")) {
htmlRendererBuilder.nodeRendererFactory(new FootnoteNodeRenderer.Factory());
}
Expand All @@ -77,7 +77,7 @@ public void extend(Parser.Builder parserBuilder) {
}

@Override
public void rendererOptions(@NotNull MutableDataHolder options) {
public void rendererOptions(@NonNull MutableDataHolder options) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.utils.footnotes.FootnoteBlock;
import run.halo.app.utils.footnotes.FootnoteExtension;

Expand Down Expand Up @@ -115,9 +115,9 @@ public boolean affectsGlobalScope() {
return false;
}

@NotNull
@NonNull
@Override
public BlockParserFactory apply(@NotNull DataHolder options) {
public BlockParserFactory apply(@NonNull DataHolder options) {
return new BlockFactory(options);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;
import org.springframework.lang.NonNull;
import run.halo.app.utils.footnotes.Footnote;
import run.halo.app.utils.footnotes.FootnoteBlock;
import run.halo.app.utils.footnotes.FootnoteExtension;
Expand All @@ -33,14 +33,14 @@ public int getBracketNestingLevel() {
}

@Override
public boolean isMatch(@NotNull BasedSequence nodeChars) {
public boolean isMatch(@NonNull BasedSequence nodeChars) {
return nodeChars.length() >= 3 && nodeChars.charAt(0) == '[' && nodeChars.charAt(1) == '^'
&& nodeChars.endCharAt(1) == ']';
}

@NotNull
@NonNull
@Override
public Node createNode(@NotNull BasedSequence nodeChars) {
public Node createNode(@NonNull BasedSequence nodeChars) {
BasedSequence footnoteId = nodeChars.midSequence(2, -1).trim();
FootnoteBlock footnoteBlock =
footnoteId.length() > 0 ? footnoteRepository.get(footnoteId.toString()) : null;
Expand All @@ -55,39 +55,39 @@ public Node createNode(@NotNull BasedSequence nodeChars) {
return footnote;
}

@NotNull
@NonNull
@Override
public BasedSequence adjustInlineText(@NotNull Document document, @NotNull Node node) {
public BasedSequence adjustInlineText(@NonNull Document document, @NonNull Node node) {
assert node instanceof Footnote;
return ((Footnote) node).getText();
}

@Override
public boolean allowDelimiters(@NotNull BasedSequence chars, @NotNull Document document,
@NotNull Node node) {
public boolean allowDelimiters(@NonNull BasedSequence chars, @NonNull Document document,
@NonNull Node node) {
return true;
}

@Override
public void updateNodeElements(@NotNull Document document, @NotNull Node node) {
public void updateNodeElements(@NonNull Document document, @NonNull Node node) {

}

public static class Factory implements LinkRefProcessorFactory {

@NotNull
@NonNull
@Override
public LinkRefProcessor apply(@NotNull Document document) {
public LinkRefProcessor apply(@NonNull Document document) {
return new FootnoteLinkRefProcessor(document);
}

@Override
public boolean getWantExclamationPrefix(@NotNull DataHolder options) {
public boolean getWantExclamationPrefix(@NonNull DataHolder options) {
return WANT_EXCLAMATION_PREFIX;
}

@Override
public int getBracketNestingLevel(@NotNull DataHolder options) {
public int getBracketNestingLevel(@NonNull DataHolder options) {
return BRACKET_NESTING_LEVEL;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.utils.footnotes.Footnote;
import run.halo.app.utils.footnotes.FootnoteBlock;
import run.halo.app.utils.footnotes.FootnoteExtension;
Expand Down Expand Up @@ -100,9 +100,9 @@ private void render(Footnote node, NodeFormatterContext context, MarkdownWriter

public static class Factory implements NodeFormatterFactory {

@NotNull
@NonNull
@Override
public NodeFormatter create(@NotNull DataHolder options) {
public NodeFormatter create(@NonNull DataHolder options) {
return new FootnoteNodeFormatter(options);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Locale;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.lang.NonNull;
import run.halo.app.utils.footnotes.Footnote;
import run.halo.app.utils.footnotes.FootnoteBlock;
import run.halo.app.utils.footnotes.FootnoteExtension;
Expand Down Expand Up @@ -53,8 +53,8 @@ public Set<RenderingPhase> getRenderingPhases() {
}

@Override
public void renderDocument(@NotNull NodeRendererContext context, @NotNull HtmlWriter html,
@NotNull Document document, @NotNull RenderingPhase phase) {
public void renderDocument(@NonNull NodeRendererContext context, @NonNull HtmlWriter html,
@NonNull Document document, @NonNull RenderingPhase phase) {
if (phase == RenderingPhase.BODY_TOP) {
if (recheckUndefinedReferences) {
// need to see if have undefined footnotes that were defined after parsing
Expand Down Expand Up @@ -171,9 +171,9 @@ private void render(Footnote node, NodeRendererContext context, HtmlWriter html)

public static class Factory implements NodeRendererFactory {

@NotNull
@NonNull
@Override
public NodeRenderer apply(@NotNull DataHolder options) {
public NodeRenderer apply(@NonNull DataHolder options) {
return new FootnoteNodeRenderer(options);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
import org.springframework.lang.NonNull;
import run.halo.app.utils.footnotes.Footnote;
import run.halo.app.utils.footnotes.FootnoteBlock;
import run.halo.app.utils.footnotes.FootnoteExtension;
Expand Down Expand Up @@ -79,19 +79,19 @@ public FootnoteRepository(DataHolder options) {
super(FootnoteExtension.FOOTNOTES_KEEP.get(options));
}

@NotNull
@NonNull
@Override
public DataKey<FootnoteRepository> getDataKey() {
return FootnoteExtension.FOOTNOTES;
}

@NotNull
@NonNull
@Override
public DataKey<KeepType> getKeepDataKey() {
return FootnoteExtension.FOOTNOTES_KEEP;
}

@NotNull
@NonNull
@Override
public Set<FootnoteBlock> getReferencedElements(Node parent) {
HashSet<FootnoteBlock> references = new HashSet<>();
Expand Down

0 comments on commit 0059668

Please sign in to comment.