1 import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
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 buildDir = getBuildDirectory()
17 
18 defaultTasks = listOf("installDist", "test", "shadowJar", "createArchive", "ktlint")
19 
<lambda>null20 repositories {
21     google()
22     jcenter()
23 }
24 
<lambda>null25 plugins {
26     kotlin("jvm") version "1.3.20"
27     id("application")
28     id("java")
29     id("com.github.johnrengelman.shadow") version "4.0.4"
30     id("maven-publish")
31 }
32 
33 group = "com.android"
34 version = getMetalavaVersion()
35 
<lambda>null36 application {
37     mainClassName = "com.android.tools.metalava.Driver"
38     applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
39 }
40 
<lambda>null41 java {
42     sourceCompatibility = JavaVersion.VERSION_1_8
43     targetCompatibility = JavaVersion.VERSION_1_8
44 }
45 
<lambda>null46 tasks.withType(KotlinCompile::class.java) {
47     sourceCompatibility = "1.8"
48     targetCompatibility = "1.8"
49 
50     kotlinOptions {
51         jvmTarget = "1.8"
52         apiVersion = "1.3"
53         languageVersion = "1.3"
54     }
55 }
56 
57 val studioVersion: String = "26.5.0"
58 val kotlinVersion: String = "1.3.20"
59 
<lambda>null60 dependencies {
61     implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
62     implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion")
63     implementation("com.android.tools.lint:lint-api:$studioVersion")
64     implementation("com.android.tools.lint:lint-checks:$studioVersion")
65     implementation("com.android.tools.lint:lint-gradle:$studioVersion")
66     implementation("com.android.tools.lint:lint:$studioVersion")
67     implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
68     implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
69     testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
70     testImplementation("junit:junit:4.11")
71 }
72 
<lambda>null73 tasks.withType(ShadowJar::class.java) {
74     archiveBaseName.set("metalava-full-${project.version}")
75     archiveClassifier.set(null as String?)
76     archiveVersion.set(null as String?)
77     setZip64(true)
78     destinationDirectory.set(getDistributionDirectory())
79 }
80 
<lambda>null81 tasks.withType(Test::class.java) {
82     val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) {
83         destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
84         archiveFileName.set("metalava-tests.zip")
85     }
86     if (isBuildingOnServer()) ignoreFailures = true
87     finalizedBy(zipTask)
88     doFirst {
89         zipTask.configure {
90             from(reports.junitXml.destination)
91         }
92     }
93 }
94 
getMetalavaVersionnull95 fun getMetalavaVersion(): Any {
96     val versionPropertyFile = File(projectDir, "src/main/resources/version.properties")
97     if (versionPropertyFile.canRead()) {
98         val versionProps = Properties()
99         versionProps.load(FileInputStream(versionPropertyFile))
100         val metalavaVersion = versionProps["metalavaVersion"]
101             ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
102         return if (isBuildingOnServer()) {
103             metalavaVersion
104         } else {
105             // Local builds are not public release candidates.
106             "$metalavaVersion-SNAPSHOT"
107         }
108     } else {
109         throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
110     }
111 }
112 
getBuildDirectorynull113 fun getBuildDirectory(): File {
114     return if (System.getenv("OUT_DIR") != null) {
115         File(System.getenv("OUT_DIR"), "host/common/metalava")
116     } else {
117         File("../../out/host/common")
118     }
119 }
120 
121 /**
122  * The build server will copy the contents of the distribution directory and make it available for
123  * download.
124  */
getDistributionDirectorynull125 fun getDistributionDirectory(): File {
126     return if (System.getenv("DIST_DIR") != null) {
127         File(System.getenv("DIST_DIR"))
128     } else {
129         File("../../out/dist")
130     }
131 }
132 
isBuildingOnServernull133 fun isBuildingOnServer(): Boolean {
134     return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
135 }
136 
137 /**
138  * @return build id string for current build
139  *
140  * The build server does not pass the build id so we infer it from the last folder of the
141  * distribution directory name.
142  */
getBuildIdnull143 fun getBuildId(): String {
144     return if (System.getenv("DIST_DIR") != null) File(System.getenv("DIST_DIR")).name else "0"
145 }
146 
147 // KtLint: https://github.com/shyiko/ktlint
148 
Projectnull149 fun Project.getKtlintConfiguration(): Configuration {
150     return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
151         val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0")
152         dependencies.add(dependency)
153     }
154 }
155 
<lambda>null156 tasks.register("ktlint", JavaExec::class.java) {
157     description = "Check Kotlin code style."
158     group = "Verification"
159     classpath = getKtlintConfiguration()
160     main = "com.pinterest.ktlint.Main"
161     args = listOf("src/**/*.kt", "build.gradle.kts")
162 }
163 
<lambda>null164 tasks.register("ktlintFormat", JavaExec::class.java) {
165     description = "Fix Kotlin code style deviations."
166     group = "formatting"
167     classpath = getKtlintConfiguration()
168     main = "com.pinterest.ktlint.Main"
169     args = listOf("-F", "src/**/*.kt", "build.gradle.kts")
170 }
171 
172 val libraryName = "Metalava"
173 val repositoryName = "Dist"
174 
<lambda>null175 publishing {
176     publications {
177         create<MavenPublication>(libraryName) {
178             from(components["java"])
179             pom {
180                 licenses {
181                     license {
182                         name.set("The Apache License, Version 2.0")
183                         url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
184                     }
185                 }
186                 developers {
187                     developer {
188                         name.set("The Android Open Source Project")
189                     }
190                 }
191                 scm {
192                     connection.set("scm:git:https://android.googlesource.com/platform/tools/metalava")
193                     url.set("https://android.googlesource.com/platform/tools/metalava/")
194                 }
195             }
196         }
197     }
198 
199     repositories {
200         maven {
201             name = repositoryName
202             url = uri("file://${getDistributionDirectory().canonicalPath}/repo/m2repository")
203         }
204     }
205 }
206 
<lambda>null207 tasks.register("createArchive", Zip::class.java) {
208     description = "Create a zip of the library in a maven format"
209     group = "publishing"
210 
211     from("${getDistributionDirectory().canonicalPath}/repo")
212     archiveFileName.set("top-of-tree-m2repository-all-${getBuildId()}.zip")
213     destinationDirectory.set(getDistributionDirectory())
214     dependsOn("publish${libraryName}PublicationTo${repositoryName}Repository")
215 }
216