diff --git a/pom.xml b/pom.xml index 23b077c..d290a61 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.jenkins-ci.plugins zanata - 0.2 + 0.3 hpi diff --git a/src/main/java/org/jenkinsci/plugins/zanata/ZanataBuilder.java b/src/main/java/org/jenkinsci/plugins/zanata/ZanataBuilder.java index 4a2323f..4866559 100644 --- a/src/main/java/org/jenkinsci/plugins/zanata/ZanataBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/zanata/ZanataBuilder.java @@ -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; @@ -47,6 +49,7 @@ 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; @@ -55,20 +58,16 @@ of this software and associated documentation files (the "Software"), to deal public class ZanataBuilder 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 ZanataBuilder(String projFile, boolean syncG2zanata, boolean syncZ2git) { this.projFile = projFile; this.syncG2zanata = syncG2zanata; this.syncZ2git = syncZ2git; - this.commandG2Z = commandG2Z; - this.commandZ2G = commandZ2G; } /** @@ -79,14 +78,6 @@ public String getProjFile() { return projFile; } - public String getCommandG2Z() { - return commandG2Z; - } - - public String getCommandZ2G() { - return commandZ2G; - } - public boolean getSyncG2zanata() { return syncG2zanata; } @@ -98,14 +89,18 @@ 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"); } @@ -113,10 +108,12 @@ public void perform(Run build, FilePath workspace, Launcher launcher, TaskL 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"); } }; @@ -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 sysEnvs = System.getenv(); + + Map allEnvs = new HashMap (); + 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"));) { @@ -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(); @@ -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. @@ -192,6 +213,8 @@ public static final class DescriptorImpl extends BuildStepDescriptor { */ private String iniLocation; private String iniContents; + private String commandG2Z; + private String commandZ2G; /** @@ -204,9 +227,7 @@ public DescriptorImpl() { public FormValidation doCheckProjFile(@QueryParameter String value, - @QueryParameter String commandG2Z, @QueryParameter boolean syncG2zanata, - @QueryParameter String commandZ2G, @QueryParameter boolean syncZ2git) throws IOException, ServletException { @@ -214,9 +235,7 @@ public FormValidation doCheckProjFile(@QueryParameter String value, 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 (); @@ -224,6 +243,7 @@ public FormValidation doCheckProjFile(@QueryParameter String value, } + @Override public boolean isApplicable(Class aClass) { // Indicates that this builder can be used with all kinds of project types return true; @@ -232,6 +252,7 @@ public boolean isApplicable(Class aClass) { /** * This human readable name is used in the configuration screen. */ + @Override public String getDisplayName() { return "Zanata Localization Sync"; } @@ -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 { @@ -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); } @@ -292,6 +327,12 @@ public String getIniLocation() { public String getIniContents() { return iniContents; } + public String getCommandG2Z() { + return commandG2Z; + } + public String getCommandZ2G() { + return commandZ2G; + } } } diff --git a/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/config.jelly b/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/config.jelly index 3ced79e..a92223c 100644 --- a/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/config.jelly @@ -13,16 +13,10 @@ - - - - - - diff --git a/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/global.jelly b/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/global.jelly index 46df0fe..e540574 100644 --- a/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/global.jelly +++ b/src/main/resources/org/jenkinsci/plugins/zanata/ZanataBuilder/global.jelly @@ -21,5 +21,11 @@ description="The contents of zanata.ini"> + + + + + +