Migrating to the docs.spring.io

This commit is contained in:
Marcin Grzejszczak
2020-06-04 13:11:04 +02:00
parent 9fc5902087
commit d4fefdcd77
7 changed files with 524 additions and 376 deletions

View File

@@ -1,112 +0,0 @@
package org.springframework.cloud.internal
import java.util.regex.Pattern
import groovy.json.JsonSlurper
import groovy.transform.CompileStatic
import org.springframework.core.io.Resource
import org.springframework.core.io.support.PathMatchingResourcePatternResolver
/**
* @author Marcin Grzejszczak
*/
class Main {
@CompileStatic
static void main(String... args) {
String outputFile = args[0]
String inclusionPattern = args.length > 1 ? args[1] : ".*"
File parent = new File(outputFile).parentFile
if (!parent.exists()) {
println "No parent directory [${parent.toString()}] found. Won't generate the configuration properties file"
return
}
new Generator().generate(outputFile, inclusionPattern)
}
static class Generator {
void generate(String outputFile, String inclusionPattern) {
println "Parsing all configuration metadata"
Resource[] resources = getResources()
println "Found [${resources.length}] configuration metadata jsons"
TreeSet names = new TreeSet()
def descriptions = [:]
int count = 0
int matchingPropertyCount = 0
int propertyCount = 0
Pattern pattern = Pattern.compile(inclusionPattern)
resources.each { Resource resource ->
if (resourceNameContainsPattern(resource)) {
count++
def slurper = new JsonSlurper()
slurper.parseText(resource.inputStream.text).properties.each { val ->
propertyCount++
if (!pattern.matcher(val.name).matches()) {
return
}
matchingPropertyCount++
names.add val.name
descriptions[val.name] = new ConfigValue(val.name, val.description, val.defaultValue)
}
}
}
println "Found [${count}] Cloud projects configuration metadata jsons. [${matchingPropertyCount}/${propertyCount}] were matching the pattern [${inclusionPattern}]"
println "Successfully built the description table"
if (names.empty) {
println("Will not update the table, since no configuration properties were found!")
return
}
new File(outputFile).text = """\
|===
|Name | Default | Description
${names.collect { it -> return descriptions[it] }.join("\n")}
|===
"""
println "Successfully stored the output file"
}
protected boolean resourceNameContainsPattern(Resource resource) {
try {
return resource.getURL().toString().contains("cloud")
}
catch (Exception e) {
println("Exception [${e}] for resource [${resource}] occurred while trying to retrieve its URL")
return false
}
}
protected Resource[] getResources() {
return new PathMatchingResourcePatternResolver()
.getResources("classpath*:/META-INF/spring-configuration-metadata.json")
}
}
@CompileStatic
static class ConfigValue {
String name
String description
Object defaultValue
ConfigValue() {}
ConfigValue(String name, String description, Object defaultValue) {
this.name = name
this.description = escapedValue(description)
this.defaultValue = escapedValue(defaultValue)
}
private String escapedValue(Object value) {
return value != null ?
value.toString().replaceAll('\\|', '\\\\|') : ''
}
String toString() {
"|${name} | ${defaultValue} | ${description}"
}
}
}

View File

