PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/opup-maven-plugin/src/main/java/com/atlassian/maven/plugins/opup/mojo/UpgradeMojo.java

https://bitbucket.org/jwalton/opup
Java | 216 lines | 172 code | 40 blank | 4 comment | 8 complexity | d106a344c0858a2d36f1213797508c7c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.maven.plugins.opup.mojo;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import com.atlassian.labs.opup.task.CreateUpgradeBranchesTask;
  8. import com.atlassian.labs.opup.task.MergeTask;
  9. import com.atlassian.labs.opup.task.ShowResultsTask;
  10. import org.apache.commons.io.FileUtils;
  11. import org.apache.maven.plugin.MojoExecutionException;
  12. import org.apache.maven.plugin.MojoFailureException;
  13. import org.apache.maven.plugin.logging.Log;
  14. import org.apache.maven.plugins.annotations.Mojo;
  15. import org.apache.maven.plugins.annotations.Parameter;
  16. @Mojo(name = "upgrade", aggregator = true)
  17. public class UpgradeMojo extends AbstractOpUpMojo
  18. {
  19. @Parameter(defaultValue = "${project.build.directory}/opup/working-git-clone")
  20. public File repositoryClone;
  21. @Parameter(defaultValue = "${project.build.directory}/opup")
  22. public File opupDirectory;
  23. @Parameter(defaultValue = "${project.build.directory}/opup/opup-log/build-pass-fail")
  24. public File passFailFile;
  25. @Parameter(defaultValue = "opup/opup-log")
  26. public String relativeLogDirectory;
  27. @Parameter(defaultValue = "${project.build.directory}/opup-results.html")
  28. public File resultsFile;
  29. @Parameter(property = "buildCommand", defaultValue = "mvn clean test")
  30. public String buildCommand;
  31. void logOutput(Process p) throws IOException, InterruptedException
  32. {
  33. Thread err = new Thread(new InputStreamLogger(p.getErrorStream()));
  34. Thread out = new Thread(new InputStreamLogger(p.getInputStream()));
  35. err.start();
  36. out.start();
  37. err.join();
  38. out.join();
  39. }
  40. void exec(String[] cmd) throws IOException
  41. {
  42. try
  43. {
  44. Process p = Runtime.getRuntime().exec(cmd);
  45. logOutput(p);
  46. if (p.waitFor() != 0)
  47. {
  48. throw new IOException("Failed to clone Git repository into " + repositoryClone);
  49. }
  50. }
  51. catch (InterruptedException ie)
  52. {
  53. Thread.currentThread().interrupt();
  54. }
  55. }
  56. @Override
  57. public void execute() throws MojoExecutionException, MojoFailureException
  58. {
  59. /* Install the scripts */
  60. try
  61. {
  62. installScript("build-and-log.sh");
  63. installScript("find-success.sh");
  64. }
  65. catch (IOException ioe)
  66. {
  67. throw new MojoExecutionException("Failed to create scripts", ioe);
  68. }
  69. /* Clone the current repository */
  70. String[] cloneCmd = {
  71. "git", "clone", ".", repositoryClone.getPath()
  72. };
  73. try
  74. {
  75. exec(cloneCmd);
  76. }
  77. catch (IOException ioe)
  78. {
  79. throw new MojoExecutionException("Failed to clone Git repository", ioe);
  80. }
  81. /* Push the current branch to 'master' */
  82. String[] pushRefCmd = {
  83. "git", "push", repositoryClone.getPath(), "HEAD:master"
  84. };
  85. try
  86. {
  87. exec(pushRefCmd);
  88. }
  89. catch (IOException ioe)
  90. {
  91. throw new MojoExecutionException("Failed to push current branch to master in working clone", ioe);
  92. }
  93. CreateUpgradeBranchesTask task = new CreateUpgradeBranchesTask(strategy, newHttpCache(), getUpgradeAdvice(), getVersionClient(), repositoryClone);
  94. task.generateUpgradeBranches(reportFile, getLog());
  95. getLog().info("OpUp report: " + "file://" + reportFile);
  96. String[] findSuccessCmd = {
  97. new File(opupDirectory, "bin/find-success.sh").getAbsolutePath(),
  98. repositoryClone.getAbsolutePath(),
  99. buildCommand,
  100. passFailFile.getParentFile().getAbsolutePath()
  101. };
  102. // Run the script
  103. try
  104. {
  105. exec(findSuccessCmd);
  106. }
  107. catch (IOException ioe)
  108. {
  109. throw new MojoExecutionException("Failed to run find-success.sh", ioe);
  110. }
  111. MergeTask mt = MergeTask.from(repositoryClone,
  112. new File(opupDirectory, "commit.keys"),
  113. task.poms(),
  114. new File(opupDirectory, "opup-log/upgrade-refs"));
  115. mt.merge();
  116. ShowResultsTask resultsTask = new ShowResultsTask(opupDirectory, passFailFile, relativeLogDirectory);
  117. resultsTask.generateResults(resultsFile);
  118. getLog().info("OpUp upgrade results: " + "file://" + resultsFile);
  119. String[] fetchCmd = {
  120. "git", "fetch", "-f", repositoryClone.getPath(), "master:opup/upgrade"
  121. };
  122. try
  123. {
  124. exec(fetchCmd);
  125. }
  126. catch (IOException ioe)
  127. {
  128. throw new MojoExecutionException("Failed to fetch upgrade branch", ioe);
  129. }
  130. }
  131. private void installScript(String name) throws IOException
  132. {
  133. InputStream in = getClass().getResourceAsStream(name);
  134. if (in == null)
  135. {
  136. throw new IOException("Missing resource: " + name);
  137. }
  138. File binDir = new File(opupDirectory, "bin");
  139. if (!binDir.isDirectory())
  140. {
  141. binDir.mkdirs();
  142. }
  143. File target = new File(binDir, name);
  144. FileUtils.copyInputStreamToFile(in, target);
  145. if (!target.setExecutable(true))
  146. {
  147. throw new IOException("Failed to make script executable: " + target);
  148. }
  149. }
  150. private class InputStreamLogger implements Runnable
  151. {
  152. private final BufferedReader br;
  153. private final Log log;
  154. public InputStreamLogger(InputStream in)
  155. {
  156. this.br = new BufferedReader(new InputStreamReader(in));
  157. log = getLog();
  158. }
  159. @Override
  160. public void run()
  161. {
  162. try
  163. {
  164. String s;
  165. while ((s = br.readLine()) != null)
  166. {
  167. log.info(s);
  168. }
  169. }
  170. catch (IOException ioe)
  171. {
  172. log.error("Failed to read from external command", ioe);
  173. }
  174. }
  175. }
  176. }