Unified and improved GitHubFetcher
This commit is contained in:
parent
eb1f0c29d5
commit
d96ce14496
|
@ -5,8 +5,7 @@ import io.gitlab.jfronny.resclone.api.RescloneEntry;
|
||||||
import io.gitlab.jfronny.resclone.config.ConfigLoader;
|
import io.gitlab.jfronny.resclone.config.ConfigLoader;
|
||||||
import io.gitlab.jfronny.resclone.fetchers.BasicFileFetcher;
|
import io.gitlab.jfronny.resclone.fetchers.BasicFileFetcher;
|
||||||
import io.gitlab.jfronny.resclone.fetchers.CurseforgeFetcher;
|
import io.gitlab.jfronny.resclone.fetchers.CurseforgeFetcher;
|
||||||
import io.gitlab.jfronny.resclone.fetchers.GithubMasterFetcher;
|
import io.gitlab.jfronny.resclone.fetchers.GitHubFetcher;
|
||||||
import io.gitlab.jfronny.resclone.fetchers.GithubReleaseFetcher;
|
|
||||||
import io.gitlab.jfronny.resclone.processors.PruneVanillaProcessor;
|
import io.gitlab.jfronny.resclone.processors.PruneVanillaProcessor;
|
||||||
import net.fabricmc.api.EnvType;
|
import net.fabricmc.api.EnvType;
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
|
@ -16,8 +15,7 @@ public class RescloneEntryDefault implements RescloneEntry {
|
||||||
@Override
|
@Override
|
||||||
public void init(RescloneApi api) {
|
public void init(RescloneApi api) {
|
||||||
api.addFetcher(new BasicFileFetcher());
|
api.addFetcher(new BasicFileFetcher());
|
||||||
api.addFetcher(new GithubMasterFetcher());
|
api.addFetcher(new GitHubFetcher());
|
||||||
api.addFetcher(new GithubReleaseFetcher());
|
|
||||||
api.addFetcher(new CurseforgeFetcher());
|
api.addFetcher(new CurseforgeFetcher());
|
||||||
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
|
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
|
||||||
api.addProcessor(new PruneVanillaProcessor());
|
api.addProcessor(new PruneVanillaProcessor());
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
package io.gitlab.jfronny.resclone.fetchers;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import io.gitlab.jfronny.resclone.Resclone;
|
||||||
|
import io.gitlab.jfronny.resclone.util.UrlUtils;
|
||||||
|
|
||||||
|
public class GitHubFetcher extends PackFetcher {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSourceTypeName() {
|
||||||
|
return "github";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String getDownloadUrl(String baseUrl) throws Exception {
|
||||||
|
String[] parts = baseUrl.split("/");
|
||||||
|
|
||||||
|
if (parts.length < 2) {
|
||||||
|
throw new Exception("Minimum source must contain \"user/repo\".");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "user/repo" - Gets from latest commit of main/master branch. */
|
||||||
|
else if (parts.length == 2) {
|
||||||
|
Resclone.LOGGER.info("Getting from main branch.");
|
||||||
|
return getFromMain(parts[0], parts[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "user/repo/branch/branchName" - Gets from latest commit of specified branch. */
|
||||||
|
else if (parts[2].equalsIgnoreCase("branch")) {
|
||||||
|
if (parts.length < 4) throw new Exception("Missing branch name in source definition.");
|
||||||
|
else {
|
||||||
|
Resclone.LOGGER.info("Getting from " + parts[3] + " branch.");
|
||||||
|
return getFromBranch(parts[0], parts[1], parts[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "user/repo/release" - Gets from the latest release. */
|
||||||
|
else if (parts[2].equalsIgnoreCase("release")) {
|
||||||
|
try {
|
||||||
|
JsonObject latestRelease = UrlUtils.readJsonFromURL("https://api.github.com/repos/" + parts[0] + "/" + parts[1] + "/releases/latest", JsonObject.class);
|
||||||
|
String res = null;
|
||||||
|
|
||||||
|
for (JsonElement element : latestRelease.get("assets").getAsJsonArray()) {
|
||||||
|
JsonObject o = element.getAsJsonObject();
|
||||||
|
if ("application/x-zip-compressed".equals(o.get("content_type").getAsString()) || o.get("name").getAsString().endsWith(".zip")) {
|
||||||
|
res = o.get("browser_download_url").getAsString();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Resclone.LOGGER.info("Getting from latest release.");
|
||||||
|
|
||||||
|
if (res == null) return latestRelease.get("zipball_url").getAsString();
|
||||||
|
else return res;
|
||||||
|
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new Exception("Failed to get github release asset", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* "user/repo/tag/tagNum" - Gets from a specified tag. */
|
||||||
|
else if (parts[2].equalsIgnoreCase("tag")) {
|
||||||
|
if (parts.length < 4) throw new Exception("Missing tag number in source definition.");
|
||||||
|
else {
|
||||||
|
Resclone.LOGGER.info("Getting from " + parts[3] + " tag.");
|
||||||
|
return getFromTag(parts[0], parts[1], parts[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFromMain(String user, String repo) {
|
||||||
|
String url = getFromBranch(user, repo, "main");
|
||||||
|
if (!UrlUtils.urlValid(url)) url = getFromBranch(user, repo, "master");
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFromBranch(String user, String repo, String branch) {
|
||||||
|
return "https://github.com/" + user + "/" + repo + "/archive/refs/heads/" + branch + ".zip";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFromTag(String user, String repo, String tag) {
|
||||||
|
return "https://github.com/" + user + "/" + repo + "/archive/refs/tags/" + tag + ".zip";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,36 +0,0 @@
|
||||||
package io.gitlab.jfronny.resclone.fetchers;
|
|
||||||
|
|
||||||
import io.gitlab.jfronny.resclone.util.UrlUtils;
|
|
||||||
|
|
||||||
public class GithubMasterFetcher extends PackFetcher {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getSourceTypeName() {
|
|
||||||
return "github-master";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getDownloadUrl(String baseUrl) throws Exception {
|
|
||||||
String[] parts = baseUrl.split("/");
|
|
||||||
String url;
|
|
||||||
|
|
||||||
switch (parts.length) {
|
|
||||||
case 2:
|
|
||||||
url = getStr(parts[0], parts[1], "main");
|
|
||||||
if (!UrlUtils.urlValid(url)) url = getStr(parts[0], parts[1], "master");
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
url = getStr(parts[0], parts[1], parts[2]);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Exception("Format for github-master is USER/REPO[/BRANCH]");
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getStr(String user, String repo, String branch) {
|
|
||||||
return "https://github.com/" + user + "/" + repo + "/archive/refs/heads/" + branch + ".zip";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,43 +0,0 @@
|
||||||
package io.gitlab.jfronny.resclone.fetchers;
|
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import io.gitlab.jfronny.resclone.Resclone;
|
|
||||||
import io.gitlab.jfronny.resclone.util.UrlUtils;
|
|
||||||
|
|
||||||
public class GithubReleaseFetcher extends PackFetcher {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getSourceTypeName() {
|
|
||||||
return "github-release";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
String getDownloadUrl(String baseUrl) throws Exception {
|
|
||||||
String[] parts = baseUrl.split("/");
|
|
||||||
if (parts.length == 2) {
|
|
||||||
try {
|
|
||||||
JsonObject latestRelease = UrlUtils.readJsonFromURL("https://api.github.com/repos/" + parts[0] + "/" + parts[1] + "/releases/latest", JsonObject.class);
|
|
||||||
String res = null;
|
|
||||||
for (JsonElement element : latestRelease.get("assets").getAsJsonArray()) {
|
|
||||||
JsonObject o = element.getAsJsonObject();
|
|
||||||
if ("application/x-zip-compressed".equals(o.get("content_type").getAsString())
|
|
||||||
|| o.get("name").getAsString().endsWith(".zip")) {
|
|
||||||
res = o.get("browser_download_url").getAsString();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (res == null) {
|
|
||||||
Resclone.LOGGER.info("Could not find release asset for " + baseUrl + ", using zipball");
|
|
||||||
return latestRelease.get("zipball_url").getAsString();
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
} catch (Throwable e) {
|
|
||||||
throw new Exception("Failed to get github release asset", e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new Exception("Format for github-release is USER/REPO");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -39,7 +39,7 @@ public abstract class PackFetcher {
|
||||||
while ((bytesRead = is.read(dataBuffer, 0, 1024)) != -1) {
|
while ((bytesRead = is.read(dataBuffer, 0, 1024)) != -1) {
|
||||||
os.write(dataBuffer, 0, bytesRead);
|
os.write(dataBuffer, 0, bytesRead);
|
||||||
}
|
}
|
||||||
Resclone.LOGGER.info("Completed download");
|
Resclone.LOGGER.info("Finished downloading.");
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
throw new Exception("Could not download pack", e);
|
throw new Exception("Could not download pack", e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue