Minor improvements in tool tasks

- Use java 8 types in UpdateDocsTask
- Simplify permissions page writing task to write to the file without any options
- Various minor simplifications (Java 8) to avoid casting etc.
This commit is contained in:
ljacqu 2016-10-08 00:38:51 +02:00
parent a2732da0c3
commit dd9312f581
5 changed files with 36 additions and 93 deletions

View File

@ -9,14 +9,15 @@ import tools.utils.ToolTask;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
/** /**
* Task that runs all tasks which update files in the docs folder. * Task that runs all tasks which update files in the docs folder.
*/ */
public class UpdateDocsTask implements AutoToolTask { public class UpdateDocsTask implements AutoToolTask {
private static final Set<Class<? extends ToolTask>> TASKS = ImmutableSet.<Class<? extends ToolTask>>of( private static final Set<Class<? extends ToolTask>> TASKS = ImmutableSet
CommandPageCreater.class, HashAlgorithmsDescriptionTask.class, PermissionsListWriter.class); .of(CommandPageCreater.class, HashAlgorithmsDescriptionTask.class, PermissionsListWriter.class);
@Override @Override
public String getTaskName() { public String getTaskName() {
@ -25,22 +26,14 @@ public class UpdateDocsTask implements AutoToolTask {
@Override @Override
public void execute(final Scanner scanner) { public void execute(final Scanner scanner) {
executeTasks(new TaskRunner() { executeTasks(task -> task.execute(scanner));
@Override
public void execute(ToolTask task) {
task.execute(scanner);
}
});
} }
@Override @Override
public void executeDefault() { public void executeDefault() {
executeTasks(new TaskRunner() { executeTasks(task -> {
@Override if (task instanceof AutoToolTask) {
public void execute(ToolTask task) { ((AutoToolTask) task).executeDefault();
if (task instanceof AutoToolTask) {
((AutoToolTask) task).executeDefault();
}
} }
}); });
} }
@ -49,24 +42,15 @@ public class UpdateDocsTask implements AutoToolTask {
try { try {
return clazz.newInstance(); return clazz.newInstance();
} catch (IllegalAccessException | InstantiationException e) { } catch (IllegalAccessException | InstantiationException e) {
throw new UnsupportedOperationException(e); throw new UnsupportedOperationException("Could not instantiate task class '" + clazz + "'", e);
} }
} }
private static void executeTasks(TaskRunner runner) { private static void executeTasks(Consumer<ToolTask> taskRunner) {
for (Class<? extends ToolTask> taskClass : TASKS) { for (Class<? extends ToolTask> taskClass : TASKS) {
try { ToolTask task = instantiateTask(taskClass);
ToolTask task = instantiateTask(taskClass); System.out.println("\nRunning " + task.getTaskName() + "\n-------------------");
System.out.println("\nRunning " + task.getTaskName() + "\n-------------------"); taskRunner.accept(task);
runner.execute(task);
} catch (UnsupportedOperationException e) {
System.err.println("Error running task of class '" + taskClass + "'");
e.printStackTrace();
}
} }
} }
private interface TaskRunner {
void execute(ToolTask task);
}
} }

View File

@ -3,6 +3,7 @@ package tools.hashmethods;
import ch.jalu.injector.Injector; import ch.jalu.injector.Injector;
import ch.jalu.injector.InjectorBuilder; import ch.jalu.injector.InjectorBuilder;
import com.github.authme.configme.properties.Property; import com.github.authme.configme.properties.Property;
import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.crypts.EncryptionMethod; import fr.xephi.authme.security.crypts.EncryptionMethod;
import fr.xephi.authme.security.crypts.HexSaltedMethod; import fr.xephi.authme.security.crypts.HexSaltedMethod;
@ -19,7 +20,6 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static com.google.common.collect.Sets.newHashSet;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -30,9 +30,8 @@ import static org.mockito.Mockito.when;
*/ */
public class EncryptionMethodInfoGatherer { public class EncryptionMethodInfoGatherer {
@SuppressWarnings("unchecked")
private final static Set<Class<? extends Annotation>> RELEVANT_ANNOTATIONS = private final static Set<Class<? extends Annotation>> RELEVANT_ANNOTATIONS =
newHashSet(HasSalt.class, Recommendation.class, AsciiRestricted.class); ImmutableSet.of(HasSalt.class, Recommendation.class, AsciiRestricted.class);
private static Injector injector = createInitializer(); private static Injector injector = createInitializer();

View File

@ -1,18 +1,14 @@
package tools.permissions; package tools.permissions;
import fr.xephi.authme.permission.AdminPermission; import fr.xephi.authme.ClassCollector;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.permission.PlayerStatePermission;
import tools.utils.FileUtils; import tools.utils.FileUtils;
import tools.utils.ToolsConstants; import tools.utils.ToolsConstants;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -31,36 +27,22 @@ public class PermissionNodesGatherer {
+ "(.*?)\\s+\\*/" // Capture everything until we encounter '*/' + "(.*?)\\s+\\*/" // Capture everything until we encounter '*/'
+ "\\s+([A-Z_]+)\\("); // Match the enum name (e.g. 'LOGIN'), until before the first '(' + "\\s+([A-Z_]+)\\("); // Match the enum name (e.g. 'LOGIN'), until before the first '('
/**
* Return a sorted collection of all permission nodes.
*
* @return AuthMe permission nodes sorted alphabetically
*/
public Set<String> gatherNodes() {
Set<String> nodes = new TreeSet<>();
for (PermissionNode perm : PlayerPermission.values()) {
nodes.add(perm.getNode());
}
for (PermissionNode perm : AdminPermission.values()) {
nodes.add(perm.getNode());
}
return nodes;
}
/** /**
* Return a sorted collection of all permission nodes, including its JavaDoc description. * Return a sorted collection of all permission nodes, including its JavaDoc description.
* *
* @return Ordered map whose keys are the permission nodes and the values the associated JavaDoc * @return Ordered map whose keys are the permission nodes and the values the associated JavaDoc
*/ */
public Map<String, String> gatherNodesWithJavaDoc() { public <T extends Enum<T> & PermissionNode> Map<String, String> gatherNodesWithJavaDoc() {
Map<String, String> result = new TreeMap<>(); Map<String, String> result = new TreeMap<>();
result.put("authme.admin.*", "Give access to all admin commands."); result.put("authme.admin.*", "Give access to all admin commands.");
result.put("authme.player.*", "Permission to use all player (non-admin) commands."); result.put("authme.player.*", "Permission to use all player (non-admin) commands.");
// TODO ljacqu 20160109: Add authme.player.email manual description? result.put("authme.player.email", "Grants all email permissions.");
addDescriptionsForClass(PlayerPermission.class, result); new ClassCollector(ToolsConstants.MAIN_SOURCE_ROOT, "")
addDescriptionsForClass(AdminPermission.class, result); .collectClasses(PermissionNode.class)
addDescriptionsForClass(PlayerStatePermission.class, result); .stream()
.filter(Class::isEnum)
.forEach(clz -> addDescriptionsForClass((Class<T>) clz, result));
return result; return result;
} }

View File

@ -8,7 +8,6 @@ import tools.utils.ToolsConstants;
import java.util.Map; import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set;
/** /**
* Task responsible for formatting a permissions node list and * Task responsible for formatting a permissions node list and
@ -25,23 +24,7 @@ public class PermissionsListWriter implements AutoToolTask {
@Override @Override
public void execute(Scanner scanner) { public void execute(Scanner scanner) {
// Ask if result should be written to file generateAndWriteFile();
System.out.println("Include description? [Enter 'n' for no]");
boolean includeDescription = !matches("n", scanner);
boolean writeToFile = false;
if (includeDescription) {
System.out.println("Write to file? [Enter 'n' for no]");
writeToFile = !matches("n", scanner);
}
if (!includeDescription) {
outputSimpleList();
} else if (writeToFile) {
generateAndWriteFile();
} else {
System.out.println(generatePermissionsList());
}
} }
@Override @Override
@ -70,20 +53,4 @@ public class PermissionsListWriter implements AutoToolTask {
} }
return permissionTags; return permissionTags;
} }
private static void outputSimpleList() {
PermissionNodesGatherer gatherer = new PermissionNodesGatherer();
Set<String> nodes = gatherer.gatherNodes();
for (String node : nodes) {
System.out.println(node);
}
System.out.println();
System.out.println("Total: " + nodes.size());
}
private static boolean matches(String answer, Scanner sc) {
String userInput = sc.nextLine();
return answer.equalsIgnoreCase(userInput);
}
} }

View File

@ -3,7 +3,12 @@ package tools.utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* Value of a tag.
*
* @param <T> the tag value type
* @see TagReplacer
*/
public abstract class TagValue<T> { public abstract class TagValue<T> {
private final T value; private final T value;
@ -18,6 +23,9 @@ public abstract class TagValue<T> {
public abstract boolean isEmpty(); public abstract boolean isEmpty();
/**
* Text value.
*/
public static final class TextTagValue extends TagValue<String> { public static final class TextTagValue extends TagValue<String> {
public TextTagValue(String value) { public TextTagValue(String value) {
super(value); super(value);
@ -29,9 +37,12 @@ public abstract class TagValue<T> {
} }
} }
/**
* List value (iterable).
*/
public static final class NestedTagValue extends TagValue<List<TagValueHolder>> { public static final class NestedTagValue extends TagValue<List<TagValueHolder>> {
public NestedTagValue() { public NestedTagValue() {
super(new ArrayList<TagValueHolder>()); super(new ArrayList<>());
} }
@Override @Override