|
| 1 | +/* |
| 2 | + * Copyright 2012-present the original author or authors. |
| 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 | + |
| 17 | +package org.springframework.grpc.internal; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.TreeSet; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | +import java.util.regex.Pattern; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 31 | + |
| 32 | +import org.springframework.core.io.Resource; |
| 33 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 34 | +import org.springframework.util.StreamUtils; |
| 35 | +import org.springframework.util.StringUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * Generate Asciidoc for configuration properties. |
| 39 | + * <p> |
| 40 | + * Copied from 'spring-cloud-build' to avoid direct dependency on Spring Cloud. |
| 41 | + * |
| 42 | + * @author Marcin Grzejszczak |
| 43 | + * @author Chris Bono |
| 44 | + */ |
| 45 | +public class ConfigurationPropertiesAsciidocGenerator { |
| 46 | + |
| 47 | + public static void main(String... args) { |
| 48 | + String outputFile = args[0]; |
| 49 | + String inclusionPattern = args.length > 1 ? args[1] : ".*"; |
| 50 | + File parent = new File(outputFile).getParentFile(); |
| 51 | + if (!parent.exists()) { |
| 52 | + System.out.println("No parent directory [" + parent.toString() |
| 53 | + + "] found. Will not generate the configuration properties file"); |
| 54 | + return; |
| 55 | + } |
| 56 | + new Generator().generate(outputFile, inclusionPattern); |
| 57 | + } |
| 58 | + |
| 59 | + static class Generator { |
| 60 | + |
| 61 | + void generate(String outputFile, String inclusionPattern) { |
| 62 | + try { |
| 63 | + System.out.println("Parsing all configuration metadata"); |
| 64 | + Resource[] resources = getResources(); |
| 65 | + System.out.println("Found [" + resources.length + "] configuration metadata jsons"); |
| 66 | + TreeSet<String> names = new TreeSet<>(); |
| 67 | + Map<String, ConfigValue> descriptions = new HashMap<>(); |
| 68 | + final AtomicInteger count = new AtomicInteger(); |
| 69 | + final AtomicInteger matchingPropertyCount = new AtomicInteger(); |
| 70 | + final AtomicInteger propertyCount = new AtomicInteger(); |
| 71 | + Pattern pattern = Pattern.compile(inclusionPattern); |
| 72 | + for (Resource resource : resources) { |
| 73 | + if (resourceNameContainsPattern(resource)) { |
| 74 | + count.incrementAndGet(); |
| 75 | + byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream()); |
| 76 | + Map<String, Object> response = new ObjectMapper().readValue(bytes, HashMap.class); |
| 77 | + List<Map<String, Object>> properties = (List<Map<String, Object>>) response.get("properties"); |
| 78 | + properties.forEach(val -> { |
| 79 | + propertyCount.incrementAndGet(); |
| 80 | + String name = String.valueOf(val.get("name")); |
| 81 | + if (!pattern.matcher(name).matches()) { |
| 82 | + return; |
| 83 | + } |
| 84 | + Object description = val.get("description"); |
| 85 | + Object defaultValue = val.get("defaultValue"); |
| 86 | + matchingPropertyCount.incrementAndGet(); |
| 87 | + names.add(name); |
| 88 | + descriptions.put(name, new ConfigValue(name, description, defaultValue)); |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + System.out.println( |
| 93 | + "Found [" + count + "] Spring projects configuration metadata jsons. [" + matchingPropertyCount |
| 94 | + + "/" + propertyCount + "] were matching the pattern [" + inclusionPattern + "]"); |
| 95 | + System.out.println("Successfully built the description table"); |
| 96 | + if (names.isEmpty()) { |
| 97 | + System.out.println("Will not update the table, since no configuration properties were found!"); |
| 98 | + return; |
| 99 | + } |
| 100 | + Files.write(new File(outputFile).toPath(), ("|===\n" + "|Name | Default | Description\n\n" |
| 101 | + + names.stream().map(it -> descriptions.get(it).toString()).collect(Collectors.joining("\n")) |
| 102 | + + "\n\n" + "|===") |
| 103 | + .getBytes()); |
| 104 | + System.out.println("Successfully stored the output file"); |
| 105 | + } |
| 106 | + catch (IOException e) { |
| 107 | + throw new IllegalStateException(e); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + protected boolean resourceNameContainsPattern(Resource resource) { |
| 112 | + try { |
| 113 | + return resource.getURL().toString().contains("spring"); |
| 114 | + } |
| 115 | + catch (Exception e) { |
| 116 | + System.out.println("Exception [" + e + "] for resource [" + resource |
| 117 | + + "] occurred while trying to retrieve its URL"); |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + protected Resource[] getResources() throws IOException { |
| 123 | + return new PathMatchingResourcePatternResolver() |
| 124 | + .getResources("classpath*:/META-INF/spring-configuration-metadata.json"); |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + static class ConfigValue { |
| 130 | + |
| 131 | + public String name; |
| 132 | + |
| 133 | + public String description; |
| 134 | + |
| 135 | + public String defaultValue; |
| 136 | + |
| 137 | + ConfigValue() { |
| 138 | + } |
| 139 | + |
| 140 | + ConfigValue(String name, Object description, Object defaultValue) { |
| 141 | + this.name = name; |
| 142 | + this.description = escapedValue(description); |
| 143 | + this.defaultValue = escapedValue(defaultValue); |
| 144 | + } |
| 145 | + |
| 146 | + private String escapedValue(Object value) { |
| 147 | + return value != null ? value.toString().replaceAll("\\|", "\\\\|") : ""; |
| 148 | + } |
| 149 | + |
| 150 | + public String toString() { |
| 151 | + return "|" + name + " | " + (StringUtils.hasText(defaultValue) ? ("`+++" + defaultValue + "+++`") : "") |
| 152 | + + " | " + description; |
| 153 | + } |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments