|
| 1 | +/* |
| 2 | + * Copyright the GradleX team. |
| 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 | + * http://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 | + |
| 17 | +package org.gradlex.javamodule.dependencies.initialization; |
| 18 | + |
| 19 | +import org.gradle.api.Action; |
| 20 | +import org.gradle.api.IsolatedAction; |
| 21 | +import org.gradle.api.NonNullApi; |
| 22 | +import org.gradle.api.Project; |
| 23 | +import org.gradle.api.initialization.ProjectDescriptor; |
| 24 | +import org.gradle.api.initialization.Settings; |
| 25 | +import org.gradle.api.model.ObjectFactory; |
| 26 | +import org.gradle.api.plugins.ApplicationPlugin; |
| 27 | +import org.gradle.api.plugins.JavaApplication; |
| 28 | +import org.gradle.api.plugins.JavaPlatformPlugin; |
| 29 | +import org.gradle.util.GradleVersion; |
| 30 | +import org.gradlex.javamodule.dependencies.JavaModuleDependenciesPlugin; |
| 31 | +import org.gradlex.javamodule.dependencies.JavaModuleVersionsPlugin; |
| 32 | +import org.gradlex.javamodule.dependencies.internal.utils.ModuleInfo; |
| 33 | +import org.gradlex.javamodule.dependencies.internal.utils.ModuleInfoCache; |
| 34 | + |
| 35 | +import javax.annotation.Nullable; |
| 36 | +import javax.inject.Inject; |
| 37 | +import java.io.File; |
| 38 | +import java.nio.file.Paths; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +public abstract class JavaModulesExtension { |
| 42 | + |
| 43 | + static final boolean SUPPORT_PROJECT_ISOLATION = |
| 44 | + GradleVersion.current().compareTo(GradleVersion.version("8.8")) >= 0; |
| 45 | + |
| 46 | + private final Settings settings; |
| 47 | + private final ModuleInfoCache moduleInfoCache; |
| 48 | + |
| 49 | + @Inject |
| 50 | + public abstract ObjectFactory getObjects(); |
| 51 | + |
| 52 | + @Inject |
| 53 | + public JavaModulesExtension(Settings settings) { |
| 54 | + this.settings = settings; |
| 55 | + this.moduleInfoCache = getObjects().newInstance(ModuleInfoCache.class); |
| 56 | + } |
| 57 | + |
| 58 | + public void module(String folder) { |
| 59 | + module(folder, m -> {}); |
| 60 | + } |
| 61 | + |
| 62 | + public void module(String folder, Action<Module> action) { |
| 63 | + Module module = getObjects().newInstance(Module.class); |
| 64 | + module.getFolder().set(folder); |
| 65 | + action.execute(module); |
| 66 | + includeModule(module, new File(settings.getRootDir(), module.getFolder().get())); |
| 67 | + } |
| 68 | + |
| 69 | + public void modules(String folder) { |
| 70 | + modules(folder, m -> {}); |
| 71 | + } |
| 72 | + |
| 73 | + public void modules(String folder, Action<Modules> action) { |
| 74 | + Modules moduleGroup = getObjects().newInstance(Modules.class); |
| 75 | + action.execute(moduleGroup); |
| 76 | + |
| 77 | + File[] projectFolders = new File(settings.getRootDir(), folder).listFiles(); |
| 78 | + if (projectFolders == null) { |
| 79 | + throw new RuntimeException("Failed to inspect: " + new File(settings.getRootDir(), folder)); |
| 80 | + } |
| 81 | + |
| 82 | + for (File projectFolder : projectFolders) { |
| 83 | + if (moduleGroup.customizedModules.containsKey(projectFolder.getName())) { |
| 84 | + includeModule(moduleGroup.customizedModules.get(projectFolder.getName()), projectFolder); |
| 85 | + } else { |
| 86 | + includeModule(moduleGroup.addModule(projectFolder.getName()), projectFolder); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public void versions(String folder) { |
| 92 | + String projectName = Paths.get(folder).getFileName().toString(); |
| 93 | + settings.include(projectName); |
| 94 | + settings.project(":" + projectName).setProjectDir(new File(settings.getRootDir(), folder)); |
| 95 | + if (SUPPORT_PROJECT_ISOLATION) { |
| 96 | + settings.getGradle().getLifecycle().beforeProject(new ApplyJavaModuleVersionsPluginAction(projectName)); |
| 97 | + } else { |
| 98 | + settings.getGradle().beforeProject(new ApplyJavaModuleVersionsPluginAction(projectName)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void includeModule(Module module, File projectDir) { |
| 103 | + ModuleInfo moduleInfo = moduleInfoCache.get(projectDir, module.getModuleInfoPath().get(), |
| 104 | + module.getArtifact().get(), module.getGroup(), settings.getProviders()); |
| 105 | + if (moduleInfo == ModuleInfo.EMPTY) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + String artifact = module.getArtifact().get(); |
| 110 | + settings.include(artifact); |
| 111 | + ProjectDescriptor project = settings.project(":" + artifact); |
| 112 | + project.setProjectDir(projectDir); |
| 113 | + |
| 114 | + String group = module.getGroup().getOrNull(); |
| 115 | + |
| 116 | + List<String> plugins = module.getPlugins().get(); |
| 117 | + if (SUPPORT_PROJECT_ISOLATION) { |
| 118 | + settings.getGradle().getLifecycle().beforeProject(new ApplyPluginsAction(artifact, group, plugins, moduleInfo.getModuleName())); |
| 119 | + } else { |
| 120 | + settings.getGradle().beforeProject(new ApplyPluginsAction(artifact, group, plugins, moduleInfo.getModuleName())); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @NonNullApi |
| 125 | + private static class ApplyPluginsAction implements IsolatedAction<Project>, Action<Project> { |
| 126 | + |
| 127 | + private final String artifact; |
| 128 | + private final String group; |
| 129 | + private final List<String> plugins; |
| 130 | + private final String modueName; |
| 131 | + |
| 132 | + public ApplyPluginsAction(String artifact, @Nullable String group, List<String> plugins, String modueName) { |
| 133 | + this.artifact = artifact; |
| 134 | + this.group = group; |
| 135 | + this.plugins = plugins; |
| 136 | + this.modueName = modueName; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void execute(Project project) { |
| 141 | + if (project.getName().equals(artifact)) { |
| 142 | + if (group != null) project.setGroup(group); |
| 143 | + project.getPlugins().apply(JavaModuleDependenciesPlugin.class); |
| 144 | + plugins.forEach(id -> project.getPlugins().apply(id)); |
| 145 | + project.getPlugins().withType(ApplicationPlugin.class, p -> |
| 146 | + project.getExtensions().getByType(JavaApplication.class).getMainModule().set(modueName)); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @NonNullApi |
| 152 | + private static class ApplyJavaModuleVersionsPluginAction implements IsolatedAction<Project>, Action<Project> { |
| 153 | + |
| 154 | + private final String projectName; |
| 155 | + |
| 156 | + public ApplyJavaModuleVersionsPluginAction(String projectName) { |
| 157 | + this.projectName = projectName; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void execute(Project project) { |
| 162 | + if (projectName.equals(project.getName())) { |
| 163 | + project.getPlugins().apply(JavaPlatformPlugin.class); |
| 164 | + project.getPlugins().apply(JavaModuleVersionsPlugin.class); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments