Skip to content

Commit

Permalink
Build CLI using installShadowDist
Browse files Browse the repository at this point in the history
  • Loading branch information
egorikftp committed Nov 20, 2024
1 parent 4d494ce commit eb77694
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ other available gradle commands:

- apply formatting: `./gradlew spotlessApply`

- build CLI: `./gradlew :cli:build`
- build CLI: `./gradlew buildCLI`

## Join our community

Expand Down
81 changes: 21 additions & 60 deletions cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.buildConfig)
alias(libs.plugins.shadow)
application
}

val baseName = "valkyrie"
val versionName = rootProject.providers.gradleProperty("VERSION_NAME").get()
version = versionName

buildConfig {
buildConfigField("VERSION_NAME", versionName)
Expand All @@ -22,80 +22,41 @@ tasks.withType<Jar>().configureEach {
attributes["Implementation-Version"] = versionName
}
}

tasks.shadowJar {
dependsOn(tasks.jar)

exclude(
"**/*.kotlin_metadata",
"**/*.kotlin_builtins",
"**/*.kotlin_module",
"**/module-info.class",
"assets/**",
"font_metrics.properties",
"META-INF/AL2.0",
"META-INF/DEPENDENCIES",
"META-INF/jdom-info.xml",
"META-INF/LGPL2.1",
"META-INF/maven/**",
"META-INF/native-image/**",
"META-INF/*.version",
"**/*.proto",
"**/*.dex",
"**/LICENSE**",
"**/NOTICE**",
"r8-version.properties",
"migrateToAndroidx/*",
)
application {
mainClass = "io.github.composegears.valkyrie.cli.MainKt"
applicationName = "valkyrie"
version = versionName
}

val r8File = layout.buildDirectory.file("libs/$baseName-$version-r8.jar").map { it.asFile }
val rulesFile = project.file("src/main/proguard-rules.pro")
val r8Jar by tasks.registering(JavaExec::class) {
dependsOn(tasks.shadowJar)
val buildWithR8 by tasks.registering(JavaExec::class) {
dependsOn(tasks.installShadowDist)

val fatJarFile = tasks.shadowJar.get().archiveFile
inputs.file(fatJarFile)
inputs.file(rulesFile)
outputs.file(r8File)
val proguardRulesFile = layout.projectDirectory.file("proguard-rules.pro").asFile
val jar = layout.buildDirectory.file("install/cli-shadow/lib/$baseName-$version-all.jar").map { it.asFile }

inputs.file(jar)
inputs.file(proguardRulesFile)
outputs.file(jar)

classpath(r8)
mainClass = com.android.tools.r8.R8::class.java.canonicalName
args(
"--release",
"--classfile",
"--output", r8File.get().path,
"--pg-conf", rulesFile.path,
"--output", jar.get().path,
"--pg-conf", proguardRulesFile.path,
"--lib", providers.systemProperty("java.home").get(),
fatJarFile.get().toString(),
jar.get().toString(),
)
}

val binaryFile = layout.buildDirectory.file("libs/$baseName-$version-binary.jar").map { it.asFile }
val binaryJar by tasks.registering {
dependsOn(r8Jar)

val r8FileProvider = layout.file(r8File)
val binaryFileProvider = layout.file(binaryFile)
inputs.files(r8FileProvider)
outputs.file(binaryFileProvider)
val buildCLI by tasks.registering(Zip::class) {
dependsOn(buildWithR8)

doLast {
val r8File = r8FileProvider.get().asFile
val binaryFile = binaryFileProvider.get().asFile

binaryFile.parentFile.mkdirs()
binaryFile.delete()
binaryFile.writeText("#!/bin/sh\n\nexec java \$JAVA_OPTS -jar \$0 \"\$@\"\n\n")
binaryFile.appendBytes(r8File.readBytes())

binaryFile.setExecutable(true, false)
}
}
from(layout.buildDirectory.file("install/cli-shadow"))

tasks.test {
dependsOn(binaryJar)
systemProperty("CLI_PATH", binaryFile.get().absolutePath)
archiveFileName.set("$baseName-cli-$version.zip")
destinationDirectory.set(layout.buildDirectory.dir("distributions/"))
}

val r8: Configuration by configurations.creating
Expand Down
File renamed without changes.

0 comments on commit eb77694

Please sign in to comment.