package com.atlassian.maven.plugins.jgitflow;

import java.util.List;
import java.util.Map;

import com.atlassian.maven.plugins.jgitflow.exception.ProjectRewriteException;
import com.atlassian.maven.plugins.jgitflow.helper.ProjectHelper;
import com.atlassian.maven.plugins.jgitflow.rewrite.ProjectChangeset;
import com.atlassian.maven.plugins.jgitflow.rewrite.ProjectRewriter;

import com.google.common.base.Function;
import com.google.common.collect.Maps;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import static com.atlassian.maven.plugins.jgitflow.rewrite.ArtifactReleaseVersionChange.artifactReleaseVersionChange;
import static com.atlassian.maven.plugins.jgitflow.rewrite.ParentReleaseVersionChange.parentReleaseVersionChange;
import static com.atlassian.maven.plugins.jgitflow.rewrite.ProjectReleaseVersionChange.projectReleaseVersionChange;

@Mojo(name = "build-number", aggregator = true)
public class BuildNumberMojo extends AbstractJGitFlowMojo
{

    @Parameter(property = "buildNumber")
    private String buildNumber;

    @Parameter(defaultValue = "true", property = "updateDependencies")
    private boolean updateDependencies;

    @Component
    protected ProjectHelper projectHelper;

    @Component
    protected ProjectRewriter projectRewriter;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException
    {
        List<MavenProject> reactorProjects = getReactorProjects();
        String key = "buildNum";

        Map<String, String> originalVersions = projectHelper.getOriginalVersions(key, reactorProjects);
        Map<String, String> featureVersions = projectHelper.getOriginalVersions(key, reactorProjects);

        Map<String, String> featureSuffixedVersions = Maps.transformValues(featureVersions, new Function<String, String>()
        {
            @Override
            public String apply(String input)
            {
                if (input.endsWith("-SNAPSHOT"))
                {
                    return StringUtils.substringBeforeLast(input, "-SNAPSHOT") + "-build" + buildNumber;
                }
                else
                {
                    return input;
                }
            }
        });

        for (MavenProject project : reactorProjects)
        {
            ProjectChangeset changes = new ProjectChangeset()
                    .with(parentReleaseVersionChange(originalVersions, featureSuffixedVersions))
                    .with(projectReleaseVersionChange(featureSuffixedVersions))
                    .with(artifactReleaseVersionChange(originalVersions, featureSuffixedVersions, updateDependencies));
            try
            {
                projectRewriter.applyChanges(project, changes);
            }
            catch (ProjectRewriteException e)
            {
                throw new MojoExecutionException("Error updating poms with build numbers versions", e);
            }
        }
    }
}

@@ -51,190 +51,117 @@
 ort 
-java.util.List;%0Aimport java.util.Map;%0A%0Aimport com.atlassian.maven.plugins.jgitflow.exception.ProjectRewriteException;%0Aimport com.atlassian.maven.plugins.jgitflow.helper.ProjectHelper
+static com.atlassian.maven.plugins.jgitflow.rewrite.ArtifactReleaseVersionChange.artifactReleaseVersionChange
 ;%0Aim
@@ -157,32 +157,39 @@
 nChange;%0Aimport 
+static 
 com.atlassian.ma
@@ -218,31 +218,68 @@
 ewrite.P
-rojectChangeset
+arentReleaseVersionChange.parentReleaseVersionChange
 ;%0Aimport
@@ -275,24 +275,31 @@
 nge;%0Aimport 
+static 
 com.atlassia
@@ -344,94 +344,100 @@
 ctRe
-writer;%0A%0Aimport com.google.common.base.Function;%0Aimport com.google.common.collect
+leaseVersionChange.projectReleaseVersionChange;%0A%0Aimport java.util.List;%0Aimport java.util
 .Map
-s
 ;%0A%0Ai
@@ -793,39 +793,32 @@
 roject;%0A%0Aimport 
-static 
 com.atlassian.ma
@@ -842,73 +842,107 @@
 low.
-rewrite.ArtifactReleaseVersionChange.artifactReleaseVersionChange
+exception.ProjectRewriteException;%0Aimport com.atlassian.maven.plugins.jgitflow.helper.ProjectHelper
 ;%0Aim
@@ -938,39 +938,32 @@
 tHelper;%0Aimport 
-static 
 com.atlassian.ma
@@ -996,60 +996,23 @@
 te.P
-arentReleaseVersionChange.parentReleaseVersionChange
+rojectChangeset
 ;%0Aim
@@ -1008,39 +1008,32 @@
 angeset;%0Aimport 
-static 
 com.atlassian.ma
@@ -1074,54 +1074,93 @@
 ctRe
-leaseVersionChange.projectReleaseVersionChange
+writer;%0Aimport com.google.common.base.Function;%0Aimport com.google.common.collect.Maps
 ;%0A%0A@
@@ -1453,16 +1453,141 @@
 ncies;%0A%0A
+    @Parameter(defaultValue = %22-build%22, property = %22buildNumberVersionSuffix%22)%0A    private String buildNumberVersionSuffix;%0A%0A
     @Com
@@ -2470,24 +2470,40 @@
 HOT%22) + 
-%22-
 build
-%22
+NumberVersionSuffix
  + build

Allow to use custom build number version suffix instead of '-build'