Implemented config & doc pregen

#59
The doc gen works
This commit is contained in:
Norbi Peti 2019-04-08 12:46:47 +02:00
parent 9ea811ba40
commit c8067257f9
7 changed files with 58 additions and 6 deletions

View file

@ -21,7 +21,7 @@
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.19" level="project" /> <orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.19" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.12-SNAPSHOT" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.12-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-io:commons-io:1.3.2" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: commons-io:commons-io:1.3.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.TBMCPlugins.ButtonCore:Towny:master-98b73aaac3-1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: com.github.TBMCPlugins.ButtonCore:Towny:master-03eb8d480a-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.milkbowl:VaultAPI:master-68f14eca20-1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: com.github.milkbowl:VaultAPI:master-68f14eca20-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.13.1-R0.1-SNAPSHOT" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.13.1-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" /> <orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />

View file

@ -53,7 +53,7 @@ public class Channel {
*/ */
public final ConfigData<String> DisplayName() { public final ConfigData<String> DisplayName() {
throwGame(); throwGame();
return component.getConfig().getData(ID + ".displayName", defDisplayName); return component.getConfig().getData(ID + ".displayName", defDisplayName); //TODO: Use config map
} }
public final ConfigData<Color> Color() { public final ConfigData<Color> Color() {

View file

@ -42,6 +42,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
@Override @Override
public final void onEnable() { public final void onEnable() {
loadConfig(); loadConfig();
IHaveConfig.pregenConfig(this, iConfig);
try { try {
pluginEnable(); pluginEnable();
} catch (Exception e) { } catch (Exception e) {

View file

@ -85,6 +85,7 @@ public abstract class Component<TP extends JavaPlugin> {
} }
component.plugin = plugin; component.plugin = plugin;
updateConfig(plugin, component); updateConfig(plugin, component);
IHaveConfig.pregenConfig(component, component.config);
component.register(plugin); component.register(plugin);
components.put(component.getClass(), component); components.put(component.getClass(), component);
if (plugin instanceof ButtonPlugin) if (plugin instanceof ButtonPlugin)

View file

@ -4,6 +4,7 @@ import buttondevteam.core.MainPlugin;
import buttondevteam.lib.ThorpeUtils; import buttondevteam.lib.ThorpeUtils;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration; import org.bukkit.configuration.Configuration;
@ -29,6 +30,7 @@ public class ConfigData<T> {
* May be null for testing * May be null for testing
*/ */
private final ConfigurationSection config; private final ConfigurationSection config;
@Getter
private final String path; private final String path;
private final T def; private final T def;
private final Object primitiveDef; private final Object primitiveDef;

View file

@ -1,10 +1,10 @@
package buttondevteam.lib.architecture; package buttondevteam.lib.architecture;
import buttondevteam.core.MainPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import lombok.Getter; import lombok.Getter;
import lombok.val; import lombok.val;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap; import java.util.HashMap;
import java.util.function.Function; import java.util.function.Function;
@ -117,4 +117,30 @@ public final class IHaveConfig {
} }
return (ConfigData<T>) data; return (ConfigData<T>) data;
} }
/**
* Generates the config YAML.
*
* @param obj The object which has config methods
* @param config The config that holds the values
*/
public static void pregenConfig(Object obj, IHaveConfig config) {
val ms = obj.getClass().getDeclaredMethods();
for (val m : ms) {
if (!m.getReturnType().getName().equals(ConfigData.class.getName())) continue;
try {
if (m.getParameterCount() == 0) {
m.setAccessible(true);
ConfigData<?> c = (ConfigData<?>) m.invoke(obj);
if (!c.getPath().equals(m.getName()))
MainPlugin.Instance.getLogger().warning("Config name does not match: " + c.getPath() + " instead of " + m.getName()); //TODO: Set it here
c.get(); //Saves the default value if needed - also checks validity
} else if (m.getParameterCount() == 1) {
//TODO: Config map
}
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to pregenerate " + m.getName() + " for " + obj + "!", e);
}
}
}
} }

View file

@ -4,7 +4,11 @@ import org.bukkit.configuration.file.YamlConfiguration;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier; import javax.lang.model.element.Modifier;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.StandardLocation; import javax.tools.StandardLocation;
import java.io.File; import java.io.File;
@ -29,11 +33,29 @@ public class ConfigProcessor {
public void process(Element targetcl) { public void process(Element targetcl) {
if (targetcl.getModifiers().contains(Modifier.ABSTRACT)) return; if (targetcl.getModifiers().contains(Modifier.ABSTRACT)) return;
final String path = "components." + targetcl.getSimpleName();
for (Element e : targetcl.getEnclosedElements()) {
/*System.out.println("Element: "+e);
System.out.println("Type: "+e.getClass()+" - "+e.getKind());
if(e instanceof ExecutableElement)
System.out.println("METHOD!");*/
if (!(e instanceof ExecutableElement)) continue;
TypeMirror tm = ((ExecutableElement) e).getReturnType();
if (tm.getKind() != TypeKind.DECLARED) continue;
DeclaredType dt = (DeclaredType) tm;
if (!dt.asElement().getSimpleName().contentEquals("ConfigData")) return;
System.out.println("Config: " + e.getSimpleName());
System.out.println("Value: " + ((ExecutableElement) e).getDefaultValue());
String doc = procEnv.getElementUtils().getDocComment(e);
if (doc == null) continue;
System.out.println("DOC: " + doc);
yaml.set(path + "." + e.getSimpleName() + "_doc", doc); //methodName_doc
}
String javadoc = procEnv.getElementUtils().getDocComment(targetcl); String javadoc = procEnv.getElementUtils().getDocComment(targetcl);
if (javadoc == null) return; if (javadoc == null) return;
System.out.println("JAVADOC"); //TODO: Config methods System.out.println("JAVADOC");
System.out.println(javadoc); System.out.println(javadoc);
yaml.set("components." + targetcl.getSimpleName() + "._doc", javadoc); yaml.set(path + "._doc", javadoc);
try { try {
yaml.save(file); yaml.save(file);
} catch (IOException e) { } catch (IOException e) {