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 | c2325f7 | 2021-02-17 16:53:53 -0800 | [diff] [blame] | 34 | kotlin("jvm") version "1.4.30" |
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 | 28dd044 | 2020-08-18 16:44:00 -0700 | [diff] [blame] | 59 | apiVersion = "1.4" |
| 60 | languageVersion = "1.4" |
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 { |
Aurimas Liutikas | 5dc56d3 | 2021-03-29 16:42:34 -0700 | [diff] [blame] | 70 | "30.0.0-alpha14" |
Aurimas Liutikas | 222577c | 2020-05-01 17:21:57 -0700 | [diff] [blame] | 71 | } |
Aurimas Liutikas | c2325f7 | 2021-02-17 16:53:53 -0800 | [diff] [blame] | 72 | val kotlinVersion: String = "1.4.30" |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 73 | |
| 74 | dependencies { |
| 75 | implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion") |
Aurimas Liutikas | ed52e2f | 2020-11-17 09:36:44 -0800 | [diff] [blame] | 76 | implementation("com.android.tools.external.com-intellij:kotlin-compiler:$studioVersion") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 77 | 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 Liutikas | ed52e2f | 2020-11-17 09:36:44 -0800 | [diff] [blame] | 82 | implementation("com.android.tools:common:$studioVersion") |
| 83 | implementation("com.android.tools:sdk-common:$studioVersion") |
| 84 | implementation("com.android.tools:sdklib:$studioVersion") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 85 | implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion") |
| 86 | implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion") |
Aurimas Liutikas | ed52e2f | 2020-11-17 09:36:44 -0800 | [diff] [blame] | 87 | implementation("org.ow2.asm:asm:8.0") |
| 88 | implementation("org.ow2.asm:asm-tree:8.0") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 89 | testImplementation("com.android.tools.lint:lint-tests:$studioVersion") |
| 90 | testImplementation("junit:junit:4.11") |
Aurimas Liutikas | ed52e2f | 2020-11-17 09:36:44 -0800 | [diff] [blame] | 91 | testImplementation("com.google.truth:truth:1.0") |
| 92 | testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 95 | tasks.withType(Test::class.java) { |
Aurimas Liutikas | 6d6b55b | 2020-04-29 10:24:34 -0700 | [diff] [blame] | 96 | testLogging.events = hashSetOf( |
| 97 | TestLogEvent.FAILED, |
| 98 | TestLogEvent.PASSED, |
| 99 | TestLogEvent.SKIPPED, |
| 100 | TestLogEvent.STANDARD_OUT, |
| 101 | TestLogEvent.STANDARD_ERROR |
| 102 | ) |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 103 | val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) { |
| 104 | destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports")) |
| 105 | archiveFileName.set("metalava-tests.zip") |
| 106 | } |
| 107 | if (isBuildingOnServer()) ignoreFailures = true |
| 108 | finalizedBy(zipTask) |
| 109 | doFirst { |
| 110 | zipTask.configure { |
| 111 | from(reports.junitXml.destination) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fun getMetalavaVersion(): Any { |
Aurimas Liutikas | b5d8fc2 | 2019-07-08 10:49:40 -0700 | [diff] [blame] | 117 | val versionPropertyFile = File(projectDir, "src/main/resources/version.properties") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 118 | if (versionPropertyFile.canRead()) { |
| 119 | val versionProps = Properties() |
| 120 | versionProps.load(FileInputStream(versionPropertyFile)) |
| 121 | val metalavaVersion = versionProps["metalavaVersion"] |
| 122 | ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}") |
| 123 | return if (isBuildingOnServer()) { |
| 124 | metalavaVersion |
| 125 | } else { |
| 126 | // Local builds are not public release candidates. |
| 127 | "$metalavaVersion-SNAPSHOT" |
| 128 | } |
| 129 | } else { |
| 130 | throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}") |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fun getBuildDirectory(): File { |
| 135 | return if (System.getenv("OUT_DIR") != null) { |
| 136 | File(System.getenv("OUT_DIR"), "host/common/metalava") |
| 137 | } else { |
| 138 | File("../../out/host/common") |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * The build server will copy the contents of the distribution directory and make it available for |
| 144 | * download. |
| 145 | */ |
| 146 | fun getDistributionDirectory(): File { |
| 147 | return if (System.getenv("DIST_DIR") != null) { |
| 148 | File(System.getenv("DIST_DIR")) |
| 149 | } else { |
| 150 | File("../../out/dist") |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | fun isBuildingOnServer(): Boolean { |
| 155 | return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null |
| 156 | } |
| 157 | |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 158 | /** |
| 159 | * @return build id string for current build |
| 160 | * |
| 161 | * The build server does not pass the build id so we infer it from the last folder of the |
| 162 | * distribution directory name. |
| 163 | */ |
| 164 | fun getBuildId(): String { |
| 165 | return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0" |
| 166 | } |
| 167 | |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 168 | // KtLint: https://github.com/shyiko/ktlint |
| 169 | |
| 170 | fun Project.getKtlintConfiguration(): Configuration { |
| 171 | return configurations.findByName("ktlint") ?: configurations.create("ktlint") { |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 172 | val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 173 | dependencies.add(dependency) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | tasks.register("ktlint", JavaExec::class.java) { |
| 178 | description = "Check Kotlin code style." |
| 179 | group = "Verification" |
| 180 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 181 | main = "com.pinterest.ktlint.Main" |
| 182 | args = listOf("src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | tasks.register("ktlintFormat", JavaExec::class.java) { |
| 186 | description = "Fix Kotlin code style deviations." |
| 187 | group = "formatting" |
| 188 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 189 | main = "com.pinterest.ktlint.Main" |
| 190 | args = listOf("-F", "src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 191 | } |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 192 | |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 193 | val publicationName = "Metalava" |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 194 | val repositoryName = "Dist" |
| 195 | |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 196 | publishing { |
| 197 | publications { |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 198 | create<MavenPublication>(publicationName) { |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 199 | from(components["java"]) |
| 200 | pom { |
| 201 | licenses { |
| 202 | license { |
| 203 | name.set("The Apache License, Version 2.0") |
| 204 | url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") |
| 205 | } |
| 206 | } |
| 207 | developers { |
| 208 | developer { |
| 209 | name.set("The Android Open Source Project") |
| 210 | } |
| 211 | } |
| 212 | scm { |
| 213 | connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava") |
| 214 | url.set("https://android.googlesource.com/platform/tools/metalava/") |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | repositories { |
| 221 | maven { |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 222 | name = repositoryName |
| 223 | url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository") |
Aurimas Liutikas | 40d8f83 | 2019-07-26 14:23:58 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
Aurimas Liutikas | 0b1d707 | 2019-08-07 06:26:24 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Aurimas Liutikas | 606c410 | 2020-05-14 16:03:11 -0700 | [diff] [blame] | 228 | // Workaround for https://github.com/gradle/gradle/issues/11717 |
| 229 | tasks.withType(GenerateModuleMetadata::class.java).configureEach { |
| 230 | doLast { |
| 231 | val metadata = outputFile.asFile.get() |
Aurimas Liutikas | e073948 | 2021-04-08 16:11:15 -0700 | [diff] [blame^] | 232 | val text = metadata.readText() |
Aurimas Liutikas | 606c410 | 2020-05-14 16:03:11 -0700 | [diff] [blame] | 233 | metadata.writeText( |
| 234 | text.replace( |
| 235 | "\"buildId\": .*".toRegex(), |
| 236 | "\"buildId:\": \"${getBuildId()}\"") |
| 237 | ) |
| 238 | } |
Aurimas Liutikas | d507ea7 | 2020-08-13 16:23:54 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Aurimas Liutikas | a8d4d7f | 2020-08-13 17:57:19 -0700 | [diff] [blame] | 241 | configureBuildInfoTask(project, isBuildingOnServer(), getDistributionDirectory()) |
Aurimas Liutikas | e2d9909 | 2020-08-13 17:26:58 -0700 | [diff] [blame] | 242 | configurePublishingArchive( |
| 243 | project, |
| 244 | publicationName, |
| 245 | repositoryName, |
| 246 | getBuildId(), |
| 247 | getDistributionDirectory() |
| 248 | ) |