|
| 1 | +package com.baeldung.jenkins.helloworld; |
| 2 | + |
| 3 | +import hudson.Extension; |
| 4 | +import hudson.FilePath; |
| 5 | +import hudson.Launcher; |
| 6 | +import hudson.model.AbstractBuild; |
| 7 | +import hudson.model.AbstractProject; |
| 8 | +import hudson.model.BuildListener; |
| 9 | +import hudson.tasks.BuildWrapper; |
| 10 | +import hudson.tasks.BuildWrapperDescriptor; |
| 11 | +import org.kohsuke.stapler.DataBoundConstructor; |
| 12 | + |
| 13 | +import javax.annotation.Nonnull; |
| 14 | +import java.io.*; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.Stack; |
| 17 | + |
| 18 | +public class ProjectStatsBuildWrapper extends BuildWrapper { |
| 19 | + |
| 20 | + private static final String REPORT_TEMPLATE_PATH = "/stats.html"; |
| 21 | + private static final String PROJECT_NAME_VAR = "$PROJECT_NAME$"; |
| 22 | + private static final String CLASSES_NUMBER_VAR = "$CLASSES_NUMBER$"; |
| 23 | + private static final String LINES_NUMBER_VAR = "$LINES_NUMBER$"; |
| 24 | + |
| 25 | + @DataBoundConstructor |
| 26 | + public ProjectStatsBuildWrapper() { |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public Environment setUp(AbstractBuild build, final Launcher launcher, BuildListener listener) { |
| 31 | + return new Environment() { |
| 32 | + @Override |
| 33 | + public boolean tearDown(AbstractBuild build, BuildListener listener) |
| 34 | + throws IOException, InterruptedException |
| 35 | + { |
| 36 | + ProjectStats stats = buildStats(build.getWorkspace()); |
| 37 | + String report = generateReport(build.getProject().getDisplayName(), stats); |
| 38 | + File artifactsDir = build.getArtifactsDir(); |
| 39 | + if (!artifactsDir.isDirectory()) { |
| 40 | + boolean success = artifactsDir.mkdirs(); |
| 41 | + if (!success) { |
| 42 | + listener.getLogger().println("Can't create artifacts directory at " |
| 43 | + + artifactsDir.getAbsolutePath()); |
| 44 | + } |
| 45 | + } |
| 46 | + String path = artifactsDir.getCanonicalPath() + REPORT_TEMPLATE_PATH; |
| 47 | + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), |
| 48 | + StandardCharsets.UTF_8))) { |
| 49 | + writer.write(report); |
| 50 | + } |
| 51 | + return super.tearDown(build, listener); |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + private static ProjectStats buildStats(FilePath root) throws IOException, InterruptedException { |
| 57 | + int classesNumber = 0; |
| 58 | + int linesNumber = 0; |
| 59 | + Stack<FilePath> toProcess = new Stack<>(); |
| 60 | + toProcess.push(root); |
| 61 | + while (!toProcess.isEmpty()) { |
| 62 | + FilePath path = toProcess.pop(); |
| 63 | + if (path.isDirectory()) { |
| 64 | + toProcess.addAll(path.list()); |
| 65 | + } else if (path.getName().endsWith(".java")) { |
| 66 | + classesNumber++; |
| 67 | + linesNumber += countLines(path); |
| 68 | + } |
| 69 | + } |
| 70 | + return new ProjectStats(classesNumber, linesNumber); |
| 71 | + } |
| 72 | + |
| 73 | + private static int countLines(FilePath path) throws IOException, InterruptedException { |
| 74 | + byte[] buffer = new byte[1024]; |
| 75 | + int result = 1; |
| 76 | + try (InputStream in = path.read()) { |
| 77 | + while (true) { |
| 78 | + int read = in.read(buffer); |
| 79 | + if (read < 0) { |
| 80 | + return result; |
| 81 | + } |
| 82 | + for (int i = 0; i < read; i++) { |
| 83 | + if (buffer[i] == '\n') { |
| 84 | + result++; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static String generateReport(String projectName, ProjectStats stats) throws IOException { |
| 92 | + ByteArrayOutputStream bOut = new ByteArrayOutputStream(); |
| 93 | + try (InputStream in = ProjectStatsBuildWrapper.class.getResourceAsStream(REPORT_TEMPLATE_PATH)) { |
| 94 | + byte[] buffer = new byte[1024]; |
| 95 | + int read; |
| 96 | + while ((read = in.read(buffer)) >= 0) { |
| 97 | + bOut.write(buffer, 0, read); |
| 98 | + } |
| 99 | + } |
| 100 | + String content = new String(bOut.toByteArray(), StandardCharsets.UTF_8); |
| 101 | + content = content.replace(PROJECT_NAME_VAR, projectName); |
| 102 | + content = content.replace(CLASSES_NUMBER_VAR, String.valueOf(stats.getClassesNumber())); |
| 103 | + content = content.replace(LINES_NUMBER_VAR, String.valueOf(stats.getLinesNumber())); |
| 104 | + return content; |
| 105 | + } |
| 106 | + |
| 107 | + @Extension |
| 108 | + public static final class DescriptorImpl extends BuildWrapperDescriptor { |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean isApplicable(AbstractProject<?, ?> item) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + @Nonnull |
| 116 | + @Override |
| 117 | + public String getDisplayName() { |
| 118 | + return "Construct project stats during build"; |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments