1 import org.gradle.api.tasks.testing.logging.TestLogEvent
2 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3 import java.io.FileInputStream
4 import java.io.FileNotFoundException
5 import java.util.Properties
6 
<lambda>null7 buildscript {
8     repositories {
9         jcenter()
10     }
11     dependencies {
12         classpath("com.github.jengelman.gradle.plugins:shadow:4.0.4")
13     }
14 }
15 
16 if (JavaVersion.current() != JavaVersion.VERSION_1_8) {
17     throw GradleException("You are using Java ${JavaVersion.current()}, but this build only supports Java 8. Please set your JAVA_HOME to JDK 8")
18 }
19 
20 buildDir = getBuildDirectory()
21 
22 defaultTasks = mutableListOf("installDist", "test", "createArchive", "ktlint")
23 
<lambda>null24 repositories {
25     google()
26     jcenter()
27     val lintRepo = project.findProperty("lintRepo") as String?
28     if (lintRepo != null) {
29         logger.warn("Building using custom $lintRepo maven repository")
30         maven {
31             url = uri(lintRepo)
32         }
33     }
34 }
35 
<lambda>null36 plugins {
37     kotlin("jvm") version "1.3.72"
38     id("application")
39     id("java")
40     id("maven-publish")
41 }
42 
43 group = "com.android"
44 version = getMetalavaVersion()
45 
<lambda>null46 application {
47     mainClassName = "com.android.tools.metalava.Driver"
48     applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
49 }
50 
<lambda>null51 java {
52     sourceCompatibility = JavaVersion.VERSION_1_8
53     targetCompatibility = JavaVersion.VERSION_1_8
54 }
55 
<lambda>null56 tasks.withType(KotlinCompile::class.java) {
57     sourceCompatibility = "1.8"
58     targetCompatibility = "1.8"
59 
60     kotlinOptions {
61         jvmTarget = "1.8"
62         apiVersion = "1.3"
63         languageVersion = "1.3"
64         allWarningsAsErrors = true
65     }
66 }
67 
68 val customLintVersion = findProperty("lintVersion") as String?
69 val studioVersion: String = if (customLintVersion != null) {
70     logger.warn("Building using custom $customLintVersion version of Android Lint")
71     customLintVersion
72 } else {
73     "27.2.0-alpha01"
74 }
75 val kotlinVersion: String = "1.3.72"
76 
<lambda>null77 dependencies {
78     implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
79     implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion")
80     implementation("com.android.tools.lint:lint-api:$studioVersion")
81     implementation("com.android.tools.lint:lint-checks:$studioVersion")
82     implementation("com.android.tools.lint:lint-gradle:$studioVersion")
83     implementation("com.android.tools.lint:lint:$studioVersion")
84     implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
85     implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
86     testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
87     testImplementation("junit:junit:4.11")
88 }
89 
<lambda>null90 tasks.withType(Test::class.java) {
91     testLogging.events = hashSetOf(
92         TestLogEvent.FAILED,
93         TestLogEvent.PASSED,
94         TestLogEvent.SKIPPED,
95         TestLogEvent.STANDARD_OUT,
96         TestLogEvent.STANDARD_ERROR
97     )
98     val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) {
99         destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
100         archiveFileName.set("metalava-tests.zip")
101     }
102     if (isBuildingOnServer()) ignoreFailures = true
103     finalizedBy(zipTask)
104     doFirst {
105         zipTask.configure {
106             from(reports.junitXml.destination)
107         }
108     }
109 }
110 
getMetalavaVersionnull111 fun getMetalavaVersion(): Any {
112     val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
113     if (versionPropertyFile.canRead()) {
114         val versionProps = Properties()
115         versionProps.load(FileInputStream(versionPropertyFile))
116         val metalavaVersion = versionProps["metalavaVersion"]
117             ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
118         return if (isBuildingOnServer()) {
119             metalavaVersion
120         } else {
121             // Local builds are not public release candidates.
122             "$metalavaVersion-SNAPSHOT"
123         }
124     } else {
125         throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
126     }
127 }
128 
getBuildDirectorynull129 fun getBuildDirectory(): File {
130     return if (System.getenv("OUT_DIR") != null) {
131         File(System.getenv("OUT_DIR"), "host/common/metalava")
132     } else {
133         File("../../out/host/common")
134     }
135 }
136 
137 /**
138  * The build server will copy the contents of the distribution directory and make it available for
139  * download.
140  */
getDistributionDirectorynull141 fun getDistributionDirectory(): File {
142     return if (System.getenv("DIST_DIR") != null) {
143         File(System.getenv("DIST_DIR"))
144     } else {
145         File("../../out/dist")
146     }
147 }
148 
isBuildingOnServernull149 fun isBuildingOnServer(): Boolean {
150     return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
151 }
152 
153 /**
154  * @return build id string for current build
155  *
156  * The build server does not pass the build id so we infer it from the last folder of the
157  * distribution directory name.
158  */
getBuildIdnull159 fun getBuildId(): String {
160     return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
161 }
162 
163 // KtLint: https://github.com/shyiko/ktlint
164 
Projectnull165 fun Project.getKtlintConfiguration(): Configuration {
166     return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
167         val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0")
168         dependencies.add(dependency)
169     }
170 }
171 
<lambda>null172 tasks.register("ktlint", JavaExec::class.java) {
173     description = "Check Kotlin code style."
174     group = "Verification"
175     classpath = getKtlintConfiguration()
176     main = "com.pinterest.ktlint.Main"
177     args = listOf("src/**/*.kt", "build.gradle.kts")
178 }
179 
<lambda>null180 tasks.register("ktlintFormat", JavaExec::class.java) {
181     description = "Fix Kotlin code style deviations."
182     group = "formatting"
183     classpath = getKtlintConfiguration()
184     main = "com.pinterest.ktlint.Main"
185     args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
186 }
187 
188 val libraryName = "Metalava"
189 val repositoryName = "Dist"
190 
<lambda>null191 publishing {
192     publications {
193         create<MavenPublication>(libraryName) {
194             from(components["java"])
195             pom {
196                 licenses {
197                     license {
198                         name.set("The Apache License, Version 2.0")
199                         url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
200                     }
201                 }
202                 developers {
203                     developer {
204                         name.set("The Android Open Source Project")
205                     }
206                 }
207                 scm {
208                     connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava")
209                     url.set("https://android.googlesource.com/platform/tools/metalava/")
210                 }
211             }
212         }
213     }
214 
215     repositories {
216         maven {
217             name = repositoryName
218             url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
219         }
220     }
221 }
222 
<lambda>null223 tasks.register("createArchive", Zip::class.java) {
224     description = "Create a zip of the library in a maven format"
225     group = "publishing"
226 
227     from("${getDistributionDirectory().canonicalPath}/repo")
228     archiveFileName.set("top-of-tree-m2repository-all-${getBuildId()}.zip")
229     destinationDirectory.set(getDistributionDirectory())
230     dependsOn("publish${libraryName}PublicationTo${repositoryName}Repository")
231 }
232 
233 // Workaround for https://github.com/gradle/gradle/issues/11717
234 tasks.withType(GenerateModuleMetadata::class.java).configureEach {
<lambda>null235     doLast {
236         val metadata = outputFile.asFile.get()
237         var text = metadata.readText()
238         metadata.writeText(
239             text.replace(
240                 "\"buildId\": .*".toRegex(),
241                 "\"buildId:\": \"${getBuildId()}\"")
242         )
243     }
244 }