Skip to content

Commit

Permalink
Simplify kts (square#6926)
Browse files Browse the repository at this point in the history
* Simplify kts

* Use extensions

* Use extensions

* Simplify kts
  • Loading branch information
Goooler authored Nov 26, 2021
1 parent 3417fd0 commit d0759a7
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 116 deletions.
24 changes: 12 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ buildscript {

allprojects {
group = "com.squareup.okhttp3"
project.ext["artifactId"] = Projects.publishedArtifactId(project.name)
project.ext["artifactId"] = project.name.publishedArtifactId()
version = "5.0.0-SNAPSHOT"

repositories {
Expand All @@ -38,7 +38,7 @@ allprojects {
maven(url = "https://dl.bintray.com/kotlin/dokka")
}

val downloadDependencies by tasks.creating {
tasks.create("downloadDependencies") {
description = "Download all dependencies to the Gradle cache"
doLast {
for (configuration in configurations) {
Expand Down Expand Up @@ -73,7 +73,7 @@ subprojects {
apply(plugin = "biz.aQute.bnd.builder")

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
options.encoding = Charsets.UTF_8.toString()
}

configure<JavaPluginExtension> {
Expand All @@ -98,14 +98,14 @@ subprojects {
configure<CheckstyleExtension> {
config = resources.text.fromArchiveEntry(checkstyleConfig, "google_checks.xml")
toolVersion = Versions.checkStyle
sourceSets = listOf(project.sourceSets.getByName("main"))
sourceSets = listOf(project.sourceSets["main"])
}
}

// Animal Sniffer confirms we generally don't use APIs not on Java 8.
configure<AnimalSnifferExtension> {
annotation = "okhttp3.internal.SuppressSignatureCheck"
sourceSets = listOf(project.sourceSets.getByName("main"))
sourceSets = listOf(project.sourceSets["main"])
}
val signature by configurations.getting
dependencies {
Expand All @@ -115,7 +115,7 @@ subprojects {

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
jvmTarget = JavaVersion.VERSION_1_8.toString()
freeCompilerArgs = listOf(
"-Xjvm-default=compatibility",
"-Xopt-in=kotlin.RequiresOptIn"
Expand Down Expand Up @@ -153,7 +153,7 @@ subprojects {

if (platform == "jdk8alpn") {
// Add alpn-boot on Java 8 so we can use HTTP/2 without a stable API.
val alpnBootVersion = Alpn.alpnBootVersion()
val alpnBootVersion = alpnBootVersion()
if (alpnBootVersion != null) {
val alpnBootJar = configurations.detachedConfiguration(
dependencies.create("org.mortbay.jetty.alpn:alpn-boot:$alpnBootVersion")
Expand Down Expand Up @@ -223,14 +223,14 @@ subprojects {
}

publications {
val maven by creating(MavenPublication::class) {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.ext["artifactId"].toString()
version = project.version.toString()
if (bom) {
from(components.getByName("javaPlatform"))
from(components["javaPlatform"])
} else {
from(components.getByName("java"))
from(components["java"])
}
pom {
name.set(project.name)
Expand All @@ -239,7 +239,7 @@ subprojects {
licenses {
license {
name.set("The Apache Software License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
url.set("https://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
Expand Down Expand Up @@ -270,7 +270,7 @@ subprojects {

val publishing = extensions.getByType<PublishingExtension>()
configure<SigningExtension> {
sign(publishing.publications.getByName("maven"))
sign(publishing.publications["maven"])
}
}

Expand Down
63 changes: 30 additions & 33 deletions buildSrc/src/main/kotlin/alpn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,39 @@
* limitations under the License.
*/

object Alpn {
// https://www.eclipse.org/jetty/documentation/current/alpn-chapter.html#alpn-versions
fun alpnBootVersionForPatchVersion(patchVersion: Int): String? {
return when (patchVersion) {
in 0..24 -> "8.1.0.v20141016"
in 25..30 -> "8.1.2.v20141202"
in 31..50 -> "8.1.3.v20150130"
in 51..59 -> "8.1.4.v20150727"
in 60..64 -> "8.1.5.v20150921"
in 65..70 -> "8.1.6.v20151105"
in 71..77 -> "8.1.7.v20160121"
in 78..101 -> "8.1.8.v20160420"
in 102..111 -> "8.1.9.v20160720"
in 112..120 -> "8.1.10.v20161026"
in 121..160 -> "8.1.11.v20170118"
in 161..181 -> "8.1.12.v20180117"
in 191..242 -> "8.1.13.v20181017"
else -> null
}
// https://www.eclipse.org/jetty/documentation/current/alpn-chapter.html#alpn-versions
private fun alpnBootVersionForPatchVersion(patchVersion: Int): String? {
return when (patchVersion) {
in 0..24 -> "8.1.0.v20141016"
in 25..30 -> "8.1.2.v20141202"
in 31..50 -> "8.1.3.v20150130"
in 51..59 -> "8.1.4.v20150727"
in 60..64 -> "8.1.5.v20150921"
in 65..70 -> "8.1.6.v20151105"
in 71..77 -> "8.1.7.v20160121"
in 78..101 -> "8.1.8.v20160420"
in 102..111 -> "8.1.9.v20160720"
in 112..120 -> "8.1.10.v20161026"
in 121..160 -> "8.1.11.v20170118"
in 161..181 -> "8.1.12.v20180117"
in 191..242 -> "8.1.13.v20181017"
else -> null
}
}

/**
* Returns the alpn-boot version specific to this OpenJDK 8 JVM, or null if this is not a Java 8 VM.
* https://github.com/xjdr/xio/blob/master/alpn-boot.gradle
*/
@JvmStatic
fun alpnBootVersion(): String? {
val version = System.getProperty("alpn.boot.version")
/**
* Returns the alpn-boot version specific to this OpenJDK 8 JVM, or null if this is not a Java 8 VM.
* https://github.com/xjdr/xio/blob/master/alpn-boot.gradle
*/
fun alpnBootVersion(): String? {
val version = System.getProperty("alpn.boot.version")

if (version != null) {
return version
}
if (version != null) {
return version
}

val javaVersion = System.getProperty("java.version")
val match = "1\\.8\\.0_(\\d+)(-.*)?".toRegex().find(javaVersion) ?: return null
val javaVersion = System.getProperty("java.version")
val match = "1\\.8\\.0_(\\d+)(-.*)?".toRegex().find(javaVersion) ?: return null

return alpnBootVersionForPatchVersion(match.groupValues.first().toInt())
}
return alpnBootVersionForPatchVersion(match.groupValues.first().toInt())
}
91 changes: 41 additions & 50 deletions buildSrc/src/main/kotlin/artifacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,51 @@ import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.withConvention

object Projects {
/** Returns the artifact ID for the project, or null if it is not published. */
@JvmStatic
fun publishedArtifactId(projectName: String): String? {
return when (projectName) {
"okhttp-logging-interceptor" -> "logging-interceptor"
"mockwebserver" -> "mockwebserver3"
"mockwebserver-junit4" -> "mockwebserver3-junit4"
"mockwebserver-junit5" -> "mockwebserver3-junit5"
"mockwebserver-deprecated" -> "mockwebserver"
"okcurl",
"okhttp",
"okhttp-bom",
"okhttp-brotli",
"okhttp-dnsoverhttps",
"okhttp-sse",
"okhttp-tls",
"okhttp-urlconnection" -> projectName
else -> null
}
fun String.publishedArtifactId(): String? {
return when (this) {
"okhttp-logging-interceptor" -> "logging-interceptor"
"mockwebserver" -> "mockwebserver3"
"mockwebserver-junit4" -> "mockwebserver3-junit4"
"mockwebserver-junit5" -> "mockwebserver3-junit5"
"mockwebserver-deprecated" -> "mockwebserver"
"okcurl",
"okhttp",
"okhttp-bom",
"okhttp-brotli",
"okhttp-dnsoverhttps",
"okhttp-sse",
"okhttp-tls",
"okhttp-urlconnection" -> this
else -> null
}
}

@JvmStatic
fun applyOsgi(project: Project, vararg bndProperties: String) {
project.run {
apply(plugin = "biz.aQute.bnd.builder")
sourceSets.create("osgi")
tasks["jar"].withConvention(BundleTaskConvention::class) {
setClasspath(sourceSets["osgi"].compileClasspath + project.sourceSets["main"].compileClasspath)
bnd(*bndProperties)
}
dependencies.add("osgiApi", Dependencies.kotlinStdlibOsgi)
}
fun Project.applyOsgi(vararg bndProperties: String) {
apply(plugin = "biz.aQute.bnd.builder")
sourceSets.create("osgi")
tasks["jar"].withConvention(BundleTaskConvention::class) {
setClasspath(sourceSets["osgi"].compileClasspath + project.sourceSets["main"].compileClasspath)
bnd(*bndProperties)
}
dependencies.add("osgiApi", Dependencies.kotlinStdlibOsgi)
}

/**
* Returns a .jar file for the golden version of this project.
* https://github.com/Visistema/Groovy1/blob/ba5eb9b2f19ca0cc8927359ce414c4e1974b7016/gradle/binarycompatibility.gradle#L48
*/
@JvmStatic
@JvmOverloads
fun baselineJar(project: Project, version: String = "3.14.1"): File? {
val group = project.group
val artifactId = project.extra["artifactId"]
return try {
val jarFile = "$artifactId-${version}.jar"
project.group = "virtual_group_for_japicmp"
val dependency = project.dependencies.create("$group:$artifactId:$version@jar")
project.configurations.detachedConfiguration(dependency).files.find { (it.name == jarFile) }
} catch (e: Exception) {
null
} finally {
project.group = group
}
/**
* Returns a .jar file for the golden version of this project.
* https://github.com/Visistema/Groovy1/blob/ba5eb9b2f19ca0cc8927359ce414c4e1974b7016/gradle/binarycompatibility.gradle#L48
*/
fun Project.baselineJar(version: String = "3.14.1"): File? {
val originalGroup = group
val artifactId = extra["artifactId"]
return try {
val jarFile = "$artifactId-${version}.jar"
group = "virtual_group_for_japicmp"
val dependency = dependencies.create("$originalGroup:$artifactId:$version@jar")
configurations.detachedConfiguration(dependency).files.find { (it.name == jarFile) }
} catch (e: Exception) {
null
} finally {
group = originalGroup
}
}

Expand Down
2 changes: 1 addition & 1 deletion mockwebserver-deprecated/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ afterEvaluate {

tasks.register<JapicmpTask>("japicmp") {
dependsOn("jar")
oldClasspath = files(Projects.baselineJar(project))
oldClasspath = files(project.baselineJar())
newClasspath = files(tasks.jar.get().archiveFile)
isOnlyBinaryIncompatibleModified = true
isFailOnModification = true
Expand Down
2 changes: 1 addition & 1 deletion okhttp-bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
dependencies {
constraints {
project.rootProject.subprojects.forEach { subproject ->
val artifactId = Projects.publishedArtifactId(subproject.name)
val artifactId = subproject.name.publishedArtifactId()
if (artifactId != null && artifactId != "okhttp-bom") {
api(subproject)
}
Expand Down
3 changes: 1 addition & 2 deletions okhttp-brotli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ plugins {
id("org.jetbrains.dokka")
}

Projects.applyOsgi(
project,
project.applyOsgi(
"Export-Package: okhttp3.brotli",
"Automatic-Module-Name: okhttp3.brotli",
"Bundle-SymbolicName: com.squareup.okhttp3.brotli"
Expand Down
3 changes: 1 addition & 2 deletions okhttp-dnsoverhttps/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ plugins {
id("org.jetbrains.dokka")
}

Projects.applyOsgi(
project,
project.applyOsgi(
"Export-Package: okhttp3.dnsoverhttps",
"Automatic-Module-Name: okhttp3.dnsoverhttps",
"Bundle-SymbolicName: com.squareup.okhttp3.dnsoverhttps"
Expand Down
5 changes: 2 additions & 3 deletions okhttp-logging-interceptor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ plugins {
id("me.champeau.gradle.japicmp")
}

Projects.applyOsgi(
project,
project.applyOsgi(
"Export-Package: okhttp3.logging",
"Automatic-Module-Name: okhttp3.logging",
"Bundle-SymbolicName: com.squareup.okhttp3.logging"
Expand Down Expand Up @@ -36,7 +35,7 @@ afterEvaluate {

tasks.register<JapicmpTask>("japicmp") {
dependsOn("jar")
oldClasspath = files(Projects.baselineJar(project))
oldClasspath = files(project.baselineJar())
newClasspath = files(tasks.jar.get().archiveFile)
isOnlyBinaryIncompatibleModified = true
isFailOnModification = true
Expand Down
5 changes: 2 additions & 3 deletions okhttp-sse/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ plugins {
id("me.champeau.gradle.japicmp")
}

Projects.applyOsgi(
project,
project.applyOsgi(
"Export-Package: okhttp3.sse",
"Automatic-Module-Name: okhttp3.sse",
"Bundle-SymbolicName: com.squareup.okhttp3.sse"
Expand All @@ -35,7 +34,7 @@ afterEvaluate {

tasks.register<JapicmpTask>("japicmp") {
dependsOn("jar")
oldClasspath = files(Projects.baselineJar(project))
oldClasspath = files(project.baselineJar())
newClasspath = files(tasks.jar.get().archiveFile)
isOnlyBinaryIncompatibleModified = true
isFailOnModification = true
Expand Down
5 changes: 2 additions & 3 deletions okhttp-tls/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ plugins {
id("me.champeau.gradle.japicmp")
}

Projects.applyOsgi(
project,
project.applyOsgi(
"Export-Package: okhttp3.tls",
"Automatic-Module-Name: okhttp3.tls",
"Bundle-SymbolicName: com.squareup.okhttp3.tls"
Expand Down Expand Up @@ -41,7 +40,7 @@ animalsniffer {

tasks.register<JapicmpTask>("japicmp") {
dependsOn("jar")
oldClasspath = files(Projects.baselineJar(project))
oldClasspath = files(project.baselineJar())
newClasspath = files(tasks.jar.get().archiveFile)
isOnlyBinaryIncompatibleModified = true
isFailOnModification = true
Expand Down
5 changes: 2 additions & 3 deletions okhttp-urlconnection/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ plugins {

val mainProj: Project by lazy { project(":okhttp") }

Projects.applyOsgi(
project,
project.applyOsgi(
"Fragment-Host: com.squareup.okhttp3; bundle-version=\"\${range;[==,+);\${version_cleanup;${mainProj.version}}}\"",
"Automatic-Module-Name: okhttp3.urlconnection",
"Bundle-SymbolicName: com.squareup.okhttp3.urlconnection",
Expand Down Expand Up @@ -38,7 +37,7 @@ afterEvaluate {

tasks.register<JapicmpTask>("japicmp") {
dependsOn("jar")
oldClasspath = files(Projects.baselineJar(project))
oldClasspath = files(project.baselineJar())
newClasspath = files(tasks.jar.get().archiveFile)
isOnlyBinaryIncompatibleModified = true
isFailOnModification = true
Expand Down
Loading

0 comments on commit d0759a7

Please sign in to comment.