aboutsummaryrefslogtreecommitdiff
path: root/buildSrc
diff options
context:
space:
mode:
authorTing-Yuan Huang <laszio@google.com>2022-02-23 02:44:34 -0800
committerlaszio <ting-yuan@users.noreply.github.com>2022-02-23 20:41:53 -0800
commit2943cb08c73fb9af25d08e1b81961775979ea1c4 (patch)
treeb030d3cc5909ead721681e5ca65a8c9c00b5d940 /buildSrc
parent7eacd30944667cd186bdf509ce76c3119683f43b (diff)
downloadksp-2943cb08c73fb9af25d08e1b81961775979ea1c4.tar.gz
Track API changes in CI
using Metalava.
Diffstat (limited to 'buildSrc')
-rw-r--r--buildSrc/src/main/kotlin/com/google/devtools/ksp/ApiCheck.kt77
1 files changed, 77 insertions, 0 deletions
diff --git a/buildSrc/src/main/kotlin/com/google/devtools/ksp/ApiCheck.kt b/buildSrc/src/main/kotlin/com/google/devtools/ksp/ApiCheck.kt
new file mode 100644
index 00000000..ddd04a29
--- /dev/null
+++ b/buildSrc/src/main/kotlin/com/google/devtools/ksp/ApiCheck.kt
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2022 Google LLC
+ * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
+ *
+ * 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
+ *
+ * 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.
+ */
+package com.google.devtools.ksp
+
+import org.gradle.api.Project
+import org.gradle.api.artifacts.Configuration
+import org.gradle.api.tasks.JavaExec
+
+val API_BASE_FILE="api.base"
+
+/**
+ * Adapted from ktlint
+ */
+fun Project.configureMetalava() {
+ val checkProvider = tasks.register("checkApi", JavaExec::class.java) { task ->
+ task.configureCommonMetalavaArgs(this@configureMetalava)
+ task.description = "Check API compatibility."
+ task.group = "Verification"
+ task.args = listOf("--check-compatibility:api:released", API_BASE_FILE) + task.args!!
+ }
+
+ afterEvaluate {
+ // check task is not available yet, which is why we use afterEvaluate
+ project.tasks.named("check").configure { checkTask ->
+ checkTask.dependsOn(checkProvider)
+ }
+ }
+
+ tasks.register("updateApi", JavaExec::class.java) { task ->
+ task.configureCommonMetalavaArgs(this@configureMetalava)
+ task.description = "Update API base file."
+ task.group = "formatting"
+ task.args = listOf("--api", API_BASE_FILE) + task.args!!
+ }
+}
+
+/**
+ * Configures common Metalava parameters
+ */
+private fun JavaExec.configureCommonMetalavaArgs(
+ project: Project
+) {
+ val apiFiles = project.fileTree(project.projectDir).also {
+ it.include("**/*.kt")
+ it.include("**/*.java")
+ it.exclude("**/testData/**")
+ it.exclude("**/build/**")
+ it.exclude("**/.*/**")
+ }
+ inputs.files(apiFiles)
+ classpath = project.getMetalavaConfiguration()
+ main = "com.android.tools.metalava.Driver"
+ args = listOf(
+ "--source-files",
+ ) + apiFiles.files.map { it.absolutePath }
+}
+
+private fun Project.getMetalavaConfiguration(): Configuration {
+ return configurations.findByName("metalava") ?: configurations.create("metalava") {
+ val dependency = dependencies.create("com.android.tools.metalava:metalava:1.0.0-alpha04")
+ it.dependencies.add(dependency)
+ }
+}