Skip to content

Commit

Permalink
Merge branch 'libby' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx authored Aug 20, 2024
2 parents 52c218d + 181eed3 commit 0cd87bf
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/xyz/srnyx/lazylibrary/LazyLoader.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
119 changes: 119 additions & 0 deletions src/main/java/xyz/srnyx/lazylibrary/RuntimeLibrary.java
Original file line number Diff line number Diff line change
@@ -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<String> repositories;
/**
* The library to load
*/
@NotNull public final Function<LazyLoader, Library> library;

/**
* Creates a new {@link RuntimeLibrary}
*
* @param repositories {@link #repositories}
* @param library {@link #library}
*/
RuntimeLibrary(@NotNull Collection<String> repositories, @NotNull Function<LazyLoader, Library> 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<LazyLoader, Library> 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));
}
}

0 comments on commit 0cd87bf

Please sign in to comment.