diff --git a/groovier-engine/pom.xml b/groovier-engine/pom.xml index 673b09b..e0352e7 100644 --- a/groovier-engine/pom.xml +++ b/groovier-engine/pom.xml @@ -5,7 +5,7 @@ groovier org.groovier - 0.0.4 + 0.0.51-SNAPSHOT 4.0.0 diff --git a/groovier-engine/src/main/java/com/ericlam/mc/groovier/GroovierAddon.java b/groovier-engine/src/main/java/com/ericlam/mc/groovier/GroovierAddon.java new file mode 100644 index 0000000..5a26fe4 --- /dev/null +++ b/groovier-engine/src/main/java/com/ericlam/mc/groovier/GroovierAddon.java @@ -0,0 +1,15 @@ +package com.ericlam.mc.groovier; + +import com.google.inject.Module; + +/** + * groovier addon + */ +public interface GroovierAddon { + + /** + * install guice module + * @param module module + */ + void installModule(Module module); +} diff --git a/groovier-plugin/pom.xml b/groovier-plugin/pom.xml index 2ea5bc5..3d99a7a 100644 --- a/groovier-plugin/pom.xml +++ b/groovier-plugin/pom.xml @@ -5,7 +5,7 @@ groovier org.groovier - 0.0.4 + 0.0.51-SNAPSHOT diff --git a/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierCore.java b/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierCore.java index b78ff03..32d0881 100644 --- a/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierCore.java +++ b/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierCore.java @@ -1,160 +1,180 @@ package com.ericlam.mc.groovier; -import com.ericlam.mc.groovier.providers.ArgumentParserProvider; -import com.ericlam.mc.groovier.providers.GroovierLifeCycleProvider; -import com.ericlam.mc.groovier.providers.ServiceInjectorProvider; -import com.ericlam.mc.groovier.scriptloaders.*; -import com.google.inject.Guice; -import com.google.inject.Injector; - -import javax.inject.Provider; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.nio.file.*; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import java.util.Collections; import java.util.Optional; import java.util.concurrent.CompletableFuture; +import java.util.logging.Level; -public class GroovierCore implements GroovierAPI { - - private final GroovierModule groovierModule = new GroovierModule(); - - private static GroovierAPI api; - - public GroovierCore() { - api = this; - - this.bindInstance(GroovierAPI.class, this); - this.addScriptLoader(ServiceScriptsManager.class); - this.addScriptLoader(CommandScriptsManager.class); - this.addScriptLoader(EventScriptsManager.class); - this.addScriptLoader(ArgumentScriptManager.class); - this.addScriptLoader(LifeCycleScriptsManager.class); - this.bindProvider(ArgumentParser.class, ArgumentParserProvider.class); - this.bindProvider(ServiceInjector.class, ServiceInjectorProvider.class); - this.bindProvider(GroovierLifeCycle.class, GroovierLifeCycleProvider.class); - } +import javax.inject.Provider; - public static GroovierAPI getApi() { - return Optional.ofNullable(api).orElseThrow(() -> new IllegalStateException("groovier not initialized")); - } +import org.jetbrains.annotations.NotNull; - private Injector injector; - private GroovierScriptLoader loader; +import com.ericlam.mc.groovier.providers.ArgumentParserProvider; +import com.ericlam.mc.groovier.providers.GroovierLifeCycleProvider; +import com.ericlam.mc.groovier.providers.ServiceInjectorProvider; +import com.ericlam.mc.groovier.scriptloaders.ArgumentScriptManager; +import com.ericlam.mc.groovier.scriptloaders.CommandScriptsManager; +import com.ericlam.mc.groovier.scriptloaders.EventScriptsManager; +import com.ericlam.mc.groovier.scriptloaders.GroovierLifeCycle; +import com.ericlam.mc.groovier.scriptloaders.LifeCycleScriptsManager; +import com.ericlam.mc.groovier.scriptloaders.ServiceScriptsManager; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Module; + +public class GroovierCore implements GroovierAPI, GroovierAddon { + + private final GroovierModule groovierModule = new GroovierModule(); + + private static GroovierAPI api; + + public GroovierCore() { + api = this; + + this.bindInstance(GroovierAPI.class, this); + this.addScriptLoader(ServiceScriptsManager.class); + this.addScriptLoader(CommandScriptsManager.class); + this.addScriptLoader(EventScriptsManager.class); + this.addScriptLoader(ArgumentScriptManager.class); + this.addScriptLoader(LifeCycleScriptsManager.class); + this.bindProvider(ArgumentParser.class, ArgumentParserProvider.class); + this.bindProvider(ServiceInjector.class, ServiceInjectorProvider.class); + this.bindProvider(GroovierLifeCycle.class, GroovierLifeCycleProvider.class); + } - private GroovierLifeCycle lifeCycle; + public static GroovierAPI getApi() { + return Optional.ofNullable(api).orElseThrow(() -> new IllegalStateException("groovier not initialized")); + } + private Injector injector; + private GroovierScriptLoader loader; - public void onLoad(ScriptPlugin plugin) { - groovierModule.bindScriptPlugin(plugin); - plugin.copyResources(); - } + private GroovierLifeCycle lifeCycle; - public void onEnable(ScriptPlugin plugin) { - injector = Guice.createInjector(groovierModule); - loader = injector.getInstance(GroovierScriptLoader.class); - loader.addClassPath(); - loader.loadAllScripts().whenComplete((v, e) -> { - if (e != null) { - plugin.getLogger().severe("error while loading scripts: "+e.getMessage()); - e.printStackTrace(); - } - lifeCycle = injector.getInstance(GroovierLifeCycle.class); - plugin.runSyncTask(() -> lifeCycle.onEnable()); - }); - } + public void onLoad(ScriptPlugin plugin) { + groovierModule.bindScriptPlugin(plugin); + plugin.copyResources(); + } + + + public void onEnable(ScriptPlugin plugin) { + injector = Guice.createInjector(groovierModule); + loader = injector.getInstance(GroovierScriptLoader.class); + loader.addClassPath(); + loader.loadAllScripts().whenComplete((v, e) -> { + if (e != null) { + plugin.getLogger().log(Level.SEVERE, e, () -> "error while loading scripts: " + e.getMessage()); + } + lifeCycle = injector.getInstance(GroovierLifeCycle.class); + plugin.runSyncTask(() -> lifeCycle.onEnable()); + }); + } - public void onDisable(ScriptPlugin plugin) { - lifeCycle.onDisable(); - loader.unloadAllScripts(); - } + public void onDisable(ScriptPlugin plugin) { + lifeCycle.onDisable(); + loader.unloadAllScripts(); + } - public CompletableFuture reloadAllScripts(){ - return loader.reloadAllScripts(); - } + public CompletableFuture reloadAllScripts() { + return loader.reloadAllScripts(); + } - @Override - public void addScriptLoader(Class scriptLoader) { - this.groovierModule.addReloadable(scriptLoader); - } + @Override + public void addScriptLoader(Class scriptLoader) { + this.groovierModule.addReloadable(scriptLoader); + } - @Override - public void bindRegisters(Class validator, T ins) { - this.groovierModule.bindRegisters(validator, ins); - } + @Override + public void bindRegisters(Class validator, T ins) { + this.groovierModule.bindRegisters(validator, ins); + } - @Override - public void bindInstance(Class type, T ins) { - this.groovierModule.bindInstance(type, ins); - } + @Override + public void bindInstance(Class type, T ins) { + this.groovierModule.bindInstance(type, ins); + } - @Override - public void bindType(Class type, Class clazz) { - this.groovierModule.bindType(type, clazz); - } + @Override + public void bindType(Class type, Class clazz) { + this.groovierModule.bindType(type, clazz); + } - @Override - public > void bindProvider(Class type, Class

clazz) { - this.groovierModule.bindProvider(type, clazz); - } + @Override + public > void bindProvider(Class type, Class

clazz) { + this.groovierModule.bindProvider(type, clazz); + } - @Override - public Injector getBaseInjector() { - return Optional.ofNullable(injector).orElseThrow(() -> new IllegalStateException("groovier not initialized")); - } + @Override + public void installModule(Module module) { + this.groovierModule.installModule(module); + } - @Override - public ServiceInjector getServiceInjector() { - return getBaseInjector().getInstance(ServiceInjector.class); - } + @Override + public Injector getBaseInjector() { + return Optional.ofNullable(injector).orElseThrow(() -> new IllegalStateException("groovier not initialized")); + } - @Override - public ArgumentParser getArgumentParser() { - return getBaseInjector().getInstance(ArgumentParser.class); - } + @Override + public ServiceInjector getServiceInjector() { + return getBaseInjector().getInstance(ServiceInjector.class); + } - public void copyFromJar(String source, final Path target) throws URISyntaxException, IOException { + @Override + public ArgumentParser getArgumentParser() { + return getBaseInjector().getInstance(ArgumentParser.class); + } - URL url = getClass().getResource(""); + public void copyFromJar(String source, final Path target) throws URISyntaxException, IOException { - if (url == null) { - throw new IllegalStateException("can't find resource inside jar"); - } - - URI resource = url.toURI(); + URL url = getClass().getResource(""); + + if (url == null) { + throw new IllegalStateException("can't find resource inside jar"); + } + + URI resource = url.toURI(); - try (FileSystem fileSystem = FileSystems.newFileSystem( - resource, - Collections.emptyMap() - )) { + try (FileSystem fileSystem = FileSystems.newFileSystem( + resource, + Collections.emptyMap() + )) { - final Path jarPath = fileSystem.getPath(source); + final Path jarPath = fileSystem.getPath(source); - Files.walkFileTree(jarPath, new SimpleFileVisitor<>() { + Files.walkFileTree(jarPath, new SimpleFileVisitor<>() { - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - Path currentTarget = target.resolve(jarPath.relativize(dir).toString()); - if (Files.notExists(currentTarget)) Files.createDirectories(currentTarget); - return FileVisitResult.CONTINUE; - } + @Override + public @NotNull FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + Path currentTarget = target.resolve(jarPath.relativize(dir).toString()); + if (Files.notExists(currentTarget)) Files.createDirectories(currentTarget); + return FileVisitResult.CONTINUE; + } - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - var targetPath = target.resolve(jarPath.relativize(file).toString()); - if (Files.notExists(targetPath)) Files.copy(file, targetPath, StandardCopyOption.REPLACE_EXISTING); - return FileVisitResult.CONTINUE; - } + @Override + public @NotNull FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + var targetPath = target.resolve(jarPath.relativize(file).toString()); + if (Files.notExists(targetPath)) Files.copy(file, targetPath, StandardCopyOption.REPLACE_EXISTING); + return FileVisitResult.CONTINUE; + } + + }); + + } - }); - } - - - } + } } diff --git a/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierModule.groovy b/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierModule.groovy index 4921f6e..2b9687f 100644 --- a/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierModule.groovy +++ b/groovier-plugin/src/main/groovy/com/ericlam/mc/groovier/GroovierModule.groovy @@ -1,6 +1,7 @@ package com.ericlam.mc.groovier import com.google.inject.AbstractModule +import com.google.inject.Module import com.google.inject.Scopes import com.google.inject.multibindings.Multibinder @@ -13,7 +14,9 @@ class GroovierModule extends AbstractModule { private final Map registerMap = new HashMap<>() private final Map classMap = new HashMap<>() - private final Map>> providerMap = new HashMap<>(); + private final Map>> providerMap = new HashMap<>() + + private final Set modules = new HashSet<>() private final GroovyClassLoader classLoader = new GroovyClassLoader(getClass().getClassLoader()) @@ -34,6 +37,7 @@ class GroovierModule extends AbstractModule { registerMap.forEach((type, obj) -> bind(type).toInstance(obj)) classMap.forEach((type, clazz) -> bind(type).to(clazz).in(Scopes.SINGLETON)) providerMap.forEach((type, provider) -> bind(type).toProvider(provider).in(Scopes.SINGLETON)) + modules.forEach(this::install) } @@ -61,4 +65,8 @@ class GroovierModule extends AbstractModule { this.registerMap.put(validator, ins) } + def installModule(Module module) { + this.modules.add(module) + } + } diff --git a/groovier-scripts/pom.xml b/groovier-scripts/pom.xml index ab5e663..2e9d239 100644 --- a/groovier-scripts/pom.xml +++ b/groovier-scripts/pom.xml @@ -5,7 +5,7 @@ groovier org.groovier - 0.0.4 + 0.0.51-SNAPSHOT 4.0.0 diff --git a/pom.xml b/pom.xml index c96463b..737f0e9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.groovier groovier - 0.0.4 + 0.0.51-SNAPSHOT pom Groovier @@ -260,7 +260,7 @@ org.junit.jupiter junit-jupiter-engine - 5.3.1 + 5.11.4 test