Skip to content

Commit 6fe1fb1

Browse files
committed
It's publishing time
1 parent e0e4c1a commit 6fe1fb1

File tree

6 files changed

+133
-15
lines changed

6 files changed

+133
-15
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ jobs:
3232
key: ${{ runner.os }}-gradle-${{ hashFiles('**/gradle-wrapper.properties') }}
3333
restore-keys: |
3434
${{ runner.os }}-gradle-
35-
- name: Build artifacts
36-
run: ./gradlew :native:jar
35+
- name: Build and publish artifacts
36+
run: ./gradlew publish
37+
env:
38+
MAVEN_USER: ${{ secrets.MAVEN_USER }}
39+
MAVEN_PASS: ${{ secrets.MAVEN_PASS }}
3740
- name: Upload build artifacts
3841
uses: actions/upload-artifact@v4
3942
with:
@@ -64,8 +67,8 @@ jobs:
6467
key: ${{ runner.os }}-gradle-${{ hashFiles('**/gradle-wrapper.properties') }}
6568
restore-keys: |
6669
${{ runner.os }}-gradle-
67-
- name: Build artifacts
68-
run: ./gradlew :native:jar
70+
- name: Build and publish artifacts
71+
run: ./gradlew :native:publish
6972
- name: Upload build artifacts
7073
uses: actions/upload-artifact@v4
7174
with:

build.gradle.kts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,32 @@ plugins {
33
}
44

55
subprojects {
6+
apply(plugin = "java")
7+
apply(plugin = "maven-publish")
8+
69
group = "xyz.bluspring"
7-
version = "0.2.0"
10+
version = "${rootProject.property("unitytranslate_version")}"
811

912
repositories {
1013
mavenCentral()
1114
}
15+
16+
publishing {
17+
repositories {
18+
maven("https://mvn.devos.one/snapshots") {
19+
credentials {
20+
username = System.getenv()["MAVEN_USER"]
21+
password = System.getenv()["MAVEN_PASS"]
22+
}
23+
}
24+
}
25+
publications {
26+
register("maven", MavenPublication::class) {
27+
groupId = "xyz.bluspring"
28+
artifactId = "UnityTranslateLib"
29+
version = "${rootProject.property("unitytranslate_version")}"
30+
from(components.getByName("java"))
31+
}
32+
}
33+
}
1234
}

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
unitytranslate_version = 0.2.0-SNAPSHOT
2+
13
# This isn't actually used in the download process, however it's used for the sake of caching the downloaded files.
24
# https://github.com/OpenNMT/CTranslate2/blob/master/python/ctranslate2/version.py
35
ct2_version = 4.5.0

library/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
plugins {
22
kotlin("jvm") version "2.1.0"
33
kotlin("plugin.serialization") version "2.1.0"
4-
id("com.gradleup.shadow") version "8.3.5"
54
}
65

7-
group = "xyz.bluspring"
8-
version = "0.2.0"
9-
106
repositories {
117
mavenCentral()
128
}

library/src/main/kotlin/xyz/bluspring/unitytranslate/library/UnityTranslateLib.kt

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import xyz.bluspring.unitytranslate.library.models.argos.ArgosPackageIndex
88
import xyz.bluspring.unitytranslate.library.translator.DummyTranslator
99
import xyz.bluspring.unitytranslate.library.translator.ModelBasedTranslator
1010
import xyz.bluspring.unitytranslate.library.translator.Translator
11+
import java.nio.file.Files
1112
import java.nio.file.Path
13+
import java.nio.file.Paths
14+
import java.nio.file.StandardCopyOption
1215
import java.util.concurrent.ConcurrentHashMap
13-
import kotlin.io.path.Path
1416
import kotlin.io.path.absolutePathString
17+
import kotlin.io.path.exists
1518

