|
4 | 4 |
|
5 | 5 | package kotlinx.rpc.buf |
6 | 6 |
|
| 7 | +import kotlinx.rpc.buf.tasks.BufExecTask |
7 | 8 | import org.gradle.api.Action |
8 | 9 | import org.gradle.api.Project |
9 | 10 | import org.gradle.api.provider.Property |
10 | 11 | import org.gradle.kotlin.dsl.property |
11 | 12 | import java.io.File |
12 | 13 | import javax.inject.Inject |
| 14 | +import kotlin.time.Duration |
| 15 | +import kotlinx.rpc.proto.ProtocPlugin |
| 16 | +import org.gradle.api.model.ObjectFactory |
| 17 | +import org.gradle.api.provider.ListProperty |
| 18 | +import org.gradle.api.provider.Provider |
| 19 | +import org.gradle.kotlin.dsl.listProperty |
| 20 | +import kotlin.reflect.KClass |
13 | 21 |
|
14 | | -public open class BufExtension @Inject constructor(internal val project: Project) { |
15 | | - public val configFile: Property<File> = project.objects.property<File>() |
| 22 | +/** |
| 23 | + * Options for the Buf tasks. |
| 24 | + * |
| 25 | + * @see <a href="https://buf.build/docs/reference/cli/buf/">buf commands</a> |
| 26 | + */ |
| 27 | +public open class BufExtension @Inject constructor(objects: ObjectFactory) { |
| 28 | + /** |
| 29 | + * `--config` argument value. |
| 30 | + * |
| 31 | + * @see <a href="https://buf.build/docs/reference/cli/buf/">buf commands</a> |
| 32 | + */ |
| 33 | + public val configFile: Property<File> = objects.property<File>() |
| 34 | + |
| 35 | + /** |
| 36 | + * `--log-format` option. |
| 37 | + * |
| 38 | + * @see <a href="https://buf.build/docs/reference/cli/buf/#log-format">buf --log-format</a> |
| 39 | + */ |
| 40 | + public val logFormat: Property<LogFormat> = objects.property<LogFormat>().convention(LogFormat.Default) |
16 | 41 |
|
17 | | - public val generate: BufGenerateExtension = project.objects.newInstance(BufGenerateExtension::class.java, project) |
| 42 | + /** |
| 43 | + * Possible values for `--log-format` [logFormat] option. |
| 44 | + */ |
| 45 | + public enum class LogFormat { |
| 46 | + Text, |
| 47 | + Color, |
| 48 | + Json, |
| 49 | + |
| 50 | + /** |
| 51 | + * Buf's default value. |
| 52 | + */ |
| 53 | + Default, |
| 54 | + ; |
| 55 | + } |
18 | 56 |
|
| 57 | + /** |
| 58 | + * `--timeout` option. |
| 59 | + * |
| 60 | + * Value to Buf is passed in seconds using [Duration.inWholeSeconds]. |
| 61 | + * |
| 62 | + * @see <a href="https://buf.build/docs/reference/cli/buf/#timeout">buf --timeout</a> |
| 63 | + */ |
| 64 | + public val timeout: Property<Duration> = objects.property<Duration>().convention(Duration.ZERO) |
| 65 | + |
| 66 | + /** |
| 67 | + * `buf generate` options. |
| 68 | + * |
| 69 | + * @see <a href="https://buf.build/docs/reference/cli/buf/generate/">"buf generate" command</a> |
| 70 | + * @see [BUF_GEN_YAML] |
| 71 | + */ |
| 72 | + public val generate: BufGenerateExtension = objects.newInstance(BufGenerateExtension::class.java) |
| 73 | + |
| 74 | + /** |
| 75 | + * Configures the `buf generate` options. |
| 76 | + * |
| 77 | + * @see <a href="https://buf.build/docs/reference/cli/buf/generate/">"buf generate" command</a> |
| 78 | + * @see [BUF_GEN_YAML] |
| 79 | + */ |
19 | 80 | public fun generate(configure: Action<BufGenerateExtension>) { |
20 | 81 | configure.execute(generate) |
21 | 82 | } |
| 83 | + |
| 84 | + /** |
| 85 | + * Use this extension to register custom Buf tasks |
| 86 | + * that will operate on the generated workspace. |
| 87 | + */ |
| 88 | + public val tasks: BufTasksExtension = objects.newInstance(BufTasksExtension::class.java) |
| 89 | + |
| 90 | + /** |
| 91 | + * Use this extension to register custom Buf tasks |
| 92 | + * that will operate on the generated workspace. |
| 93 | + */ |
| 94 | + public fun tasks(configure: Action<BufTasksExtension>) { |
| 95 | + configure.execute(tasks) |
| 96 | + } |
22 | 97 | } |
23 | 98 |
|
| 99 | +/** |
| 100 | + * Allows registering custom Buf tasks that can operate on the generated workspace. |
| 101 | + */ |
| 102 | +public open class BufTasksExtension @Inject constructor(internal val project: Project) { |
| 103 | + // TODO change to commonMain/commonTest in docs when it's supported |
| 104 | + /** |
| 105 | + * Registers a custom Buf task that operates on the generated workspace. |
| 106 | + * |
| 107 | + * Name conventions: |
| 108 | + * `lint` input for [name] will result in tasks |
| 109 | + * named 'bufLintMain' and 'bufLintTest' for Kotlin/JVM projects |
| 110 | + * and 'bufLintJvmMain' and 'bufLintJvmTest' for Kotlin/Multiplatform projects. |
| 111 | + * |
| 112 | + * Note the by default 'test' task doesn't depend on 'main' task. |
| 113 | + */ |
| 114 | + public fun <T : BufExecTask> registerWorkspaceTask(kClass: KClass<T>, name: String, configure: Action<T>): Provider<T> { |
| 115 | + val property = project.objects.property(kClass) |
| 116 | + |
| 117 | + @Suppress("UNCHECKED_CAST") |
| 118 | + customTasks.add(Definition(name, kClass, configure, property as Property<BufExecTask>)) |
| 119 | + |
| 120 | + return property |
| 121 | + } |
| 122 | + |
| 123 | + // TODO change to commonMain/commonTest in docs when it's supported |
| 124 | + /** |
| 125 | + * Registers a custom Buf task that operates on the generated workspace. |
| 126 | + * |
| 127 | + * Name conventions: |
| 128 | + * `lint` input for [name] will result in tasks |
| 129 | + * named 'bufLintMain' and 'bufLintTest' for Kotlin/JVM projects |
| 130 | + * and 'bufLintJvmMain' and 'bufLintJvmTest' for Kotlin/Multiplatform projects. |
| 131 | + * |
| 132 | + * Note the by default 'test' task doesn't depend on 'main' task. |
| 133 | + */ |
| 134 | + public inline fun <reified T : BufExecTask> registerWorkspaceTask(name: String, configure: Action<T>): Provider<T> { |
| 135 | + return registerWorkspaceTask(T::class, name, configure) |
| 136 | + } |
| 137 | + |
| 138 | + internal val customTasks: ListProperty<Definition<out BufExecTask>> = project.objects.listProperty() |
| 139 | + |
| 140 | + internal class Definition<T : BufExecTask>( |
| 141 | + val name: String, |
| 142 | + val kClass: KClass<T>, |
| 143 | + val configure: Action<T>, |
| 144 | + val property: Property<BufExecTask>, |
| 145 | + ) |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Options for the `buf generate` command. |
| 150 | + * |
| 151 | + * @see <a href="https://buf.build/docs/reference/cli/buf/generate/">"buf generate" command</a> |
| 152 | + * @see [BUF_GEN_YAML] |
| 153 | + */ |
24 | 154 | public open class BufGenerateExtension @Inject constructor(internal val project: Project) { |
| 155 | + /** |
| 156 | + * `--include-imports` option. |
| 157 | + * |
| 158 | + * @see <a href="https://buf.build/docs/reference/cli/buf/generate/#include-imports">buf generate --include-imports</a> |
| 159 | + * @see [ProtocPlugin.includeImports] |
| 160 | + */ |
25 | 161 | public val includeImports: Property<Boolean> = project.objects.property<Boolean>().convention(false) |
| 162 | + |
| 163 | + /** |
| 164 | + * `--include-wkt` option. |
| 165 | + * |
| 166 | + * @see <a href="https://buf.build/docs/reference/cli/buf/generate/#include-wkt">buf generate --include-wkt</a> |
| 167 | + * @see [ProtocPlugin.includeWkt] |
| 168 | + */ |
| 169 | + public val includeWkt: Property<Boolean> = project.objects.property<Boolean>().convention(false) |
| 170 | + |
| 171 | + /** |
| 172 | + * `--error-format` option. |
| 173 | + * |
| 174 | + * @see <a href="https://buf.build/docs/reference/cli/buf/generate/#error-format">buf generate --error-format</a> |
| 175 | + */ |
| 176 | + public val errorFormat: Property<ErrorFormat> = project.objects.property<ErrorFormat>().convention(ErrorFormat.Default) |
| 177 | + |
| 178 | + /** |
| 179 | + * Possible values for `--error-format` option. |
| 180 | + * |
| 181 | + * @see <a href="https://buf.build/docs/reference/cli/buf/generate/#error-format">buf generate --error-format</a> |
| 182 | + */ |
| 183 | + public enum class ErrorFormat(internal val cliValue: String) { |
| 184 | + Text("text"), |
| 185 | + Json("json"), |
| 186 | + Msvs("msvs"), |
| 187 | + Junit("junit"), |
| 188 | + GithubActions("github-actions"), |
| 189 | + |
| 190 | + /** |
| 191 | + * Buf's default value. |
| 192 | + */ |
| 193 | + Default(""), |
| 194 | + ; |
| 195 | + } |
26 | 196 | } |
0 commit comments