Skip to content

Commit

Permalink
refactor repositories to be more abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Dec 15, 2024
1 parent 53cb4a7 commit 2585627
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 25 deletions.
3 changes: 2 additions & 1 deletion api/src/main/java/revxrsal/zapper/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.jetbrains.annotations.CheckReturnValue;
import org.jetbrains.annotations.NotNull;
import revxrsal.zapper.repository.Repository;

import java.io.File;
import java.io.InputStream;
Expand Down Expand Up @@ -75,7 +76,7 @@ public int hashCode() {
}

@CheckReturnValue
public @NotNull DependencyDownloadResult download(File file, Repository repository) {
public @NotNull DependencyDownloadResult download(@NotNull File file, @NotNull Repository repository) {
try {
if (!file.exists()) {
file.getParentFile().mkdirs();
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/revxrsal/zapper/DependencyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import revxrsal.zapper.classloader.URLClassLoaderWrapper;
import revxrsal.zapper.relocation.Relocation;
import revxrsal.zapper.relocation.Relocator;
import revxrsal.zapper.repository.Repository;

import java.io.File;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void load() {
if (result.wasSuccessful())
break;
else
(failedRepos == null ? failedRepos = new ArrayList<>() : failedRepos).add(repository.getRepositoryURL());
(failedRepos == null ? failedRepos = new ArrayList<>() : failedRepos).add(repository.toString());
}
if (failedRepos != null) {
throw new DependencyDownloadException(dep, "Could not find dependency in any of the following repositories: " + String.join("\n", failedRepos));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.NotNull;
import revxrsal.zapper.relocation.Relocation;
import revxrsal.zapper.repository.Repository;
import revxrsal.zapper.util.ClassLoaderReader;

import java.io.BufferedReader;
Expand Down
4 changes: 1 addition & 3 deletions api/src/main/java/revxrsal/zapper/relocation/Relocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

import org.jetbrains.annotations.NotNull;
import revxrsal.zapper.Dependency;
import revxrsal.zapper.Repository;
import revxrsal.zapper.classloader.IsolatedClassLoader;
import revxrsal.zapper.util.ClassLoaderReader;
import revxrsal.zapper.repository.Repository;

import java.io.File;
import java.lang.reflect.Constructor;
Expand All @@ -37,7 +36,6 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* The relocator utility
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package revxrsal.zapper.repository;

import org.jetbrains.annotations.NotNull;
import revxrsal.zapper.Dependency;

import java.io.File;
import java.net.URL;

/**
* Represents the local maven repository
*/
final class LocalMavenRepository implements Repository {

private final @NotNull File directory;

LocalMavenRepository(@NotNull File directory) {
this.directory = directory;
}

@Override
public @NotNull URL resolve(@NotNull Dependency dependency) throws Exception {
String systemPath = dependency.getMavenPath().replace('/', File.separatorChar);
File file = new File(directory, systemPath);
return file.toURI().toURL();
}

@Override
public String toString() {
return directory.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,46 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package revxrsal.zapper;
package revxrsal.zapper.repository;

import org.jetbrains.annotations.NotNull;
import revxrsal.zapper.Dependency;

import java.net.MalformedURLException;
import java.net.URL;

/**
* Represents a Maven repository with a URL
*/
public class Repository {
final class MavenRepository implements Repository {

private static final Repository MAVEN_CENTRAL = new Repository("https://repo1.maven.org/maven2/");
private static final Repository JITPACK = new Repository("https://jitpack.io/");
private static final Repository MINECRAFT = new Repository("https://libraries.minecraft.net/");
private static final Repository PAPER = new Repository("https://papermc.io/repo/repository/maven-public/");
private static final MavenRepository MAVEN_CENTRAL = new MavenRepository("https://repo1.maven.org/maven2/");
private static final MavenRepository JITPACK = new MavenRepository("https://jitpack.io/");
private static final MavenRepository MINECRAFT = new MavenRepository("https://libraries.minecraft.net/");
private static final MavenRepository PAPER = new MavenRepository("https://papermc.io/repo/repository/maven-public/");

public static @NotNull Repository mavenCentral() {
public static @NotNull MavenRepository mavenCentral() {
return MAVEN_CENTRAL;
}

public static @NotNull Repository jitpack() {
public static @NotNull MavenRepository jitpack() {
return JITPACK;
}

public static @NotNull Repository minecraft() {
public static @NotNull MavenRepository minecraft() {
return MINECRAFT;
}

public static @NotNull Repository paper() {
public static @NotNull MavenRepository paper() {
return PAPER;
}

public static @NotNull Repository maven(@NotNull String url) {
return new Repository(url);
public static @NotNull MavenRepository maven(@NotNull String url) {
return new MavenRepository(url);
}

private final String repoURL;

private Repository(@NotNull String repoURL) {
private MavenRepository(@NotNull String repoURL) {
if (repoURL.charAt(repoURL.length() - 1) != '/')
repoURL += '/';
this.repoURL = repoURL;
Expand All @@ -70,12 +70,12 @@ public String getRepositoryURL() {
return repoURL;
}

public URL resolve(Dependency dependency) {
try {
return new URL(repoURL + dependency.getMavenPath());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
@Override
public String toString() {
return getRepositoryURL();
}

public @NotNull URL resolve(@NotNull Dependency dependency) throws Exception {
return new URL(repoURL + dependency.getMavenPath());
}
}
100 changes: 100 additions & 0 deletions api/src/main/java/revxrsal/zapper/repository/Repository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package revxrsal.zapper.repository;

import org.jetbrains.annotations.NotNull;
import revxrsal.zapper.Dependency;

import java.io.File;
import java.net.URL;

/**
* Represents a repository that can resolve {@link revxrsal.zapper.Dependency dependencies}
*/
public interface Repository {

/**
* Resolves the dependency URL that is downloaded.
*
* @param dependency Dependency to resolve
* @return The URL to resolve
* @throws Exception any exception that indicates that this repository could not
* resolve the dependency
*/
@NotNull URL resolve(@NotNull Dependency dependency) throws Exception;

/**
* Creates a Maven repository from the specified URL.
*
* @param url the URL of the repository
* @return the configured Maven repository
*/
static @NotNull Repository maven(@NotNull URL url) {
return MavenRepository.maven(url.toString());
}

/**
* Creates a Maven repository from the specified URL string.
*
* @param url the URL of the repository as a string
* @return the configured Maven repository
*/
static @NotNull Repository maven(@NotNull String url) {
return MavenRepository.maven(url);
}

/**
* Returns a repository representing the local Maven directory (default: ~/.m2).
*
* @return the local Maven repository
*/
static @NotNull Repository mavenLocal() {
String userHome = System.getProperty("user.home");
File m2 = new File(userHome, ".m2");
return new LocalMavenRepository(m2);
}

/**
* Returns a repository representing a custom local Maven directory.
*
* @param directory the local Maven directory
* @return the local Maven repository
*/
static @NotNull Repository mavenLocal(@NotNull File directory) {
return new LocalMavenRepository(directory);
}

/**
* Returns the Maven Central repository.
*
* @return the Maven Central repository
*/
static @NotNull Repository mavenCentral() {
return MavenRepository.mavenCentral();
}

/**
* Returns the JitPack repository.
*
* @return the JitPack repository
*/
static @NotNull Repository jitpack() {
return MavenRepository.jitpack();
}

/**
* Returns the PaperMC repository.
*
* @return the PaperMC repository
*/
static @NotNull Repository paper() {
return MavenRepository.paper();
}

/**
* Returns the Minecraft repository.
*
* @return the Minecraft repository
*/
static @NotNull Repository minecraft() {
return MavenRepository.minecraft();
}
}

0 comments on commit 2585627

Please sign in to comment.