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