Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 1 | /* |
Aurimas Liutikas | 526389b | 2018-02-27 14:01:24 -0800 | [diff] [blame] | 2 | * Copyright 2018 The Android Open Source Project |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Aurimas Liutikas | 526389b | 2018-02-27 14:01:24 -0800 | [diff] [blame] | 17 | package androidx.build |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 18 | |
| 19 | import groovy.lang.Closure |
Jeff Gaston | ce468a9 | 2020-01-08 12:51:16 -0500 | [diff] [blame] | 20 | import org.gradle.api.GradleException |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 21 | import org.gradle.api.Project |
| 22 | import java.util.ArrayList |
| 23 | |
| 24 | /** |
Aurimas Liutikas | 2cc275f | 2019-04-04 19:33:53 +0100 | [diff] [blame] | 25 | * Extension for [AndroidXPlugin]. |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 26 | */ |
Aurimas Liutikas | b63ef63 | 2019-04-01 04:37:49 -0700 | [diff] [blame] | 27 | open class AndroidXExtension(val project: Project) { |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 28 | var name: String? = null |
Aurimas Liutikas | 75e93a0 | 2019-05-28 16:31:38 -0700 | [diff] [blame] | 29 | var mavenVersion: Version? = null |
| 30 | set(value) { |
Jeff Gaston | ce468a9 | 2020-01-08 12:51:16 -0500 | [diff] [blame] | 31 | field = value |
| 32 | chooseProjectVersion() |
Aurimas Liutikas | 75e93a0 | 2019-05-28 16:31:38 -0700 | [diff] [blame] | 33 | } |
Oussama Ben Abdelbaki | 34f21e0 | 2019-02-27 13:38:27 -0500 | [diff] [blame] | 34 | var mavenGroup: LibraryGroup? = null |
Jeff Gaston | ce468a9 | 2020-01-08 12:51:16 -0500 | [diff] [blame] | 35 | set(value) { |
| 36 | field = value |
| 37 | chooseProjectVersion() |
| 38 | } |
Alan Viverette | 30a7564 | 2020-02-07 13:09:18 -0500 | [diff] [blame] | 39 | |
Jeff Gaston | ce468a9 | 2020-01-08 12:51:16 -0500 | [diff] [blame] | 40 | private fun chooseProjectVersion() { |
| 41 | val version: Version |
| 42 | val groupVersion: Version? = mavenGroup?.forcedVersion |
| 43 | val mavenVersion: Version? = mavenVersion |
| 44 | if (mavenVersion != null) { |
Alan Viverette | 30a7564 | 2020-02-07 13:09:18 -0500 | [diff] [blame] | 45 | if (groupVersion != null && !isGroupVersionOverrideAllowed()) { |
Jeff Gaston | ce468a9 | 2020-01-08 12:51:16 -0500 | [diff] [blame] | 46 | throw GradleException("Cannot set mavenVersion (" + mavenVersion + |
| 47 | ") for a project (" + project + |
| 48 | ") whose mavenGroup already specifies forcedVersion (" + groupVersion + |
| 49 | ")") |
| 50 | } else { |
| 51 | version = mavenVersion |
| 52 | } |
| 53 | } else { |
| 54 | if (groupVersion != null) { |
| 55 | version = groupVersion |
| 56 | } else { |
| 57 | return |
| 58 | } |
| 59 | } |
| 60 | project.version = if (isSnapshotBuild()) version.copy(extra = "-SNAPSHOT") else version |
| 61 | versionIsSet = true |
| 62 | } |
Alan Viverette | 30a7564 | 2020-02-07 13:09:18 -0500 | [diff] [blame] | 63 | |
| 64 | private fun isGroupVersionOverrideAllowed(): Boolean { |
| 65 | // Grant an exception to the same-version-group policy for artifacts that haven't shipped a |
| 66 | // stable API surface, e.g. 1.0.0-alphaXX, to allow for rapid early-stage development. |
| 67 | val version = mavenVersion |
| 68 | return version != null && version.major == 1 && version.minor == 0 && version.patch == 0 && |
| 69 | version.isAlpha() |
| 70 | } |
| 71 | |
Jeff Gaston | ce468a9 | 2020-01-08 12:51:16 -0500 | [diff] [blame] | 72 | private var versionIsSet = false |
| 73 | fun isVersionSet(): Boolean { |
| 74 | return versionIsSet |
| 75 | } |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 76 | var description: String? = null |
| 77 | var inceptionYear: String? = null |
| 78 | var url = SUPPORT_URL |
| 79 | private var licenses: MutableCollection<License> = ArrayList() |
Aurimas Liutikas | 75e93a0 | 2019-05-28 16:31:38 -0700 | [diff] [blame] | 80 | var publish: Publish = Publish.NONE |
Aurimas Liutikas | bfef093 | 2018-04-04 13:25:38 -0700 | [diff] [blame] | 81 | var failOnDeprecationWarnings = true |
| 82 | |
Jake Wharton | 792bd53 | 2018-09-13 16:49:07 -0400 | [diff] [blame] | 83 | var compilationTarget: CompilationTarget = CompilationTarget.DEVICE |
| 84 | |
Sergey Vasilinets | 9c24688 | 2017-12-11 18:31:29 -0800 | [diff] [blame] | 85 | /** |
Sergey Vasilinets | e8d12f2 | 2018-07-12 16:32:26 -0700 | [diff] [blame] | 86 | * It disables docs generation and api tracking for tooling modules like annotation processors. |
| 87 | * We don't expect such modules to be used by developers as libraries, so we don't guarantee |
| 88 | * any api stability and don't expose any docs about them. |
Sergey Vasilinets | 9c24688 | 2017-12-11 18:31:29 -0800 | [diff] [blame] | 89 | */ |
Sergey Vasilinets | e8d12f2 | 2018-07-12 16:32:26 -0700 | [diff] [blame] | 90 | var toolingProject = false |
| 91 | |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 92 | fun license(closure: Closure<*>): License { |
| 93 | val license = project.configure(License(), closure) as License |
| 94 | licenses.add(license) |
| 95 | return license |
| 96 | } |
| 97 | |
| 98 | fun getLicenses(): Collection<License> { |
| 99 | return licenses |
| 100 | } |
| 101 | |
| 102 | companion object { |
| 103 | @JvmField |
Sergey Vasilinets | e8d12f2 | 2018-07-12 16:32:26 -0700 | [diff] [blame] | 104 | val ARCHITECTURE_URL = |
| 105 | "https://developer.android.com/topic/libraries/architecture/index.html" |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 106 | @JvmField |
Jake Wharton | 5c15cfa | 2019-06-06 10:27:43 -0400 | [diff] [blame] | 107 | val SUPPORT_URL = "https://developer.android.com/jetpack/androidx" |
Oussama Ben Abdelbaki | 34f21e0 | 2019-02-27 13:38:27 -0500 | [diff] [blame] | 108 | val DEFAULT_UNSPECIFIED_VERSION = "unspecified" |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Jake Wharton | 792bd53 | 2018-09-13 16:49:07 -0400 | [diff] [blame] | 112 | enum class CompilationTarget { |
| 113 | /** This library is meant to run on the host machine (like an annotation processor). */ |
| 114 | HOST, |
| 115 | /** This library is meant to run on an Android device. */ |
| 116 | DEVICE |
| 117 | } |
| 118 | |
Nick Anthony | eece3f4 | 2019-11-08 10:20:40 -0500 | [diff] [blame] | 119 | /** |
| 120 | * Publish Enum: |
| 121 | * Publish.NONE -> Generates no aritfacts; does not generate snapshot artifacts |
| 122 | * or releasable maven artifacts |
| 123 | * Publish.SNAPSHOT_ONLY -> Only generates snapshot artifacts |
| 124 | * Publish.SNAPSHOT_AND_RELEASE -> Generates both snapshot artifacts and releasable maven artifact |
| 125 | */ |
Aurimas Liutikas | 75e93a0 | 2019-05-28 16:31:38 -0700 | [diff] [blame] | 126 | enum class Publish { |
| 127 | NONE, SNAPSHOT_ONLY, SNAPSHOT_AND_RELEASE; |
| 128 | |
| 129 | fun shouldRelease() = this == SNAPSHOT_AND_RELEASE |
| 130 | fun shouldPublish() = this == SNAPSHOT_ONLY || this == SNAPSHOT_AND_RELEASE |
| 131 | } |
| 132 | |
Aurimas Liutikas | 92d9756 | 2017-11-13 13:54:27 -0800 | [diff] [blame] | 133 | class License { |
| 134 | var name: String? = null |
| 135 | var url: String? = null |
Jake Wharton | 585b8f9 | 2018-07-09 13:34:52 -0400 | [diff] [blame] | 136 | } |