Skip to content

Commit

Permalink
Implement Circuit DSL (#637)
Browse files Browse the repository at this point in the history
This makes it easy to add circuit up to a project.

```kotlin
slack {
  features {
    circuit()
  }
}
```

<!--
  ⬆ Put your description above this! ⬆

  Please be descriptive and detailed.
  
Please read our [Contributing
Guidelines](https://github.com/tinyspeck/slack-gradle-plugin/blob/main/.github/CONTRIBUTING.md)
and [Code of Conduct](https://slackhq.github.io/code-of-conduct).

Don't worry about deleting this, it's not visible in the PR!
-->
  • Loading branch information
ZacSweers authored Nov 15, 2023
1 parent 2348cb9 commit b66359a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ slack {

## Features

### Circuit

The Circuit feature automatically sets up [Circuit](https://github.com/slackhq/circuit) in the project. This includes
controls for different Circuit artifacts and code gen. The default `circuit()` call will just enable Circuit's
runtime + code gen.

### Dagger

The Dagger feature automatically sets up both Dagger and Anvil. This includes optional parameters to control whether or
Expand Down
103 changes: 99 additions & 4 deletions slack-plugin/src/main/kotlin/slack/gradle/SlackExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ constructor(
}
}

if (featuresHandler.circuitHandler.codegen.getOrElse(false)) {
markKspNeeded("Circuit")
dependencies.add(aptConfiguration(), "com.slack.circuit:circuit-codegen")
dependencies.add("compileOnly", "com.slack.circuit:circuit-codegen-annotations")
}

if (featuresHandler.autoValue.getOrElse(false)) {
markKaptNeeded("AutoValue")
dependencies.add("compileOnly", SlackDependencies.Auto.Value.annotations)
Expand Down Expand Up @@ -351,6 +357,9 @@ constructor(
// Dagger features
internal val daggerHandler = objects.newInstance<DaggerHandler>()

// Circuit features
internal val circuitHandler = objects.newInstance<CircuitHandler>()

/** Enables AutoService on this project. */
internal abstract val autoService: Property<Boolean>

Expand Down Expand Up @@ -385,6 +394,32 @@ constructor(
this.androidExtension = androidExtension
}

/**
* Configures Circuit on this project.
*
* @param codegen configures code gen on this project, including KSP setup and annotations. True
* by default.
* @param runtime configures the circuit-runtime dependencies, including screen/ui/presenter. True
* by default.
* @param commonBundle configures the project-common dependencies, defined by the circuit-common
* bundle ID in `libs.versions.toml`. True by default.
* @param foundation configures the circuit-foundation dependency, false by default.
* @param action optional configuration block for further Circuit config, such as CircuitX.
*/
public fun circuit(
codegen: Boolean = true,
runtime: Boolean = true,
commonBundle: Boolean = true,
foundation: Boolean = false,
action: Action<CircuitHandler> = Action {},
) {
circuitHandler.codegen.set(codegen)
circuitHandler.runtime.set(runtime)
circuitHandler.commonBundle.set(commonBundle)
circuitHandler.foundation.set(foundation)
action.execute(circuitHandler)
}

/**
* Enables dagger for this project.
*
Expand Down Expand Up @@ -517,11 +552,11 @@ constructor(

internal fun applyTo(project: Project) {
composeHandler.applyTo(project, slackProperties)
circuitHandler.applyTo(project, slackProperties)
}
}

@SlackExtensionMarker
@Suppress("UnnecessaryAbstractClass")
public abstract class MoshiHandler {
internal abstract val moshi: Property<Boolean>
internal abstract val moshiAdapters: Property<Boolean>
Expand Down Expand Up @@ -578,8 +613,70 @@ public abstract class MoshiHandler {
}
}

/** DSL for configuring Circuit. */
@SlackExtensionMarker
public abstract class CircuitHandler @Inject constructor(objects: ObjectFactory) {
/** Sets up circuit code gen, includes annotations and KSP setup. */
public val codegen: Property<Boolean> = objects.property<Boolean>().convention(false)
/** Includes the circuit-runtime dep (including screen, ui, presenter). */
public val runtime: Property<Boolean> = objects.property<Boolean>().convention(false)
/** Includes the circuit-foundation dep. */
public val foundation: Property<Boolean> = objects.property<Boolean>().convention(false)
/**
* When enabled, includes a common bundle as defined by the `circuit-common` bundle ID in
* `libs.versions.toml`.
*/
public val commonBundle: Property<Boolean> = objects.property<Boolean>().convention(false)
public val circuitx: CircuitXHandler = objects.newInstance<CircuitXHandler>()

/** DSL entrypoint for CircuitX dependencies. */
public fun circuitx(action: Action<CircuitXHandler>) {
action.execute(circuitx)
}

internal fun applyTo(project: Project, slackProperties: SlackProperties) {
if (runtime.getOrElse(false)) {
project.dependencies.add("implementation", "com.slack.circuit:circuit-runtime")
project.dependencies.add("implementation", "com.slack.circuit:circuit-runtime-presenter")
project.dependencies.add("implementation", "com.slack.circuit:circuit-runtime-ui")
project.dependencies.add("testImplementation", "com.slack.circuit:circuit-test")
}
if (foundation.getOrElse(false)) {
project.dependencies.add("implementation", "com.slack.circuit:circuit-foundation")
}
if (commonBundle.getOrElse(false)) {
slackProperties.versions.bundles.commonCircuit.ifPresent {
project.dependencies.add("implementation", it)
}
}

circuitx.applyTo(project)
}

@SlackExtensionMarker
public abstract class CircuitXHandler @Inject constructor(objects: ObjectFactory) {
/** Correpsonds to the circuitx-android artifact. */
public val android: Property<Boolean> = objects.property<Boolean>().convention(false)
/** Correpsonds to the circuitx-gesture-navigation artifact. */
public val gestureNav: Property<Boolean> = objects.property<Boolean>().convention(false)
/** Correpsonds to the circuitx-overlays artifact. */
public val overlays: Property<Boolean> = objects.property<Boolean>().convention(false)

internal fun applyTo(project: Project) {
if (android.getOrElse(false)) {
project.dependencies.add("implementation", "com.slack.circuit:circuitx-android")
}
if (gestureNav.getOrElse(false)) {
project.dependencies.add("implementation", "com.slack.circuit:circuitx-gesture-navigation")
}
if (overlays.getOrElse(false)) {
project.dependencies.add("implementation", "com.slack.circuit:circuitx-overlays")
}
}
}
}

@SlackExtensionMarker
@Suppress("UnnecessaryAbstractClass")
public abstract class DaggerHandler @Inject constructor(objects: ObjectFactory) {
internal val enabled: Property<Boolean> = objects.property<Boolean>().convention(false)
internal val useDaggerCompiler: Property<Boolean> = objects.property<Boolean>().convention(false)
Expand Down Expand Up @@ -661,7 +758,6 @@ public abstract class DaggerHandler @Inject constructor(objects: ObjectFactory)
}

@SlackExtensionMarker
@Suppress("UnnecessaryAbstractClass")
public abstract class ComposeHandler
@Inject
constructor(
Expand Down Expand Up @@ -722,7 +818,6 @@ constructor(
}

@SlackExtensionMarker
@Suppress("UnnecessaryAbstractClass")
public abstract class AndroidHandler
@Inject
constructor(
Expand Down
3 changes: 3 additions & 0 deletions slack-plugin/src/main/kotlin/slack/gradle/SlackVersions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ internal class SlackVersions(val catalog: VersionCatalog) {
val commonTest: Optional<Provider<ExternalModuleDependencyBundle>> by lazy {
catalog.findBundle("common-test")
}
val commonCircuit: Optional<Provider<ExternalModuleDependencyBundle>> by lazy {
catalog.findBundle("common-circuit")
}
}

internal fun getValue(key: String): String {
Expand Down

0 comments on commit b66359a

Please sign in to comment.