Skip to content

Commit

Permalink
Updating to version 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
yshao committed Jan 26, 2017
1 parent 6e940b5 commit b842246
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 96 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>zanata</artifactId>
<version>0.2</version>
<version>0.3</version>
<packaging>hpi</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ of this software and associated documentation files (the "Software"), to deal
import hudson.util.FormValidation;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.Build;
import hudson.model.TaskListener;
import hudson.model.*;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import jenkins.tasks.SimpleBuildStep;
Expand All @@ -47,28 +49,25 @@ of this software and associated documentation files (the "Software"), to deal
import java.io.File;
import java.io.Writer;
import java.io.*;
import java.util.*;
import hudson.EnvVars;
import hudson.Launcher.*;
import hudson.Proc;


public class ZanataBuilder extends Builder implements SimpleBuildStep {
public class ZanataCliBuilder extends Builder implements SimpleBuildStep {

private final String projFile;
private final String commandG2Z;
private final String commandZ2G;
private final boolean syncG2zanata;
private final boolean syncZ2git;


// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public ZanataBuilder(String projFile, String commandG2Z, boolean syncG2zanata, String commandZ2G, boolean syncZ2git) {
public ZanataCliBuilder(String projFile, boolean syncG2zanata, boolean syncZ2git) {
this.projFile = projFile;
this.syncG2zanata = syncG2zanata;
this.syncZ2git = syncZ2git;
this.commandG2Z = commandG2Z;
this.commandZ2G = commandZ2G;
}

/**
Expand All @@ -79,14 +78,6 @@ public String getProjFile() {
return projFile;
}

public String getCommandG2Z() {
return commandG2Z;
}

public String getCommandZ2G() {
return commandZ2G;
}

public boolean getSyncG2zanata() {
return syncG2zanata;
}
Expand All @@ -98,25 +89,31 @@ public boolean getSyncZ2git() {
@Override
public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) {

listener.getLogger().println("Running Zanata Sync, project file: " + projFile);
String commandG2Z;
String commandZ2G;

listener.getLogger().println("Running Zanata Sync, project file: " + projFile);

if (syncG2zanata) {
commandG2Z = getDescriptor().getCommandG2Z();

listener.getLogger().println("Git to Zanata sync is enabled, running command:");
listener.getLogger().println(commandG2Z + "\n");

if (runShellCommandInBuild(commandG2Z, listener, build)){
if (runShellCommandInBuild(commandG2Z, listener, build, workspace)){
listener.getLogger().println("Git to Zanata sync finished.\n");
}

};


if (syncZ2git) {
commandZ2G = getDescriptor().getCommandZ2G();

listener.getLogger().println("Zanata to Git sync is enabled, running command:");
listener.getLogger().println(commandZ2G + "\n");

if (runShellCommandInBuild(commandZ2G, listener, build)){
if (runShellCommandInBuild(commandZ2G, listener, build, workspace)){
listener.getLogger().println("Zanata to Git sync finished.\n");
}
};
Expand All @@ -129,15 +126,23 @@ public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskL
*/
}

private boolean runShellCommandInBuild(String command, TaskListener listener, Run<?,?> builder){
private boolean runShellCommandInBuild(String command, TaskListener listener, Run<?,?> builder, FilePath workspace){

try {

EnvVars envs = builder.getEnvironment(listener);
EnvVars jenkinsEnvs = builder.getEnvironment(listener);
Map<String, String> sysEnvs = System.getenv();

Map<String, String> allEnvs = new HashMap<String, String> ();
allEnvs.putAll(sysEnvs);
allEnvs.putAll(jenkinsEnvs);

listener.getLogger().println("workspace: " + workspace.toURI());

Process pg = Runtime.getRuntime().exec(new String[]{"bash","-c",command},
envs.toString().split(", "),
new File(envs.get("WORKSPACE")));
allEnvs.toString().split(", "),
new File(workspace.toURI()));


try (BufferedReader in = new BufferedReader(
new InputStreamReader(pg.getInputStream(),"UTF8"));) {
Expand All @@ -152,6 +157,22 @@ private boolean runShellCommandInBuild(String command, TaskListener listener, Ru
e.printStackTrace();
return false;
}
try (BufferedReader in = new BufferedReader(
new InputStreamReader(pg.getErrorStream(),"UTF8"));) {
String line = null;
while ((line = in.readLine()) != null)
{ System.out.println(line);
listener.getLogger().println(line);
}
in.close();
} catch (IOException e) {
listener.getLogger().println("Can't generate error message of command:" + command);
e.printStackTrace();
return false;
}
pg.waitFor();
listener.getLogger().println("Run command return: " + Integer.toString(pg.exitValue()));

} catch (IOException e) {
listener.getLogger().println("Can't run command:" + command);
e.printStackTrace();
Expand All @@ -161,8 +182,8 @@ private boolean runShellCommandInBuild(String command, TaskListener listener, Ru
e.printStackTrace();
return false;
}
return true;

return true;
}

// Overridden for better type safety.
Expand All @@ -174,11 +195,11 @@ public DescriptorImpl getDescriptor() {
}

/**
* Descriptor for {@link ZanataBuilder}. Used as a singleton.
* Descriptor for {@link ZanataCliBuilder}. Used as a singleton.
* The class is marked as public so that it can be accessed from views.
*
* <p>
* See {@code src/main/resources/hudson/plugins/hello_world/ZanataBuilder/*.jelly}
* See {@code src/main/resources/hudson/plugins/hello_world/ZanataCliBuilder/*.jelly}
* for the actual HTML fragment for the configuration screen.
*/
@Extension // This indicates to Jenkins that this is an implementation of an extension point.
Expand All @@ -192,6 +213,8 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
*/
private String iniLocation;
private String iniContents;
private String commandG2Z;
private String commandZ2G;


/**
Expand All @@ -204,26 +227,23 @@ public DescriptorImpl() {


public FormValidation doCheckProjFile(@QueryParameter String value,
@QueryParameter String commandG2Z,
@QueryParameter boolean syncG2zanata,
@QueryParameter String commandZ2G,
@QueryParameter boolean syncZ2git)
throws IOException, ServletException {

if (value.length() == 0)
return FormValidation.error("Please set a project name such as zanata.xml");

System.out.println("Project File is : " + value);
System.out.println("Project G2Z is : " + commandG2Z);
System.out.println(syncG2zanata);
System.out.println("Project Z2G is : " + commandZ2G);
System.out.println(syncZ2git);

save ();
return FormValidation.ok();
}


@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
// Indicates that this builder can be used with all kinds of project types
return true;
Expand All @@ -232,6 +252,7 @@ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
/**
* This human readable name is used in the configuration screen.
*/
@Override
public String getDisplayName() {
return "Zanata Localization Sync";
}
Expand All @@ -246,8 +267,21 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc

iniLocation = formData.getString("iniLocation");
iniContents = formData.getString("iniContents");
commandG2Z = formData.getString("commandG2Z");
commandZ2G = formData.getString("commandZ2G");

String iniFullPath;

if (System.getProperty("JENKINS_HOME") != null) {
iniFullPath = System.getProperty("JENKINS_HOME") + "/" + iniLocation;
} else {
if (System.getProperty("user.home") != null) {
iniFullPath = System.getProperty("user.home") + "/" + iniLocation;
} else {
iniFullPath = "/var/lib/jenkins" + "/" + iniLocation;
}
}

String iniFullPath = System.getProperty("JENKINS_HOME") + "/" + iniLocation;
File file = new File(iniFullPath);

try {
Expand All @@ -266,16 +300,17 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
e.printStackTrace();
}

try (Writer fstream = new OutputStreamWriter(new FileOutputStream(iniFullPath), "UTF8");) {
fstream.write(iniContents);
fstream.flush();
fstream.close();
System.out.println("File written Succesfully: " + iniFullPath);
} catch (IOException e) {
System.out.println("File write NOT Succesfully: " + iniFullPath);
e.printStackTrace();
if (file.exists()) {
try (Writer fstream = new OutputStreamWriter(new FileOutputStream(iniFullPath), "UTF8");) {
fstream.write(iniContents);
fstream.flush();
fstream.close();
System.out.println("File written Succesfully: " + iniFullPath);
} catch (IOException e) {
System.out.println("File write NOT Succesfully: " + iniFullPath);
e.printStackTrace();
}
}

save();
return super.configure(req,formData);
}
Expand All @@ -292,6 +327,12 @@ public String getIniLocation() {
public String getIniContents() {
return iniContents;
}
public String getCommandG2Z() {
return commandG2Z;
}
public String getCommandZ2G() {
return commandZ2G;
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<!--
This jelly script is used for per-project configuration.
See global.jelly for a general discussion about jelly script.
-->

<!--
Creates a text field that shows the value of the "name" property.
When submitted, it will be passed to the corresponding constructor parameter.
-->
<f:entry title="Project File" field="projFile">
<f:textbox default="zanata.xml"></f:textbox>
</f:entry>
<f:entry title="Enable" field="syncG2zanata"
description="Check to sync from Git Repo to Zanata">
<f:checkbox/>
</f:entry>
<f:entry title="Enable" field="syncZ2git"
description="Check to prepare commit of translation from Zanata to Git, you need to push in After Build.">
<f:checkbox/>
</f:entry>


</j:jelly>
Loading

0 comments on commit b842246

Please sign in to comment.