Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 1 | import com.android.tools.metalava.CREATE_ARCHIVE_TASK |
| 2 | import com.android.tools.metalava.CREATE_BUILD_INFO_TASK |
Aurimas Liutikas | d507ea7 | 2020-08-13 16:23:54 -0700 | [diff] [blame] | 3 | import com.android.tools.metalava.configureBuildInfoTask |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 4 | import com.android.tools.metalava.configurePublishingArchive |
Aurimas Liutikas | 6d6b55b | 2020-04-29 10:24:34 -0700 | [diff] [blame] | 5 | import org.gradle.api.tasks.testing.logging.TestLogEvent |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 6 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile |
| 7 | import java.io.FileInputStream |
| 8 | import java.io.FileNotFoundException |
| 9 | import java.util.Properties |
| 10 | |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 11 | buildDir = getBuildDirectory() |
| 12 | |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 13 | defaultTasks = mutableListOf( |
| 14 | "installDist", |
| 15 | "test", |
| 16 | CREATE_ARCHIVE_TASK, |
| 17 | CREATE_BUILD_INFO_TASK, |
| 18 | "ktlint" |
| 19 | ) |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 20 | |
| 21 | repositories { |
| 22 | google() |
Aurimas Liutikas | c2325f7 | 2021-02-17 16:53:53 -0800 | [diff] [blame] | 23 | mavenCentral() |
Aurimas Liutikas | 6c2e7f8 | 2020-05-05 10:24:57 -0700 | [diff] [blame] | 24 | val lintRepo = project.findProperty("lintRepo") as String? |
Aurimas Liutikas | 222577c | 2020-05-01 17:21:57 -0700 | [diff] [blame] | 25 | if (lintRepo != null) { |
| 26 | logger.warn("Building using custom $lintRepo maven repository") |
| 27 | maven { |
| 28 | url = uri(lintRepo) |
| 29 | } |
| 30 | } |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | plugins { |
Aurimas Liutikas | 983d671 | 2022-02-17 16:36:49 -0800 | [diff] [blame] | 34 | alias(libs.plugins.kotlinJvm) |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 35 | id("application") |
| 36 | id("java") |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 37 | id("maven-publish") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Aurimas Liutikas | f5a53df | 2020-08-13 17:32:40 -0700 | [diff] [blame] | 40 | group = "com.android.tools.metalava" |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 41 | version = getMetalavaVersion() |
| 42 | |
| 43 | application { |
Aurimas Liutikas | e073948 | 2021-04-08 16:11:15 -0700 | [diff] [blame] | 44 | mainClass.set("com.android.tools.metalava.Driver") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 45 | applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g") |
| 46 | } |
| 47 | |
| 48 | java { |
| 49 | sourceCompatibility = JavaVersion.VERSION_1_8 |
| 50 | targetCompatibility = JavaVersion.VERSION_1_8 |
| 51 | } |
| 52 | |
| 53 | tasks.withType(KotlinCompile::class.java) { |
| 54 | sourceCompatibility = "1.8" |
| 55 | targetCompatibility = "1.8" |
| 56 | |
| 57 | kotlinOptions { |
| 58 | jvmTarget = "1.8" |
Aurimas Liutikas | 9b786f3 | 2021-12-16 15:04:14 -0800 | [diff] [blame] | 59 | apiVersion = "1.6" |
| 60 | languageVersion = "1.6" |
Aurimas Liutikas | f3306a2 | 2020-05-05 14:46:08 -0700 | [diff] [blame] | 61 | allWarningsAsErrors = true |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Aurimas Liutikas | 6c2e7f8 | 2020-05-05 10:24:57 -0700 | [diff] [blame] | 65 | val customLintVersion = findProperty("lintVersion") as String? |
Aurimas Liutikas | 222577c | 2020-05-01 17:21:57 -0700 | [diff] [blame] | 66 | val studioVersion: String = if (customLintVersion != null) { |
| 67 | logger.warn("Building using custom $customLintVersion version of Android Lint") |
| 68 | customLintVersion |
| 69 | } else { |
Ember Rose | c792332 | 2022-03-18 17:26:48 -0400 | [diff] [blame] | 70 | "30.3.0-alpha07" |
Aurimas Liutikas | 222577c | 2020-05-01 17:21:57 -0700 | [diff] [blame] | 71 | } |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 72 | |
| 73 | dependencies { |
| 74 | implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion") |
Aurimas Liutikas | ed52e2f | 2020-11-17 09:36:44 -0800 | [diff] [blame] | 75 | implementation("com.android.tools.external.com-intellij:kotlin-compiler:$studioVersion") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 76 | implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion") |
| 77 | implementation("com.android.tools.lint:lint-api:$studioVersion") |
| 78 | implementation("com.android.tools.lint:lint-checks:$studioVersion") |
| 79 | implementation("com.android.tools.lint:lint-gradle:$studioVersion") |
| 80 | implementation("com.android.tools.lint:lint:$studioVersion") |
Aurimas Liutikas | ed52e2f | 2020-11-17 09:36:44 -0800 | [diff] [blame] | 81 | implementation("com.android.tools:common:$studioVersion") |
| 82 | implementation("com.android.tools:sdk-common:$studioVersion") |
| 83 | implementation("com.android.tools:sdklib:$studioVersion") |
Aurimas Liutikas | 983d671 | 2022-02-17 16:36:49 -0800 | [diff] [blame] | 84 | implementation(libs.kotlinStdlib) |
| 85 | implementation(libs.kotlinReflect) |
Aurimas Liutikas | ed52e2f | 2020-11-17 09:36:44 -0800 | [diff] [blame] | 86 | implementation("org.ow2.asm:asm:8.0") |
| 87 | implementation("org.ow2.asm:asm-tree:8.0") |
Aurimas Liutikas | f50bf05 | 2021-09-21 15:58:21 -0700 | [diff] [blame] | 88 | implementation("com.google.guava:guava:30.1.1-jre") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 89 | testImplementation("com.android.tools.lint:lint-tests:$studioVersion") |
Aurimas Liutikas | 391fe94 | 2021-10-08 14:33:06 -0700 | [diff] [blame] | 90 | testImplementation("junit:junit:4.13.2") |
Aurimas Liutikas | f50bf05 | 2021-09-21 15:58:21 -0700 | [diff] [blame] | 91 | testImplementation("com.google.truth:truth:1.1.3") |
Aurimas Liutikas | 983d671 | 2022-02-17 16:36:49 -0800 | [diff] [blame] | 92 | testImplementation(libs.kotlinTest) |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Aurimas Liutikas | 21dc23e | 2021-05-05 17:13:13 -0700 | [diff] [blame] | 95 | val zipTask: TaskProvider<Zip> = project.tasks.register( |
| 96 | "zipResultsOf${name.capitalize()}", |
| 97 | Zip::class.java |
| 98 | ) { |
| 99 | destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports")) |
| 100 | archiveFileName.set("metalava-tests.zip") |
| 101 | } |
| 102 | |
| 103 | val testTask = tasks.named("test", Test::class.java) |
| 104 | testTask.configure { |
Aurimas Liutikas | ea8fd9a | 2021-05-19 15:58:42 -0700 | [diff] [blame] | 105 | maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1 |
Aurimas Liutikas | 6d6b55b | 2020-04-29 10:24:34 -0700 | [diff] [blame] | 106 | testLogging.events = hashSetOf( |
| 107 | TestLogEvent.FAILED, |
| 108 | TestLogEvent.PASSED, |
| 109 | TestLogEvent.SKIPPED, |
| 110 | TestLogEvent.STANDARD_OUT, |
| 111 | TestLogEvent.STANDARD_ERROR |
| 112 | ) |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 113 | if (isBuildingOnServer()) ignoreFailures = true |
| 114 | finalizedBy(zipTask) |
Aurimas Liutikas | 21dc23e | 2021-05-05 17:13:13 -0700 | [diff] [blame] | 115 | } |
| 116 | zipTask.configure { |
| 117 | from(testTask.map { it.reports.junitXml.outputLocation.get() }) |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | fun getMetalavaVersion(): Any { |
Aurimas Liutikas | b5d8fc2 | 2019-07-08 10:49:40 -0700 | [diff] [blame] | 121 | val versionPropertyFile = File(projectDir, "src/main/resources/version.properties") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 122 | if (versionPropertyFile.canRead()) { |
| 123 | val versionProps = Properties() |
| 124 | versionProps.load(FileInputStream(versionPropertyFile)) |
| 125 | val metalavaVersion = versionProps["metalavaVersion"] |
| 126 | ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}") |
| 127 | return if (isBuildingOnServer()) { |
| 128 | metalavaVersion |
| 129 | } else { |
| 130 | // Local builds are not public release candidates. |
| 131 | "$metalavaVersion-SNAPSHOT" |
| 132 | } |
| 133 | } else { |
| 134 | throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}") |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fun getBuildDirectory(): File { |
| 139 | return if (System.getenv("OUT_DIR") != null) { |
Aurimas Liutikas | 983d671 | 2022-02-17 16:36:49 -0800 | [diff] [blame] | 140 | File(System.getenv("OUT_DIR"), "metalava") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 141 | } else { |
Aurimas Liutikas | 983d671 | 2022-02-17 16:36:49 -0800 | [diff] [blame] | 142 | File(projectDir, "../../out/metalava") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * The build server will copy the contents of the distribution directory and make it available for |
| 148 | * download. |
| 149 | */ |
| 150 | fun getDistributionDirectory(): File { |
| 151 | return if (System.getenv("DIST_DIR") != null) { |
| 152 | File(System.getenv("DIST_DIR")) |
| 153 | } else { |
Aurimas Liutikas | 983d671 | 2022-02-17 16:36:49 -0800 | [diff] [blame] | 154 | File(projectDir, "../../out/dist") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | fun isBuildingOnServer(): Boolean { |
| 159 | return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null |
| 160 | } |
| 161 | |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 162 | /** |
| 163 | * @return build id string for current build |
| 164 | * |
| 165 | * The build server does not pass the build id so we infer it from the last folder of the |
| 166 | * distribution directory name. |
| 167 | */ |
| 168 | fun getBuildId(): String { |
| 169 | return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0" |
| 170 | } |
| 171 | |
Aurimas Liutikas | 7c6066e | 2021-07-14 16:01:48 -0700 | [diff] [blame] | 172 | // KtLint: https://github.com/pinterest/ktlint |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 173 | |
| 174 | fun Project.getKtlintConfiguration(): Configuration { |
| 175 | return configurations.findByName("ktlint") ?: configurations.create("ktlint") { |
Aurimas Liutikas | 7c6066e | 2021-07-14 16:01:48 -0700 | [diff] [blame] | 176 | val dependency = project.dependencies.create("com.pinterest:ktlint:0.41.0") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 177 | dependencies.add(dependency) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | tasks.register("ktlint", JavaExec::class.java) { |
| 182 | description = "Check Kotlin code style." |
| 183 | group = "Verification" |
| 184 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 7c6066e | 2021-07-14 16:01:48 -0700 | [diff] [blame] | 185 | mainClass.set("com.pinterest.ktlint.Main") |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 186 | args = listOf("src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | tasks.register("ktlintFormat", JavaExec::class.java) { |
| 190 | description = "Fix Kotlin code style deviations." |
| 191 | group = "formatting" |
| 192 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 7c6066e | 2021-07-14 16:01:48 -0700 | [diff] [blame] | 193 | mainClass.set("com.pinterest.ktlint.Main") |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 194 | args = listOf("-F", "src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 195 | } |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 196 | |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 197 | val publicationName = "Metalava" |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 198 | val repositoryName = "Dist" |
| 199 | |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 200 | publishing { |
| 201 | publications { |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 202 | create<MavenPublication>(publicationName) { |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 203 | from(components["java"]) |
| 204 | pom { |
| 205 | licenses { |
| 206 | license { |
| 207 | name.set("The Apache License, Version 2.0") |
| 208 | url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") |
| 209 | } |
| 210 | } |
| 211 | developers { |
| 212 | developer { |
| 213 | name.set("The Android Open Source Project") |
| 214 | } |
| 215 | } |
| 216 | scm { |
| 217 | connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava") |
| 218 | url.set("https://android.googlesource.com/platform/tools/metalava/") |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | repositories { |
| 225 | maven { |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 226 | name = repositoryName |
| 227 | url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository") |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Aurimas Liutikas | 606c410 | 2020-05-14 16:03:11 -0700 | [diff] [blame] | 232 | // Workaround for https://github.com/gradle/gradle/issues/11717 |
| 233 | tasks.withType(GenerateModuleMetadata::class.java).configureEach { |
Aurimas Liutikas | 21dc23e | 2021-05-05 17:13:13 -0700 | [diff] [blame] | 234 | val outDirProvider = project.providers.environmentVariable("DIST_DIR") |
| 235 | inputs.property("buildOutputDirectory", outDirProvider).optional(true) |
Aurimas Liutikas | 606c410 | 2020-05-14 16:03:11 -0700 | [diff] [blame] | 236 | doLast { |
| 237 | val metadata = outputFile.asFile.get() |
Aurimas Liutikas | e073948 | 2021-04-08 16:11:15 -0700 | [diff] [blame] | 238 | val text = metadata.readText() |
Aurimas Liutikas | 21dc23e | 2021-05-05 17:13:13 -0700 | [diff] [blame] | 239 | val buildId = outDirProvider.orNull?.let { File(it).name } ?: "0" |
Aurimas Liutikas | 606c410 | 2020-05-14 16:03:11 -0700 | [diff] [blame] | 240 | metadata.writeText( |
| 241 | text.replace( |
| 242 | "\"buildId\": .*".toRegex(), |
Aurimas Liutikas | 7c6066e | 2021-07-14 16:01:48 -0700 | [diff] [blame] | 243 | "\"buildId:\": \"${buildId}\"" |
| 244 | ) |
Aurimas Liutikas | 606c410 | 2020-05-14 16:03:11 -0700 | [diff] [blame] | 245 | ) |
| 246 | } |
Aurimas Liutikas | d507ea7 | 2020-08-13 16:23:54 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Aurimas Liutikas | 479f42d | 2021-06-08 15:07:16 -0700 | [diff] [blame] | 249 | val archiveTaskProvider = configurePublishingArchive( |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 250 | project, |
| 251 | publicationName, |
| 252 | repositoryName, |
| 253 | getBuildId(), |
| 254 | getDistributionDirectory() |
| 255 | ) |
Aurimas Liutikas | 479f42d | 2021-06-08 15:07:16 -0700 | [diff] [blame] | 256 | configureBuildInfoTask( |
| 257 | project, |
| 258 | isBuildingOnServer(), |
| 259 | getDistributionDirectory(), |
| 260 | archiveTaskProvider |
| 261 | ) |