Build: Rename custom-dependencies -> dependencies
This commit is contained in:
+115
@@ -0,0 +1,115 @@
|
||||
|
||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||
import java.io.File
|
||||
import org.gradle.internal.os.OperatingSystem
|
||||
|
||||
// TODO: consider adding dx sources (the only jar used on the compile time so far)
|
||||
// e.g. from "https://android.googlesource.com/platform/dalvik/+archive/android-5.0.0_r2/dx.tar.gz"
|
||||
|
||||
repositories {
|
||||
ivy {
|
||||
artifactPattern("https://dl-ssl.google.com/android/repository/[artifact]-[revision].[ext]")
|
||||
artifactPattern("https://dl-ssl.google.com/android/repository/[artifact]_[revision](-[classifier]).[ext]")
|
||||
artifactPattern("https://dl.google.com/android/repository/[artifact]_[revision](-[classifier]).[ext]")
|
||||
}
|
||||
}
|
||||
|
||||
val androidSdk by configurations.creating
|
||||
val androidJar by configurations.creating
|
||||
val androidPlatform by configurations.creating
|
||||
val dxSources by configurations.creating
|
||||
val buildTools by configurations.creating
|
||||
|
||||
val libsDestDir = File(buildDir, "libs")
|
||||
val sdkDestDir = File(buildDir, "androidSdk")
|
||||
|
||||
val toolsOs = when {
|
||||
OperatingSystem.current().isWindows -> "windows"
|
||||
OperatingSystem.current().isMacOsX -> "macosx"
|
||||
OperatingSystem.current().isLinux -> "linux"
|
||||
else -> {
|
||||
logger.error("Unknown operating system for android tools: ${OperatingSystem.current().name}")
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
val prepareSdk by task<DefaultTask> {
|
||||
doLast {}
|
||||
}
|
||||
|
||||
fun unzipSdkTask(
|
||||
sdkName: String, sdkVer: String, destinationSubdir: String, coordinatesSuffix: String,
|
||||
additionalConfig: Configuration? = null, dirLevelsToSkipOnUnzip: Int = 0, ext: String = "zip",
|
||||
unzipFilter: CopySpec.() -> Unit = {}
|
||||
): Task {
|
||||
val id = "${sdkName}_$sdkVer"
|
||||
val cfg = configurations.create(id)
|
||||
val dependency = "google:$sdkName:$sdkVer${coordinatesSuffix.takeIf { it.isNotEmpty() }?.let { ":$it" } ?: ""}@$ext"
|
||||
dependencies.add(cfg.name, dependency)
|
||||
|
||||
val unzipTask = task("unzip_$id") {
|
||||
dependsOn(cfg)
|
||||
inputs.files(cfg)
|
||||
val targetDir = file("$sdkDestDir/$destinationSubdir")
|
||||
outputs.dirs(targetDir)
|
||||
doFirst {
|
||||
project.copy {
|
||||
when (ext) {
|
||||
"zip" -> from(zipTree(cfg.singleFile))
|
||||
"tar.gz" -> from(tarTree(resources.gzip(cfg.singleFile)))
|
||||
else -> throw GradleException("Don't know how to handle the extension \"$ext\"")
|
||||
}
|
||||
unzipFilter.invoke(this)
|
||||
if (dirLevelsToSkipOnUnzip > 0) {
|
||||
eachFile {
|
||||
path = path.split("/").drop(dirLevelsToSkipOnUnzip).joinToString("/")
|
||||
if (path.isBlank()) {
|
||||
exclude()
|
||||
}
|
||||
}
|
||||
}
|
||||
into(targetDir)
|
||||
}
|
||||
}
|
||||
}
|
||||
prepareSdk.dependsOn(unzipTask)
|
||||
|
||||
additionalConfig?.also {
|
||||
dependencies.add(it.name, dependency)
|
||||
}
|
||||
|
||||
return unzipTask
|
||||
}
|
||||
|
||||
unzipSdkTask("platform", "26_r02", "platforms/android-26", "", androidPlatform, 1)
|
||||
unzipSdkTask("android_m2repository", "r44", "extras/android", "")
|
||||
unzipSdkTask("platform-tools", "r25.0.3", "", toolsOs)
|
||||
unzipSdkTask("tools", "r24.3.4", "", toolsOs)
|
||||
unzipSdkTask("build-tools", "r23.0.1", "build-tools/23.0.1", toolsOs, buildTools, 1)
|
||||
unzipSdkTask("build-tools", "r28.0.2", "build-tools/28.0.2", toolsOs, buildTools, 1)
|
||||
|
||||
|
||||
val clean by task<Delete> {
|
||||
delete(buildDir)
|
||||
}
|
||||
|
||||
val extractAndroidJar by tasks.creating {
|
||||
dependsOn(androidPlatform)
|
||||
inputs.files(androidPlatform)
|
||||
val targetFile = File(libsDestDir, "android.jar")
|
||||
outputs.files(targetFile)
|
||||
doFirst {
|
||||
project.copy {
|
||||
from(zipTree(androidPlatform.singleFile).matching { include("**/android.jar") }.files.first())
|
||||
into(libsDestDir)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
artifacts.add(androidSdk.name, file("$sdkDestDir")) {
|
||||
builtBy(prepareSdk)
|
||||
}
|
||||
|
||||
artifacts.add(androidJar.name, file("$libsDestDir/android.jar")) {
|
||||
builtBy(extractAndroidJar)
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
|
||||
val protobufVersion by extra("2.6.1")
|
||||
|
||||
allprojects {
|
||||
group = "org.jetbrains.kotlin"
|
||||
version = protobufVersion
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.jar.JarEntry
|
||||
import java.util.jar.JarFile
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
plugins {
|
||||
base
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
val relocatedProtobuf by configurations.creating
|
||||
val relocatedProtobufSources by configurations.creating
|
||||
|
||||
val protobufVersion: String by rootProject.extra
|
||||
val protobufJarPrefix = "protobuf-$protobufVersion"
|
||||
val outputJarPath = "$buildDir/libs/$protobufJarPrefix-lite.jar"
|
||||
|
||||
dependencies {
|
||||
relocatedProtobuf(project(":protobuf-relocated"))
|
||||
}
|
||||
|
||||
val prepare by tasks.creating {
|
||||
inputs.files(relocatedProtobuf) // this also adds a dependency
|
||||
outputs.file(outputJarPath)
|
||||
doFirst {
|
||||
File(outputJarPath).parentFile.mkdirs()
|
||||
}
|
||||
doLast {
|
||||
val INCLUDE_START = "<include>**/"
|
||||
val INCLUDE_END = ".java</include>"
|
||||
val POM_PATH = "META-INF/maven/com.google.protobuf/protobuf-java/pom.xml"
|
||||
|
||||
fun loadAllFromJar(file: File): Map<String, Pair<JarEntry, ByteArray>> {
|
||||
val result = hashMapOf<String, Pair<JarEntry, ByteArray>>()
|
||||
JarFile(file).use { jar ->
|
||||
for (jarEntry in jar.entries()) {
|
||||
result[jarEntry.name] = Pair(jarEntry, jar.getInputStream(jarEntry).readBytes())
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
val mainJar = relocatedProtobuf.resolvedConfiguration.resolvedArtifacts.single {
|
||||
it.name == "protobuf-relocated" && it.classifier == null
|
||||
}.file
|
||||
|
||||
val allFiles = loadAllFromJar(mainJar)
|
||||
|
||||
val keepClasses = arrayListOf<String>()
|
||||
|
||||
val pomBytes = allFiles[POM_PATH]?.second ?: error("pom.xml is not found in protobuf jar at $POM_PATH")
|
||||
val lines = String(pomBytes).lines()
|
||||
|
||||
var liteProfileReached = false
|
||||
for (lineUntrimmed in lines) {
|
||||
val line = lineUntrimmed.trim()
|
||||
|
||||
if (liteProfileReached && line == "</includes>") {
|
||||
break
|
||||
}
|
||||
else if (line == "<id>lite</id>") {
|
||||
liteProfileReached = true
|
||||
continue
|
||||
}
|
||||
|
||||
if (liteProfileReached && line.startsWith(INCLUDE_START) && line.endsWith(INCLUDE_END)) {
|
||||
keepClasses.add(line.removeSurrounding(INCLUDE_START, INCLUDE_END))
|
||||
}
|
||||
}
|
||||
|
||||
assert(liteProfileReached && keepClasses.isNotEmpty()) { "Wrong pom.xml or the format has changed, check its contents at $POM_PATH" }
|
||||
|
||||
val outputFile = File(outputJarPath).apply { delete() }
|
||||
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputFile))).use { output ->
|
||||
for ((name, value) in allFiles) {
|
||||
val className = name.substringAfter("org/jetbrains/kotlin/protobuf/").substringBeforeLast(".class")
|
||||
if (keepClasses.any { className == it || className.startsWith(it + "$") }) {
|
||||
val (entry, bytes) = value
|
||||
output.putNextEntry(entry)
|
||||
output.write(bytes)
|
||||
output.closeEntry()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val mainArtifact = artifacts.add(
|
||||
"default",
|
||||
provider {
|
||||
prepare.outputs.files.singleFile
|
||||
}
|
||||
) {
|
||||
builtBy(prepare)
|
||||
classifier = ""
|
||||
}
|
||||
|
||||
val sourcesArtifact = artifacts.add(
|
||||
"default",
|
||||
provider {
|
||||
relocatedProtobuf.resolvedConfiguration.resolvedArtifacts.single { it.name == "protobuf-relocated" && it.classifier == "sources" }.file
|
||||
}
|
||||
) {
|
||||
classifier = "sources"
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
artifact(mainArtifact)
|
||||
artifact(sourcesArtifact)
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
url = uri("${rootProject.buildDir}/internal/repo")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||
import java.io.File
|
||||
|
||||
plugins {
|
||||
`java-base`
|
||||
`maven-publish`
|
||||
id("com.github.johnrengelman.shadow") version "4.0.3" apply false
|
||||
}
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
val baseProtobuf by configurations.creating
|
||||
val baseProtobufSources by configurations.creating
|
||||
|
||||
val protobufVersion: String by rootProject.extra
|
||||
val protobufJarPrefix = "protobuf-$protobufVersion"
|
||||
|
||||
val renamedSources = "$buildDir/renamedSrc/"
|
||||
val outputJarsPath = "$buildDir/libs"
|
||||
|
||||
dependencies {
|
||||
baseProtobuf("com.google.protobuf:protobuf-java:$protobufVersion")
|
||||
baseProtobufSources("com.google.protobuf:protobuf-java:$protobufVersion:sources")
|
||||
}
|
||||
|
||||
val prepare = task<ShadowJar>("prepare") {
|
||||
destinationDir = File(outputJarsPath)
|
||||
version = protobufVersion
|
||||
classifier = ""
|
||||
from(
|
||||
provider {
|
||||
baseProtobuf.files.find { it.name.startsWith("protobuf-java") }?.canonicalPath
|
||||
}
|
||||
)
|
||||
|
||||
relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf" ) {
|
||||
exclude("META-INF/maven/com.google.protobuf/protobuf-java/pom.properties")
|
||||
}
|
||||
}
|
||||
|
||||
artifacts.add("default", prepare)
|
||||
|
||||
val relocateSources = task<Copy>("relocateSources") {
|
||||
from(
|
||||
provider {
|
||||
zipTree(baseProtobufSources.files.find { it.name.startsWith("protobuf-java") && it.name.endsWith("-sources.jar") }
|
||||
?: throw GradleException("sources jar not found among ${baseProtobufSources.files}"))
|
||||
}
|
||||
)
|
||||
|
||||
into(renamedSources)
|
||||
|
||||
filter { it.replace("com.google.protobuf", "org.jetbrains.kotlin.protobuf") }
|
||||
}
|
||||
|
||||
val prepareSources = task<Jar>("prepareSources") {
|
||||
destinationDir = File(outputJarsPath)
|
||||
version = protobufVersion
|
||||
classifier = "sources"
|
||||
from(relocateSources)
|
||||
}
|
||||
|
||||
artifacts.add("default", prepareSources)
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
artifact(prepare)
|
||||
artifact(prepareSources)
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
url = uri("${rootProject.buildDir}/internal/repo")
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
include ":protobuf-lite", ":protobuf-relocated"
|
||||
Reference in New Issue
Block a user