Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 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 | |
| 7 | 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("clean", "installDist") |
| 19 | |
| 20 | repositories { |
| 21 | google() |
| 22 | jcenter() |
| 23 | } |
| 24 | |
| 25 | 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 | } |
| 31 | |
| 32 | group = "com.android" |
| 33 | version = getMetalavaVersion() |
| 34 | |
| 35 | application { |
| 36 | mainClassName = "com.android.tools.metalava.Driver" |
| 37 | applicationDefaultJvmArgs = listOf("-ea", "-Xms2g", "-Xmx4g") |
| 38 | } |
| 39 | |
| 40 | java { |
| 41 | sourceCompatibility = JavaVersion.VERSION_1_8 |
| 42 | targetCompatibility = JavaVersion.VERSION_1_8 |
| 43 | } |
| 44 | |
| 45 | tasks.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 | |
| 56 | val studioVersion: String = "26.4.0-beta01" |
| 57 | val kotlinVersion: String = "1.3.20" |
| 58 | |
| 59 | dependencies { |
| 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 | |
| 72 | tasks.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 | |
| 80 | tasks.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 | |
| 94 | fun getMetalavaVersion(): Any { |
Aurimas Liutikas | b5d8fc2 | 2019-07-08 10:49:40 -0700 | [diff] [blame^] | 95 | val versionPropertyFile = File(projectDir, "src/main/resources/version.properties") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 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 | |
| 112 | fun 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 | */ |
| 124 | fun 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 | |
| 132 | fun isBuildingOnServer(): Boolean { |
| 133 | return System.getenv("OUT_DIR") != null && System.getenv("DIST_DIR") != null |
| 134 | } |
| 135 | |
| 136 | // KtLint: https://github.com/shyiko/ktlint |
| 137 | |
| 138 | fun Project.getKtlintConfiguration(): Configuration { |
| 139 | return configurations.findByName("ktlint") ?: configurations.create("ktlint") { |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 140 | val dependency = project.dependencies.create("com.pinterest:ktlint:0.33.0") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 141 | dependencies.add(dependency) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | tasks.register("ktlint", JavaExec::class.java) { |
| 146 | description = "Check Kotlin code style." |
| 147 | group = "Verification" |
| 148 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 149 | main = "com.pinterest.ktlint.Main" |
| 150 | args = listOf("src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | tasks.register("ktlintFormat", JavaExec::class.java) { |
| 154 | description = "Fix Kotlin code style deviations." |
| 155 | group = "formatting" |
| 156 | classpath = getKtlintConfiguration() |
Aurimas Liutikas | 95563a0 | 2019-07-03 16:27:38 -0700 | [diff] [blame] | 157 | main = "com.pinterest.ktlint.Main" |
| 158 | args = listOf("-F", "src/**/*.kt", "build.gradle.kts") |
Aurimas Liutikas | 467bb9a | 2019-07-03 16:21:01 -0700 | [diff] [blame] | 159 | } |