Skip to content

Scala build infra #1211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 62 additions & 46 deletions build-logic/src/main/kotlin/polaris-java.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,25 @@ apply<PublishingHelperPlugin>()

tasks.withType(JavaCompile::class.java).configureEach {
options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:deprecation"))
options.errorprone.disableAllWarnings = true
options.errorprone.disableWarningsInGeneratedCode = true
options.errorprone.excludedPaths =
".*/${project.layout.buildDirectory.get().asFile.relativeTo(projectDir)}/generated/.*"
options.errorprone.error(
"DefaultCharset",
"FallThrough",
"MissingCasesInEnumSwitch",
"MissingOverride",
"ModifiedButNotUsed",
"OrphanedFormatString",
"PatternMatchingInstanceof",
"StringCaseLocaleUsage",
)

if (project.extra.has("reused-project-dir")) {
options.errorprone.disableAllChecks = true
} else {
options.errorprone.disableAllWarnings = true
options.errorprone.disableWarningsInGeneratedCode = true
options.errorprone.excludedPaths =
".*/${project.layout.buildDirectory.get().asFile.relativeTo(projectDir)}/generated/.*"
options.errorprone.error(
"DefaultCharset",
"FallThrough",
"MissingCasesInEnumSwitch",
"MissingOverride",
"ModifiedButNotUsed",
"OrphanedFormatString",
"PatternMatchingInstanceof",
"StringCaseLocaleUsage",
)
}
}

tasks.register("compileAll").configure {
Expand Down Expand Up @@ -150,42 +155,53 @@ tasks.withType(Jar::class).configureEach {
}
}

