mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-20 15:47:38 +01:00
Remove unused import/export messages tool tasks
This commit is contained in:
parent
5b6d0f5d97
commit
e8692160c5
@ -1,58 +0,0 @@
|
||||
package tools.messages.translation;
|
||||
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Extension of {@link YamlConfiguration} to customize the writing style.
|
||||
*/
|
||||
public class AuthMeYamlConfiguration extends YamlConfiguration {
|
||||
|
||||
// Differences to YamlConfiguration: Texts are always in single quotes
|
||||
// and line breaks are only applied after 200 chars
|
||||
@Override
|
||||
public String saveToString() {
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
|
||||
options.setPrettyFlow(true);
|
||||
options.setWidth(200);
|
||||
Yaml yaml = new Yaml(options);
|
||||
|
||||
String header = buildHeader();
|
||||
String dump = yaml.dump(getValues(false));
|
||||
|
||||
if (dump.equals(BLANK_CONFIG)) {
|
||||
dump = "";
|
||||
}
|
||||
// By setting the scalar style to SINGLE_QUOTED both keys and values will be enclosed in single quotes.
|
||||
// We want all texts wrapped in single quotes, but not the keys. Seems like this is not possible in SnakeYAML
|
||||
dump = Pattern.compile("^'([a-zA-Z0-9-_]+)': ", Pattern.MULTILINE)
|
||||
.matcher(dump).replaceAll("$1: ");
|
||||
|
||||
return header + dump;
|
||||
}
|
||||
|
||||
/**
|
||||
* Behaves similarly to {@link YamlConfiguration#loadConfiguration(File)} but returns an object
|
||||
* of this class instead.
|
||||
*
|
||||
* @param file the file to load
|
||||
* @return the constructed AuthMeYamlConfiguration instance
|
||||
*/
|
||||
public static AuthMeYamlConfiguration loadConfiguration(File file) {
|
||||
AuthMeYamlConfiguration config = new AuthMeYamlConfiguration();
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (IOException | InvalidConfigurationException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
@ -1,106 +0,0 @@
|
||||
package tools.messages.translation;
|
||||
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.gson.Gson;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import tools.utils.ToolTask;
|
||||
import tools.utils.ToolsConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Task to export a language's messages to the remote translation service.
|
||||
*/
|
||||
public class ExportMessagesTask implements ToolTask {
|
||||
|
||||
/** The folder containing the messages files. */
|
||||
protected static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||
/** The remote URL to send an updated file to. */
|
||||
private static final String UPDATE_URL = "http://jalu.ch/ext/authme/update.php";
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
return "exportMessages";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Scanner scanner) {
|
||||
System.out.println("Enter language code of messages to export:");
|
||||
String languageCode = scanner.nextLine().trim();
|
||||
|
||||
File file = new File(MESSAGES_FOLDER + "messages_" + languageCode + ".yml");
|
||||
if (!file.exists()) {
|
||||
throw new IllegalStateException("File '" + file.getAbsolutePath() + "' does not exist");
|
||||
}
|
||||
|
||||
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
|
||||
String json = convertToJson(languageCode, loadDefaultMessages(), configuration);
|
||||
|
||||
String result = sendJsonToRemote(languageCode, json);
|
||||
System.out.println("Answer: " + result);
|
||||
}
|
||||
|
||||
protected String convertToJson(String code, FileConfiguration defaultMessages, FileConfiguration messageFile) {
|
||||
List<MessageExport> list = new ArrayList<>();
|
||||
for (MessageKey key : MessageKey.values()) {
|
||||
list.add(new MessageExport(key.getKey(), key.getTags(), getString(key, defaultMessages),
|
||||
getString(key, messageFile)));
|
||||
}
|
||||
|
||||
return gson.toJson(new LanguageExport(code, list));
|
||||
}
|
||||
|
||||
protected FileConfiguration loadDefaultMessages() {
|
||||
return YamlConfiguration.loadConfiguration(new File(MESSAGES_FOLDER + "messages_en.yml"));
|
||||
}
|
||||
|
||||
private static String getString(MessageKey key, FileConfiguration configuration) {
|
||||
return configuration.getString(key.getKey(), "");
|
||||
}
|
||||
|
||||
private static String sendJsonToRemote(String language, String json) {
|
||||
try {
|
||||
String encodedData = "file=" + URLEncoder.encode(json, "UTF-8")
|
||||
+ "&language=" + URLEncoder.encode(language, "UTF-8");
|
||||
|
||||
URL url = new URL(UPDATE_URL);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setDoOutput(true);
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
|
||||
OutputStream os = conn.getOutputStream();
|
||||
os.write(encodedData.getBytes());
|
||||
os.flush();
|
||||
os.close();
|
||||
|
||||
return "Response code: " + conn.getResponseCode()
|
||||
+ "\n" + inputStreamToString(conn.getInputStream());
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String inputStreamToString(InputStream is) {
|
||||
try (InputStreamReader isr = new InputStreamReader(is)) {
|
||||
return CharStreams.toString(isr);
|
||||
} catch (IOException e) {
|
||||
return "Failed to read output - " + StringUtils.formatException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,116 +0,0 @@
|
||||
package tools.messages.translation;
|
||||
|
||||
import com.google.common.io.Resources;
|
||||
import com.google.gson.Gson;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import tools.messages.MessageFileVerifier;
|
||||
import tools.messages.VerifyMessagesTask;
|
||||
import tools.utils.FileIoUtils;
|
||||
import tools.utils.ToolTask;
|
||||
import tools.utils.ToolsConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Imports a message file from a remote JSON export and validates the resulting file.
|
||||
* <p>
|
||||
* Comments at the top of an existing file should remain after the import, but it is important
|
||||
* to verify that no unwanted changes have been applied to the file. Note that YAML comments
|
||||
* tend to disappear if there is no space between the <code>#</code> and the first character.
|
||||
*/
|
||||
public class ImportMessagesTask implements ToolTask {
|
||||
|
||||
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||
private Gson gson = new Gson();
|
||||
private Set<String> messageCodes;
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
return "importMessages";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Scanner scanner) {
|
||||
System.out.println("Enter URL to import from");
|
||||
// Dirty trick: replace https:// with http:// so we don't have to worry about installing certificates...
|
||||
String url = scanner.nextLine().replace("https://", "http://");
|
||||
|
||||
LanguageExport languageExport = getLanguageExportFromUrl(url);
|
||||
if (languageExport == null) {
|
||||
throw new IllegalStateException("An error occurred: constructed language export is null");
|
||||
}
|
||||
|
||||
mergeExportIntoFile(languageExport);
|
||||
System.out.println("Saved to messages file for code '" + languageExport.code + "'");
|
||||
}
|
||||
|
||||
private LanguageExport getLanguageExportFromUrl(String location) {
|
||||
try {
|
||||
URL url = new URL(location);
|
||||
String json = Resources.toString(url, StandardCharsets.UTF_8);
|
||||
return gson.fromJson(json, LanguageExport.class);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void mergeExportIntoFile(LanguageExport export) {
|
||||
String languageCode = export.code;
|
||||
String fileName = MESSAGES_FOLDER + "messages_" + languageCode + ".yml";
|
||||
File file = new File(fileName);
|
||||
FileConfiguration fileConfiguration;
|
||||
if (file.exists()) {
|
||||
removeAllTodoComments(fileName);
|
||||
fileConfiguration = AuthMeYamlConfiguration.loadConfiguration(file);
|
||||
} else {
|
||||
fileConfiguration = new AuthMeYamlConfiguration();
|
||||
}
|
||||
|
||||
buildMessageCodeList();
|
||||
for (MessageExport messageExport : export.messages) {
|
||||
if (!messageCodes.contains(messageExport.key)) {
|
||||
throw new IllegalStateException("Message key '" + messageExport.key + "' does not exist");
|
||||
} else if (!messageExport.translatedMessage.isEmpty()) {
|
||||
fileConfiguration.set(messageExport.key, messageExport.translatedMessage);
|
||||
}
|
||||
}
|
||||
try {
|
||||
fileConfiguration.save(file);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
MessageFileVerifier verifier = new MessageFileVerifier(file);
|
||||
VerifyMessagesTask.verifyFileAndAddKeys(verifier, YamlConfiguration.loadConfiguration(
|
||||
new File(MESSAGES_FOLDER + "messages_en.yml")));
|
||||
}
|
||||
|
||||
private void buildMessageCodeList() {
|
||||
messageCodes = new HashSet<>(MessageKey.values().length);
|
||||
for (MessageKey messageKey : MessageKey.values()) {
|
||||
messageCodes.add(messageKey.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all to-do comments written by {@link VerifyMessagesTask}. This is helpful as the YamlConfiguration
|
||||
* moves those comments otherwise upon saving.
|
||||
*
|
||||
* @param file The file whose to-do comments should be removed
|
||||
*/
|
||||
private static void removeAllTodoComments(String file) {
|
||||
String contents = FileIoUtils.readFromFile(file);
|
||||
String regex = "^# TODO .*$";
|
||||
contents = Pattern.compile(regex, Pattern.MULTILINE).matcher(contents).replaceAll("");
|
||||
FileIoUtils.writeToFile(file, contents);
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package tools.messages.translation;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Export of a language's messages.
|
||||
*/
|
||||
public class LanguageExport {
|
||||
|
||||
public final String code;
|
||||
public final List<MessageExport> messages;
|
||||
|
||||
public LanguageExport(String code, List<MessageExport> messages) {
|
||||
this.code = code;
|
||||
this.messages = Collections.unmodifiableList(messages);
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package tools.messages.translation;
|
||||
|
||||
/**
|
||||
* Container class for one translatable message.
|
||||
*/
|
||||
public class MessageExport {
|
||||
|
||||
public final String key;
|
||||
public final String tags;
|
||||
public final String defaultMessage;
|
||||
public final String translatedMessage;
|
||||
|
||||
public MessageExport(String key, String[] tags, String defaultMessage, String translatedMessage) {
|
||||
this.key = key;
|
||||
this.tags = String.join(",", tags);
|
||||
this.defaultMessage = defaultMessage;
|
||||
this.translatedMessage = translatedMessage;
|
||||
}
|
||||
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package tools.messages.translation;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import tools.utils.FileIoUtils;
|
||||
import tools.utils.ToolsConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
|
||||
/**
|
||||
* Task which exports all messages to a local folder.
|
||||
*/
|
||||
public class WriteAllExportsTask extends ExportMessagesTask {
|
||||
|
||||
private static final String OUTPUT_FOLDER = ToolsConstants.TOOLS_SOURCE_ROOT + "messages/translation/export/";
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
return "writeAllExports";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Scanner scanner) {
|
||||
final File[] messageFiles = listFilesOrThrow(new File(MESSAGES_FOLDER));
|
||||
final FileConfiguration defaultMessages = loadDefaultMessages();
|
||||
for (File file : messageFiles) {
|
||||
String code = file.getName().substring("messages_".length(), file.getName().length() - ".yml".length());
|
||||
String json = convertToJson(code, defaultMessages, YamlConfiguration.loadConfiguration(file));
|
||||
FileIoUtils.writeToFile(OUTPUT_FOLDER + "messages_" + code + ".json", json);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user