Skip to content

Commit

Permalink
Merge pull request #16 from alpaca0984/feat-support-namespace
Browse files Browse the repository at this point in the history
feat: create FQCN with module namespace
  • Loading branch information
gaelmarhic authored Jan 27, 2024
2 parents aaa8cab + 7746ab8 commit 701c010
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 49 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Versions {

// Plugin
const val androidPlugin = "3.5.0"
const val androidPlugin = "7.3.0"
const val kotlinPoet = "1.10.2"

// Testing
Expand Down
17 changes: 9 additions & 8 deletions src/main/kotlin/com/gaelmarhic/quadrant/QuadrantPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ class QuadrantPlugin : Plugin<Project> {
val variants = block(extension)
val mainSourceSet = extension.sourceSet(MAIN_SOURCE_SET)

registerTask(createGenerateActivityClassNameConstantsTask(), variants)
gradle.projectsEvaluated {
registerTask(createGenerateActivityClassNameConstantsTask(), variants)
}
addTargetDirectoryToSourceSet(mainSourceSet)
}

private fun <V : BaseVariant> Project.registerTask(
taskToBeRegistered: Task,
variants: DomainObjectCollection<V>
) {
afterEvaluate {
variants.all { variant ->
tasks.all { task ->
if (task.isCompileOrKspKotlinTask(variant)) {
task.dependsOn(taskToBeRegistered)
}
variants.all { variant ->
tasks.all { task ->
if (task.isCompileOrKspKotlinTask(variant)) {
task.dependsOn(taskToBeRegistered)
}
}
}
Expand Down Expand Up @@ -95,7 +95,8 @@ class QuadrantPlugin : Plugin<Project> {

private fun Project.toRawModule() = RawModule(
name = name,
manifestFiles = manifestFiles
manifestFiles = manifestFiles,
namespace = extensions.findByType(BaseExtension::class.java)?.namespace.orEmpty()
)

private val Project.manifestFiles: List<File>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class ActivityFilteringHelper(
.mapNotNull { it.applyFilter() }

private fun ParsedModule.applyFilter() =
manifestList
.generateFullyQualifiedClassNames()
generateFullyQualifiedClassNames()
.filterClassNames()
.takeIf { it.isNotEmpty() }
?.let { classNames ->
Expand All @@ -34,9 +33,10 @@ class ActivityFilteringHelper(
)
}

private fun List<ParsedManifest>.generateFullyQualifiedClassNames() = map { manifest ->
private fun ParsedModule.generateFullyQualifiedClassNames() = manifestList.map { manifest ->
val activityList = manifest.application.activityList.map { activity ->
val className = createFullyQualifiedClassName(manifest.packageName, activity.className)
val packageName = manifest.packageName.ifEmpty { namespace }
val className = createFullyQualifiedClassName(packageName, activity.className)
Activity(className, activity.metaDataList)
}.toMutableList()
val application = manifest.application.copy(activityList = activityList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class ManifestParsingHelper {

private fun RawModule.parse() = ParsedModule(
name = name,
manifestList = manifestFiles.toManifestList()
manifestList = manifestFiles.toManifestList(),
namespace = namespace,
)

private fun List<File>.toManifestList() = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.gaelmarhic.quadrant.models.manifest.Application

data class ParsedModule(
val name: String,
val manifestList: List<ParsedManifest>
val manifestList: List<ParsedManifest>,
val namespace: String = "",
)

data class ParsedManifest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ import java.io.Serializable

data class RawModule(
val name: String,
val manifestFiles: List<File>
val manifestFiles: List<File>,
// A namespace which is used as the Kotlin or Java package name.
// See https://developer.android.com/build/configure-app-module#set-namespace
val namespace: String = "",
) : Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -193,50 +193,102 @@ internal class ActivityFilteringHelperTest {
assertContainsClassName(filteredModules, activityFqcn)
}

@Test
fun `Then activity defined with PQCN should be mapped to FQCN`() {
@Nested
@DisplayName("When activity has a partially qualified name")
inner class PartiallyQualifiedClassName {

// Given
val manifestPackageName = "com.gaelmarhic.quadrant"
val activityPqcn = ".Activity"
val activityFqcn = manifestPackageName + activityPqcn
@Test
fun `Then activity should be mapped to FQCN with manifest package name`() {

val parsedModule = ParsedModule(
name = "module",
manifestList = listOf(
ParsedManifest(
path = "manifestPath",
application = Application(
activityList = mutableListOf(
Activity(
className = activityPqcn,
metaDataList = mutableListOf(
MetaData(
name = "addressable",
value = "true"
// Given
val manifestPackageName = "com.gaelmarhic.quadrant"
val activityPqcn = ".Activity"
val activityFqcn = manifestPackageName + activityPqcn

val parsedModule = ParsedModule(
name = "module",
manifestList = listOf(
ParsedManifest(
path = "manifestPath",
application = Application(
activityList = mutableListOf(
Activity(
className = activityPqcn,
metaDataList = mutableListOf(
MetaData(
name = "addressable",
value = "true"
)
)
)
),
metaDataList = mutableListOf(
MetaData(
name = "addressable",
value = "true"
)
)
),
metaDataList = mutableListOf(
MetaData(
name = "addressable",
value = "true"
)
)
),
packageName = manifestPackageName
packageName = manifestPackageName
)
)
)
)

val parsedModules = listOf(parsedModule)
val parsedModules = listOf(parsedModule)

// When
val filteredModules = activityFilteringHelper.filter(parsedModules)
// When
val filteredModules = activityFilteringHelper.filter(parsedModules)

// Then
assertContainsClassName(filteredModules, activityFqcn)
// Then
assertContainsClassName(filteredModules, activityFqcn)
}

@Test
fun `Then activity should be mapped to FQCN with module namespace`() {

// Given
val namespace = "com.gaelmarhic.quadrant"
val activityPqcn = ".Activity"
val activityFqcn = namespace + activityPqcn

val parsedModule = ParsedModule(
name = "module",
manifestList = listOf(
ParsedManifest(
path = "manifestPath",
application = Application(
activityList = mutableListOf(
Activity(
className = activityPqcn,
metaDataList = mutableListOf(
MetaData(
name = "addressable",
value = "true"
)
)
)
),
metaDataList = mutableListOf(
MetaData(
name = "addressable",
value = "true"
)
)
),
packageName = "",
)
),
namespace = namespace,
)

val parsedModules = listOf(parsedModule)

// When
val filteredModules = activityFilteringHelper.filter(parsedModules)

// Then
assertContainsClassName(filteredModules, activityFqcn)
}
}
}

Expand Down

0 comments on commit 701c010

Please sign in to comment.