Skip to content

Commit

Permalink
added project decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Morfly committed Oct 29, 2023
1 parent 0f212bf commit 0edbeb1
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 39 deletions.
1 change: 1 addition & 0 deletions airin-gradle-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
implementation(libs.pendant.starlark)
implementation(libs.pendant.library.bazel)
compileOnly(libs.gradlePlugin.android.api)
compileOnly(libs.gradlePlugin.android)
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.morfly.airin

import java.io.Serializable

data class AndroidMetadata(
val applicationId: String?,
val packageName: String?,
val composeEnabled: Boolean
): Serializable {

companion object {
const val ID = "androidMetadata"
}
}

var GradleProject.androidMetadata: AndroidMetadata?
get() = properties[AndroidMetadata.ID] as? AndroidMetadata
set(value) {
properties[AndroidMetadata.ID] = value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.morfly.airin

import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.CommonExtension
import com.android.build.gradle.BaseExtension
import groovy.xml.XmlSlurper
import groovy.xml.slurpersupport.NodeChild
import org.gradle.api.Project
import java.io.File

private val xmlParser = XmlSlurper()

open class AndroidProjectDecorator : GradleProjectDecorator {

override fun GradleProject.decorate(target: Project) {
with(target.plugins) {
if (!hasPlugin("com.android.application") && !hasPlugin("com.android.library")) {
return
}
}

androidMetadata = AndroidMetadata(
applicationId = target.applicationId,
packageName = target.namespace ?: target.manifestPackageName,
composeEnabled = target.composeEnabled
)
}
}

val Project.namespace: String?
get() = extensions.findByType(CommonExtension::class.java)?.namespace

val Project.composeEnabled: Boolean
get() = extensions.findByType(CommonExtension::class.java)?.buildFeatures?.compose ?: false

val Project.applicationId: String?
get() = extensions.findByType(ApplicationExtension::class.java)?.defaultConfig?.applicationId

val Project.androidManifestFile: File?
get() = extensions.findByType(BaseExtension::class.java)
?.sourceSets
?.map { it.manifest.srcFile }
?.firstOrNull(File::exists)

val Project.manifestPackageName: String?
get() = androidManifestFile
?.let(xmlParser::parse)
?.list()
?.filterIsInstance<NodeChild>()
?.firstOrNull { it.name() == "manifest" }
?.attributes()?.get("package")?.toString()
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.morfly.airin.GradleProject
import io.morfly.airin.module.AndroidLibraryModule
import io.morfly.airin.module.RootModule
import io.morfly.airin.plugin.AirinAndroidGradlePlugin
import io.morfly.airin.plugin.isComposeEnabled
import io.morfly.airin.composeEnabled
import io.morfly.pendant.starlark.KtAndroidLibraryContext
import io.morfly.pendant.starlark.artifact
import io.morfly.pendant.starlark.kt_compiler_plugin
Expand All @@ -21,7 +21,7 @@ abstract class JetpackComposeFeature : GradleFeatureComponent() {
}

override fun canProcess(target: Project): Boolean =
target.isComposeEnabled || target.plugins.hasPlugin(AirinAndroidGradlePlugin.ID)
target.composeEnabled || target.plugins.hasPlugin(AirinAndroidGradlePlugin.ID)

override fun FeatureContext.onInvoke(packageDescriptor: GradleProject) {
onContext<BuildContext>(id = RootModule.ID_THIRD_PARTY_BUILD) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.morfly.airin.GradleProject
import io.morfly.airin.PackageContext
import io.morfly.airin.applyDependenciesFrom
import io.morfly.airin.feature.AndroidLibraryArtifactMappingFeature
import io.morfly.airin.property
import io.morfly.airin.androidMetadata
import io.morfly.pendant.starlark.glob
import io.morfly.pendant.starlark.kt_android_library
import io.morfly.pendant.starlark.lang.context.BUILD
Expand Down Expand Up @@ -35,7 +35,7 @@ abstract class AndroidLibraryModule : GradlePackageComponent() {

name = packageDescriptor.name
srcs = glob("src/main/**/*.kt")
custom_package = "io.morfly.airin.sample"
custom_package = packageDescriptor.androidMetadata?.packageName
manifest = "src/main/AndroidManifest.xml"
resource_files = glob("src/main/res/**")
visibility = list["//visibility:public"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package io.morfly.airin.plugin

import com.android.build.api.dsl.ApplicationExtension
import org.gradle.api.Project
import com.android.build.api.dsl.CommonExtension
import io.morfly.airin.AndroidProjectDecorator

class AirinAndroidGradlePlugin : AirinGradlePlugin() {

override val defaultDecoratorClass = AndroidProjectDecorator::class.java

companion object {
const val ID = "io.morfly.airin.android"
}
}

val Project.isComposeEnabled: Boolean
get() = extensions.findByType(CommonExtension::class.java)?.buildFeatures?.compose ?: false

val Project.applicationId: String?
get() = extensions.findByType(ApplicationExtension::class.java)?.defaultConfig?.applicationId

Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ data class GradleProject(
override val label: GradleLabel,
override val dirPath: String,
override val isRoot: Boolean,
override val ignored: Boolean,
override val packageComponentId: String?,
override val featureComponentIds: Set<String>
) : PackageDescriptor, Serializable {

override val ignored: Boolean
get() = packageComponentId == null

override var packageComponentId: String? = null
internal set

override lateinit var featureComponentIds: Set<String>
internal set

override lateinit var originalDependencies: Map<ConfigurationName, List<Label>>
internal set

override lateinit var dependencies: Map<ConfigurationName, Set<Label>>
internal set

override lateinit var subpackages: List<GradleProject>
internal set

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.morfly.airin

import org.gradle.api.Project

interface GradleProjectDecorator {

fun GradleProject.decorate(target: Project)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.morfly.airin.GradlePackageComponent
import io.morfly.airin.GradleProject
import io.morfly.airin.InternalAirinApi
import io.morfly.airin.MissingComponentResolution
import io.morfly.airin.GradleProjectDecorator
import io.morfly.airin.dsl.AirinExtension
import io.morfly.airin.dsl.AirinProperties
import io.morfly.airin.label.GradleLabel
Expand All @@ -24,6 +25,8 @@ import org.gradle.kotlin.dsl.register

abstract class AirinGradlePlugin : Plugin<Project> {

abstract val defaultDecoratorClass: Class<out GradleProjectDecorator>

override fun apply(target: Project) {
require(target.rootProject.path == target.path) {
"Airin must be applied to the root project but was applied to ${target.path}!"
Expand All @@ -33,11 +36,13 @@ abstract class AirinGradlePlugin : Plugin<Project> {

target.tasks.register<MigrateToBazelTask>(MigrateToBazelTask.NAME) {
val components = prepareComponents(inputs.subcomponents)
val decorator = inputs.objects.newInstance(defaultDecoratorClass)
val outputFiles = mutableListOf<RegularFile>()
val root = prepareProjects(
root = target,
components = components,
properties = inputs,
decorator = decorator,
outputFiles = outputFiles
)

Expand Down Expand Up @@ -76,6 +81,7 @@ abstract class AirinGradlePlugin : Plugin<Project> {
root: Project,
components: Map<ComponentId, GradlePackageComponent>,
properties: AirinProperties,
decorator: GradleProjectDecorator,
outputFiles: MutableList<RegularFile>
): GradleProject {

Expand All @@ -90,14 +96,17 @@ abstract class AirinGradlePlugin : Plugin<Project> {
isRoot = target.rootProject.path == target.path,
label = GradleLabel(path = target.path, name = target.name),
dirPath = target.projectDir.path,
ignored = packageComponent == null,
packageComponentId = packageComponent?.id,
).apply {
packageComponentId = packageComponent?.id
featureComponentIds = featureComponents.map { it.id }.toSet()
)
project.originalDependencies =
if (!project.ignored) prepareDependencies(target, properties)
else emptyMap()
project.subpackages = target.childProjects.values.map(::traverse)
originalDependencies =
if (!ignored) prepareDependencies(target, properties)
else emptyMap()
subpackages = target.childProjects.values.map(::traverse)
}
with(decorator) {
project.decorate(target)
}

if (!project.ignored && packageComponent != null) {
outputFiles += project
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pendant-library-bazel = { module = "io.morfly.pendant:pendant-library-bazel", ve
gradlePlugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
gradlePlugin-mavenPublish = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "mavenPublish" }
gradlePlugin-android-api = { module = "com.android.tools.build:gradle-api", version.ref = "agp" }
gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "agp" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
2 changes: 1 addition & 1 deletion samples/simple-android/feature-A/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@rules_jvm_external//:defs.bzl", "artifact")
kt_android_library(
name = "feature-A",
srcs = glob(["src/main/**/*.kt"]),
custom_package = "io.morfly.airin.sample",
custom_package = "io.morfly.airin.sample.featureA",
manifest = "src/main/AndroidManifest.xml",
resource_files = glob(["src/main/res/**"]),
visibility = ["//visibility:public"],
Expand Down
2 changes: 1 addition & 1 deletion samples/simple-android/feature-B/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@rules_jvm_external//:defs.bzl", "artifact")
kt_android_library(
name = "feature-B",
srcs = glob(["src/main/**/*.kt"]),
custom_package = "io.morfly.airin.sample",
custom_package = "io.morfly.airin.sample.featureB",
manifest = "src/main/AndroidManifest.xml",
resource_files = glob(["src/main/res/**"]),
visibility = ["//visibility:public"],
Expand Down

0 comments on commit 0edbeb1

Please sign in to comment.