• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 2022 Google LLC
3  * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.google.devtools.ksp
18 
19 import org.gradle.api.Project
20 import org.gradle.api.artifacts.Configuration
21 import org.gradle.api.tasks.JavaExec
22 
23 val API_BASE_FILE="api.base"
24 
25 /**
26  * Adapted from ktlint
27  */
28 fun Project.configureMetalava() {
29     val checkProvider = tasks.register("checkApi", JavaExec::class.java) { task ->
30         task.configureCommonMetalavaArgs(this@configureMetalava)
31         task.description = "Check API compatibility."
32         task.group = "Verification"
33         task.args = listOf("--check-compatibility:api:released", API_BASE_FILE) + task.args!!
34     }
35 
36     afterEvaluate {
37         // check task is not available yet, which is why we use afterEvaluate
38         project.tasks.named("check").configure { checkTask ->
39             checkTask.dependsOn(checkProvider)
40         }
41     }
42 
43     tasks.register("updateApi", JavaExec::class.java) { task ->
44         task.configureCommonMetalavaArgs(this@configureMetalava)
45         task.description = "Update API base file."
46         task.group = "formatting"
47         task.args = listOf("--api", API_BASE_FILE) + task.args!!
48     }
49 }
50 
51 /**
52  * Configures common Metalava parameters
53  */
JavaExecnull54 private fun JavaExec.configureCommonMetalavaArgs(
55     project: Project
56 ) {
57     val jdkHome = org.gradle.internal.jvm.Jvm.current().getJavaHome().absolutePath
58     val compileClasspath = project.getCompileClasspath()
59     val apiFiles = project.fileTree(project.projectDir).also {
60         it.include("**/*.kt")
61         it.include("**/*.java")
62         it.exclude("**/testData/**")
63         it.exclude("**/build/**")
64         it.exclude("**/.*/**")
65     }
66     inputs.files(apiFiles)
67     classpath = project.getMetalavaConfiguration()
68     mainClass.set("com.android.tools.metalava.Driver")
69     args = listOf(
70         "--jdk-home", jdkHome,
71         "--classpath", compileClasspath,
72         "--source-files",
73     ) + apiFiles.files.map { it.absolutePath }
74 }
75 
Projectnull76 private fun Project.getCompileClasspath(): String =
77     configurations.findByName("compileClasspath")!!.files.map { it.absolutePath }.joinToString(":")
78 
Projectnull79 private fun Project.getMetalavaConfiguration(): Configuration {
80     return configurations.findByName("metalava") ?: configurations.create("metalava") {
81         val dependency = dependencies.create("com.android.tools.metalava:metalava:1.0.0-alpha04")
82         it.dependencies.add(dependency)
83     }
84 }
85