diff --git a/build.gradle.kts b/build.gradle.kts index 2264155..29f007e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,10 +14,10 @@ plugins { } setupJava("xyz.srnyx", "3.1.0", "A simple library for JDA Discord bots", JavaVersion.VERSION_19) -application.mainClass.set("xyz.srnyx.lazylibrary.LazyLibrary") +application.mainClass.set("xyz.srnyx.lazylibrary.LazyLoader") addCompilerArgs("-parameters") -repository(Repository.MAVEN_CENTRAL, Repository.JITPACK) +repository(Repository.ALESSIO_DP, Repository.MAVEN_CENTRAL, Repository.JITPACK) dependencies { compileOnly("net.dv8tion", "JDA", "5.0.2") // JDA implementation("xyz.srnyx", "java-utilities", "a073202b43") // General Java utility library diff --git a/src/main/java/xyz/srnyx/lazylibrary/LazyLoader.java b/src/main/java/xyz/srnyx/lazylibrary/LazyLoader.java new file mode 100644 index 0000000..bdd86b2 --- /dev/null +++ b/src/main/java/xyz/srnyx/lazylibrary/LazyLoader.java @@ -0,0 +1,40 @@ +package xyz.srnyx.lazylibrary; + +import net.byteflux.libby.LibraryManager; +import net.byteflux.libby.classloader.URLClassLoaderHelper; +import net.byteflux.libby.logging.adapters.JDKLogAdapter; + +import org.jetbrains.annotations.NotNull; + +import java.net.URLClassLoader; +import java.nio.file.Path; +import java.util.logging.Logger; + + +public class LazyLoader { + @NotNull public final LazyManager libraryManager = new LazyManager(); + + public LazyLoader() { + RuntimeLibrary.JDA.getLibrary(this); + RuntimeLibrary.JAVA_UTILITIES.getLibrary(this); + RuntimeLibrary.BOT_COMMANDS.getLibrary(this); + RuntimeLibrary.CONFIGURATE_YAML.getLibrary(this); + RuntimeLibrary.POSTEGRESQL.getLibrary(this); + RuntimeLibrary.HIKARCICP.getLibrary(this); + RuntimeLibrary.LOGBACK_CLASSIC.getLibrary(this); + } + + public static class LazyManager extends LibraryManager { + @NotNull private final URLClassLoaderHelper classLoader; + + public LazyManager() { + super(new JDKLogAdapter(Logger.getLogger("LazyManager")), Path.of("."), "libs"); + classLoader = new URLClassLoaderHelper((URLClassLoader) getClass().getClassLoader(), this); + } + + @Override + protected void addToClasspath(@NotNull Path file) { + classLoader.addToClasspath(file); + } + } +} diff --git a/src/main/java/xyz/srnyx/lazylibrary/RuntimeLibrary.java b/src/main/java/xyz/srnyx/lazylibrary/RuntimeLibrary.java new file mode 100644 index 0000000..c196f4a --- /dev/null +++ b/src/main/java/xyz/srnyx/lazylibrary/RuntimeLibrary.java @@ -0,0 +1,119 @@ +package xyz.srnyx.lazylibrary; + +import net.byteflux.libby.Library; +import net.byteflux.libby.LibraryManager; + +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.Collections; +import java.util.TreeSet; +import java.util.function.Function; + + +/** + * All runtime libraries that Lazy Library downloads + */ +public enum RuntimeLibrary { + /** + * net.dv8tion:JDA + */ + JDA("https://repo1.maven.org/maven2/", loader -> Library.builder() + .groupId("net{}dv8tion") + .artifactId("JDA") + .version("5.0.0-beta.24").build()), + /** + * xyz.srnyx:java-utilities + */ + JAVA_UTILITIES("https://jitpack.io/", loader -> Library.builder() + .groupId("xyz{}srnyx") + .artifactId("java-utilities") + .version("a71551bc2d").build()), + /** + * org.javassist:javassist + */ + BOT_COMMANDS("https://repo1.maven.org/maven2/", loader -> Library.builder() + .groupId("io{}github{}freya022") + .artifactId("BotCommands") + .version("2.10.3").build()), + /** + * org.spongepowered:configurate-yaml + */ + CONFIGURATE_YAML("https://repo1.maven.org/maven2/", loader -> Library.builder() + .groupId("org{}spongepowered") + .artifactId("configurate-yaml") + .version("4.1.2").build()), + /** + * org.postgresql:postgresql + */ + POSTEGRESQL("https://repo1.maven.org/maven2/", loader -> Library.builder() + .groupId("org{}postgresql") + .artifactId("postgresql") + .version("42.7.3").build()), + /** + * com.zaxxer:HikariCP + */ + HIKARCICP("https://repo1.maven.org/maven2/", loader -> Library.builder() + .groupId("com{}zaxxer") + .artifactId("HikariCP") + .version("5.1.0").build()), + /** + * ch.qos.logback:logback-classic + */ + LOGBACK_CLASSIC("https://repo1.maven.org/maven2/", loader -> Library.builder() + .groupId("ch{}qos{}logback") + .artifactId("logback-classic") + .version("1.5.3").build()); + + /** + * The repositories to add before loading the library + */ + @NotNull public final TreeSet repositories; + /** + * The library to load + */ + @NotNull public final Function library; + + /** + * Creates a new {@link RuntimeLibrary} + * + * @param repositories {@link #repositories} + * @param library {@link #library} + */ + RuntimeLibrary(@NotNull Collection repositories, @NotNull Function library) { + this.repositories = new TreeSet<>(repositories); + this.library = library; + } + + /** + * Creates a new {@link RuntimeLibrary} with a single repository + * + * @param repository the repository to add + * @param library the library to load + */ + RuntimeLibrary(@NotNull String repository, @NotNull Function library) { + this.repositories = new TreeSet<>(Collections.singleton(repository)); + this.library = library; + } + + /** + * Gets the library to load + * + * @param loader the {@link LazyLoader} to load the library into + */ + @NotNull + public Library getLibrary(@NotNull LazyLoader loader) { + return library.apply(loader); + } + + /** + * Downloads and loads the library into the specified bot + * + * @param loader the {@link LazyLoader} to load the library into + */ + public void load(@NotNull LazyLoader loader) { + final LibraryManager manager = loader.libraryManager; + for (final String repository : repositories) manager.addRepository(repository); + manager.loadLibrary(getLibrary(loader)); + } +}