|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
| 16 | +import java.nio.charset.StandardCharsets |
| 17 | +import org.gradle.api.GradleException |
| 18 | +import org.gradle.api.JavaVersion |
16 | 19 | import org.gradle.api.Plugin |
17 | 20 | import org.gradle.api.Project |
| 21 | +import org.gradle.api.artifacts.VersionCatalog |
| 22 | +import org.gradle.api.artifacts.VersionCatalogsExtension |
| 23 | +import org.gradle.api.tasks.compile.JavaCompile |
| 24 | +import org.jetbrains.kotlin.gradle.dsl.JvmDefaultMode |
| 25 | +import org.jetbrains.kotlin.gradle.dsl.JvmTarget |
| 26 | +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile |
| 27 | +import tapmoc.configureKotlinCompatibility |
18 | 28 |
|
19 | 29 | class BuildSupport : Plugin<Project> { |
20 | 30 | override fun apply(project: Project) { |
21 | | - // Do nothing. |
| 31 | + project.configureKotlinCompatibility(project.getVersionByName("kotlinCoreLibrariesVersion")) |
| 32 | + |
| 33 | + // `project.configureJavaCompatibility(8)` is not used as the code would compile with JDK 8. |
| 34 | + // This will fail as `RealBufferedSource.kt` uses a Java API not available in Java 8:` InputStream.transferTo` |
| 35 | + // To use `project.configureJavaCompatibility`, the min supported Java version would need to be bumped to 11 |
| 36 | + project.tasks.withType(KotlinCompile::class.java).configureEach { |
| 37 | + compilerOptions { |
| 38 | + jvmTarget.set(JvmTarget.JVM_1_8) |
| 39 | + jvmDefault.set(JvmDefaultMode.NO_COMPATIBILITY) |
| 40 | + } |
| 41 | + } |
| 42 | + project.tasks.withType(JavaCompile::class.java) { |
| 43 | + options.encoding = StandardCharsets.UTF_8.toString() |
| 44 | + sourceCompatibility = JavaVersion.VERSION_1_8.toString() |
| 45 | + targetCompatibility = JavaVersion.VERSION_1_8.toString() |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +val Project.versionCatalog: VersionCatalog |
| 51 | + get() = project.extensions.getByType(VersionCatalogsExtension::class.java).find("libs").get() |
| 52 | + |
| 53 | +fun Project.getVersionByName(name: String): String { |
| 54 | + val version = versionCatalog.findVersion(name) |
| 55 | + return if (version.isPresent) { |
| 56 | + version.get().requiredVersion |
| 57 | + } else { |
| 58 | + throw GradleException("Could not find a version for `$name`") |
22 | 59 | } |
23 | 60 | } |
0 commit comments