Skip to content

Commit 75fbb79

Browse files
authored
Updated version of assemble_maven rewritten in Kotlin (typedb#293)
## What is the goal of this PR? Rewrite `assemble_maven` in Kotlin in order for us to understand it better and improve its maintability/readability. ## What are the changes implemented in this PR? Implement `assemble_maven` in `@graknlabs_bazel_distribution//maven:new_rules.bzl`
1 parent f3a3934 commit 75fbb79

File tree

7 files changed

+643
-3
lines changed

7 files changed

+643
-3
lines changed

WORKSPACE

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,28 @@ workspace(name="graknlabs_bazel_distribution")
2222
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
2323

2424
# Load @rules_python
25-
load("//common:deps.bzl", "rules_python")
25+
load("//common:deps.bzl", "rules_python", "rules_kotlin", "rules_jvm_external")
2626
rules_python()
27+
rules_kotlin()
28+
rules_jvm_external()
29+
30+
# Load @io_bazel_rules_kotlin
31+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")
32+
kotlin_repositories()
33+
kt_register_toolchains()
34+
35+
load("@graknlabs_bazel_distribution//maven:deps.bzl", "maven_artifacts_with_versions")
36+
load("@rules_jvm_external//:defs.bzl", "maven_install")
37+
maven_install(
38+
artifacts = maven_artifacts_with_versions,
39+
repositories = [
40+
"https://repo1.maven.org/maven2",
41+
],
42+
strict_visibility = True,
43+
version_conflict_policy = "pinned",
44+
fetch_sources = True,
45+
)
46+
2747

2848
# Load @graknlabs_bazel_distribution_pip
2949
load("//pip:deps.bzl", pip_deps = "deps")
@@ -43,3 +63,4 @@ git_repository(
4363
)
4464
load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")
4565
stardoc_repositories()
66+

common/deps.bzl

+19
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,22 @@ def rules_pkg():
2828
],
2929
patch_args = ["-p1"],
3030
)
31+
32+
33+
def rules_kotlin():
34+
http_archive(
35+
name = "io_bazel_rules_kotlin",
36+
urls = ["https://github.com/bazelbuild/rules_kotlin/archive/legacy-1.3.0.zip"],
37+
type = "zip",
38+
strip_prefix = "rules_kotlin-legacy-1.3.0",
39+
sha256 = "4fd769fb0db5d3c6240df8a9500515775101964eebdf85a3f9f0511130885fde",
40+
)
41+
42+
43+
def rules_jvm_external():
44+
http_archive(
45+
name = "rules_jvm_external",
46+
strip_prefix = "rules_jvm_external-3.2",
47+
sha256 = "82262ff4223c5fda6fb7ff8bd63db8131b51b413d26eb49e3131037e79e324af",
48+
url = "https://github.com/bazelbuild/rules_jvm_external/archive/3.2.zip",
49+
)

maven/BUILD

+43-2
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,56 @@ py_binary(
4545
# FIXME(vmax): I couldn't make it a kt_jvm_binary because
4646
# it complained it couldn't find `java` executable
4747
kt_jvm_library(
48-
name = "repackager_lib",
48+
name = "repackager-lib",
4949
srcs = ["Repackager.kt"],
5050
)
5151

52+
53+
kt_jvm_library(
54+
name = "pom-generator-lib",
55+
deps = [
56+
"@maven//:com_eclipsesource_minimal_json_minimal_json",
57+
"@maven//:info_picocli_picocli",
58+
],
59+
srcs = [
60+
"PomGenerator.kt",
61+
]
62+
)
63+
64+
java_binary(
65+
name = "pom-generator",
66+
runtime_deps = [
67+
":pom-generator-lib"
68+
],
69+
main_class = "PomGeneratorKt",
70+
visibility = ["//visibility:public"]
71+
)
72+
5273
java_binary(
5374
name = "repackager",
5475
runtime_deps = [
55-
":repackager_lib"
76+
":repackager-lib"
5677
],
5778
main_class = "RepackagerKt",
5879
visibility = ["//visibility:public"]
5980
)
81+
82+
83+
kt_jvm_library(
84+
name = "jar-assembler-lib",
85+
deps = [
86+
"@maven//:info_picocli_picocli",
87+
],
88+
srcs = [
89+
"JarAssembler.kt"
90+
]
91+
)
92+
93+
java_binary(
94+
name = "jar-assembler",
95+
runtime_deps = [
96+
":jar-assembler-lib"
97+
],
98+
main_class = "JarAssemblerKt",
99+
visibility = ["//visibility:public"]
100+
)

maven/JarAssembler.kt

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import picocli.CommandLine
2+
import picocli.CommandLine.Command
3+
import picocli.CommandLine.Option
4+
import java.io.BufferedInputStream
5+
import java.io.BufferedOutputStream
6+
import java.io.File
7+
import java.io.FileOutputStream
8+
import java.util.*
9+
import java.util.concurrent.Callable
10+
import java.util.zip.ZipEntry
11+
import java.util.zip.ZipFile
12+
import java.util.zip.ZipOutputStream
13+
import kotlin.system.exitProcess
14+
15+
16+
@Command(name = "jar-assembler", mixinStandardHelpOptions = true)
17+
class JarAssembler : Callable<Unit> {
18+
19+
@Option(names = ["--output"], required = true)
20+
lateinit var output_file: File
21+
22+
@Option(names = ["--prefix"])
23+
lateinit var prefix: String
24+
25+
@Option(names = ["--group-id"])
26+
var groupId = ""
27+
28+
@Option(names = ["--artifact-id"])
29+
var artifactId = ""
30+
31+
@Option(names = ["--pom-file"])
32+
var pomFile: File? = null
33+
34+
@Option(names = ["--jars"], split = ";")
35+
lateinit var jars: Array<File>
36+
37+
val entryNames = mutableSetOf<String>()
38+
39+
override fun call() {
40+
ZipOutputStream(BufferedOutputStream(FileOutputStream(output_file))).use { out ->
41+
if (pomFile != null) {
42+
val pomFileEntry = ZipEntry("META-INF/maven/${groupId}/${artifactId}/pom.xml")
43+
out.putNextEntry(pomFileEntry)
44+
out.write(pomFile!!.readBytes())
45+
}
46+
for (jar in jars) {
47+
ZipFile(jar).use { jarZip ->
48+
jarZip.entries().asSequence().forEach { entry ->
49+
if (entry.name.contains("META-INF")) {
50+
return@forEach
51+
}
52+
if (entryNames.contains(entry.name)) {
53+
return@forEach
54+
}
55+
entryNames.add(entry.name)
56+
BufferedInputStream(jarZip.getInputStream(entry)).use { inputStream ->
57+
val newEntry = ZipEntry(prefix + entry.name)
58+
out.putNextEntry(newEntry)
59+
inputStream.copyTo(out, 1024)
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
68+
fun main(args: Array<String>): Unit = exitProcess(CommandLine(JarAssembler()).execute(*args))

0 commit comments

Comments
 (0)