diff --git a/build.gradle b/build.gradle index 4362ed1..ae55550 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ targetCompatibility = 1.7 // Artifact settings group = 'be.lukin.poeditor' -version = '0.3.2' +version = '0.3.3' archivesBaseName = 'poeditor-client' // http://gradle.org/docs/current/userguide/application_plugin.html diff --git a/docs/upload.md b/docs/upload.md index d35e7a0..648693f 100644 --- a/docs/upload.md +++ b/docs/upload.md @@ -4,7 +4,7 @@ Upload Create and publish the archive: http://gradle.org/docs/current/userguide/artifact_management.html ``` -gradle uploadArchives +gradle uploadArchives -Premote ``` This will build, sign and upload the archive. diff --git a/src/main/java/be/lukin/poeditor/JarMain.java b/src/main/java/be/lukin/poeditor/JarMain.java index bea2b52..76bbb22 100644 --- a/src/main/java/be/lukin/poeditor/JarMain.java +++ b/src/main/java/be/lukin/poeditor/JarMain.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; public class JarMain { @@ -17,40 +19,54 @@ public class JarMain { */ public static void main(String[] args) throws IOException { if(args.length == 0){ - System.out.println("No command given, choose: \n\tinit\n\tpull\n\tpushTerms"); + System.out.println("No command given, choose: \n\tinit\n\tpull\n\tpush\n\tpushTerms"); return; } + Map parameters = new HashMap(); + if(args.length > 1){ + for(String s : args){ + String[] splitted = s.split("="); + if(splitted.length==2){ + parameters.put(splitted[0], splitted[1]); + } + } + } + // Read config Path current = Paths.get(""); File configFile = new File(current.toAbsolutePath().toString(), "poeditor.properties"); Config config = Config.load(configFile); + BaseTask task = null; if("init".equals(args[0])){ System.out.println("Initialize project"); - InitTask initTask = new InitTask(); - initTask.configure(config); - initTask.handle(); - } else if("pull".equals(args[0])){ + task = new InitTask(); + + } else if("pull".equals(args[0])) { System.out.println("Pull languages"); - PullTask pullTask = new PullTask(); - pullTask.configure(config); - pullTask.handle(); + task = new PullTask(); + + } else if("push".equals(args[0])){ + System.out.println("Push languages"); + task = new PushTask(); + } else if("pushTerms".equals(args[0])){ System.out.println("Push terms"); - PushTermsTask pushTermsTask = new PushTermsTask(); - pushTermsTask.configure(config); - pushTermsTask.handle(); + task = new PushTermsTask(); + } else if("generate".equals(args[0])){ System.out.println("Generate config"); - GenerateTask generateTask = new GenerateTask(); - generateTask.configure(config); - generateTask.handle(); + task = new GenerateTask(); + } else if("status".equals(args[0])){ System.out.println("Status"); - StatusTask statusTask = new StatusTask(); - statusTask.configure(config); - statusTask.handle(); + task = new StatusTask(); + } + + if(task != null) { + task.configure(config, parameters); + task.handle(); } } } diff --git a/src/main/java/be/lukin/poeditor/tasks/BaseTask.java b/src/main/java/be/lukin/poeditor/tasks/BaseTask.java index 61ac268..c00aa07 100644 --- a/src/main/java/be/lukin/poeditor/tasks/BaseTask.java +++ b/src/main/java/be/lukin/poeditor/tasks/BaseTask.java @@ -3,12 +3,59 @@ import be.lukin.poeditor.Config; import be.lukin.poeditor.POEditorClient; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public abstract class BaseTask { + + class Params { + private Map parameters = new HashMap(); + + public Params(Map parameters) { + this.parameters = parameters; + } + + public String getString(String key){ + return parameters.get(key); + } + + public String[] getStringArray(String key){ + String s = parameters.get(key); + + if(s != null){ + return s.split(","); + } + + return null; + } + + public List getStringList(String key){ + String[] s = getStringArray(key); + + if(s != null) { + return Arrays.asList(s); + } + return null; + } + + public boolean getBoolean(String key){ + return Boolean.parseBoolean(parameters.get(key)); + } + } + + Params params; Config config; POEditorClient client; public BaseTask configure(Config config){ + return configure(config, null); + } + + public BaseTask configure(Config config, Map parameters){ this.config = config; + this.params = new Params(parameters != null ? parameters : new HashMap()); this.client = new POEditorClient(config.getApiKey()); return this; } diff --git a/src/main/java/be/lukin/poeditor/tasks/PushTask.java b/src/main/java/be/lukin/poeditor/tasks/PushTask.java new file mode 100644 index 0000000..97286f0 --- /dev/null +++ b/src/main/java/be/lukin/poeditor/tasks/PushTask.java @@ -0,0 +1,58 @@ +package be.lukin.poeditor.tasks; + +import be.lukin.poeditor.Config; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +public class PushTask extends BaseTask { + + @Override + public void handle() { + // params init + System.out.println("Uploading translations"); + Config config = super.config; + Path current = Paths.get(""); + + boolean override = super.params.getBoolean("override"); + boolean noSleep = super.params.getBoolean("noSleep"); + List languages = super.params.getStringList("languages"); + + if(noSleep){ + System.out.println("- Sleep disabled"); + } + + if(override){ + System.out.println("- override translations"); + } + + boolean first = true; + + for(String lang : config.getLanguageKeys()){ + if(languages != null && !languages.contains(lang)){ + continue; + } + + if(!noSleep && !first) { + /** + * Before a new file upload we'll sleep for 30 seconds to avoid API Exceptions. + * + * 4048 - Too many upload requests in a short period of time + * https://poeditor.com/api_reference/error_codes#code-4048 + * */ + try { + Thread.sleep(30000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + File langFile = new File(current.toAbsolutePath().toString(), config.getLanguage(lang)); + client.uploadLanguage(config.getProjectId(), langFile, lang, override); + System.out.println("- lang uploaded: " + lang); + first = false; + } + } +}