Skip to content

Commit

Permalink
Handle Android Architecture components special since their module nam…
Browse files Browse the repository at this point in the history
…e sucks. (#31)
  • Loading branch information
vanniktech authored Mar 17, 2018
1 parent 2539beb commit 8262824
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ internal class DotGenerator(

private fun append(dependency: ResolvedDependency, parentIdentifier: String, content: StringBuilder) {
if (generator.include.invoke(dependency)) {
val name = dependency.moduleName
val identifier = (dependency.moduleGroup + dependency.moduleName).dotIdentifier

val pair = parentIdentifier to identifier
if (!addedConnections.contains(pair)) { // We don't want to re-add the same dependencies.
addedConnections.add(pair)

// Special case since Android Architecture components have weird module names.
val name = if (dependency.moduleGroup.startsWith("android.arch.")) {
dependency.moduleGroup
.replace("android.arch.", "")
.replace('.', '-')
.plus("-")
.plus(dependency.moduleName)
} else {
dependency.moduleName
}

content.append(" $identifier ${generator.dependencyFormattingOptions.invoke(dependency).withLabel(name)};\n")
content.append(" $parentIdentifier -> $identifier;\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DotGeneratorTest {
private lateinit var singleEmpty: Project
private lateinit var singleProject: Project
private lateinit var multiProject: Project
private lateinit var androidProject: DefaultProject
private lateinit var androidProject: DefaultProject // We always need to call evaluate() for Android Projects.
private lateinit var androidProjectExtension: AppExtension

@Before @Suppress("Detekt.LongMethod") fun setUp() {
Expand Down Expand Up @@ -54,7 +54,10 @@ class DotGeneratorTest {

androidProject = ProjectBuilder.builder().withName("android").build() as DefaultProject
androidProject.plugins.apply(AppPlugin::class.java)
androidProject.repositories.run { add(mavenCentral()) }
androidProject.repositories.run {
add(mavenCentral())
add(google())
}

androidProjectExtension = androidProject.extensions.getByType(AppExtension::class.java)
androidProjectExtension.compileSdkVersion(27)
Expand Down Expand Up @@ -222,14 +225,58 @@ class DotGeneratorTest {
|""".trimMargin())
}

@Test fun androidProjectArchitectureComponents() {
androidProject.evaluate()

androidProject.dependencies.add("implementation", "android.arch.persistence.room:runtime:1.0.0")

assertThat(DotGenerator(androidProject, ALL).generateContent()).isEqualTo("""
|digraph G {
| android [label="android", shape="box"];
| androidarchpersistenceroomruntime [label="persistence-room-runtime", shape="box"];
| android -> androidarchpersistenceroomruntime;
| androidarchpersistenceroomcommon [label="persistence-room-common", shape="box"];
| androidarchpersistenceroomruntime -> androidarchpersistenceroomcommon;
| comandroidsupportsupportannotations [label="support-annotations", shape="box"];
| androidarchpersistenceroomcommon -> comandroidsupportsupportannotations;
| androidarchpersistencedbframework [label="persistence-db-framework", shape="box"];
| androidarchpersistenceroomruntime -> androidarchpersistencedbframework;
| androidarchpersistencedb [label="persistence-db", shape="box"];
| androidarchpersistencedbframework -> androidarchpersistencedb;
| comandroidsupportsupportannotations [label="support-annotations", shape="box"];
| androidarchpersistencedb -> comandroidsupportsupportannotations;
| comandroidsupportsupportannotations [label="support-annotations", shape="box"];
| androidarchpersistencedbframework -> comandroidsupportsupportannotations;
| androidarchpersistencedb [label="persistence-db", shape="box"];
| androidarchpersistenceroomruntime -> androidarchpersistencedb;
| androidarchcoreruntime [label="core-runtime", shape="box"];
| androidarchpersistenceroomruntime -> androidarchcoreruntime;
| androidarchcorecommon [label="core-common", shape="box"];
| androidarchcoreruntime -> androidarchcorecommon;
| comandroidsupportsupportannotations [label="support-annotations", shape="box"];
| androidarchcorecommon -> comandroidsupportsupportannotations;
| comandroidsupportsupportannotations [label="support-annotations", shape="box"];
| androidarchcoreruntime -> comandroidsupportsupportannotations;
| comandroidsupportsupportcoreutils [label="support-core-utils", shape="box"];
| androidarchpersistenceroomruntime -> comandroidsupportsupportcoreutils;
| comandroidsupportsupportcompat [label="support-compat", shape="box"];
| comandroidsupportsupportcoreutils -> comandroidsupportsupportcompat;
| comandroidsupportsupportannotations [label="support-annotations", shape="box"];
| comandroidsupportsupportcompat -> comandroidsupportsupportannotations;
| comandroidsupportsupportannotations [label="support-annotations", shape="box"];
| comandroidsupportsupportcoreutils -> comandroidsupportsupportannotations;
|}
|""".trimMargin())
}

@Test fun androidProjectIncludeAllFlavorsByDefault() {
androidProjectExtension.flavorDimensions("test")
androidProjectExtension.productFlavors {
it.create("flavor1").dimension = "test"
it.create("flavor2").dimension = "test"
}

androidProject.evaluate() // Since we're adding custom productFlavors we need this.
androidProject.evaluate()

androidProject.dependencies.add("flavor1Implementation", "io.reactivex.rxjava2:rxandroid:2.0.2")
androidProject.dependencies.add("flavor2DebugImplementation", "io.reactivex.rxjava2:rxjava:2.1.10")
Expand Down Expand Up @@ -259,7 +306,7 @@ class DotGeneratorTest {
it.create("staging")
}

androidProject.evaluate() // Since we're adding a custom buildType we need this.
androidProject.evaluate()

androidProject.dependencies.add("releaseImplementation", "io.reactivex.rxjava2:rxandroid:2.0.2")
androidProject.dependencies.add("debugImplementation", "io.reactivex.rxjava2:rxjava:2.1.10")
Expand Down Expand Up @@ -290,7 +337,7 @@ class DotGeneratorTest {
it.create("staging")
}

androidProject.evaluate() // Since we're adding a custom buildType we need this.
androidProject.evaluate()

androidProject.dependencies.add("releaseImplementation", "io.reactivex.rxjava2:rxandroid:2.0.2")
androidProject.dependencies.add("debugImplementation", "io.reactivex.rxjava2:rxjava:2.1.10")
Expand All @@ -308,6 +355,8 @@ class DotGeneratorTest {
}

@Test fun androidProjectDoNotIncludeTestDependency() {
androidProject.evaluate()

androidProject.dependencies.add("testImplementation", "junit:junit:4.12")

assertThat(DotGenerator(androidProject, ALL).generateContent()).isEqualTo("""
Expand All @@ -318,6 +367,8 @@ class DotGeneratorTest {
}

@Test fun androidProjectDoNotIncludeAndroidTestDependency() {
androidProject.evaluate()

androidProject.dependencies.add("androidTestImplementation", "junit:junit:4.12")

assertThat(DotGenerator(androidProject, ALL).generateContent()).isEqualTo("""
Expand Down

0 comments on commit 8262824

Please sign in to comment.