spotless {
java {
target("src/*/java/**/*.java")
googleJavaFormat()
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"))
endWithNewline()
custom(
"disallowWildcardImports",
object : Serializable, FormatterFunc {
override fun apply(text: String): String {
val regex = "~/import .*\\.\\*;/".toRegex()
if (regex.matches(text)) {
throw GradleException("Wildcard imports disallowed - ${regex.findAll(text)}")
if (!project.extra.has("reused-project-dir") && !gradle.ideSyncActive()) {
spotless {
java {
target("src/*/java/**/*.java")
googleJavaFormat()
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"))
endWithNewline()
custom(
"disallowWildcardImports",
object : Serializable, FormatterFunc {
override fun apply(text: String): String {
val regex = "~/import .*\\.\\*;/".toRegex()
if (regex.matches(text)) {
throw GradleException("Wildcard imports disallowed - ${regex.findAll(text)}")
}
return text
}
return text
}
},
)
toggleOffOn()
}
kotlinGradle {
ktfmt().googleStyle()
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"), "$")
target("*.gradle.kts")
}
format("xml") {
target("src/**/*.xml", "src/**/*.xsd")
targetExclude("codestyle/copyright-header.xml")
eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep.XML)
.configFile(rootProject.file("codestyle/org.eclipse.wst.xml.core.prefs"))
// getting the license-header delimiter right is a bit tricky.
// licenseHeaderFile(rootProject.file("codestyle/copyright-header.xml"), '<^[!?].*$')
},
)
toggleOffOn()
}
scala {
scalafmt(requiredDependencyVersion("scalafmt"))
.configFile(rootProject.file("codestyle/scalafmt.conf").toString())
licenseHeaderFile(
rootProject.file("codestyle/copyright-header-java.txt"),
"^(package|import) .*$",
)
target("src/**/scala/**/*.scala")
}
kotlinGradle {
ktfmt().googleStyle()
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"), "$")
target("*.gradle.kts")
}
format("xml") {
target("src/**/*.xml", "src/**/*.xsd")
targetExclude("codestyle/copyright-header.xml")
eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep.XML)
.configFile(rootProject.file("codestyle/org.eclipse.wst.xml.core.prefs"))
// getting the license-header delimiter right is a bit tricky.
// licenseHeaderFile(rootProject.file("codestyle/copyright-header.xml"), '<^[!?].*$')
}
}
}

dependencies { errorprone(versionCatalogs.named("libs").findLibrary("errorprone").get()) }
dependencies { errorprone(requiredDependency("errorprone")) }

java {
withJavadocJar()
Expand Down
2 changes: 1 addition & 1 deletion build-logic/src/main/kotlin/polaris-root.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ spotless {
}
}

if (System.getProperty("idea.sync.active").toBoolean()) {
if (gradle.ideSyncActive()) {
idea {
module {
isDownloadJavadoc = false // was 'true', but didn't work
Expand Down
66 changes: 66 additions & 0 deletions build-logic/src/main/kotlin/polaris-scala.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.bundling.Jar
import org.gradle.api.tasks.scala.ScalaCompile
import org.gradle.api.tasks.scala.ScalaDoc
import org.gradle.kotlin.dsl.*
import org.gradle.language.scala.tasks.KeepAliveMode

plugins {
id("polaris-server")
scala
}

tasks.withType<ScalaCompile>().configureEach {
options.release = 21
scalaCompileOptions.additionalParameters.add("-release:21")
sourceCompatibility = "21"
targetCompatibility = "21"
}

tasks.withType<ScalaCompile>().configureEach {
scalaCompileOptions.keepAliveMode = KeepAliveMode.DAEMON
scalaCompileOptions.encoding = "UTF-8"
}

val scaladoc = tasks.named<ScalaDoc>("scaladoc")
val scaladocJar = tasks.register<Jar>("scaladocJar")

scaladocJar.configure {
dependsOn(scaladoc)
val baseJar = tasks.getByName<Jar>("jar")
from(scaladoc.get().destinationDir)
destinationDirectory = baseJar.destinationDirectory
archiveClassifier = "scaladoc"
}

tasks.named("assemble").configure { dependsOn(scaladocJar) }

configure<PublishingExtension> {
publications {
withType(MavenPublication::class.java) {
if (name == "maven") {
artifact(scaladocJar)
}
}
}
}
42 changes: 42 additions & 0 deletions build-logic/src/main/kotlin/util.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.lang.IllegalStateException
import org.gradle.api.Project
import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.invocation.Gradle
import org.gradle.kotlin.dsl.getByType

fun Gradle.ideSyncActive(): Boolean =
System.getProperty("idea.sync.active").toBoolean() ||
System.getProperty("eclipse.product") != null ||
startParameter.taskNames.any { it.startsWith("eclipse") }

fun Project.requiredDependencyVersion(dependencyName: String): String =
requiredDependency(dependencyName).version!!

fun Project.requiredDependency(dependencyName: String): MinimalExternalModuleDependency {
val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
val dependency =
versionCatalog.findLibrary(dependencyName).orElseThrow {
IllegalStateException("No library '$dependencyName' defined in version catalog 'libs'")
}
return dependency.get()
}
42 changes: 42 additions & 0 deletions codestyle/scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

version = 3.9.4
runner.dialect = scala213

maxColumn = 100

preset = default
align.preset = some

assumeStandardLibraryStripMargin = true
align.stripMargin = true

rewrite.rules = [
AvoidInfix
RedundantBraces
RedundantParens
SortModifiers
PreferCurlyFors
Imports
]

rewrite.imports.sort = original
docstrings.style = Asterisk
docstrings.wrap = fold
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ prometheus-metrics-exporter-servlet-jakarta = { module = "io.prometheus:promethe
quarkus-bom = { module = "io.quarkus.platform:quarkus-bom", version.ref = "quarkus" }
scala212-lang-library = { module = "org.scala-lang:scala-library", version.ref = "scala212" }
scala212-lang-reflect = { module = "org.scala-lang:scala-reflect", version.ref = "scala212" }
scalafmt = { module = "org.scalameta:scalafmt-core_2.13", version = "3.9.4" }
s3mock-testcontainers = { module = "com.adobe.testing:s3mock-testcontainers", version = "3.12.0" }
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
smallrye-common-annotation = { module = "io.smallrye.common:smallrye-common-annotation", version = "2.10.0" }
Expand Down