1619
class UnityTranslateLib(val path: Path) {
1720
val packageIndex = ModelPackageManager(this).apply {
@@ -58,8 +61,104 @@ class UnityTranslateLib(val path: Path) {
5861
companion object {
5962
val logger: Logger = LoggerFactory.getLogger(UnityTranslateLib::class.java)
6063

64+
// Modified from ImGui-java's library loading - https://github.com/SpaiR/imgui-java/blob/main/imgui-binding/src/main/java/imgui/ImGui.java
6165
init {
62-
System.load(Path("UnityTranslateLibRsAgain.dll").toAbsolutePath().absolutePathString())
66+
val libPath = System.getProperty("unitytranslate.library.path")
67+
val libName = System.getProperty("unitytranslate.library.name", "UnityTranslateLib")
68+
val fullLibName = resolveFullLibName()
69+
70+
if (libPath != null) {
71+
System.load(Paths.get(libPath).resolve(fullLibName).absolutePathString())
72+
} else {
73+
try {
74+
System.loadLibrary(libName)
75+
} catch (e: Throwable) {
76+
val extractedPath = try {
77+
tryLoadFromClassPath(fullLibName)
78+
} catch (e2: Exception) {
79+
val joined = RuntimeException("Failed to load natives for UnityTranslateLib!")
80+
joined.addSuppressed(e2)
81+
joined.addSuppressed(e)
82+
83+
throw joined
84+
}
85+
86+
System.load(extractedPath)
87+
}
88+
}
89+
}
90+
91+
private fun resolveFullLibName(): String {
92+
val osName = System.getProperty("os.name").lowercase()
93+
val isWindows = osName.contains("win")
94+
val isMac = osName.contains("mac")
95+
96+
val libPrefix = if (isWindows) "" else "lib"
97+
val libSuffix = if (isWindows) ".dll" else if (isMac) ".dylib" else ".so"
98+
99+
return System.getProperty("unitytranslate.library.name", "${libPrefix}UnityTranslateLib${libSuffix}")
100+
}
101+
102+
private val platformLibs: List<String>
103+
get() {
104+
val osName = System.getProperty("os.name").lowercase()
105+
val osArch = System.getProperty("os.arch").lowercase()
106+
val isWindows = osName.contains("win")
107+
val isMac = osName.contains("mac")
108+
109+
val dir = "unitytranslate/${if (isWindows) "windows" else if (isMac) "osx" else "linux"}-${osArch}"
110+
111+
return if (osArch == "amd64") {
112+
if (isWindows)
113+
listOf(
114+
"$dir/UnityTranslateLib.dll",
115+
"$dir/ctranslate2.dll",
116+
"$dir/cudnn64_9.dll",
117+
"$dir/libiomp5md.dll"
118+
)
119+
else if (isMac)
120+
listOf()
121+
else
122+
listOf(
123+
"$dir/libctranslate2.so",
124+
"$dir/libcudnn.so",
125+
"$dir/libgomp.so",
126+
"$dir/libUnityTranslateLib.so"
127+
)
128+
} else emptyList()
129+
}
130+
131+
private fun tryLoadFromClassPath(fullLibName: String): String {
132+
val classLoader = UnityTranslateLib::class.java.classLoader
133+
val libs = platformLibs
134+
135+
if (libs.isEmpty())
136+
throw Exception("Unsupported platform ${System.getProperty("os.name")} (${System.getProperty("os.arch")})!")
137+
138+
val tmpDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve("unitytranslate-natives")
139+
140+
if (!tmpDir.exists())
141+
tmpDir.toFile().mkdirs()
142+
143+
for (packedLibPath in libs) {
144+
val libName = packedLibPath.split("/").last()
145+
146+
classLoader.getResourceAsStream(packedLibPath)?.use {
147+
val libPath = tmpDir.resolve(libName)
148+
try {
149+
Files.copy(it, libPath, StandardCopyOption.REPLACE_EXISTING)
150+
} catch (e: AccessDeniedException) {
151+
if (!libPath.exists())
152+
throw e
153+
}
154+
}
155+
}
156+
157+
val unityTranslatePath = tmpDir.resolve(fullLibName)
158+
if (!unityTranslatePath.exists())
159+
throw Exception("Failed to load library files for UnityTranslateLib!")
160+
161+
return unityTranslatePath.absolutePathString()
63162
}
64163
}
65164
}

native/build.gradle.kts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import java.net.URI
22
import java.util.zip.ZipFile
33

4-
plugins {
5-
java
6-
}
7-
84
val CT2_URL = "https://nightly.link/OpenNMT/CTranslate2/workflows/ci/master/python-wheels-{dist}-{arch}.zip"
95
val ct2Version = rootProject.property("ct2_version")!! as String
106

0 commit comments

Comments
 (0)