mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-08 03:29:41 +01:00
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:
parent
a2732da0c3
commit
dd9312f581
@ -9,14 +9,15 @@ import tools.utils.ToolTask;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Task that runs all tasks which update files in the docs folder.
|
||||
*/
|
||||
public class UpdateDocsTask implements AutoToolTask {
|
||||
|
||||
private static final Set<Class<? extends ToolTask>> TASKS = ImmutableSet.<Class<? extends ToolTask>>of(
|
||||
CommandPageCreater.class, HashAlgorithmsDescriptionTask.class, PermissionsListWriter.class);
|
||||
private static final Set<Class<? extends ToolTask>> TASKS = ImmutableSet
|
||||
.of(CommandPageCreater.class, HashAlgorithmsDescriptionTask.class, PermissionsListWriter.class);
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
@ -25,22 +26,14 @@ public class UpdateDocsTask implements AutoToolTask {
|
||||
|
||||
@Override
|
||||
public void execute(final Scanner scanner) {
|
||||
executeTasks(new TaskRunner() {
|
||||
@Override
|
||||
public void execute(ToolTask task) {
|
||||
task.execute(scanner);
|
||||
}
|
||||
});
|
||||
executeTasks(task -> task.execute(scanner));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeDefault() {
|
||||
executeTasks(new TaskRunner() {
|
||||
@Override
|
||||
public void execute(ToolTask task) {
|
||||
if (task instanceof AutoToolTask) {
|
||||
((AutoToolTask) task).executeDefault();
|
||||
}
|
||||
executeTasks(task -> {
|
||||
if (task instanceof AutoToolTask) {
|
||||
((AutoToolTask) task).executeDefault();
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -49,24 +42,15 @@ public class UpdateDocsTask implements AutoToolTask {
|
||||
try {
|
||||
return clazz.newInstance();
|
||||
} 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) {
|
||||
try {
|
||||
ToolTask task = instantiateTask(taskClass);
|
||||
System.out.println("\nRunning " + task.getTaskName() + "\n-------------------");
|
||||
runner.execute(task);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
System.err.println("Error running task of class '" + taskClass + "'");
|
||||
e.printStackTrace();
|
||||
}
|
||||
ToolTask task = instantiateTask(taskClass);
|
||||
System.out.println("\nRunning " + task.getTaskName() + "\n-------------------");
|
||||
taskRunner.accept(task);
|
||||
}
|
||||
}
|
||||
|
||||
private interface TaskRunner {
|
||||
void execute(ToolTask task);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package tools.hashmethods;
|
||||
import ch.jalu.injector.Injector;
|
||||
import ch.jalu.injector.InjectorBuilder;
|
||||
import com.github.authme.configme.properties.Property;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import fr.xephi.authme.security.HashAlgorithm;
|
||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||
import fr.xephi.authme.security.crypts.HexSaltedMethod;
|
||||
@ -19,7 +20,6 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.google.common.collect.Sets.newHashSet;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@ -30,9 +30,8 @@ import static org.mockito.Mockito.when;
|
||||
*/
|
||||
public class EncryptionMethodInfoGatherer {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
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();
|
||||
|
||||
|
@ -1,18 +1,14 @@
|
||||
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.PlayerPermission;
|
||||
import fr.xephi.authme.permission.PlayerStatePermission;
|
||||
import tools.utils.FileUtils;
|
||||
import tools.utils.ToolsConstants;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -31,36 +27,22 @@ public class PermissionNodesGatherer {
|
||||
+ "(.*?)\\s+\\*/" // Capture everything until we encounter '*/'
|
||||
+ "\\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 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<>();
|
||||
result.put("authme.admin.*", "Give access to all 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);
|
||||
addDescriptionsForClass(AdminPermission.class, result);
|
||||
addDescriptionsForClass(PlayerStatePermission.class, result);
|
||||
new ClassCollector(ToolsConstants.MAIN_SOURCE_ROOT, "")
|
||||
.collectClasses(PermissionNode.class)
|
||||
.stream()
|
||||
.filter(Class::isEnum)
|
||||
.forEach(clz -> addDescriptionsForClass((Class<T>) clz, result));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,6 @@ import tools.utils.ToolsConstants;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Task responsible for formatting a permissions node list and
|
||||
@ -25,23 +24,7 @@ public class PermissionsListWriter implements AutoToolTask {
|
||||
|
||||
@Override
|
||||
public void execute(Scanner scanner) {
|
||||
// Ask if result should be written to file
|
||||
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());
|
||||
}
|
||||
generateAndWriteFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -70,20 +53,4 @@ public class PermissionsListWriter implements AutoToolTask {
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,12 @@ package tools.utils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Value of a tag.
|
||||
*
|
||||
* @param <T> the tag value type
|
||||
* @see TagReplacer
|
||||
*/
|
||||
public abstract class TagValue<T> {
|
||||
|
||||
private final T value;
|
||||
@ -18,6 +23,9 @@ public abstract class TagValue<T> {
|
||||
|
||||
public abstract boolean isEmpty();
|
||||
|
||||
/**
|
||||
* Text value.
|
||||
*/
|
||||
public static final class TextTagValue extends TagValue<String> {
|
||||
public TextTagValue(String 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 NestedTagValue() {
|
||||
super(new ArrayList<TagValueHolder>());
|
||||
super(new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user