Skip to content

Commit 9a392dd

Browse files
authored
Merge pull request #412 from square/ralf/sync-sources
Add an option in the Anvil DSL to sync generated sources in the IDE.
2 parents b272634 + f339c45 commit 9a392dd

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

gradle-plugin/src/main/java/com/squareup/anvil/plugin/AnvilExtension.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public abstract class AnvilExtension @Inject constructor(objects: ObjectFactory)
4444
public val disableComponentMerging: Property<Boolean> = objects.property(Boolean::class.java)
4545
.convention(false)
4646

47+
/**
48+
* Add Anvil generated source directories to sourceSets in Gradle for indexing visibility in the
49+
* IDE. This can be useful in debugging and is disabled by default.
50+
*/
51+
public val syncGeneratedSources: Property<Boolean> = objects.property(Boolean::class.java)
52+
.convention(false)
53+
4754
@Suppress("PropertyName")
4855
internal var _variantFilter: Action<VariantFilter>? = null
4956

gradle-plugin/src/main/java/com/squareup/anvil/plugin/AnvilPlugin.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ internal open class AnvilPlugin : KotlinCompilerPluginSupportPlugin {
130130
"anvil${File.separator}src-gen-${variant.name}"
131131
)
132132

133+
if (variant.variantFilter.syncGeneratedSources) {
134+
val isIdeSyncProvider = project.providers
135+
.systemProperty("idea.sync.active")
136+
.forUseAtConfigurationTime()
137+
138+
if (isIdeSyncProvider.getOrElse("false").toBoolean()) {
139+
// Only add source sets during the IDE sync. Don't add them for compilation, otherwise
140+
// we'll see weird compile errors especially with incremental compilation. For a longer
141+
// explanation why this is a bad idea, see here:
142+
// https://github.com/square/anvil/pull/207#issuecomment-850768750
143+
kotlinCompilation.defaultSourceSet {
144+
kotlin.srcDir(srcGenDir)
145+
}
146+
}
147+
}
148+
133149
return project.provider {
134150
listOf(
135151
FilesSubpluginOption(

gradle-plugin/src/main/java/com/squareup/anvil/plugin/VariantFilter.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public interface VariantFilter : Named {
3030
* details.
3131
*/
3232
public var disableComponentMerging: Boolean
33+
34+
/**
35+
* Add Anvil generated source directories to sourceSets in Gradle for indexing visibility in the
36+
* IDE. This can be useful in debugging and is disabled by default.
37+
*/
38+
public var syncGeneratedSources: Boolean
3339
}
3440

3541
internal class CommonFilter(
@@ -59,6 +65,13 @@ internal class CommonFilter(
5965
set(value) {
6066
disableComponentMergingOverride = value
6167
}
68+
69+
private var syncGeneratedSourcesOverride: Boolean? = null
70+
override var syncGeneratedSources: Boolean
71+
get() = syncGeneratedSourcesOverride ?: extension.syncGeneratedSources.get()
72+
set(value) {
73+
syncGeneratedSourcesOverride = value
74+
}
6275
}
6376

6477
public class JvmVariantFilter internal constructor(

0 commit comments

Comments
 (0)