mirror of
https://github.com/hexedtech/codemp.git
synced 2024-12-23 05:14:54 +01:00
chore(java): dropped shadow, NativeUtils as class file
This commit is contained in:
parent
bab4fa6ea3
commit
64c272f0af
7 changed files with 171 additions and 44 deletions
25
dist/java/build.gradle
vendored
25
dist/java/build.gradle
vendored
|
@ -1,14 +1,11 @@
|
|||
import com.vanniktech.maven.publish.SonatypeHost
|
||||
|
||||
plugins {
|
||||
id 'java-library'
|
||||
id "com.vanniktech.maven.publish" version "0.29.0"
|
||||
id 'com.github.johnrengelman.shadow' version '8.1.1'
|
||||
id 'com.google.osdetector' version '1.7.3'
|
||||
}
|
||||
|
||||
group = 'mp.code'
|
||||
version = '0.0.1'
|
||||
version = '0.0.2'
|
||||
|
||||
java {
|
||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
||||
|
@ -16,7 +13,6 @@ java {
|
|||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven { url 'https://jitpack.io' }
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
@ -24,7 +20,6 @@ sourceSets {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.github.adamheinrich:native-utils:master-SNAPSHOT'
|
||||
compileOnly 'org.projectlombok:lombok:1.18.34'
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.34'
|
||||
}
|
||||
|
@ -34,13 +29,15 @@ tasks.register('cargoBuild', Exec) {
|
|||
commandLine 'cargo', 'build', '--release', '--features=java'
|
||||
}
|
||||
|
||||
jar.archiveClassifier = osdetector.classifier
|
||||
|
||||
def rustDir = projectDir.toPath()
|
||||
.parent
|
||||
.parent
|
||||
.resolve('target')
|
||||
.resolve('release')
|
||||
.toFile()
|
||||
shadowJar {
|
||||
processResources {
|
||||
dependsOn cargoBuild
|
||||
outputs.upToDateWhen { false } // no caching
|
||||
from(rustDir) {
|
||||
|
@ -49,22 +46,12 @@ shadowJar {
|
|||
include('*dylib')
|
||||
into('natives/')
|
||||
}
|
||||
|
||||
archiveClassifier = osdetector.classifier
|
||||
dependencies {
|
||||
include(dependency('com.github.adamheinrich:native-utils:master-SNAPSHOT'))
|
||||
}
|
||||
}
|
||||
|
||||
build.finalizedBy shadowJar
|
||||
|
||||
import com.vanniktech.maven.publish.SonatypeHost
|
||||
mavenPublishing {
|
||||
//publishToMavenCentral(SonatypeHost.S01) // for snapshots
|
||||
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL) // central repository
|
||||
// publishToMavenCentral(SonatypeHost.DEFAULT) // default
|
||||
|
||||
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL, true)
|
||||
signAllPublications()
|
||||
|
||||
coordinates(project.group, rootProject.name, project.version)
|
||||
|
||||
pom {
|
||||
|
|
2
dist/java/src/mp/code/BufferController.java
vendored
2
dist/java/src/mp/code/BufferController.java
vendored
|
@ -122,6 +122,6 @@ public final class BufferController {
|
|||
}
|
||||
|
||||
static {
|
||||
Extensions.loadLibraryIfNotPresent();
|
||||
NativeUtils.loadLibraryIfNeeded();
|
||||
}
|
||||
}
|
||||
|
|
2
dist/java/src/mp/code/Client.java
vendored
2
dist/java/src/mp/code/Client.java
vendored
|
@ -152,6 +152,6 @@ public final class Client {
|
|||
}
|
||||
|
||||
static {
|
||||
Extensions.loadLibraryIfNotPresent();
|
||||
NativeUtils.loadLibraryIfNeeded();
|
||||
}
|
||||
}
|
||||
|
|
2
dist/java/src/mp/code/CursorController.java
vendored
2
dist/java/src/mp/code/CursorController.java
vendored
|
@ -99,6 +99,6 @@ public final class CursorController {
|
|||
}
|
||||
|
||||
static {
|
||||
Extensions.loadLibraryIfNotPresent();
|
||||
NativeUtils.loadLibraryIfNeeded();
|
||||
}
|
||||
}
|
||||
|
|
22
dist/java/src/mp/code/Extensions.java
vendored
22
dist/java/src/mp/code/Extensions.java
vendored
|
@ -37,27 +37,7 @@ public final class Extensions {
|
|||
*/
|
||||
public static native void setupTracing(String path, boolean debug);
|
||||
|
||||
private static boolean loaded = false;
|
||||
static synchronized void loadLibraryIfNotPresent() {
|
||||
if(loaded) return;
|
||||
try {
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
|
||||
String filename;
|
||||
if(os.startsWith("windows")) {
|
||||
filename = "/natives/codemp.dll";
|
||||
} else if(os.startsWith("mac")) {
|
||||
filename = "/natives/libcodemp.dylib";;
|
||||
} else filename = "/natives/libcodemp.so";
|
||||
cz.adamh.utils.NativeUtils.loadLibraryFromJar(filename);
|
||||
|
||||
loaded = true;
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static {
|
||||
Extensions.loadLibraryIfNotPresent();
|
||||
NativeUtils.loadLibraryIfNeeded();
|
||||
}
|
||||
}
|
||||
|
|
160
dist/java/src/mp/code/NativeUtils.java
vendored
Normal file
160
dist/java/src/mp/code/NativeUtils.java
vendored
Normal file
|
@ -0,0 +1,160 @@
|
|||
/*
|
||||
* Class NativeUtils is published under the The MIT License:
|
||||
*
|
||||
* Copyright (c) 2012 Adam Heinrich <adam@adamh.cz>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package mp.code;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.FileSystemNotFoundException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.ProviderNotFoundException;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A simple library class which helps with loading dynamic libraries stored in the
|
||||
* JAR archive. These libraries usually contain implementation of some methods in
|
||||
* native code (using JNI - Java Native Interface).
|
||||
* @see <a href="http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar">http://adamheinrich.com/blog/2012/how-to-load-native-jni-library-from-jar</a>
|
||||
* @see <a href="https://github.com/adamheinrich/native-utils">https://github.com/adamheinrich/native-utils</a>
|
||||
*/
|
||||
class NativeUtils {
|
||||
|
||||
/**
|
||||
* The minimum length a prefix for a file has to have according to {@link File#createTempFile(String, String)}}.
|
||||
*/
|
||||
private static final int MIN_PREFIX_LENGTH = 3;
|
||||
|
||||
/**
|
||||
* Prefix of the temporary folder where the native is stored.
|
||||
*/
|
||||
public static final String NATIVE_FOLDER_PATH_PREFIX = "nativeutils";
|
||||
|
||||
/**
|
||||
* Temporary directory which will contain the DLLs.
|
||||
*/
|
||||
private static File temporaryDir;
|
||||
|
||||
/**
|
||||
* Private constructor - this class will never be instanced
|
||||
*/
|
||||
private NativeUtils() {}
|
||||
|
||||
/**
|
||||
* Loads library from current JAR archive.
|
||||
* The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after
|
||||
* exiting.
|
||||
* Method uses String as filename because the pathname is "abstract", not system-dependent.
|
||||
* @param path The path of file inside JAR as absolute path (beginning with '/'), e.g. /package/File.ext
|
||||
* @throws IOException If temporary file creation or read/write operation fails
|
||||
* @throws IllegalArgumentException If source file (param path) does not exist
|
||||
* @throws IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters
|
||||
* (restriction of {@link File#createTempFile(java.lang.String, java.lang.String)}).
|
||||
* @throws FileNotFoundException If the file could not be found inside the JAR.
|
||||
*/
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private static void loadLibraryFromJar(String path) throws IOException {
|
||||
if(null == path || !path.startsWith("/")) {
|
||||
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
|
||||
}
|
||||
|
||||
// Obtain filename from path
|
||||
String[] parts = path.split("/");
|
||||
String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
|
||||
|
||||
// Check if the filename is okay
|
||||
if(filename == null || filename.length() < MIN_PREFIX_LENGTH) {
|
||||
throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
|
||||
}
|
||||
|
||||
// Prepare temporary file
|
||||
if(temporaryDir == null) {
|
||||
temporaryDir = createTempDirectory();
|
||||
temporaryDir.deleteOnExit();
|
||||
}
|
||||
|
||||
File temp = new File(temporaryDir, filename);
|
||||
|
||||
try(InputStream is = NativeUtils.class.getResourceAsStream(path)) {
|
||||
Files.copy(Objects.requireNonNull(is), temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
temp.delete();
|
||||
throw e;
|
||||
} catch (NullPointerException e) {
|
||||
temp.delete();
|
||||
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
|
||||
}
|
||||
|
||||
try {
|
||||
System.load(temp.getAbsolutePath());
|
||||
} finally {
|
||||
if(isPosixCompliant()) {
|
||||
// Assume POSIX compliant file system, can be deleted after loading
|
||||
temp.delete();
|
||||
} else {
|
||||
// Assume non-POSIX, and don't delete until last file descriptor closed
|
||||
temp.deleteOnExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isPosixCompliant() {
|
||||
try {
|
||||
return FileSystems.getDefault()
|
||||
.supportedFileAttributeViews()
|
||||
.contains("posix");
|
||||
} catch (FileSystemNotFoundException | ProviderNotFoundException | SecurityException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static File createTempDirectory() throws IOException {
|
||||
String tempDir = System.getProperty("java.io.tmpdir");
|
||||
File generatedDir = new File(tempDir, NativeUtils.NATIVE_FOLDER_PATH_PREFIX + System.nanoTime());
|
||||
|
||||
if(!generatedDir.mkdir())
|
||||
throw new IOException("Failed to create temp directory " + generatedDir.getName());
|
||||
|
||||
return generatedDir;
|
||||
}
|
||||
|
||||
private static boolean loaded = false;
|
||||
static synchronized void loadLibraryIfNeeded() {
|
||||
if(loaded) return;
|
||||
try {
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
|
||||
String filename;
|
||||
if(os.startsWith("windows")) {
|
||||
filename = "/natives/codemp.dll";
|
||||
} else if(os.startsWith("mac")) {
|
||||
filename = "/natives/libcodemp.dylib";;
|
||||
} else filename = "/natives/libcodemp.so";
|
||||
loadLibraryFromJar(filename);
|
||||
|
||||
loaded = true;
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
2
dist/java/src/mp/code/Workspace.java
vendored
2
dist/java/src/mp/code/Workspace.java
vendored
|
@ -177,7 +177,7 @@ public final class Workspace {
|
|||
}
|
||||
|
||||
static {
|
||||
Extensions.loadLibraryIfNotPresent();
|
||||
NativeUtils.loadLibraryIfNeeded();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue