Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1350 poc type safe gradle task accessors #1511

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions automation/code-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dependencies {
implementation("io.ktor:ktor-client-content-negotiation")
implementation("io.ktor:ktor-serialization-kotlinx-json")
implementation("io.arrow-kt:arrow-fx-coroutines:1.2.4")
implementation("org.gradle:gradle-tooling-api:8.8")
runtimeOnly("org.slf4j:slf4j-simple:1.7.10")

implementation(projects.actionBindingGenerator)
implementation(projects.sharedInternal)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.typesafegithub.workflows

import org.gradle.tooling.GradleConnector
import org.gradle.tooling.model.GradleProject
import java.io.File
import java.util.Locale

fun main() {
GradleConnector.newConnector()
.forProjectDirectory(File("/Users/piotr/repos/github-workflows-kt"))
.connect()
.use { gradleConnector ->
val model = gradleConnector.getModel(GradleProject::class.java)
val allTasks = model.tasks + model.children.all.flatMap { it.tasks }
allTasks.forEach { task ->
println(task)
}
println("Generate the following accessors:")
allTasks.forEach { task ->
println("projects" + task.path.split(":").joinToString(".") { it.toCamelCase() })
}
}
}

internal fun String.toPascalCase(): String {
val hasOnlyUppercases = none { it in 'a'..'z' }
val normalizedString = if (hasOnlyUppercases) lowercase() else this
return normalizedString.replace("+", "-plus-")
.split("-", "_", " ", ".", "/")
.joinToString("") {
it.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}
}

internal fun String.toCamelCase(): String = toPascalCase().replaceFirstChar { it.lowercase() }
1 change: 1 addition & 0 deletions buildSrc/repositories.settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencyResolutionManagement {
repositories {
mavenCentral()
gradlePluginPortal()
maven { url = uri("https://repo.gradle.org/gradle/libs-releases") }

// It has to be defined here because preferring repositories config in settings apparently removes the below
// additions done by Kotlin/JS plugin.
Expand Down
2 changes: 2 additions & 0 deletions github-workflows-kt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
implementation("it.krzeminski:snakeyaml-engine-kmp:3.0.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.0")
implementation("io.github.reactivecircus.cache4k:cache4k:0.13.0")
implementation("ch.qos.logback:logback-classic:1.5.6")
implementation(projects.sharedInternal)

testImplementation("dev.zacsweers.kctfork:core:0.5.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.github.typesafegithub.workflows.domain

public data class GradleTask(val path: String)

public sealed interface TaskTreeNode

public data class TaskTreeLeaf(val name: String) : TaskTreeNode

public data class TaskTree(val tree: Map<String, TaskTreeNode>) : TaskTreeNode

public data class GradleTasks(val taskTree: TaskTree =
TaskTree(
tree = mapOf(
"foo" to TaskTreeLeaf("bar"),
"baz" to TaskTree(
tree = mapOf(
"goo" to TaskTreeLeaf("zoo"),
),
),
),
), val tasks: List<GradleTask>) {
public operator fun get(name: String): GradleTasks {
// if (name !in taskTree.tree) {
// error("'$name' not in ${taskTree.tree.keys}" )
// }

return if (this.tasks.isEmpty()) {
GradleTasks(tasks = listOf(GradleTask(name)))
} else {
GradleTasks(
tasks = tasks.dropLast(1) + GradleTask(
path = "${tasks.last().path}:$name"))
}
}

public operator fun plus(other: GradleTasks): GradleTasks {
return GradleTasks(tasks = this.tasks + other.tasks)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.typesafegithub.workflows.domain.CommandStep
import io.github.typesafegithub.workflows.domain.Concurrency
import io.github.typesafegithub.workflows.domain.Container
import io.github.typesafegithub.workflows.domain.Environment
import io.github.typesafegithub.workflows.domain.GradleTasks
import io.github.typesafegithub.workflows.domain.Job
import io.github.typesafegithub.workflows.domain.JobOutputs
import io.github.typesafegithub.workflows.domain.KotlinLogicStep
Expand Down Expand Up @@ -159,6 +160,16 @@ public class JobBuilder<OUTPUT : JobOutputs>(
return newStep
}

public fun gradle(
@Suppress("UNUSED_PARAMETER")
vararg pleaseUseNamedArguments: Unit,
name: String? = null,
tasks: GradleTasks.() -> GradleTasks,
) {
val configuredTasks = GradleTasks(tasks = emptyList()).tasks()
println(configuredTasks.tasks.joinToString(" ") { it.path })
}

public fun <T : Action.Outputs> uses(
@Suppress("UNUSED_PARAMETER")
vararg pleaseUseNamedArguments: Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ class IntegrationTest : FunSpec({
),
continueOnError = true,
)

gradle(
tasks = { this["foo"]["bar"] + this["goo"]["baz"] }
)
}
}

Expand Down
2 changes: 0 additions & 2 deletions jit-binding-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ dependencies {
implementation("io.opentelemetry:opentelemetry-sdk:1.39.0")
implementation("io.opentelemetry:opentelemetry-exporter-otlp:1.39.0")
implementation("io.opentelemetry:opentelemetry-exporter-logging:1.39.0")
implementation("io.github.reactivecircus.cache4k:cache4k:0.13.0")
implementation("ch.qos.logback:logback-classic:1.5.6")

implementation(projects.mavenBindingBuilder)
implementation(projects.sharedInternal)
Expand Down
Loading