|
| 1 | +package com.e_gineering.maven.gitflowhelper; |
| 2 | + |
| 3 | +import org.apache.maven.AbstractMavenLifecycleParticipant; |
| 4 | +import org.apache.maven.MavenExecutionException; |
| 5 | +import org.apache.maven.execution.MavenSession; |
| 6 | +import org.apache.maven.lifecycle.internal.MojoDescriptorCreator; |
| 7 | +import org.apache.maven.model.Plugin; |
| 8 | +import org.apache.maven.project.MavenProject; |
| 9 | +import org.apache.maven.scm.manager.ScmManager; |
| 10 | +import org.codehaus.plexus.component.annotations.Requirement; |
| 11 | +import org.codehaus.plexus.logging.Logger; |
| 12 | +import org.codehaus.plexus.util.cli.CommandLineUtils; |
| 13 | +import org.codehaus.plexus.util.xml.Xpp3Dom; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Properties; |
| 17 | + |
| 18 | +public abstract class AbstractBranchDetectingExtension extends AbstractMavenLifecycleParticipant { |
| 19 | + @Requirement |
| 20 | + MojoDescriptorCreator descriptorCreator; |
| 21 | + |
| 22 | + @Requirement |
| 23 | + Logger logger; |
| 24 | + |
| 25 | + @Requirement |
| 26 | + ScmManager scmManager; |
| 27 | + |
| 28 | + boolean pluginFound = false; |
| 29 | + String masterBranchPattern; |
| 30 | + String supportBranchPattern; |
| 31 | + String releaseBranchPattern; |
| 32 | + String hotfixBranchPattern; |
| 33 | + String developmentBranchPattern; |
| 34 | + String featureOrBugfixBranchPattern; |
| 35 | + String otherDeployBranchPattern; |
| 36 | + String otherBranchVersionDelimiter; |
| 37 | + GitBranchInfo branchInfo; |
| 38 | + Properties systemEnvVars; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void afterProjectsRead(MavenSession session) throws MavenExecutionException { |
| 42 | + try { |
| 43 | + systemEnvVars = CommandLineUtils.getSystemEnvVars(); |
| 44 | + } catch (IOException ioe) { |
| 45 | + throw new MavenExecutionException("Unable to read System Envirionment Variables: ", ioe); |
| 46 | + } |
| 47 | + |
| 48 | + // Look for a configured gitflow-helper-maven-plugin, |
| 49 | + // To determine what the gitBranchExpression and branch patterns are... |
| 50 | + String gitBranchExpression = null; |
| 51 | + |
| 52 | + pluginFound = false; |
| 53 | + for (MavenProject project : session.getProjects()) { |
| 54 | + for (Plugin plugin : project.getModel().getBuild().getPlugins()) { |
| 55 | + // Don't drop our plugin. Read it's config |
| 56 | + if (plugin.getKey().equals("com.e-gineering:gitflow-helper-maven-plugin")) { |
| 57 | + pluginFound = true; |
| 58 | + |
| 59 | + logger.debug("gitflow-helper-maven-plugin found in project: [" + project.getName() + "]"); |
| 60 | + |
| 61 | + if (masterBranchPattern == null) { |
| 62 | + masterBranchPattern = extractPluginConfigValue("masterBranchPattern", plugin); |
| 63 | + } |
| 64 | + |
| 65 | + if (supportBranchPattern == null) { |
| 66 | + supportBranchPattern = extractPluginConfigValue("supportBranchPattern", plugin); |
| 67 | + } |
| 68 | + |
| 69 | + if (releaseBranchPattern == null) { |
| 70 | + releaseBranchPattern = extractPluginConfigValue("releaseBranchPattern", plugin); |
| 71 | + } |
| 72 | + |
| 73 | + if (hotfixBranchPattern == null) { |
| 74 | + hotfixBranchPattern = extractPluginConfigValue("hotfixBranchPattern", plugin); |
| 75 | + } |
| 76 | + |
| 77 | + if (developmentBranchPattern == null) { |
| 78 | + developmentBranchPattern = extractPluginConfigValue("developmentBranchPattern", plugin); |
| 79 | + } |
| 80 | + |
| 81 | + if (featureOrBugfixBranchPattern == null) { |
| 82 | + featureOrBugfixBranchPattern = extractPluginConfigValue("featureOrBugfixBranchPattern", plugin); |
| 83 | + } |
| 84 | + |
| 85 | + if (otherDeployBranchPattern == null) { |
| 86 | + otherDeployBranchPattern = extractPluginConfigValue("otherDeployBranchPattern", plugin); |
| 87 | + } |
| 88 | + |
| 89 | + if (otherBranchVersionDelimiter == null) { |
| 90 | + otherBranchVersionDelimiter = extractPluginConfigValue("otherBranchVersionDelimiter", plugin); |
| 91 | + } |
| 92 | + |
| 93 | + if (gitBranchExpression == null) { |
| 94 | + gitBranchExpression = extractPluginConfigValue("gitBranchExpression", plugin); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Any missing configuration options need to be defaulted. |
| 101 | + if (pluginFound) { |
| 102 | + if (masterBranchPattern == null) { |
| 103 | + logger.debug("Using default master branch Pattern."); |
| 104 | + masterBranchPattern = "(origin/)?master"; |
| 105 | + } |
| 106 | + logger.debug("Master Branch Pattern: " + masterBranchPattern); |
| 107 | + |
| 108 | + if (supportBranchPattern == null) { |
| 109 | + logger.debug("Using default support branch Pattern."); |
| 110 | + supportBranchPattern = "(origin/)?support/(.*)"; |
| 111 | + } |
| 112 | + logger.debug("Support Branch Pattern: " + supportBranchPattern); |
| 113 | + |
| 114 | + if (releaseBranchPattern == null) { |
| 115 | + logger.debug("Using default release branch Pattern."); |
| 116 | + releaseBranchPattern = "(origin/)?release/(.*)"; |
| 117 | + } |
| 118 | + logger.debug("Release Branch Pattern: " + releaseBranchPattern); |
| 119 | + |
| 120 | + if (hotfixBranchPattern == null) { |
| 121 | + logger.debug("Using default hotfix branch Pattern."); |
| 122 | + hotfixBranchPattern = "(origin/)?hotfix/(.*)"; |
| 123 | + } |
| 124 | + logger.debug("Hotfix Branch Pattern: " + hotfixBranchPattern); |
| 125 | + |
| 126 | + if (developmentBranchPattern == null) { |
| 127 | + logger.debug("Using default development Pattern."); |
| 128 | + developmentBranchPattern = "(origin/)?develop"; |
| 129 | + } |
| 130 | + logger.debug("Development Branch Pattern: " + developmentBranchPattern); |
| 131 | + |
| 132 | + if (featureOrBugfixBranchPattern == null) { |
| 133 | + logger.debug("Using default feature or bugfix Pattern."); |
| 134 | + featureOrBugfixBranchPattern = "(origin/)?(?:feature|bugfix)/(.*)"; |
| 135 | + } |
| 136 | + logger.debug("Feature or Bugfix Branch Pattern: " + featureOrBugfixBranchPattern); |
| 137 | + |
| 138 | + if (otherDeployBranchPattern == null) { |
| 139 | + logger.debug("Using default other deployment branch Pattern."); |
| 140 | + otherDeployBranchPattern = ""; |
| 141 | + } |
| 142 | + logger.debug("Other Branch Deployment Pattern: " + otherDeployBranchPattern); |
| 143 | + |
| 144 | + if (otherBranchVersionDelimiter == null) { |
| 145 | + logger.debug("Using default otherBranchVersionDelimiter."); |
| 146 | + otherBranchVersionDelimiter = "+"; |
| 147 | + } |
| 148 | + |
| 149 | + ScmUtils scmUtils = new ScmUtils(systemEnvVars, scmManager, session.getTopLevelProject(), new PlexusLoggerToMavenLog(logger), masterBranchPattern, supportBranchPattern, releaseBranchPattern, hotfixBranchPattern, developmentBranchPattern); |
| 150 | + branchInfo = scmUtils.resolveBranchInfo(gitBranchExpression); |
| 151 | + } else { |
| 152 | + logger.debug("Unable to configure gitflow-helper-maven-plugin lifecycle extensions. No Plugin configuration found."); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private String extractPluginConfigValue(String parameter, Plugin plugin) { |
| 157 | + String value = extractConfigValue(parameter, plugin.getConfiguration()); |
| 158 | + for (int i = 0; i < plugin.getExecutions().size() && value == null; i++) { |
| 159 | + value = extractConfigValue(parameter, plugin.getExecutions().get(i).getConfiguration()); |
| 160 | + } |
| 161 | + return value; |
| 162 | + } |
| 163 | + |
| 164 | + private String extractConfigValue(String parameter, Object configuration) { |
| 165 | + try { |
| 166 | + return ((Xpp3Dom) configuration).getChild(parameter).getValue(); |
| 167 | + } catch (Exception ignored) { |
| 168 | + } |
| 169 | + return null; |
| 170 | + } |
| 171 | +} |
0 commit comments