blob: bb2b4b56137645c72844d1c98da9400717a72af0 [file] [log] [blame]
Aurimas Liutikase2d99092020-08-13 17:26:58 -07001import com.android.tools.metalava.CREATE_ARCHIVE_TASK
2import com.android.tools.metalava.CREATE_BUILD_INFO_TASK
Aurimas Liutikasd507ea72020-08-13 16:23:54 -07003import com.android.tools.metalava.configureBuildInfoTask
Aurimas Liutikase2d99092020-08-13 17:26:58 -07004import com.android.tools.metalava.configurePublishingArchive
Aurimas Liutikas6d6b55b2020-04-29 10:24:34 -07005import org.gradle.api.tasks.testing.logging.TestLogEvent
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -07006import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
7import java.io.FileInputStream
8import java.io.FileNotFoundException
9import java.util.Properties
10
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070011buildDir = getBuildDirectory()
12
Aurimas Liutikase2d99092020-08-13 17:26:58 -070013defaultTasks = mutableListOf(
14 "installDist",
15 "test",
16 CREATE_ARCHIVE_TASK,
17 CREATE_BUILD_INFO_TASK,
18 "ktlint"
19)
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070020
21repositories {
22 google()
Aurimas Liutikasc2325f72021-02-17 16:53:53 -080023 mavenCentral()
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070024 val lintRepo = project.findProperty("lintRepo") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070025 if (lintRepo != null) {
26 logger.warn("Building using custom $lintRepo maven repository")
27 maven {
28 url = uri(lintRepo)
29 }
30 }
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070031}
32
33plugins {
Aurimas Liutikasf50bf052021-09-21 15:58:21 -070034 kotlin("jvm") version "1.5.31"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070035 id("application")
36 id("java")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -070037 id("maven-publish")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070038}
39
Aurimas Liutikasf5a53df2020-08-13 17:32:40 -070040group = "com.android.tools.metalava"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070041version = getMetalavaVersion()
42
43application {
Aurimas Liutikase0739482021-04-08 16:11:15 -070044 mainClass.set("com.android.tools.metalava.Driver")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070045 applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
46}
47
48java {
49 sourceCompatibility = JavaVersion.VERSION_1_8
50 targetCompatibility = JavaVersion.VERSION_1_8
51}
52
53tasks.withType(KotlinCompile::class.java) {
54 sourceCompatibility = "1.8"
55 targetCompatibility = "1.8"
56
57 kotlinOptions {
58 jvmTarget = "1.8"
Aurimas Liutikas10b3b7d2021-05-19 13:01:58 -070059 apiVersion = "1.5"
60 languageVersion = "1.5"
Aurimas Liutikasf3306a22020-05-05 14:46:08 -070061 allWarningsAsErrors = true
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070062 }
63}
64
Aurimas Liutikas6c2e7f82020-05-05 10:24:57 -070065val customLintVersion = findProperty("lintVersion") as String?
Aurimas Liutikas222577c2020-05-01 17:21:57 -070066val studioVersion: String = if (customLintVersion != null) {
67 logger.warn("Building using custom $customLintVersion version of Android Lint")
68 customLintVersion
69} else {
Aurimas Liutikas391fe942021-10-08 14:33:06 -070070 "30.1.0-alpha13"
Aurimas Liutikas222577c2020-05-01 17:21:57 -070071}
Aurimas Liutikasf50bf052021-09-21 15:58:21 -070072val kotlinVersion: String = "1.5.31"
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070073
74dependencies {
75 implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080076 implementation("com.android.tools.external.com-intellij:kotlin-compiler:$studioVersion")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070077 implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion")
78 implementation("com.android.tools.lint:lint-api:$studioVersion")
79 implementation("com.android.tools.lint:lint-checks:$studioVersion")
80 implementation("com.android.tools.lint:lint-gradle:$studioVersion")
81 implementation("com.android.tools.lint:lint:$studioVersion")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080082 implementation("com.android.tools:common:$studioVersion")
83 implementation("com.android.tools:sdk-common:$studioVersion")
84 implementation("com.android.tools:sdklib:$studioVersion")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070085 implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
86 implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080087 implementation("org.ow2.asm:asm:8.0")
88 implementation("org.ow2.asm:asm-tree:8.0")
Aurimas Liutikasf50bf052021-09-21 15:58:21 -070089 implementation("com.google.guava:guava:30.1.1-jre")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070090 testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
Aurimas Liutikas391fe942021-10-08 14:33:06 -070091 testImplementation("junit:junit:4.13.2")
Aurimas Liutikasf50bf052021-09-21 15:58:21 -070092 testImplementation("com.google.truth:truth:1.1.3")
Aurimas Liutikased52e2f2020-11-17 09:36:44 -080093 testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -070094}
95
Aurimas Liutikas21dc23e2021-05-05 17:13:13 -070096val zipTask: TaskProvider<Zip> = project.tasks.register(
97 "zipResultsOf${name.capitalize()}",
98 Zip::class.java
99) {
100 destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
101 archiveFileName.set("metalava-tests.zip")
102}
103
104val testTask = tasks.named("test", Test::class.java)
105testTask.configure {
Aurimas Liutikasea8fd9a2021-05-19 15:58:42 -0700106 maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
Aurimas Liutikas6d6b55b2020-04-29 10:24:34 -0700107 testLogging.events = hashSetOf(
108 TestLogEvent.FAILED,
109 TestLogEvent.PASSED,
110 TestLogEvent.SKIPPED,
111 TestLogEvent.STANDARD_OUT,
112 TestLogEvent.STANDARD_ERROR
113 )
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700114 if (isBuildingOnServer()) ignoreFailures = true
115 finalizedBy(zipTask)
Aurimas Liutikas21dc23e2021-05-05 17:13:13 -0700116}
117zipTask.configure {
118 from(testTask.map { it.reports.junitXml.outputLocation.get() })
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700119}
120
121fun getMetalavaVersion(): Any {
Aurimas Liutikasb5d8fc22019-07-08 10:49:40 -0700122 val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700123 if (versionPropertyFile.canRead()) {
124 val versionProps = Properties()
125 versionProps.load(FileInputStream(versionPropertyFile))
126 val metalavaVersion = versionProps["metalavaVersion"]
127 ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
128 return if (isBuildingOnServer()) {
129 metalavaVersion
130 } else {
131 // Local builds are not public release candidates.
132 "$metalavaVersion-SNAPSHOT"
133 }
134 } else {
135 throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
136 }
137}
138
139fun getBuildDirectory(): File {
140 return if (System.getenv("OUT_DIR") != null) {
141 File(System.getenv("OUT_DIR"), "host/common/metalava")
142 } else {
143 File("../../out/host/common")
144 }
145}
146
147/**
148 * The build server will copy the contents of the distribution directory and make it available for
149 * download.
150 */
151fun getDistributionDirectory(): File {
152 return if (System.getenv("DIST_DIR") != null) {
153 File(System.getenv("DIST_DIR"))
154 } else {
155 File("../../out/dist")
156 }
157}
158
159fun isBuildingOnServer(): Boolean {
160 return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
161}
162
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700163/**
164 * @return build id string for current build
165 *
166 * The build server does not pass the build id so we infer it from the last folder of the
167 * distribution directory name.
168 */
169fun getBuildId(): String {
170 return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
171}
172
Aurimas Liutikas7c6066e2021-07-14 16:01:48 -0700173// KtLint: https://github.com/pinterest/ktlint
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700174
175fun Project.getKtlintConfiguration(): Configuration {
176 return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
Aurimas Liutikas7c6066e2021-07-14 16:01:48 -0700177 val dependency = project.dependencies.create("com.pinterest:ktlint:0.41.0")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700178 dependencies.add(dependency)
179 }
180}
181
182tasks.register("ktlint", JavaExec::class.java) {
183 description = "Check Kotlin code style."
184 group = "Verification"
185 classpath = getKtlintConfiguration()
Aurimas Liutikas7c6066e2021-07-14 16:01:48 -0700186 mainClass.set("com.pinterest.ktlint.Main")
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700187 args = listOf("src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700188}
189
190tasks.register("ktlintFormat", JavaExec::class.java) {
191 description = "Fix Kotlin code style deviations."
192 group = "formatting"
193 classpath = getKtlintConfiguration()
Aurimas Liutikas7c6066e2021-07-14 16:01:48 -0700194 mainClass.set("com.pinterest.ktlint.Main")
Aurimas Liutikas95563a02019-07-03 16:27:38 -0700195 args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -0700196}
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700197
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700198val publicationName = "Metalava"
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700199val repositoryName = "Dist"
200
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700201publishing {
202 publications {
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700203 create<MavenPublication>(publicationName) {
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700204 from(components["java"])
205 pom {
206 licenses {
207 license {
208 name.set("The Apache License, Version 2.0")
209 url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
210 }
211 }
212 developers {
213 developer {
214 name.set("The Android Open Source Project")
215 }
216 }
217 scm {
218 connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava")
219 url.set("https://android.googlesource.com/platform/tools/metalava/")
220 }
221 }
222 }
223 }
224
225 repositories {
226 maven {
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700227 name = repositoryName
228 url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
Aurimas Liutikas40d8f832019-07-26 14:23:58 -0700229 }
230 }
Aurimas Liutikas0b1d7072019-08-07 06:26:24 -0700231}
232
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700233// Workaround for https://github.com/gradle/gradle/issues/11717
234tasks.withType(GenerateModuleMetadata::class.java).configureEach {
Aurimas Liutikas21dc23e2021-05-05 17:13:13 -0700235 val outDirProvider = project.providers.environmentVariable("DIST_DIR")
236 inputs.property("buildOutputDirectory", outDirProvider).optional(true)
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700237 doLast {
238 val metadata = outputFile.asFile.get()
Aurimas Liutikase0739482021-04-08 16:11:15 -0700239 val text = metadata.readText()
Aurimas Liutikas21dc23e2021-05-05 17:13:13 -0700240 val buildId = outDirProvider.orNull?.let { File(it).name } ?: "0"
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700241 metadata.writeText(
242 text.replace(
243 "\"buildId\": .*".toRegex(),
Aurimas Liutikas7c6066e2021-07-14 16:01:48 -0700244 "\"buildId:\": \"${buildId}\""
245 )
Aurimas Liutikas606c4102020-05-14 16:03:11 -0700246 )
247 }
Aurimas Liutikasd507ea72020-08-13 16:23:54 -0700248}
249
Aurimas Liutikas479f42d2021-06-08 15:07:16 -0700250val archiveTaskProvider = configurePublishingArchive(
Aurimas Liutikase2d99092020-08-13 17:26:58 -0700251 project,
252 publicationName,
253 repositoryName,
254 getBuildId(),
255 getDistributionDirectory()
256)
Aurimas Liutikas479f42d2021-06-08 15:07:16 -0700257configureBuildInfoTask(
258 project,
259 isBuildingOnServer(),
260 getDistributionDirectory(),
261 archiveTaskProvider
262)