Skip to content

Commit

Permalink
refactor: qol
Browse files Browse the repository at this point in the history
  • Loading branch information
micartey committed Apr 24, 2024
1 parent 0b69b23 commit af80dd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
34 changes: 15 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ yawen loads GitHub releases into the Java runtime. With the use of yawen you can

### How does it work?

yawen uses the GitHub-Api to identify the latest release and all of its assets.
**It is important that the release contains a compiled .jar file which also contains any dependencies it may uses**.
This jar file will be loaded into an `UrlClassLoader` and thereby loaded into the Java runtime.
yawen uses the GitHub-Api to get the releases and all of its assets.
An asset can be loaded through an URLClassLoader and thus contains all classes from that jar.
Only assets that are jar files can be loaded.

## :ballot_box: Usage

Expand All @@ -39,24 +39,20 @@ First of all, you need to create a new `YawenRepository` object by instanciating
YawenRepository repository = new YawenRepository("Username/Repository");
```

### Load by asset name

You can specify the asset name to selcect a specific asset.
### Load an Asset

```java
repository.load("my-release.jar").ifPresent(classLoader -> {
Class fromRelease = classLoader.loadClass("my.example.project.Class");
...
Optional<Release> optRelease = repository.getLatestRelease();

optRelease.ifPresent(release -> {
Asset asset = Arrays.stream(release.getAssets()).filter(asset -> asset.name.equals("dependency.jar"))
.findFirst()
.orElseThrow(() -> new RuntimeException("No asset found"));

repository.load(asset).ifPresent(classLoader -> {
Class fromRelease = classLoader.loadClass("my.example.project.Class");
// ...
});
});
```

### Load any asset

In case you don't specify the asset name, yawen will load the first usable asset.

```java
repository.load().ifPresent(classLoader -> {
Class fromRelease = classLoader.loadClass("my.example.project.Class");
...
});
```
24 changes: 14 additions & 10 deletions src/main/java/me/micartey/yawen/YawenRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ public Set<String> getAssetNames(Release release) {
* @return Optional of {@link ClassLoader}
*/
public Optional<ClassLoader> load(Asset asset) {
return this.loadDependency(asset.browserDownloadUrl).toJavaOptional();
if (!asset.name.endsWith(".jar"))
throw new RuntimeException("Asset is not a jar file!");

return this.loadDependency(
Try.ofCallable(() -> new URL(asset.browserDownloadUrl)).get()
).toJavaOptional();
}

/**
Expand All @@ -100,6 +105,9 @@ public Optional<ClassLoader> load(Asset asset) {
* @return Optional of {@link ClassLoader}
*/
public Optional<ClassLoader> loadCached(Asset asset) {
if (!asset.name.endsWith(".jar"))
throw new RuntimeException("Asset is not a jar file!");

boolean update = Try.ofCallable(() -> {
File parent = new File(".yawen");
parent.mkdir();
Expand All @@ -122,7 +130,9 @@ public Optional<ClassLoader> loadCached(Asset asset) {
System.out.println("[yawen] Updated cache!");
}

return this.loadDependency(new File(".yawen/" + asset.id + ".jar")).toJavaOptional();
return this.loadDependency(
Try.ofCallable(() -> new File(".yawen/" + asset.id + ".jar").toURI().toURL()).get()
).toJavaOptional();
}

/**
Expand All @@ -141,15 +151,9 @@ public Optional<ClassLoader> load() {
return this.load(asset);
}

private Try<ClassLoader> loadDependency(String url) {
return Try.ofCallable(() -> new URLClassLoader(new URL[]{
new URL(url)
}, YawenRepository.class.getClassLoader()));
}

private Try<ClassLoader> loadDependency(File file) {
private Try<ClassLoader> loadDependency(URL url) {
return Try.ofCallable(() -> new URLClassLoader(new URL[]{
file.toURI().toURL()
url
}, YawenRepository.class.getClassLoader()));
}
}

0 comments on commit af80dd7

Please sign in to comment.