blob: 59ad3111fed4559fc0e13e7250ad200eec64637b [file] [log] [blame]
Aurimas Liutikas467bb9a2019-07-03 16:21:01 -07001import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3import java.io.FileInputStream
4import java.io.FileNotFoundException
5import java.util.Properties
6
7buildscript {
8 repositories {
9 jcenter()
10 }
11 dependencies {
12 classpath("com.github.jengelman.gradle.plugins:shadow:4.0.4")
13 }
14}
15
16buildDir = getBuildDirectory()
17
18defaultTasks = listOf("clean", "installDist")
19
20repositories {
21 google()
22 jcenter()
23}
24
25plugins {
26 kotlin("jvm") version "1.3.20"
27 id("application")
28 id("java")
29 id("com.github.johnrengelman.shadow") version "4.0.4"
30}
31
32group = "com.android"
33version = getMetalavaVersion()
34
35application {
36 mainClassName = "com.android.tools.metalava.Driver"
37 applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g")
38}
39
40java {
41 sourceCompatibility = JavaVersion.VERSION_1_8
42 targetCompatibility = JavaVersion.VERSION_1_8
43}
44
45tasks.withType(KotlinCompile::class.java) {
46 sourceCompatibility = "1.8"
47 targetCompatibility = "1.8"
48
49 kotlinOptions {
50 jvmTarget = "1.8"
51 apiVersion = "1.3"
52 languageVersion = "1.3"
53 }
54}
55
56val studioVersion: String = "26.4.0-beta01"
57val kotlinVersion: String = "1.3.20"
58
59dependencies {
60 implementation("com.android.tools.external.org-jetbrains:uast:$studioVersion")
61 implementation("com.android.tools.external.com-intellij:intellij-core:$studioVersion")
62 implementation("com.android.tools.lint:lint-api:$studioVersion")
63 implementation("com.android.tools.lint:lint-checks:$studioVersion")
64 implementation("com.android.tools.lint:lint-gradle:$studioVersion")
65 implementation("com.android.tools.lint:lint:$studioVersion")
66 implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
67 implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
68 testImplementation("com.android.tools.lint:lint-tests:$studioVersion")
69 testImplementation("junit:junit:4.11")
70}
71
72tasks.withType(ShadowJar::class.java) {
73 archiveBaseName.set("metalava-full-${project.version}")
74 archiveClassifier.set(null as String?)
75 archiveVersion.set(null as String?)
76 setZip64(true)
77 destinationDirectory.set(getDistributionDirectory())
78}
79
80tasks.withType(Test::class.java) {
81 val zipTask = project.tasks.register("zipResultsOf${name.capitalize()}", Zip::class.java) {
82 destinationDirectory.set(File(getDistributionDirectory(), "host-test-reports"))
83 archiveFileName.set("metalava-tests.zip")
84 }
85 if (isBuildingOnServer()) ignoreFailures = true
86 finalizedBy(zipTask)
87 doFirst {
88 zipTask.configure {
89 from(reports.junitXml.destination)
90 }
91 }
92}
93
94fun getMetalavaVersion(): Any {
95 val versionPropertyFile = File("src/main/resources/version.properties")
96 if (versionPropertyFile.canRead()) {
97 val versionProps = Properties()
98 versionProps.load(FileInputStream(versionPropertyFile))
99 val metalavaVersion = versionProps["metalavaVersion"]
100 ?: throw IllegalStateException("metalava version was not set in ${versionPropertyFile.absolutePath}")
101 return if (isBuildingOnServer()) {
102 metalavaVersion
103 } else {
104 // Local builds are not public release candidates.
105 "$metalavaVersion-SNAPSHOT"
106 }
107 } else {
108 throw FileNotFoundException("Could not read ${versionPropertyFile.absolutePath}")
109 }
110}
111
112fun getBuildDirectory(): File {
113 return if (System.getenv("OUT_DIR") != null) {
114 File(System.getenv("OUT_DIR"), "host/common/metalava")
115 } else {
116 File("../../out/host/common")
117 }
118}
119
120/**
121 * The build server will copy the contents of the distribution directory and make it available for
122 * download.
123 */
124fun getDistributionDirectory(): File {
125 return if (System.getenv("DIST_DIR") != null) {
126 File(System.getenv("DIST_DIR"))
127 } else {
128 File("../../out/dist")
129 }
130}
131
132fun isBuildingOnServer(): Boolean {
133 return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null
134}
135
136// KtLint: https://github.com/shyiko/ktlint
137
138fun Project.getKtlintConfiguration(): Configuration {
139 return configurations.findByName("ktlint") ?: configurations.create("ktlint") {
140 val dependency = project.dependencies.create("com.github.shyiko:ktlint:0.31.0")
141 dependencies.add(dependency)
142 }
143}
144
145tasks.register("ktlint", JavaExec::class.java) {
146 description = "Check Kotlin code style."
147 group = "Verification"
148 classpath = getKtlintConfiguration()
149 main = "com.github.shyiko.ktlint.Main"
150 args = listOf("src/**/*.kt")
151}
152
153tasks.register("ktlintFormat", JavaExec::class.java) {
154 description = "Fix Kotlin code style deviations."
155 group = "formatting"
156 classpath = getKtlintConfiguration()
157 main = "com.github.shyiko.ktlint.Main"
158 args = listOf("-F", "src/**/*.kt")
159}