|
| 1 | +package org.jboss.set.mavendependencyupdater.core.processingstrategies; |
| 2 | + |
| 3 | +import org.jboss.set.mavendependencyupdater.DependencyEvaluator.ComponentUpgrade; |
| 4 | +import org.jboss.set.mavendependencyupdater.configuration.Configuration; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.PrintStream; |
| 8 | +import java.time.ZonedDateTime; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import static j2html.TagCreator.a; |
| 14 | +import static j2html.TagCreator.caption; |
| 15 | +import static j2html.TagCreator.div; |
| 16 | +import static j2html.TagCreator.each; |
| 17 | +import static j2html.TagCreator.h2; |
| 18 | +import static j2html.TagCreator.li; |
| 19 | +import static j2html.TagCreator.p; |
| 20 | +import static j2html.TagCreator.span; |
| 21 | +import static j2html.TagCreator.table; |
| 22 | +import static j2html.TagCreator.td; |
| 23 | +import static j2html.TagCreator.text; |
| 24 | +import static j2html.TagCreator.th; |
| 25 | +import static j2html.TagCreator.thead; |
| 26 | +import static j2html.TagCreator.tr; |
| 27 | +import static j2html.TagCreator.ul; |
| 28 | + |
| 29 | +/** |
| 30 | + * Prints upgradable dependencies report to stdout or to given file. |
| 31 | + * <p> |
| 32 | + * Non thread safe. |
| 33 | + */ |
| 34 | +public class HtmlReportProcessingStrategy extends TextReportProcessingStrategy { |
| 35 | + |
| 36 | + private static final String BASIC_STYLES = "font-family: Verdana,sans-serif;" + |
| 37 | + "font-size: 10pt;"; |
| 38 | + private static final String TABLE_STYLES = "margin: 2em 0;" + |
| 39 | + "border-collapse: collapse;"; |
| 40 | + private static final String CAPTION_STYLES = "text-align: left;" + |
| 41 | + "font-weight: bold;"; |
| 42 | + private static final String TH_TD_STYLES = "border-bottom: 1px solid #ddd;" + |
| 43 | + "padding: 10px;" + |
| 44 | + "text-align: left;"; |
| 45 | + private static final String GAV_STYLES = "font-family: \"Courier New\";"; |
| 46 | + private static final String UL_STYLES = "list-style-type: circle;"; |
| 47 | + private static final String LI_STYLES = "margin: 7px 0;"; |
| 48 | + private static final String REPO_LABEL_STYLES = "border-radius: 5px;" + |
| 49 | + "padding: 3px;"; |
| 50 | + private static final String FOOTER_STYLES = "color: #999;"; |
| 51 | + |
| 52 | + private static final String BG_NEW = "background-color: #fffeec;"; |
| 53 | + |
| 54 | + private static final String BG1 = "background-color: #a8df65;"; |
| 55 | + private static final String BG2 = "background-color: #edf492;"; |
| 56 | + private static final String BG3 = "background-color: #efb960;"; |
| 57 | + private static final String BG4 = "background-color: #ee91bc;"; |
| 58 | + |
| 59 | + private static final String[] BACKGROUNDS = {BG1, BG2, BG3, BG4}; |
| 60 | + |
| 61 | + private List<String> repositoryKeys; |
| 62 | + |
| 63 | + public HtmlReportProcessingStrategy(Configuration configuration, File pomFile, PrintStream printStream) { |
| 64 | + super(configuration, pomFile, printStream); |
| 65 | + initRepositoryKeys(); |
| 66 | + } |
| 67 | + |
| 68 | + public HtmlReportProcessingStrategy(Configuration configuration, File pomFile, String outputFileName) { |
| 69 | + super(configuration, pomFile, outputFileName); |
| 70 | + initRepositoryKeys(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean process(List<ComponentUpgrade> upgrades) { |
| 75 | + try { |
| 76 | + if (upgrades.size() == 0) { |
| 77 | + LOG.info("No components to upgrade."); |
| 78 | + return true; |
| 79 | + } |
| 80 | + initOutputStream(); |
| 81 | + |
| 82 | + List<ComponentUpgrade> sortedUpgrades = |
| 83 | + upgrades.stream().sorted(new ComponentUpgradeComparator()) |
| 84 | + .collect(Collectors.toList()); |
| 85 | + |
| 86 | + String html = div().withStyle(BASIC_STYLES).with( |
| 87 | + h2("Component Upgrade Report"), |
| 88 | + p("Following repositories were searched:"), |
| 89 | + ul().withStyle(UL_STYLES).with( |
| 90 | + each(configuration.getRepositories().entrySet(), |
| 91 | + entry -> li().withStyle(LI_STYLES).with( |
| 92 | + span(entry.getKey()) |
| 93 | + .withStyle(REPO_LABEL_STYLES + repositoryColor(entry.getKey())), |
| 94 | + text(" " + entry.getValue()) |
| 95 | + )) |
| 96 | + ), |
| 97 | + table().withStyle(BASIC_STYLES + TABLE_STYLES).with( |
| 98 | + caption("Possible Component Upgrades").withStyle(CAPTION_STYLES), |
| 99 | + thead(tr().with( |
| 100 | + th("GAV").withStyle(TH_TD_STYLES), |
| 101 | + th("New Version").withStyle(TH_TD_STYLES), |
| 102 | + th("Repository").withStyle(TH_TD_STYLES), |
| 103 | + th("Since").withStyle(TH_TD_STYLES) |
| 104 | + )), |
| 105 | + each(sortedUpgrades, upgrade -> { |
| 106 | + boolean isNew = upgrade.getFirstSeen() == null; |
| 107 | + return tr().with( |
| 108 | + td(upgrade.getArtifact().getGroupId() |
| 109 | + + ":" + upgrade.getArtifact().getArtifactId() |
| 110 | + + ":" + upgrade.getArtifact().getVersionString()) |
| 111 | + .withStyle(TH_TD_STYLES + GAV_STYLES + (isNew ? BG_NEW : "")), |
| 112 | + td(upgrade.getNewVersion()) |
| 113 | + .withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")), |
| 114 | + td(span(upgrade.getRepository()) |
| 115 | + .withStyle(REPO_LABEL_STYLES + repositoryColor(upgrade.getRepository()))) |
| 116 | + .withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")), |
| 117 | + td(upgrade.getFirstSeen() == null ? "new" : upgrade.getFirstSeen().format(DATE_FORMATTER)) |
| 118 | + .withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")) |
| 119 | + ); |
| 120 | + }), |
| 121 | + tr(td(sortedUpgrades.size() + " items").withStyle(TH_TD_STYLES).attr("colspan", "4"))), |
| 122 | + p("Generated on " + DATE_FORMATTER.format(ZonedDateTime.now())), |
| 123 | + p().withStyle(FOOTER_STYLES).with( |
| 124 | + text("Report generated by "), |
| 125 | + a("Maven Dependency Updater") |
| 126 | + .withHref(PROJECT_URL) |
| 127 | + .withStyle(FOOTER_STYLES) |
| 128 | + ) |
| 129 | + ).render(); |
| 130 | + outputStream.println(html); |
| 131 | + return true; |
| 132 | + } catch (Exception e) { |
| 133 | + throw new RuntimeException("Report generation failed", e); |
| 134 | + } finally { |
| 135 | + if (outputStream != null && outputStream != System.out) { |
| 136 | + outputStream.close(); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private void initRepositoryKeys() { |
| 142 | + repositoryKeys = new ArrayList<>(configuration.getRepositories().keySet()); |
| 143 | + } |
| 144 | + |
| 145 | + private String repositoryColor(String key) { |
| 146 | + int idx = repositoryKeys.indexOf(key); |
| 147 | + return BACKGROUNDS[idx % BACKGROUNDS.length]; |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments