refactor: plugin template

This commit is contained in:
guqing
2022-06-24 11:38:39 +08:00
parent 0d6bcddac8
commit c06a1c0dec
10 changed files with 67 additions and 123 deletions

2
.gitignore vendored
View File

@@ -9,7 +9,7 @@ logs/
/out/
!gradle/wrapper/gradle-wrapper.jar
bin/
*.jar
### STS ###
.apt_generated
.classpath

View File

@@ -5,7 +5,7 @@ plugins {
}
group 'io.github.guqing'
version '1.0-SNAPSHOT'
version '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_17
repositories {
@@ -21,6 +21,9 @@ bootJar {
jar {
enabled = true
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest.attributes(
'Plugin-Version': "${project.version}",
)
from {
configurations.runtimeClasspath.collect {
it.isDirectory() ? it : zipTree(it)
@@ -29,11 +32,13 @@ jar {
}
dependencies {
compileOnly files("lib/halo-2.0.0-SNAPSHOT-plain.jar")
implementation 'io.github.guqing:pluggable-suite:0.0.1-SNAPSHOT'
compileOnly "io.swagger.core.v3:swagger-core-jakarta:2.2.0"
compileOnly 'org.springframework.boot:spring-boot-starter-webflux'
annotationProcessor 'io.github.guqing:pluggable-suite:0.0.1-SNAPSHOT'
compileOnly files("lib/halo-2.0.0-SNAPSHOT-plain.jar")
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

Binary file not shown.

View File

@@ -0,0 +1,26 @@
package io.github.guqing.template;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import run.halo.app.extension.AbstractExtension;
import run.halo.app.extension.GVK;
/**
* @author guqing
* @since 2.0.0
*/
@GVK(group = "apple.guqing.xyz", kind = "Apple",
version = "v1alpha1", singular = "apple", plural = "apples")
@Data
@EqualsAndHashCode(callSuper = true)
public class Apple extends AbstractExtension {
@Schema(required = true, minLength = 1)
private String varieties;
private String color;
private String size;
}

View File

@@ -0,0 +1,30 @@
package io.github.guqing.template;
import org.pf4j.PluginWrapper;
import run.halo.app.extension.Scheme;
import run.halo.app.extension.SchemeManager;
import run.halo.app.plugin.BasePlugin;
/**
* @author guqing
* @since 2.0.0
*/
public class ApplePlugin extends BasePlugin {
private final SchemeManager schemeManager;
public ApplePlugin(PluginWrapper wrapper) {
super(wrapper);
this.schemeManager = getApplicationContext().getBean(SchemeManager.class);
}
@Override
public void start() {
schemeManager.register(Apple.class);
}
@Override
public void stop() {
Scheme scheme = schemeManager.get(Apple.class);
schemeManager.unregister(scheme);
}
}

View File

@@ -1,27 +0,0 @@
package io.github.guqing.template.post;
import java.time.LocalDateTime;
import lombok.Data;
/**
* @author guqing
* @since 2.0.0
*/
@Data
public class Post {
private String id;
private String userId;
private String topicId;
private String raw;
private String cooked;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private LocalDateTime deletedAt;
private String replyToPostNumber;
private Integer replyCount;
private Integer quoteCount;
private Integer likeCount;
private Integer offTopicCount;
private Integer postType;
private Integer sortOrder;
}

View File

@@ -1,28 +0,0 @@
package io.github.guqing.template.post;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import run.halo.app.plugin.ApiVersion;
/**
* @author guqing
* @since 2.0.0
*/
@ApiVersion("v1alpha1")
@RestController
@RequestMapping("posts")
public class PostController {
private final PostService postService;
public PostController(PostService postService) {
this.postService = postService;
}
@GetMapping
public Flux<Post> list() {
return postService.list();
}
}

View File

@@ -1,43 +0,0 @@
package io.github.guqing.template.post;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
/**
* @author guqing
* @since 2.0.0
*/
@Component
public class PostRepository {
private final Map<String, Post> postMap = new ConcurrentHashMap<>();
public PostRepository() {
Post post = new Post();
post.setId(UUID.randomUUID().toString());
post.setRaw("静态方法适合生成简单的序列,当需要复杂的逻辑时,则应该使用 generate() 或 create() 方法。\n");
post.setCooked(post.getRaw());
post.setPostType(1);
post.setCreatedAt(LocalDateTime.now());
post.setLikeCount(1);
post.setReplyCount(0);
post.setOffTopicCount(0);
post.setUserId(UUID.randomUUID().toString());
post.setTopicId(UUID.randomUUID().toString());
post.setSortOrder(1);
postMap.put(post.getId(), post);
}
public Flux<Post> findAll() {
return Flux.create(sink -> {
for (Post value : postMap.values()) {
sink.next(value);
}
sink.complete();
});
}
}

View File

@@ -1,20 +0,0 @@
package io.github.guqing.template.post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
/**
* @author guqing
* @since 2.0.0
*/
@Service
public class PostService {
@Autowired
private PostRepository postRepository;
public Flux<Post> list() {
return postRepository.findAll();
}
}

View File

@@ -2,14 +2,15 @@ apiVersion: plugin.halo.run/v1alpha1
kind: Plugin
metadata:
# The name defines how the plugin is invoked,A unique name
name: io.github.guqing.apples
name: apples
spec:
enabled: true
# 'version' is a valid semantic version string (see semver.org).
version: 0.0.1
version: 0.0.1-SNAPSHOT
requires: "*"
author: guqing
logo: https://guqing.xyz/avatar
pluginClass: io.github.guqing.template.ApplePlugin
# 'homepage' usually links to the GitHub repository of the plugin
homepage: https://github.com/guqing/halo-plugin-template
# 'displayName' explains what the plugin does in only a few words