Skip to content

Commit

Permalink
Make .gradle configurator an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile committed Aug 18, 2022
1 parent 76273f4 commit 3e9d1a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.jetbrains.kotlinx.jupyter.api.VariableDeclarationCallback
import org.jetbrains.kotlinx.jupyter.api.VariableUpdateCallback
import org.jetbrains.kotlinx.jupyter.util.AcceptanceRule
import org.jetbrains.kotlinx.jupyter.util.NameAcceptanceRule
import java.io.File
import kotlin.reflect.KMutableProperty

/**
Expand Down Expand Up @@ -235,45 +234,6 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
interruptionCallbacks.add(action)
}

private val gradleLikeConfigurator = GradleLikeConfigurator()
fun gradle(configure: DependenciesConfigurator.() -> Unit) {
gradleLikeConfigurator.configure()
}

inner class GradleLikeConfigurator : DependenciesConfigurator {
private val repositoryConfigurator = RepositoryHandlerScopeImpl()

override fun repositories(action: RepositoryHandlerScope.() -> Unit) {
repositoryConfigurator.action()
}

inner class RepositoryHandlerScopeImpl : RepositoryHandlerScope {
override fun dir(dir: File) {
repositories.add(dir.absolutePath)
}

override fun maven(url: String) {
repositories.add(url)
}
}

private val dependenciesConfigurator = DependencyHandlerScopeImpl()

override fun dependencies(action: DependencyHandlerScope.() -> Unit) {
dependenciesConfigurator.action()
}

inner class DependencyHandlerScopeImpl : DependencyHandlerScope {
override fun implementation(coordinates: String) {
dependencies.add(coordinates)
}

override fun implementation(group: String, artifact: String, version: String) {
implementation("$group:$artifact:$version")
}
}
}

internal fun getDefinition() =
libraryDefinition {
it.init = init
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,53 @@ interface DependencyHandlerScope {
fun RepositoryHandlerScope.mavenCentral() = maven("https://repo.maven.apache.org/maven2/")
fun RepositoryHandlerScope.google() = maven("https://dl.google.com/dl/android/maven2/")
fun RepositoryHandlerScope.mavenLocal() = maven("*mavenLocal")

fun JupyterIntegration.Builder.gradle(action: DependenciesConfigurator.() -> Unit) {
gradleLikeConfigurator.configure(this, action)
}

private abstract class BuilderConfigurator<T : Any> {
private var _builder: JupyterIntegration.Builder? = null
protected val builder get() = _builder!!

fun configure(builder: JupyterIntegration.Builder, action: T.() -> Unit) {
_builder = builder
@Suppress("UNCHECKED_CAST")
(this as T).action()
_builder = null
}
}

private val gradleLikeConfigurator = GradleLikeConfigurator()
private val repositoryConfigurator = RepositoryHandlerScopeImpl()
private val dependenciesConfigurator = DependencyHandlerScopeImpl()

private class GradleLikeConfigurator : DependenciesConfigurator, BuilderConfigurator<DependenciesConfigurator>() {
override fun repositories(action: RepositoryHandlerScope.() -> Unit) {
repositoryConfigurator.configure(builder, action)
}

override fun dependencies(action: DependencyHandlerScope.() -> Unit) {
dependenciesConfigurator.configure(builder, action)
}
}

private class RepositoryHandlerScopeImpl : RepositoryHandlerScope, BuilderConfigurator<RepositoryHandlerScope>() {
override fun dir(dir: File) {
builder.repositories(dir.absolutePath)
}

override fun maven(url: String) {
builder.repositories(url)
}
}

private class DependencyHandlerScopeImpl : DependencyHandlerScope, BuilderConfigurator<DependencyHandlerScope>() {
override fun implementation(coordinates: String) {
builder.dependencies(coordinates)
}

override fun implementation(group: String, artifact: String, version: String) {
implementation("$group:$artifact:$version")
}
}

0 comments on commit 3e9d1a3

Please sign in to comment.