|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * <p> |
| 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 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 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 org.openrewrite.java.spring; |
| 17 | + |
| 18 | +import org.jspecify.annotations.Nullable; |
| 19 | +import org.openrewrite.*; |
| 20 | +import org.openrewrite.internal.StringUtils; |
| 21 | +import org.openrewrite.properties.AddProperty; |
| 22 | +import org.openrewrite.properties.tree.Properties; |
| 23 | +import org.openrewrite.yaml.MergeYaml; |
| 24 | +import org.openrewrite.yaml.tree.Yaml; |
| 25 | + |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.regex.Pattern; |
| 30 | + |
| 31 | +/** |
| 32 | + * A recipe to uniformly add a property to Spring configuration file. This recipe supports adding properties to |
| 33 | + * ".properties" and YAML files. This recipe will only add the property if it does not already exist within the |
| 34 | + * configuration file. |
| 35 | + * <P> |
| 36 | + * NOTE: Because an application may have a large collection of yaml files (some of which may not even be related to |
| 37 | + * Spring configuration), this recipe will only make changes to files that match one of the pathExpressions. If |
| 38 | + * the recipe is configured without pathExpressions, it will query the execution context for reasonable defaults. |
| 39 | + */ |
| 40 | +public class AddSpringProperty extends Recipe { |
| 41 | + |
| 42 | + @Option(displayName = "Property key", |
| 43 | + description = "The property key to add.", |
| 44 | + example = "management.metrics.enable.process.files") |
| 45 | + String property; |
| 46 | + |
| 47 | + @Option(displayName = "Property value", |
| 48 | + description = "The value of the new property key.", |
| 49 | + example = "true") |
| 50 | + String value; |
| 51 | + |
| 52 | + @Option(displayName = "Optional comment to be prepended to the property", |
| 53 | + description = "A comment that will be added to the new property.", |
| 54 | + required = false, |
| 55 | + example = "This is a comment") |
| 56 | + @Nullable |
| 57 | + String comment; |
| 58 | + |
| 59 | + @Option(displayName = "Optional list of file path matcher", |
| 60 | + description = "Each value in this list represents a glob expression that is used to match which files will " + |
| 61 | + "be modified. If this value is not present, this recipe will query the execution context for " + |
| 62 | + "reasonable defaults. (\"**/application.yml\", \"**/application.yml\", and \"**/application.properties\".", |
| 63 | + required = false, |
| 64 | + example = "[\"**/application.yml\"]") |
| 65 | + @Nullable |
| 66 | + List<String> pathExpressions; |
| 67 | + |
| 68 | + |
| 69 | + public AddSpringProperty(String property, String value, @Nullable String comment, |
| 70 | + @Nullable List<String> pathExpressions) { |
| 71 | + this.property = property; |
| 72 | + this.value = value; |
| 73 | + this.comment = comment; |
| 74 | + this.pathExpressions = pathExpressions; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String getDisplayName() { |
| 79 | + return "Add a spring configuration property"; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String getDescription() { |
| 84 | + return "Add a spring configuration property to a configuration file if it does not already exist in that file."; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 89 | + return new TreeVisitor<Tree, ExecutionContext>() { |
| 90 | + @Override |
| 91 | + public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { |
| 92 | + return sourceFile instanceof Yaml.Documents || sourceFile instanceof Properties.File; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public @Nullable Tree visit(@Nullable Tree t, ExecutionContext ctx) { |
| 97 | + if (t instanceof Yaml.Documents && sourcePathMatches(((SourceFile) t).getSourcePath(), ctx)) { |
| 98 | + t = createMergeYamlVisitor().getVisitor().visit(t, ctx); |
| 99 | + } else if (t instanceof Properties.File && sourcePathMatches(((SourceFile) t).getSourcePath(), ctx)) { |
| 100 | + t = new AddProperty(property, value, comment, null, null).getVisitor().visit(t, ctx); |
| 101 | + } |
| 102 | + return t; |
| 103 | + } |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + private boolean sourcePathMatches(Path sourcePath, ExecutionContext ctx) { |
| 108 | + List<String> expressions = pathExpressions; |
| 109 | + if (expressions == null || pathExpressions.isEmpty()) { |
| 110 | + //If not defined, get reasonable defaults from the execution context. |
| 111 | + expressions = SpringExecutionContextView.view(ctx).getDefaultApplicationConfigurationPaths(); |
| 112 | + } |
| 113 | + if (expressions.isEmpty()) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + for (String filePattern : expressions) { |
| 117 | + if (PathUtils.matchesGlob(sourcePath, filePattern)) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + private MergeYaml createMergeYamlVisitor() { |
| 126 | + String[] propertyParts = property.split("\\."); |
| 127 | + |
| 128 | + StringBuilder yaml = new StringBuilder(); |
| 129 | + |
| 130 | + String indent = ""; |
| 131 | + for (String part : propertyParts) { |
| 132 | + if (yaml.length() > 0) { |
| 133 | + yaml.append("\n"); |
| 134 | + } |
| 135 | + if (!StringUtils.isBlank(comment)) { |
| 136 | + //noinspection StringEquality |
| 137 | + if (part == propertyParts[propertyParts.length - 1]) { |
| 138 | + yaml.append(indent).append("# ").append(comment).append("\n"); |
| 139 | + } |
| 140 | + } |
| 141 | + yaml.append(indent).append(part).append(":"); |
| 142 | + indent = indent + " "; |
| 143 | + } |
| 144 | + if (quoteValue(value)) { |
| 145 | + yaml.append(" \"").append(value).append('"'); |
| 146 | + } else { |
| 147 | + yaml.append(" ").append(value); |
| 148 | + } |
| 149 | + return new MergeYaml("$", yaml.toString(), true, null, null, null, null, null); |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + public String getProperty() { |
| 155 | + return property; |
| 156 | + } |
| 157 | + |
| 158 | + public String getValue() { |
| 159 | + return value; |
| 160 | + } |
| 161 | + |
| 162 | + public String getComment() { |
| 163 | + return comment; |
| 164 | + } |
| 165 | + |
| 166 | + public List<String> getPathExpressions() { |
| 167 | + return pathExpressions; |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + @Override |
| 173 | + public int hashCode() { |
| 174 | + final int prime = 31; |
| 175 | + int result = super.hashCode(); |
| 176 | + result = prime * result + Objects.hash(comment, pathExpressions, property, value); |
| 177 | + return result; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean equals(Object obj) { |
| 182 | + if (this == obj) |
| 183 | + return true; |
| 184 | + if (!super.equals(obj)) |
| 185 | + return false; |
| 186 | + if (getClass() != obj.getClass()) |
| 187 | + return false; |
| 188 | + AddSpringProperty other = (AddSpringProperty) obj; |
| 189 | + return Objects.equals(comment, other.comment) && Objects.equals(pathExpressions, other.pathExpressions) |
| 190 | + && Objects.equals(property, other.property) && Objects.equals(value, other.value); |
| 191 | + } |
| 192 | + |
| 193 | + private static final Pattern scalarNeedsAQuote = Pattern.compile("[^a-zA-Z\\d\\s]*"); |
| 194 | + private boolean quoteValue(String value) { |
| 195 | + return scalarNeedsAQuote.matcher(value).matches(); |
| 196 | + } |
| 197 | +} |
0 commit comments