Skip to content

Commit

Permalink
Add ability to apply plugin to library module
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski authored and mergify-bot committed Sep 27, 2020
1 parent ae23924 commit 2647fdf
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.project.starter.easylauncher.plugin

import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project

private val supportedPlugins = listOf(
"com.android.application",
"com.android.library",
)

internal fun Project.configureSupportedPlugins(block: (DomainObjectSet<out BaseVariant>) -> Unit) {
supportedPlugins.forEach { pluginId ->
pluginManager.withPlugin(pluginId) { block(findVariants()) }
}
}

internal fun Project.findVariants(): DomainObjectSet<out BaseVariant> =
extensions.findByType(AppExtension::class.java)?.applicationVariants
?: extensions.findByType(LibraryExtension::class.java)?.libraryVariants
?: this.objects.domainObjectSet(BaseVariant::class.java)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.project.starter.easylauncher.plugin

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider
Expand All @@ -14,12 +14,12 @@ class EasyLauncherPlugin : Plugin<Project> {

logger.info("Running gradle version: ${gradle.gradleVersion}")

pluginManager.withPlugin("com.android.application") {
val android = extensions.getByType(AppExtension::class.java)
configureSupportedPlugins { variants ->
val android = extensions.getByType(BaseExtension::class.java)

val easyLauncherTasks = mutableListOf<TaskProvider<EasyLauncherTask>>()

android.applicationVariants.configureEach { variant ->
variants.configureEach { variant ->
val configs = extension.variants.filter { it.name == variant.name }.takeIf { it.isNotEmpty() }
?: findConfigs(variant, extension.productFlavors, extension.buildTypes)

Expand Down Expand Up @@ -75,13 +75,13 @@ class EasyLauncherPlugin : Plugin<Project> {
}

private fun findConfigs(
variant: ApplicationVariant,
variant: BaseVariant,
ribbonProductFlavors: Iterable<EasyLauncherConfig>,
ribbonBuildTypes: Iterable<EasyLauncherConfig>
): List<EasyLauncherConfig> =
ribbonProductFlavors.filter { config -> variant.productFlavors.any { config.name == it.name } } +
ribbonBuildTypes.filter { it.name == variant.buildType.name }

private fun Project.getGeneratedResDir(variant: ApplicationVariant) =
private fun Project.getGeneratedResDir(variant: BaseVariant) =
File(project.buildDir, "generated/easylauncher/res/${variant.name}")
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.project.starter.easylauncher.plugin

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.BaseVariant
import com.project.starter.easylauncher.filter.EasyLauncherFilter
import com.project.starter.easylauncher.plugin.models.AdaptiveIcon
import org.gradle.api.DefaultTask
Expand Down Expand Up @@ -36,8 +36,8 @@ open class EasyLauncherTask : DefaultTask() {
}

val taskExecutionTime = measureTimeMillis {
val android = project.extensions.getByType(AppExtension::class.java)
val variant = android.applicationVariants.find { it.name == variantName.get() }
val android = project.extensions.getByType(BaseExtension::class.java)
val variant = project.findVariants().find { it.name == variantName.get() }
?: throw GradleException("invalid variant name ${variantName.get()}")

val names = (iconsNames.orNull?.takeIf { it.isNotEmpty() } ?: android.getLauncherIconNames(variant)).toSet()
Expand All @@ -59,11 +59,11 @@ open class EasyLauncherTask : DefaultTask() {
logger.info("task finished in $taskExecutionTime ms")
}

private fun ApplicationVariant.getAllSourceSets() =
private fun BaseVariant.getAllSourceSets() =
sourceSets.flatMap { sourceSet -> sourceSet.resDirectories }
.filterNot { resDirectory -> resDirectory == outputDir.asFile.get() }

private fun ApplicationVariant.processIcon(adaptiveIcon: AdaptiveIcon) {
private fun BaseVariant.processIcon(adaptiveIcon: AdaptiveIcon) {
getAllSourceSets().forEach { resDir ->
val icons = resDir.getIconFiles(adaptiveIcon.foreground)
icons.forEach { iconFile ->
Expand All @@ -90,11 +90,11 @@ open class EasyLauncherTask : DefaultTask() {
}
}

private fun AppExtension.getLauncherIconNames(variant: ApplicationVariant) =
private fun BaseExtension.getLauncherIconNames(variant: BaseVariant) =
getAndroidManifestFiles(variant)
.mapNotNull { manifestFile -> manifestFile.getLauncherIcon() }

private fun AppExtension.getAndroidManifestFiles(variant: ApplicationVariant): Iterable<File> {
private fun BaseExtension.getAndroidManifestFiles(variant: BaseVariant): Iterable<File> {
return listOf("main", variant.name, variant.buildType.name, variant.flavorName)
.filter { it.isNotEmpty() }
.distinct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.project.starter.easylauncher.plugin

import com.project.starter.easylauncher.plugin.utils.WithGradleProjectTest
import com.project.starter.easylauncher.plugin.utils.buildScript
import com.project.starter.easylauncher.plugin.utils.libraryBuildscript
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

Expand Down Expand Up @@ -78,4 +79,29 @@ internal class EasyLauncherConfigTest : WithGradleProjectTest() {

runTask("assembleDebug", "--stacktrace")
}

@Test
fun `library config`() {
rootDirectory.resolve("build.gradle").libraryBuildscript(
androidBlock = {
"""
buildTypes {
debug { }
release { }
}
""".trimIndent()
},
easylauncherBlock = {
"""
buildTypes {
debug {
filters = redRibbonFilter()
}
}
""".trimIndent()
}
)

runTask("assembleDebug", "--stacktrace")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ fun File.buildScript(androidBlock: () -> String, easylauncherBlock: () -> String
writeText(buildScript)
}

fun File.libraryBuildscript(androidBlock: () -> String, easylauncherBlock: () -> String = { "" }) {
@Language("groovy")
val buildScript =
"""
plugins {
id 'com.android.library'
id 'com.starter.easylauncher'
}
repositories {
jcenter()
google()
}
android {
defaultConfig {
compileSdkVersion 29
minSdkVersion 23
}
${androidBlock()}
}
easylauncher {
${easylauncherBlock()}
}
dependencies {
testImplementation 'junit:junit:4.13'
}
""".trimIndent()
writeText(buildScript)
}

internal fun vectorFile() =
"""
<vector xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down
53 changes: 53 additions & 0 deletions sample/example-library/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apply plugin: 'com.starter.library.android'
apply plugin: 'com.starter.easylauncher'
apply plugin: 'com.facebook.testing.screenshot'

android {
defaultConfig {
minSdkVersion(26)
testInstrumentationRunner "com.starter.easylauncher.ScreenshotsRunner"
}

testOptions.unitTests.includeAndroidResources true

packagingOptions {
exclude 'META-INF/LICENSE*'
}

libraryVariants.configureEach { variant ->
variant.mergedFlavor.applicationIdSuffix = variant.name
if (variant.flavorName == "") {
variant.mergedFlavor.manifestPlaceholders = [appName: "$project.name"]
} else {
variant.mergedFlavor.manifestPlaceholders = [appName: "$variant.flavorName"]
}
}
}
tasks.register("verifyAll") {
android.libraryVariants.configureEach { variant ->
dependsOn("verify${variant.flavorName.capitalize()}DebugAndroidTestScreenshotTest")
}
}
tasks.register("recordAll") {
android.libraryVariants.configureEach { variant ->
dependsOn("record${variant.flavorName.capitalize()}DebugAndroidTestScreenshotTest")
}
}

tasks.register("installAll") {
android.libraryVariants.configureEach { variant ->
dependsOn("install${variant.name.capitalize()}")
}
}

tasks.withType(Test).configureEach {
useJUnitPlatform()
}
screenshots {
failureDir = "${buildDir}/failedScreenshots"
}

dependencies {
implementation project(":adaptive-support")
androidTestImplementation project(":screenshot-test-helpers")
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.starter.easylauncher.screenshot

import android.Manifest
import androidx.test.rule.GrantPermissionRule
import com.example.custom.adaptive.MainActivity
import com.starter.easylauncher.recordScreenshot
import org.junit.Rule
import org.junit.Test

internal class IconsTest {

@get:Rule
val grantPermission = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)

@Test
fun doScreenshot() {
recordScreenshot<MainActivity>("library")
}
}
12 changes: 12 additions & 0 deletions sample/example-library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simple"
>

<application
android:icon="@mipmap/ic_launcher"
android:label="${appName}"
/>

</manifest>
Loading

0 comments on commit 2647fdf

Please sign in to comment.