Skip to content

Commit 2137435

Browse files
Apply Validation Gradle Plugin
1 parent bf061ea commit 2137435

File tree

2 files changed

+36
-47
lines changed

2 files changed

+36
-47
lines changed

gradle-plugins/src/main/kotlin/io/spine/tools/core/jvm/gradle/plugins/CompilerConfigPlugin.kt

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
package io.spine.tools.core.jvm.gradle.plugins
3030

3131
import io.spine.tools.compiler.gradle.api.CompilerSettings
32-
import io.spine.tools.compiler.gradle.api.Names
3332
import io.spine.tools.compiler.gradle.api.Names.GRADLE_PLUGIN_ID
33+
import io.spine.tools.compiler.gradle.api.addUserClasspathDependency
3434
import io.spine.tools.compiler.gradle.api.compilerSettings
3535
import io.spine.tools.compiler.gradle.api.compilerWorkingDir
3636
import io.spine.tools.compiler.gradle.plugin.LaunchSpineCompiler
@@ -42,7 +42,6 @@ import io.spine.tools.core.jvm.entity.EntityPlugin
4242
import io.spine.tools.core.jvm.gradle.coreJvmOptions
4343
import io.spine.tools.core.jvm.gradle.generatedGrpcDirName
4444
import io.spine.tools.core.jvm.gradle.generatedJavaDirName
45-
import io.spine.tools.core.jvm.gradle.plugins.CompilerConfigPlugin.Companion.VALIDATION_PLUGIN_CLASS
4645
import io.spine.tools.core.jvm.gradle.plugins.CompilerConfigPlugin.Companion.WRITE_COMPILER_PLUGINS_SETTINGS
4746
import io.spine.tools.core.jvm.gradle.settings.CoreJvmCompilerSettings
4847
import io.spine.tools.core.jvm.marker.MarkerPlugin
@@ -53,10 +52,8 @@ import io.spine.tools.core.jvm.uuid.UuidPlugin
5352
import io.spine.tools.fs.DirectoryName
5453
import io.spine.tools.gradle.task.JavaTaskName.Companion.processResources
5554
import io.spine.tools.gradle.task.JavaTaskName.Companion.sourcesJar
56-
import io.spine.tools.meta.MavenArtifact
5755
import org.gradle.api.Plugin
5856
import org.gradle.api.Project
59-
import org.gradle.api.artifacts.Dependency
6057
import org.gradle.api.provider.Provider
6158
import org.gradle.kotlin.dsl.register
6259
import org.gradle.kotlin.dsl.withType
@@ -144,7 +141,7 @@ private fun Project.configureCompilerPlugins() {
144141
val compiler = compilerSettings
145142
compiler.setSubdirectories()
146143

147-
configureValidation(compiler)
144+
// configureValidation(compiler)
148145
configureSignals(compiler)
149146

150147
compiler.run {
@@ -174,29 +171,6 @@ private fun CompilerSettings.setSubdirectories() {
174171
)
175172
}
176173

177-
private fun Project.configureValidation(compiler: CompilerSettings) {
178-
val validationConfig = messageOptions.validation
179-
val version = validationConfig.version.get()
180-
if (validationConfig.enabled.get()) {
181-
addUserClasspathDependency(ValidationSdk.javaCodegenBundle(version))
182-
compiler.plugins(
183-
VALIDATION_PLUGIN_CLASS
184-
)
185-
} else {
186-
addUserClasspathDependency(ValidationSdk.configuration(version))
187-
}
188-
189-
// We add the dependency on runtime anyway for the following reasons:
190-
// 1. We do not want users to change their Gradle build files when they turn on or off
191-
// code generation for the validation code.
192-
//
193-
// 2. We have run-time validation rules that are going to be used in parallel with
194-
// the generated code. This includes current and new implementation for validation
195-
// rules for the already existing generated Protobuf code.
196-
//
197-
addDependency("implementation", ValidationSdk.jvmRuntime(version))
198-
}
199-
200174
private fun Project.configureSignals(compiler: CompilerSettings) {
201175
compiler.addPlugin<SignalPlugin>()
202176

@@ -206,25 +180,6 @@ private fun Project.configureSignals(compiler: CompilerSettings) {
206180
}
207181
}
208182

209-
private fun Project.addUserClasspathDependency(vararg artifacts: MavenArtifact) =
210-
artifacts.forEach {
211-
addDependency(Names.USER_CLASSPATH_CONFIGURATION, it)
212-
}
213-
214-
private fun Project.addDependency(configuration: String, artifact: MavenArtifact) {
215-
val dependency = findDependency(artifact) ?: artifact.coordinates
216-
dependencies.add(configuration, dependency)
217-
}
218-
219-
private fun Project.findDependency(artifact: MavenArtifact): Dependency? {
220-
val dependencies = configurations.flatMap { c -> c.dependencies }
221-
val found = dependencies.firstOrNull { d ->
222-
artifact.group == d.group // `d.group` could be `null`.
223-
&& artifact.name == d.name
224-
}
225-
return found
226-
}
227-
228183
private inline fun <reified T : CompilerPlugin> CompilerSettings.addPlugin() {
229184
plugins(T::class.java.name)
230185
}

gradle-plugins/src/main/kotlin/io/spine/tools/core/jvm/gradle/plugins/CoreJvmPlugin.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ import io.spine.tools.core.jvm.grpc.gradle.GrpcCoreJvmPlugin
3737
import io.spine.tools.core.jvm.routing.gradle.RoutingPlugin
3838
import io.spine.tools.gradle.DslSpec
3939
import io.spine.tools.gradle.lib.LibraryPlugin
40+
import io.spine.tools.meta.MavenArtifact
41+
import io.spine.tools.validation.gradle.ValidationGradlePlugin
4042
import org.gradle.api.Plugin
4143
import org.gradle.api.Project
44+
import org.gradle.api.artifacts.Dependency
4245

4346
/**
4447
* Spine Model Compiler for Java Gradle plugin.
@@ -63,6 +66,9 @@ public class CoreJvmPlugin : LibraryPlugin<CoreJvmOptions>(
6366
super.apply(project)
6467
project.run {
6568
applyProtobufPlugin()
69+
afterEvaluate {
70+
configureValidation()
71+
}
6672
pluginManager.withPlugin(ProtobufGradlePlugin.id) { _ ->
6773
applyCoreJvmPlugins()
6874
}
@@ -81,6 +87,20 @@ private fun Project.applyProtobufPlugin() {
8187
}
8288
}
8389

90+
private fun Project.configureValidation() {
91+
pluginManager.apply(ValidationGradlePlugin::class.java)
92+
93+
// We add the dependency on runtime anyway for the following reasons:
94+
// 1. We do not want users to change their Gradle build files when they turn on or off
95+
// code generation for the validation code.
96+
//
97+
// 2. We have run-time validation rules that are going to be used in parallel with
98+
// the generated code. This includes current and new implementation for validation
99+
// rules for the already existing generated Protobuf code.
100+
//
101+
addDependency("implementation", ValidationSdk.jvmRuntime())
102+
}
103+
84104
private fun Project.applyCoreJvmPlugins() {
85105
logApplying()
86106
val extension = project.coreJvmOptions
@@ -113,3 +133,17 @@ private fun Project.apply(plugin: Plugin<Project>) {
113133
logger.debug { "Applying `${plugin.javaClass.name}` plugin." }
114134
plugin.apply(project)
115135
}
136+
137+
internal fun Project.addDependency(configuration: String, artifact: MavenArtifact) {
138+
val dependency = findDependency(artifact) ?: artifact.coordinates
139+
dependencies.add(configuration, dependency)
140+
}
141+
142+
private fun Project.findDependency(artifact: MavenArtifact): Dependency? {
143+
val dependencies = configurations.flatMap { c -> c.dependencies }
144+
val found = dependencies.firstOrNull { d ->
145+
artifact.group == d.group // `d.group` could be `null`.
146+
&& artifact.name == d.name
147+
}
148+
return found
149+
}

0 commit comments

Comments
 (0)