diff --git a/build.gradle.kts b/build.gradle.kts index 28056dcd..675a37ab 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -40,7 +40,7 @@ idea { } } -val clean by tasks.registering(Delete::class) { +val clean by tasks.existing { delete(layout.buildDirectory) } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 770845b7..b9efd016 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -1,5 +1,18 @@ -import org.jetbrains.kotlin.config.JvmTarget - +/** + * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * + * Licensed 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 + * + * https://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. + */ plugins { `kotlin-dsl` } @@ -7,9 +20,7 @@ plugins { dependencies { implementation(libs.downloadTaskPlugin) implementation(libs.spotlessPlugin) - implementation(libs.kotlinPlugin) { - exclude(module = "kotlin-android-extensions") - } + implementation(libs.kotlinPlugin) { exclude(module = "kotlin-android-extensions") } implementation(libs.shadowPlugin) // fix from the Gradle team: makes version catalog symbols available in build scripts @@ -22,12 +33,4 @@ java { targetCompatibility = JavaVersion.VERSION_17 } -kotlin { - target { - compilations.configureEach { - kotlinOptions { - jvmTarget = "17" - } - } - } -} +kotlin { target { compilations.configureEach { kotlinOptions { jvmTarget = "17" } } } } diff --git a/buildSrc/src/main/kotlin/pklAllProjects.gradle.kts b/buildSrc/src/main/kotlin/pklAllProjects.gradle.kts index b65b51ab..ab440195 100644 --- a/buildSrc/src/main/kotlin/pklAllProjects.gradle.kts +++ b/buildSrc/src/main/kotlin/pklAllProjects.gradle.kts @@ -1,10 +1,27 @@ +/** + * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * + * Licensed 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 + * + * https://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 com.diffplug.gradle.spotless.KotlinGradleExtension +import org.gradle.accessors.dm.LibrariesForLibs import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +plugins { id("com.diffplug.spotless") } + val buildInfo = extensions.create("buildInfo", project) -dependencyLocking { - lockAllConfigurations() -} +dependencyLocking { lockAllConfigurations() } configurations { val rejectedVersionSuffix = Regex("-alpha|-beta|-eap|-m|-rc|-snapshot", RegexOption.IGNORE_CASE) @@ -13,8 +30,10 @@ configurations { componentSelection { all { if (rejectedVersionSuffix.containsMatchIn(candidate.version)) { - reject("Rejected dependency $candidate " + - "because it has a prelease version suffix matching `$rejectedVersionSuffix`.") + reject( + "Rejected dependency $candidate " + + "because it has a prelease version suffix matching `$rejectedVersionSuffix`." + ) } } } @@ -36,24 +55,13 @@ tasks.withType().configureEach { } plugins.withType(IdeaPlugin::class).configureEach { - val errorMessage = "Use IntelliJ Gradle import instead of running the `idea` task. See README for more information." + val errorMessage = + "Use IntelliJ Gradle import instead of running the `idea` task. See README for more information." - tasks.named("idea") { - doFirst { - throw GradleException(errorMessage) - } - } - tasks.named("ideaModule") { - doFirst { - throw GradleException(errorMessage) - } - } + tasks.named("idea") { doFirst { throw GradleException(errorMessage) } } + tasks.named("ideaModule") { doFirst { throw GradleException(errorMessage) } } if (project == rootProject) { - tasks.named("ideaProject") { - doFirst { - throw GradleException(errorMessage) - } - } + tasks.named("ideaProject") { doFirst { throw GradleException(errorMessage) } } } } @@ -72,11 +80,7 @@ plugins.withType(MavenPublishPlugin::class).configureEach { // dependency versions in generated POMs publications { withType(MavenPublication::class.java) { - versionMapping { - allVariants { - fromResolutionResult() - } - } + versionMapping { allVariants { fromResolutionResult() } } } } } @@ -84,13 +88,10 @@ plugins.withType(MavenPublishPlugin::class).configureEach { // settings.gradle.kts sets `--write-locks` // if Gradle command line contains this task name -val updateDependencyLocks by tasks.registering { - doLast { - configurations - .filter { it.isCanBeResolved } - .forEach { it.resolve() } +val updateDependencyLocks by + tasks.registering { + doLast { configurations.filter { it.isCanBeResolved }.forEach { it.resolve() } } } -} val allDependencies by tasks.registering(DependencyReportTask::class) @@ -118,3 +119,36 @@ tasks.withType(JavaExec::class).configureEach { server = true } } + +// Version Catalog library symbols. +private val libs = the() + +private val licenseHeaderFile by lazy { + rootProject.file("buildSrc/src/main/resources/license-header.star-block.txt") +} + +private fun KotlinGradleExtension.configureFormatter() { + ktfmt(libs.versions.ktfmt.get()).googleStyle() + targetExclude("**/generated/**", "**/build/**") + licenseHeaderFile(licenseHeaderFile, "([a-zA-Z]|@file|//)") +} + +spotless { + // When building root project, format buildSrc files too. + // We need this because buildSrc is not a subproject of the root project, so a top-level + // `spotlessApply` will not trigger `buildSrc:spotlessApply`. + if (project === rootProject) { + kotlinGradle { + configureFormatter() + target("*.kts", "buildSrc/**/*.kts") + } + kotlin { + ktfmt(libs.versions.ktfmt.get()).googleStyle() + targetExclude("**/generated/**", "**/build/**") + target("buildSrc/**/*.kt") + licenseHeaderFile(licenseHeaderFile) + } + } else { + kotlinGradle { configureFormatter() } + } +} diff --git a/buildSrc/src/main/kotlin/pklJavaLibrary.gradle.kts b/buildSrc/src/main/kotlin/pklJavaLibrary.gradle.kts index fad838b9..5a36c24c 100644 --- a/buildSrc/src/main/kotlin/pklJavaLibrary.gradle.kts +++ b/buildSrc/src/main/kotlin/pklJavaLibrary.gradle.kts @@ -1,3 +1,18 @@ +/** + * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * + * Licensed 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 + * + * https://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. + */ @file:Suppress("HttpUrlsUsage") import org.gradle.accessors.dm.LibrariesForLibs @@ -30,11 +45,14 @@ spotless { targetExclude("**/generated/**", "**/build/**") licenseHeaderFile(rootProject.file("buildSrc/src/main/resources/license-header.star-block.txt")) } + kotlin { + ktfmt(libs.versions.ktfmt.get()).googleStyle() + targetExclude("**/generated/**", "**/build/**") + licenseHeaderFile(rootProject.file("buildSrc/src/main/resources/license-header.star-block.txt")) + } } -tasks.compileKotlin { - enabled = false -} +tasks.compileKotlin { enabled = false } tasks.jar { manifest { @@ -49,15 +67,18 @@ tasks.javadoc { (options as StandardJavadocDocletOptions).addStringOption("Xdoclint:none", "-quiet") } -val workAroundKotlinGradlePluginBug by tasks.registering { - doLast { - // Works around this problem, which sporadically appears and disappears in different subprojects: - // A problem was found with the configuration of task ':pkl-executor:compileJava' (type 'JavaCompile'). - // > Directory '[...]/pkl/pkl-executor/build/classes/kotlin/main' - // specified for property 'compileKotlinOutputClasses' does not exist. - layout.buildDirectory.dir("classes/kotlin/main").get().asFile.mkdirs() +val workAroundKotlinGradlePluginBug by + tasks.registering { + doLast { + // Works around this problem, which sporadically appears and disappears in different + // subprojects: + // A problem was found with the configuration of task ':pkl-executor:compileJava' (type + // 'JavaCompile'). + // > Directory '[...]/pkl/pkl-executor/build/classes/kotlin/main' + // specified for property 'compileKotlinOutputClasses' does not exist. + layout.buildDirectory.dir("classes/kotlin/main").get().asFile.mkdirs() + } } -} tasks.compileJava { dependsOn(workAroundKotlinGradlePluginBug) diff --git a/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts b/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts index cbc72836..64040d2c 100644 --- a/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts +++ b/buildSrc/src/main/kotlin/pklKotlinLibrary.gradle.kts @@ -23,11 +23,3 @@ dependencies { tasks.compileKotlin { enabled = true // disabled by pklJavaLibrary } - -spotless { - kotlin { - ktfmt(libs.versions.ktfmt.get()).googleStyle() - targetExclude("**/generated/**", "**/build/**") - licenseHeaderFile(rootProject.file("buildSrc/src/main/resources/license-header.star-block.txt")) - } -} diff --git a/pkl-core/src/test/kotlin/org/pkl/core/runtime/DefaultModuleResolverTest.kt b/pkl-core/src/test/kotlin/org/pkl/core/runtime/DefaultModuleResolverTest.kt index 8e0bb875..184e875d 100644 --- a/pkl-core/src/test/kotlin/org/pkl/core/runtime/DefaultModuleResolverTest.kt +++ b/pkl-core/src/test/kotlin/org/pkl/core/runtime/DefaultModuleResolverTest.kt @@ -1,4 +1,4 @@ -//package org.pkl.core.runtime; +package org.pkl.core.runtime; // //import java.io.IOException; //import java.net.URI; diff --git a/pkl-core/src/test/kotlin/org/pkl/core/runtime/ModuleKeyTest.kt b/pkl-core/src/test/kotlin/org/pkl/core/runtime/ModuleKeyTest.kt index 5bf665d7..77773e82 100644 --- a/pkl-core/src/test/kotlin/org/pkl/core/runtime/ModuleKeyTest.kt +++ b/pkl-core/src/test/kotlin/org/pkl/core/runtime/ModuleKeyTest.kt @@ -1,4 +1,4 @@ -//package org.pkl.core.runtime; +package org.pkl.core.runtime; // //import java.io.File; //import java.io.IOException;