Skip to content

Commit

Permalink
repo: implement get version from maven. close #104
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Mar 26, 2022
1 parent 93ac7d8 commit cf17cf5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 49 deletions.
6 changes: 6 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
* `boot.js` - 用于启动`mirai console`
* `repo.js` - 用于获取`mirai repo`仓库中的信息

## `updater`中使用`maven`更新频道

此功能可以自动从`Maven Repo`获取最新版本。

`.\mcl --update-package 包名 --channel maven`

## 使用样例

* 修改某个包的更新频道
Expand Down
96 changes: 52 additions & 44 deletions scripts/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,56 +70,64 @@ function check(pack) {
loader.logger.error("\"" + pack.id + "\" is corrupted.");
down = true;
}
let info = loader.repo.fetchPackage(pack.id);
if (pack.type.equals("")) {
pack.type = Config.Package.getType(info.type);
let ver = "";
let info = null;
if (pack.channel.equals("maven")) {
ver = loader.repo.getLatestVersionFromMaven(pack.id);
} else {
info = loader.repo.fetchPackage(pack.id);
if (pack.type.equals("")) {
pack.type = Config.Package.getType(info.type);
}
if (pack.channel.equals("")) {
pack.channel = Config.Package.getChannel(info.defaultChannel);
}
if (!info.channels.containsKey(pack.channel)) {
loader.logger.error(AnsiMsg.newMsg()
.lightRed()
.a("Invalid update channel ")
.lightBlue().append("\"").a(pack.channel).a("\"")
.lightRed()
.a(" for package ")
.gold().a("\"").a(pack.id).a("\"")
);
loader.saveConfig();
return;
}
let target = info.channels[pack.channel];
ver = target[target.size() - 1];
}
if (pack.channel.equals("")) {
pack.channel = Config.Package.getChannel(info.defaultChannel);

if ((update && !pack.version.equals(ver) && !force) || pack.version.trim().equals("")) {
if (loader.cli.hasOption("q")) {
pack.removeFiles();
} else if (pack.type.equals(Config.Package.TYPE_PLUGIN)) {
let dir = new File(pack.type);
pack.getJarFile().renameTo(new File(dir, pack.getBasename() + ".jar.bak"));
}
pack.version = ver;
down = true;
}
if (!info.channels.containsKey(pack.channel)) {
loader.logger.error(AnsiMsg.newMsg()
.lightRed()
.a("Invalid update channel ")
.lightBlue().append("\"").a(pack.channel).a("\"")
if (!down && !pack.version.equals(ver)) {
loader.logger.warning(AnsiMsg.newMsg()
.lightRed()
.a(" for package ")
.gold().a("\"").a(pack.id).a("\"")
.a("Package ")
.reset().gold().a("\"").a(pack.id).a("\"")
.reset().lightRed().a(" has newer version ")
.reset().gold().a("\"").a(ver).a("\"")
);
} else {
let target = info.channels[pack.channel];
let ver = target[target.size() - 1];
if ((update && !pack.version.equals(ver) && !force) || pack.version.trim().equals("")) {
if (loader.cli.hasOption("q")) {
pack.removeFiles();
} else if (pack.type.equals(Config.Package.TYPE_PLUGIN)) {
let dir = new File(pack.type);
pack.getJarFile().renameTo(new File(dir, pack.getBasename() + ".jar.bak"));
}
pack.version = ver;
down = true;
}
if (!down && !pack.version.equals(ver)) {
loader.logger.warning(AnsiMsg.newMsg()
showNotice = true;
}
if (down) {
downloadFile(pack, info);
if (!Utility.checkLocalFile(pack)) {
loader.logger.error(AnsiMsg.newMsg()
.lightRed()
.a("Package ")
.reset().gold().a("\"").a(pack.id).a("\"")
.reset().lightRed().a(" has newer version ")
.reset().gold().a("\"").a(ver).a("\"")
.a("The local file ")
.gold().a("\"").a(pack.id).a("\"")
.lightRed()
.a(" is still corrupted, please check the network.")
);
showNotice = true;
}
if (down) {
downloadFile(pack, info);
if (!Utility.checkLocalFile(pack)) {
loader.logger.error(AnsiMsg.newMsg()
.lightRed()
.a("The local file ")
.gold().a("\"").a(pack.id).a("\"")
.lightRed()
.a(" is still corrupted, please check the network.")
);
}
}
}
loader.saveConfig();
Expand Down
41 changes: 36 additions & 5 deletions src/main/java/org/itxtech/mcl/component/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.itxtech.mcl.Loader;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.StringReader;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
Expand Down Expand Up @@ -48,7 +53,11 @@ public class Repository {

public Repository(Loader loader) {
this.loader = loader;
client = loader.getProxy() == null ? HttpClient.newBuilder().build() : HttpClient.newBuilder().proxy(ProxySelector.of(loader.getProxy())).build();
client = (loader.getProxy() == null ?
HttpClient.newBuilder() :
HttpClient.newBuilder().proxy(ProxySelector.of(loader.getProxy())))
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
}

public Map<String, PackageInfo> fetchPackages() throws Exception {
Expand All @@ -69,13 +78,31 @@ public Package fetchPackage(String id) throws Exception {
}.getType());
}

public Document fetchMavenMetadata(String id) throws Exception {
for (var repo : loader.config.mavenRepo) {
try {
var content = httpGet("/" + transformId(id) + "/maven-metadata.xml", repo);
var factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
return factory.newDocumentBuilder().parse(new InputSource(new StringReader(content)));
} catch (Exception e) {
loader.logger.logException(e);
}
}
throw new Exception("Cannot find valid maven metadata");
}

public String getLatestVersionFromMaven(String id) throws Exception {
return fetchMavenMetadata(id).getElementsByTagName("release").item(0).getTextContent();
}

public Metadata getMetadataFromFile(File file) throws Exception {
return new Gson().fromJson(Files.readString(file.toPath()), new TypeToken<Metadata>() {
}.getType());
}

public String getSha1Url(Config.Package pkg, Package info, String jarUrl) {
if (info.repo != null) {
if (info != null && info.repo != null) {
RepoInfo repoInfo = info.repo.get(pkg.version);
if (repoInfo != null && repoInfo.sha1 != null && !repoInfo.sha1.isBlank()) {
return repoInfo.sha1;
Expand All @@ -85,7 +112,7 @@ public String getSha1Url(Config.Package pkg, Package info, String jarUrl) {
}

public String getJarUrl(Config.Package pkg, Package info) {
if (info.repo != null) {
if (info != null && info.repo != null) {
RepoInfo repoInfo = info.repo.get(pkg.version);
if (repoInfo != null && repoInfo.archive != null && !repoInfo.archive.isBlank()) {
return repoInfo.archive;
Expand All @@ -109,7 +136,7 @@ public String getJarUrl(Config.Package pkg, Package info) {
}

public String getMetadataUrl(Config.Package pkg, Package info) {
if (info.repo != null) {
if (info != null && info.repo != null) {
RepoInfo repoInfo = info.repo.get(pkg.version);
if (repoInfo != null && repoInfo.metadata != null && !repoInfo.metadata.isBlank()) {
return repoInfo.metadata;
Expand Down Expand Up @@ -141,8 +168,12 @@ private HttpResponse<Void> httpHead(String url) throws Exception {
}

private String httpGet(String url) throws Exception {
return httpGet(url, loader.config.miraiRepo);
}

private String httpGet(String url, String server) throws Exception {
return client.send(
HttpRequest.newBuilder(URI.create(loader.config.miraiRepo + url))
HttpRequest.newBuilder(URI.create(server + url))
.timeout(Duration.ofSeconds(30))
.setHeader("User-Agent", USER_AGENT)
.build(),
Expand Down

0 comments on commit cf17cf5

Please sign in to comment.