|
| 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.tasks; |
| 18 | + |
| 19 | +import org.gradle.api.DefaultTask; |
| 20 | +import org.gradle.api.file.RegularFileProperty; |
| 21 | +import org.gradle.api.model.ObjectFactory; |
| 22 | +import org.gradle.api.provider.ListProperty; |
| 23 | +import org.gradle.api.provider.Property; |
| 24 | +import org.gradle.api.tasks.Internal; |
| 25 | +import org.gradle.api.tasks.TaskAction; |
| 26 | + |
| 27 | +import javax.inject.Inject; |
| 28 | +import java.io.File; |
| 29 | +import java.io.IOException; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +public abstract class BuildFileDependenciesGenerate extends DefaultTask { |
| 37 | + public abstract static class SourceSetDependencies { |
| 38 | + |
| 39 | + private final String name; |
| 40 | + |
| 41 | + @Inject |
| 42 | + public SourceSetDependencies(String name) { |
| 43 | + this.name = name; |
| 44 | + } |
| 45 | + |
| 46 | + public abstract ListProperty<DependencyDeclaration> getApiDependencies(); |
| 47 | + |
| 48 | + public abstract ListProperty<DependencyDeclaration> getImplementationDependencies(); |
| 49 | + |
| 50 | + public abstract ListProperty<DependencyDeclaration> getCompileOnlyApiDependencies(); |
| 51 | + |
| 52 | + public abstract ListProperty<DependencyDeclaration> getCompileOnlyDependencies(); |
| 53 | + |
| 54 | + public abstract ListProperty<DependencyDeclaration> getRuntimeOnlyDependencies(); |
| 55 | + |
| 56 | + private boolean isEmpty() { |
| 57 | + return getApiDependencies().get().isEmpty() |
| 58 | + && getImplementationDependencies().get().isEmpty() |
| 59 | + && getCompileOnlyApiDependencies().get().isEmpty() |
| 60 | + && getCompileOnlyDependencies().get().isEmpty() |
| 61 | + && getRuntimeOnlyDependencies().get().isEmpty(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static class DependencyDeclaration { |
| 66 | + private final String scope; |
| 67 | + private final String moduleName; |
| 68 | + private final String fullId; |
| 69 | + |
| 70 | + public DependencyDeclaration(String scope, String moduleName, String fullId) { |
| 71 | + this.scope = scope; |
| 72 | + this.moduleName = moduleName; |
| 73 | + this.fullId = fullId; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Internal |
| 78 | + public abstract ListProperty<SourceSetDependencies> getDependencies(); |
| 79 | + |
| 80 | + @Internal |
| 81 | + public abstract Property<Boolean> getWithCatalog(); |
| 82 | + |
| 83 | + @Internal |
| 84 | + public abstract Property<String> getOwnProjectGroup(); |
| 85 | + |
| 86 | + @Internal |
| 87 | + public abstract RegularFileProperty getBuildFile(); |
| 88 | + |
| 89 | + @Inject |
| 90 | + public abstract ObjectFactory getObjects(); |
| 91 | + |
| 92 | + public void addDependencies(String name, List<DependencyDeclaration> api, List<DependencyDeclaration> implementation, List<DependencyDeclaration> compileOnlyApi, List<DependencyDeclaration> compileOnly, List<DependencyDeclaration> runtimeOnly) { |
| 93 | + SourceSetDependencies dependencies = getObjects().newInstance(SourceSetDependencies.class, name); |
| 94 | + dependencies.getApiDependencies().convention(api); |
| 95 | + dependencies.getImplementationDependencies().convention(implementation); |
| 96 | + dependencies.getCompileOnlyApiDependencies().convention(compileOnlyApi); |
| 97 | + dependencies.getCompileOnlyDependencies().convention(compileOnly); |
| 98 | + dependencies.getRuntimeOnlyDependencies().convention(runtimeOnly); |
| 99 | + getDependencies().add(dependencies); |
| 100 | + } |
| 101 | + |
| 102 | + @TaskAction |
| 103 | + public void generate() throws IOException { |
| 104 | + File buildGradle = getBuildFile().get().getAsFile(); |
| 105 | + List<String> fileContentToPreserve = Files.readAllLines(buildGradle.toPath()); |
| 106 | + Optional<String> dependenciesBlock = fileContentToPreserve.stream().filter(line -> line.contains("dependencies")).findFirst(); |
| 107 | + if (dependenciesBlock.isPresent()) { |
| 108 | + fileContentToPreserve = fileContentToPreserve.subList(0, fileContentToPreserve.indexOf(dependenciesBlock.get())); |
| 109 | + } |
| 110 | + |
| 111 | + List<String> content = new ArrayList<>(fileContentToPreserve); |
| 112 | + if (!content.get(content.size() - 1).isEmpty()) { |
| 113 | + content.add(""); |
| 114 | + } |
| 115 | + |
| 116 | + if (!getDependencies().get().isEmpty()) { |
| 117 | + content.add("dependencies {"); |
| 118 | + getDependencies().get().stream().sorted((a, b) -> ("main".equals(a.name)) ? -1 : a.name.compareTo(b.name)).forEach(sourceSetBlock -> { |
| 119 | + content.addAll(toDeclarationString(sourceSetBlock.getApiDependencies())); |
| 120 | + content.addAll(toDeclarationString(sourceSetBlock.getImplementationDependencies())); |
| 121 | + content.addAll(toDeclarationString(sourceSetBlock.getCompileOnlyApiDependencies())); |
| 122 | + content.addAll(toDeclarationString(sourceSetBlock.getCompileOnlyDependencies())); |
| 123 | + content.addAll(toDeclarationString(sourceSetBlock.getRuntimeOnlyDependencies())); |
| 124 | + if (!sourceSetBlock.isEmpty()) { |
| 125 | + content.add(""); |
| 126 | + } |
| 127 | + }); |
| 128 | + content.remove(content.size() - 1); |
| 129 | + content.add("}"); |
| 130 | + } |
| 131 | + |
| 132 | + Files.write(buildGradle.toPath(), content); |
| 133 | + } |
| 134 | + |
| 135 | + private List<String> toDeclarationString(ListProperty<DependencyDeclaration> dependencies) { |
| 136 | + return dependencies.get().stream().map(d -> { |
| 137 | + String group = d.fullId.split(":")[0]; |
| 138 | + String artifact = d.fullId.split(":")[1]; |
| 139 | + String feature = null; |
| 140 | + if (artifact.contains("|")) { |
| 141 | + feature = artifact.split("\\|")[1]; |
| 142 | + artifact = artifact.split("\\|")[0]; |
| 143 | + } |
| 144 | + |
| 145 | + String identifier; |
| 146 | + if (group.equals(getOwnProjectGroup().get())) { |
| 147 | + if (getWithCatalog().get()) { |
| 148 | + identifier = "projects." + artifact; |
| 149 | + } else { |
| 150 | + identifier = "project(\":" + artifact + "\")"; |
| 151 | + } |
| 152 | + } else { |
| 153 | + if (getWithCatalog().get()) { |
| 154 | + identifier = "libs." + d.moduleName; |
| 155 | + } else { |
| 156 | + identifier = "\"" + group + ":" + artifact + "\""; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (feature == null) { |
| 161 | + return " " + d.scope + "(" + identifier + ")"; |
| 162 | + } else { |
| 163 | + return " " + d.scope + "(" + identifier + ") { capabilities { requireCapabilities(\"" + group + ":" + artifact + "-" + feature + "\") } }"; |
| 164 | + } |
| 165 | + }).collect(Collectors.toList()); |
| 166 | + } |
| 167 | +} |
0 commit comments