|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.webtools.flywayjooq; |
| 17 | + |
| 18 | +import java.net.URL; |
| 19 | +import java.net.URLClassLoader; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | +import javax.inject.Inject; |
| 23 | +import org.gradle.api.DefaultTask; |
| 24 | +import org.gradle.api.Plugin; |
| 25 | +import org.gradle.api.Project; |
| 26 | +import org.gradle.api.Task; |
| 27 | +import org.gradle.api.file.DirectoryProperty; |
| 28 | +import org.gradle.api.file.ProjectLayout; |
| 29 | +import org.gradle.api.model.ObjectFactory; |
| 30 | +import org.gradle.api.plugins.JavaBasePlugin; |
| 31 | +import org.gradle.api.plugins.JavaPlugin; |
| 32 | +import org.gradle.api.provider.Property; |
| 33 | +import org.gradle.api.tasks.Internal; |
| 34 | +import org.gradle.api.tasks.PathSensitivity; |
| 35 | +import org.gradle.api.tasks.TaskAction; |
| 36 | +import org.gradle.api.tasks.TaskProvider; |
| 37 | +import org.jooq.codegen.gradle.CodegenPluginExtension; |
| 38 | + |
| 39 | +/** |
| 40 | + * This plugin spools up a fresh postgres session, |
| 41 | + * runs flyway on it, then runs jooq on the result. |
| 42 | + * The postgres stays up so that the flyway result |
| 43 | + * can be used as a template database for testing. |
| 44 | + */ |
| 45 | +public class FlywayJooqPlugin implements Plugin<Project> { |
| 46 | + private static final String EXTENSION_NAME = "flywayJooq"; |
| 47 | + |
| 48 | + public static class Extension extends CodegenPluginExtension { |
| 49 | + @Inject |
| 50 | + public Extension( |
| 51 | + ObjectFactory objects, |
| 52 | + ProjectLayout layout) { |
| 53 | + super(objects, layout); |
| 54 | + } |
| 55 | + |
| 56 | + public final SetupCleanupDockerFlyway setup = new SetupCleanupDockerFlyway(); |
| 57 | + |
| 58 | + /** Ensures a database with a template prepared by Flyway is available. */ |
| 59 | + public void neededBy(TaskProvider<?> taskProvider) { |
| 60 | + taskProvider.configure(this::neededBy); |
| 61 | + } |
| 62 | + |
| 63 | + /** Ensures a database with a template prepared by Flyway is available. */ |
| 64 | + public void neededBy(Task task) { |
| 65 | + task.dependsOn(DockerUp.TASK_NAME); |
| 66 | + task.getInputs().file(setup.dockerComposeFile).withPathSensitivity(PathSensitivity.RELATIVE); |
| 67 | + task.getInputs().dir(setup.flywayMigrations).withPathSensitivity(PathSensitivity.RELATIVE); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void apply(Project project) { |
| 73 | + // FlywayPlugin needs to be applied first |
| 74 | + project.getPlugins().apply(JavaBasePlugin.class); |
| 75 | + |
| 76 | + Extension extension = project.getExtensions().create(EXTENSION_NAME, Extension.class); |
| 77 | + extension.configuration(a -> {}); |
| 78 | + extension.setup.dockerPullOnStartup = !project.getGradle().getStartParameter().isOffline(); |
| 79 | + |
| 80 | + // force all jooq versions to match |
| 81 | + String jooqVersion = detectJooqVersion(); |
| 82 | + project.getConfigurations().all(config -> { |
| 83 | + config.resolutionStrategy(strategy -> { |
| 84 | + strategy.eachDependency(details -> { |
| 85 | + String group = details.getRequested().getGroup(); |
| 86 | + String name = details.getRequested().getName(); |
| 87 | + if (group.equals("org.jooq") && name.startsWith("jooq")) { |
| 88 | + details.useTarget(group + ":" + name + ":" + jooqVersion); |
| 89 | + } |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + // create a jooq task, which will be needed by all compilation tasks |
| 95 | + TaskProvider<JooqTask> jooqTask = project.getTasks().register("jooq", JooqTask.class, task -> { |
| 96 | + task.setup = extension.setup; |
| 97 | + var generator = extension.getExecutions().maybeCreate("").getConfiguration().getGenerator(); |
| 98 | + task.generatorConfig = generator; |
| 99 | + task.getGeneratedSource().set(project.file(generator.getTarget().getDirectory())); |
| 100 | + }); |
| 101 | + extension.neededBy(jooqTask); |
| 102 | + project.getTasks().named(JavaPlugin.COMPILE_JAVA_TASK_NAME).configure(task -> { |
| 103 | + task.dependsOn(jooqTask); |
| 104 | + }); |
| 105 | + |
| 106 | + project.getTasks().register(DockerDown.TASK_NAME, DockerDown.class, task -> { |
| 107 | + task.getSetupCleanup().set(extension.setup); |
| 108 | + task.getProjectDir().set(project.getProjectDir()); |
| 109 | + }); |
| 110 | + project.getTasks().register(DockerUp.TASK_NAME, DockerUp.class, task -> { |
| 111 | + task.getSetupCleanup().set(extension.setup); |
| 112 | + task.getProjectDir().set(project.getProjectDir()); |
| 113 | + task.mustRunAfter(DockerDown.TASK_NAME); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + public abstract static class DockerUp extends DefaultTask { |
| 118 | + private static final String TASK_NAME = "dockerUp"; |
| 119 | + |
| 120 | + @Internal |
| 121 | + public abstract DirectoryProperty getProjectDir(); |
| 122 | + |
| 123 | + @Internal |
| 124 | + public abstract Property<SetupCleanupDockerFlyway> getSetupCleanup(); |
| 125 | + |
| 126 | + @TaskAction |
| 127 | + public void dockerUp() throws Exception { |
| 128 | + getSetupCleanup().get().start(getProjectDir().get().getAsFile()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public abstract static class DockerDown extends DefaultTask { |
| 133 | + private static final String TASK_NAME = "dockerDown"; |
| 134 | + |
| 135 | + @Internal |
| 136 | + public abstract DirectoryProperty getProjectDir(); |
| 137 | + |
| 138 | + @Internal |
| 139 | + public abstract Property<SetupCleanupDockerFlyway> getSetupCleanup(); |
| 140 | + |
| 141 | + @TaskAction |
| 142 | + public void dockerDown() throws Exception { |
| 143 | + getSetupCleanup().get().forceStop(getProjectDir().get().getAsFile()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** Detects the jooq version. */ |
| 148 | + private static String detectJooqVersion() { |
| 149 | + URLClassLoader loader = (URLClassLoader) FlywayJooqPlugin.class.getClassLoader(); |
| 150 | + for (URL url : loader.getURLs()) { |
| 151 | + Pattern pattern = Pattern.compile("(.*)/jooq-([0-9,.]*?).jar$"); |
| 152 | + Matcher matcher = pattern.matcher(url.getPath()); |
| 153 | + if (matcher.matches()) { |
| 154 | + return matcher.group(2); |
| 155 | + } |
| 156 | + } |
| 157 | + throw new IllegalStateException("Unable to detect jooq version."); |
| 158 | + } |
| 159 | +} |
0 commit comments