@@ -0,0 +1,151 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.internal;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.StreamUtils;
/**
* @author Marcin Grzejszczak
*/
public class Main {
public static void main(String... args) {
String outputFile = args[0];
String inclusionPattern = args.length > 1 ? args[1] : ".*";
File parent = new File(outputFile).getParentFile();
if (!parent.exists()) {
System.out.println("No parent directory [" + parent.toString()
+ "] found. Will not generate the configuration properties file");
return;
}
new Generator().generate(outputFile, inclusionPattern);
}
static class Generator {
void generate(String outputFile, String inclusionPattern) {
try {
System.out.println("Parsing all configuration metadata");
Resource[] resources = getResources();
System.out.println("Found [" + resources.length + "] configuration metadata jsons");
TreeSet<String> names = new TreeSet<>();
Map<String, ConfigValue> descriptions = new HashMap<>();
final AtomicInteger count = new AtomicInteger();
final AtomicInteger matchingPropertyCount = new AtomicInteger();
final AtomicInteger propertyCount = new AtomicInteger();
Pattern pattern = Pattern.compile(inclusionPattern);
for (Resource resource : resources) {
if (resourceNameContainsPattern(resource)) {
count.incrementAndGet();
byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream());
Map<String, Object> response = new ObjectMapper().readValue(bytes, HashMap.class);
List<Map<String, Object>> properties = (List<Map<String, Object>>) response.get("properties");
properties.forEach(val -> {
propertyCount.incrementAndGet();
String name = String.valueOf(val.get("name"));
if (!pattern.matcher(name).matches()) {
return;
}
String description = String.valueOf(val.get("description"));
Object defaultValue = val.get("defaultValue");
matchingPropertyCount.incrementAndGet();
names.add(name);
descriptions.put(name, new ConfigValue(name, description, defaultValue));
});
}
}
System.out.println(
"Found [" + count + "] Cloud projects configuration metadata jsons. [" + matchingPropertyCount
+ "/" + propertyCount + "] were matching the pattern [" + inclusionPattern + "]");
System.out.println("Successfully built the description table");
if (names.isEmpty()) {
System.out.println("Will not update the table, since no configuration properties were found!");
return;
}
Files.write(new File(outputFile).toPath(),
("|===\n"
+ "|Name | Default | Description\n\n" + names.stream()
.map(it -> descriptions.get(it).toString()).collect(Collectors.joining("\n"))
+ "\n\n" + "|===").getBytes());
System.out.println("Successfully stored the output file");
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
protected boolean resourceNameContainsPattern(Resource resource) {
try {
return resource.getURL().toString().contains("cloud");
}
catch (Exception e) {
System.out.println("Exception [" + e + "] for resource [" + resource
+ "] occurred while trying to retrieve its URL");
return false;
}
}
protected Resource[] getResources() throws IOException {
return new PathMatchingResourcePatternResolver()
.getResources("classpath*:/META-INF/spring-configuration-metadata.json");
}
}
static class ConfigValue {
public String name;
public String description;
public Object defaultValue;
ConfigValue() {
}
ConfigValue(String name, String description, Object defaultValue) {
this.name = name;
this.description = escapedValue(description);
this.defaultValue = escapedValue(defaultValue);
}
private String escapedValue(Object value) {
return value != null ? value.toString().replaceAll("\\|", "\\\\|") : "";
}
public String toString() {
return "|" + name + " | " + defaultValue + " | " + description;
}
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.internal.asciidoctor;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Preprocessor;
import org.asciidoctor.extension.PreprocessorReader;
// taken from https://github.com/hibernate/hibernate-asciidoctor-extensions/blob/1.0.3.Final/src/main/java/org/hibernate/infra/asciidoctor/extensions/savepreprocessed/SavePreprocessedOutputPreprocessor.java
/**
* Preprocessor used to save the preprocessed output of the asciidoctor conversion. It allows to generate a single file
* integrating all the includes.
*
* @author Guillaume Smet
*/
class CoalescerPreprocessor extends Preprocessor {
private final File outputFile;
private static final List<String> FILTER_LICENSE_MARKERS = Arrays.asList("[preface]", "<<<");
private static final String COMMENT_MARKER = "//";
private static final String SOURCE_MARKER = "[source";
private static final List<String> SECTION_MARKERS = Arrays.asList("----", "....");
private static final String HEADER = "////\n" + "DO NOT EDIT THIS FILE. IT WAS GENERATED.\n"
+ "Manual changes to this file will be lost when it is generated again.\n"
+ "Edit the files in the src/main/asciidoc/ directory instead.\n" + "////\n\n";
CoalescerPreprocessor(File output_file) {
outputFile = output_file;
}
@Override
public void process(Document document, PreprocessorReader preprocessorReader) {
try {
LinkedList<String> filteredLines = filterLines(preprocessorReader.readLines());
filteredLines.addFirst(HEADER);
Files.write(outputFile.toPath(), filteredLines);
}
catch (IOException e) {
throw new IllegalStateException("Unable to write the preprocessed file " + outputFile, e);
}
}
/**
* This is used to filter out the license headers that are just after the markers
* defined. It also reindents the source code with spaces to be consistent with the
* 1.1 spec.
*/
private LinkedList<String> filterLines(List<String> lines) {
LinkedList<String> filteredLines = new LinkedList<>();
ParsingState state = ParsingState.NORMAL;
int counter = 0;
for (String line : lines) {
counter++;
switch (state) {
case NORMAL:
if (FILTER_LICENSE_MARKERS.contains(line)) {
state = ParsingState.FILTER_LICENSE;
}
else if (line.startsWith(SOURCE_MARKER)) {
state = ParsingState.SOURCE;
}
break;
case SOURCE:
if (SECTION_MARKERS.contains(line)) {
state = ParsingState.SOURCE_CONTENT;
}
else {
System.err.println("[source] requires to be followed by a section marker and line number [" + counter + "] with content [" + line + "] doesn't have it");
}
break;
case SOURCE_CONTENT:
if (SECTION_MARKERS.contains(line)) {
state = ParsingState.NORMAL;
}
else {
filteredLines.add(line.replaceAll("\t", " "));
continue;
}
break;
case FILTER_LICENSE:
if (line.startsWith(COMMENT_MARKER)) {
continue;
}
else {
state = ParsingState.NORMAL;
}
break;
}
filteredLines.add(line);
}
return filteredLines;
}
private enum ParsingState {
NORMAL, FILTER_LICENSE, SOURCE, SOURCE_CONTENT,
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.internal.asciidoctor;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.asciidoctor.SafeMode;
public class ReadmeMain {
public static void main(String... args) {
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);
System.out.println("Will do the Readme conversion from [" + inputFile + "] to [" + outputFile + "]");
new ReadmeMain().convert(inputFile, outputFile);
}
void convert(File input, File output) {
Asciidoctor asciidoctor = Asciidoctor.Factory.create();
asciidoctor.javaExtensionRegistry().preprocessor(new CoalescerPreprocessor(output));
Options options = options(input, output);
try {
String fileAsString = new String(Files.readAllBytes(input.toPath()));
asciidoctor.convert(fileAsString, options);
System.out.println("Successfully converted the Readme file!\n");
} catch (IOException ex) {
throw new IllegalStateException("Failed to convert the file", ex);
}
}
private Options options(File input, File output) {
Attributes attributes = new Attributes();
attributes.setAllowUriRead(true);
attributes.setAttribute("project-root", output.getParent());
Options options = new Options();
options.setSourceDir(input.getParent());
options.setBaseDir(input.getParent());
options.setAttributes(attributes);
options.setSafe(SafeMode.UNSAFE);
options.setParseHeaderOnly(true);
return options;
}
}