Move tools folder into test folder

- Classes still won't be present in JAR but classes will be automatically compiled by Maven inside of the test scope, facilitating the execution of tool tasks
This commit is contained in:
ljacqu
2016-05-03 20:24:34 +02:00
parent 23317caa46
commit 3645806edc
46 changed files with 69 additions and 64 deletions
+58
View File
@@ -0,0 +1,58 @@
package tools.utils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
/**
* Utility class for reading from and writing to files.
*/
public final class FileUtils {
private final static Charset CHARSET = Charset.forName("utf-8");
private FileUtils() {
}
public static void generateFileFromTemplate(String templateFile, String destinationFile, TagValueHolder tags) {
String template = readFromFile(templateFile);
String result = TagReplacer.applyReplacements(template, tags);
writeToFile(destinationFile, result);
}
public static void writeToFile(String outputFile, String contents) {
try {
Files.write(Paths.get(outputFile), contents.getBytes());
} catch (IOException e) {
throw new RuntimeException("Failed to write to file '" + outputFile + "'", e);
}
}
public static void appendToFile(String outputFile, String contents) {
try {
Files.write(Paths.get(outputFile), contents.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
throw new RuntimeException("Failed to append to file '" + outputFile + "'", e);
}
}
public static String readFromFile(String file) {
try {
return new String(Files.readAllBytes(Paths.get(file)), CHARSET);
} catch (IOException e) {
throw new RuntimeException("Could not read from file '" + file + "'", e);
}
}
public static List<String> readLinesFromFile(String file) {
try {
return Files.readAllLines(Paths.get(file), CHARSET);
} catch (IOException e) {
throw new RuntimeException("Could not read from file '" + file + "'", e);
}
}
}
+110
View File
@@ -0,0 +1,110 @@
package tools.utils;
import tools.utils.TagValue.NestedTagValue;
import tools.utils.TagValue.TextTagValue;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Class responsible for replacing template tags to actual content.
* For all files, the following tags are defined:
* <ul>
* <li>{gen_date} the generation date</li>
* <li>{gen_warning} - warning not to edit the generated file directly</li>
* <li>{gen_footer} - info footer with a link to the dev repo so users can find the most up-to-date
* version (in case the page is viewed on a fork)</li>
* </ul>
*/
public class TagReplacer {
private TagReplacer() {
}
/**
* Replace a template with default tags and custom ones supplied by a map.
*
* @param template The template to process
* @param tagValues Container with tags and their associated values
* @return The filled template
*/
public static String applyReplacements(String template, TagValueHolder tagValues) {
String result = template;
for (Map.Entry<String, TagValue<?>> tagRule : tagValues.getValues().entrySet()) {
final String name = tagRule.getKey();
if (tagRule.getValue() instanceof TextTagValue) {
final TextTagValue value = (TextTagValue) tagRule.getValue();
result = replaceOptionalTag(result, name, value)
.replace("{" + name + "}", value.getValue());
} else if (tagRule.getValue() instanceof NestedTagValue) {
final NestedTagValue value = (NestedTagValue) tagRule.getValue();
result = replaceIterateTag(replaceOptionalTag(result, name, value), name, value);
} else {
throw new IllegalStateException("Unknown tag value type");
}
}
return applyReplacements(result);
}
/**
* Apply the default tag replacements.
*
* @param template The template to process
* @return The filled template
*/
public static String applyReplacements(String template) {
String curDate = new Date().toString();
return template
.replace("{gen_date}", curDate)
.replace("{gen_warning}", "AUTO-GENERATED FILE! Do not edit this directly")
.replace("{gen_footer}", "---\n\nThis page was automatically generated on the"
+ " [AuthMe-Team/AuthMeReloaded repository](" + ToolsConstants.DOCS_FOLDER_URL + ")"
+ " on " + curDate);
}
private static String replaceOptionalTag(String text, String tagName, TagValue<?> tagValue) {
Pattern regex = Pattern.compile("\\[" + tagName + "](.*?)\\[/" + tagName + "]", Pattern.DOTALL);
Matcher matcher = regex.matcher(text);
if (!matcher.find()) {
// Couldn't find results, so just return text as it is
return text;
} else if (tagValue.isEmpty()) {
// Tag is empty, replace [tagName]some_text[/tagName] to nothing
return matcher.replaceAll("");
} else {
// Tag is not empty, so replace [tagName]some_text[/tagName] to some_text
return matcher.replaceAll(matcher.group(1));
}
}
/**
* Replace iterating tags with the value. Tags of the type [#tag]...[/#tag] specify to iterate over the
* entries in {@link NestedTagValue} and to apply any replacements in there.
*
* @param text The file text
* @param tagName The tag name to handle
* @param tagValue The associated value
* @return The text with the applied replacement
*/
private static String replaceIterateTag(String text, String tagName, NestedTagValue tagValue) {
Pattern regex = Pattern.compile("\\[#" + tagName + "](.*?)\\[/#" + tagName + "]\\s?", Pattern.DOTALL);
Matcher matcher = regex.matcher(text);
if (!matcher.find()) {
return text;
} else if (tagValue.isEmpty()) {
return matcher.replaceAll("");
}
final String innerTemplate = matcher.group(1).trim() + "\n";
String result = "";
for (TagValueHolder entry : tagValue.getValue()) {
result += applyReplacements(innerTemplate, entry);
}
return matcher.replaceAll(result);
}
}
+46
View File
@@ -0,0 +1,46 @@
package tools.utils;
import java.util.ArrayList;
import java.util.List;
public abstract class TagValue<T> {
private final T value;
public TagValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public abstract boolean isEmpty();
public static final class TextTagValue extends TagValue<String> {
public TextTagValue(String value) {
super(value);
}
@Override
public boolean isEmpty() {
return getValue().isEmpty();
}
}
public static final class NestedTagValue extends TagValue<List<TagValueHolder>> {
public NestedTagValue() {
super(new ArrayList<TagValueHolder>());
}
@Override
public boolean isEmpty() {
return getValue().isEmpty();
}
public void add(TagValueHolder entry) {
getValue().add(entry);
}
}
}
@@ -0,0 +1,34 @@
package tools.utils;
import tools.utils.TagValue.TextTagValue;
import java.util.HashMap;
import java.util.Map;
public class TagValueHolder {
private Map<String, TagValue<?>> values;
private TagValueHolder() {
this.values = new HashMap<>();
}
public static TagValueHolder create() {
return new TagValueHolder();
}
public TagValueHolder put(String key, TagValue<?> value) {
values.put(key, value);
return this;
}
public TagValueHolder put(String key, String value) {
values.put(key, new TextTagValue(value));
return this;
}
public Map<String, TagValue<?>> getValues() {
return values;
}
}
+25
View File
@@ -0,0 +1,25 @@
package tools.utils;
import java.util.Scanner;
/**
* Common interface for tool tasks. Note that the implementing tasks are instantiated
* with the default constructor. It is required that it be public.
*/
public interface ToolTask {
/**
* Return the name of the task.
*
* @return Name of the task
*/
String getTaskName();
/**
* Execute the task.
*
* @param scanner Scanner to prompt the user with for options. Do not close it.
*/
void execute(Scanner scanner);
}
@@ -0,0 +1,21 @@
package tools.utils;
/**
* Constants for the src/tools folder.
*/
public final class ToolsConstants {
private ToolsConstants() {
}
public static final String MAIN_SOURCE_ROOT = "src/main/java/";
public static final String MAIN_RESOURCES_ROOT = "src/main/resources/";
public static final String TOOLS_SOURCE_ROOT = "src/test/java/tools/";
public static final String DOCS_FOLDER = "docs/";
public static final String DOCS_FOLDER_URL = "https://github.com/AuthMe-Team/AuthMeReloaded/tree/master/docs/";
}