Skip to content

Instantly share code, notes, and snippets.

@popovanton0
Last active November 11, 2022 09:25
Show Gist options
  • Save popovanton0/f73631c6a819c630d543c10f8e42fff3 to your computer and use it in GitHub Desktop.
Save popovanton0/f73631c6a819c630d543c10f8e42fff3 to your computer and use it in GitHub Desktop.
/**
* MIT Licence
*
* Copyright 2022 Anton Popov
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Explanation: https://popovanton0.medium.com/65fd86abe4e1
*/
@CacheableRule
class ShowkaseRule : ComponentMetadataRule {
override fun execute(context: ComponentMetadataContext) {
requireSupportedVersion(context)
missingBuildTypes.forEach { missingBuildType ->
variantTemplates.forEach { variantTemplate ->
val newVariant = "${missingBuildType.name}$variantTemplate"
val baseVariant = "${missingBuildType.baseName}$variantTemplate"
// creating a new variant that copies `baseVariant` but has `newVariant` name
context.details.addVariant(newVariant, baseVariant) {
attributes {
val buildTypeAttr = keySet().first { it.name == buildTypeAttrName } as Attribute<String>
// ading an attribute
attribute(buildTypeAttr, missingBuildType.name)
}
}
}
}
}
private fun requireSupportedVersion(context: ComponentMetadataContext) {
require(context.details.id.toString() == "com.airbnb.android:showkase:1.0.0-beta14") {
"This rule works only for one particular version of the showkase library. You need to test manually, " +
"whether this rule works with the other versions"
}
}
private data class BuildType(val name: String, val baseName: String)
private companion object {
const val buildTypeAttrName = "com.android.build.api.attributes.BuildTypeAttr"
val missingBuildTypes = listOf(
BuildType(name = "alpha", baseName = "debug"),
BuildType(name = "beta", baseName = "release"),
)
val variantTemplates = listOf(
"VariantMavenApiPublication",
"VariantMavenRuntimePublication",
"VariantMavenSourcePublication",
"VariantMavenJavaDocPublication",
)
}
}
dependencyResolutionManagement {
components {
withModule<ShowkaseRule>("com.airbnb.android:showkase")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment