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