Skip to content

Commit

Permalink
push languages
Browse files Browse the repository at this point in the history
  • Loading branch information
lukin0110 committed Jul 9, 2015
1 parent 7a10b5a commit e33da5c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
50 changes: 33 additions & 17 deletions src/main/java/be/lukin/poeditor/JarMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<String, String> parameters = new HashMap<String, String>();
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();
}
}
}
47 changes: 47 additions & 0 deletions src/main/java/be/lukin/poeditor/tasks/BaseTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> parameters = new HashMap<String, String>();

public Params(Map<String, String> 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<String> 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<String, String> parameters){
this.config = config;
this.params = new Params(parameters != null ? parameters : new HashMap<String, String>());
this.client = new POEditorClient(config.getApiKey());
return this;
}
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/be/lukin/poeditor/tasks/PushTask.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
}

0 comments on commit e33da5c

Please sign in to comment.