Skip to content

Commit

Permalink
Add bundled artifact variant which includes the library
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Jul 21, 2024
1 parent 01446d7 commit 333ddb8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Java 8+ JNI wrapper to the [starship battery](https://crates.io/crates/starship-
Usage
-----

The library can be installed from the `releases` repository on [maven.lostluma.net](https://maven.lostluma.net/#/releases/net/lostluma/battery).
The library can be installed from the `releases` repository on [maven.lostluma.net](https://maven.lostluma.net/#/releases/net/lostluma/battery) in two variants:

The dynamic library is currently available for Linux, MacOS, and Windows on both aarch64 and amd64 platforms.
When first being used the version for your system will be downloaded, validated, and cached for subsequent uses.
- `default`: Downloads, validates, and caches the dynamic library on demand. Saves bandwidth and disk space.
- `bundled`: Contains the dynamic library for all platforms. Recommended for situations in which first startup may be offline.

The dynamic library is currently available for Linux, MacOS, and Windows on both aarch64 and amd64 platforms.

Misc
----
Expand Down
13 changes: 13 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ tasks.withType<JavaCompile> {
}
}

/**
* Builds the bundled Jar.
*
* Put all variants of the library into a `library/` directory first!
*/
tasks.register<Jar>("buildBundled") {
archiveClassifier = "bundled"

from(files("library/"))
from(sourceSets.main.get().output)
}

publishing {
publications {
create<MavenPublication>("maven") {
Expand All @@ -51,6 +63,7 @@ publishing {
version = version

from(components["java"])
artifact(tasks.named("buildBundled").get())
}
}

Expand Down
26 changes: 21 additions & 5 deletions src/main/java/net/lostluma/battery/impl/util/NativeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Properties;

@ApiStatus.Internal
Expand Down Expand Up @@ -71,12 +72,12 @@ private static void load0() throws IOException, LibraryLoadError{
}

if (!isLibraryValid(path, hash)) {
if (!download) {
throw new LibraryLoadError("Native library could not be validated.");
}

Files.createDirectories(path.getParent());
HttpUtil.download(new URL(BASE_URL + name), path);
boolean copied = copyFileFromJar(name, path);

if (!copied && download) {
HttpUtil.download(new URL(BASE_URL + name), path);
}

if (!isLibraryValid(path, hash)) {
throw new LibraryLoadError("Native library could not be validated.");
Expand Down Expand Up @@ -116,4 +117,19 @@ private static String getName() {
private static boolean isLibraryValid(Path path, String hash) throws IOException {
return Files.exists(path) && hash.equals(CryptoUtil.sha512(path));
}

private static boolean copyFileFromJar(String name, Path destination) throws IOException {
// "+" is not valid inside ZIP files
// Gradle replaces them during build
name = "/" + name.replace("+", ".");

try (InputStream stream = NativeUtil.class.getResourceAsStream(name)) {
if (stream != null) {
Files.copy(stream, destination, StandardCopyOption.REPLACE_EXISTING);
return true;
}
}

return false;
}
}

0 comments on commit 333ddb8

Please sign in to comment.