<?xml version="1.0"?>

<project
  xmlns:j="jelly:core"
  xmlns:i="jelly:interaction"
  xmlns:u="jelly:util"
  xmlns:r="jelly:org.apache.maven.release.jelly.ReleaseTagLibrary">

  <!-- ================================================================== -->
  <!-- R E L E A S E  T A G  L I B R A R Y                                -->
  <!-- ================================================================== -->

  <!--
   | Process for a release:
   |
   | o tagging cvs
   | o building source distribution
   | o building binary distribution
   | o notifying lists - we need a mailtag that can deal with authorization.
   |   my smtp for example will not let me poke it without a password.
   | o pushing out RSS feeds maybe to javablogs?
   | o updating the <version/> element in the POM
   | o updating changes document
   | o updating the status page
   | o flipping the site from HEAD to release and deploy the site
   | o all SNAPSHOT references must be changed to timestamped versions
   | o incrementing the version - what rules do we want to follow? they
   |   have to be documented so we need a document to adhere to.
   | o Deploying the POM for the released version so there is always history on
   |   what went into a specific version. Eventually it would also be nice to
   |   trace back through the POMs so you could build any version of any
   |   project from sources at any point in time.
   | Q: so how do we find the latest timestamped version.
   | A: when a snapshot is deployed we need a file with the real version
   |    of the latest snapshot.
   -->

  <!--
   |
   | Convert SNAPSHOT version identifiers to real snapshot versions.
   | You cannot leave SNAPSHOT version identifiers in the POM for a
   | release.
   |
   -->
  <goal name="convert-snapshots">

    <r:resolve-snapshots
      transformer="transformer"
      transformations="transformations"/>

    <i:ask
      question="There are ${transformer.selectedNodeCount} snapshot dependencies, would you like to update them to use timestamped versions?"
      answer="answer"
      default="yes"/>

    <j:if test="${answer == 'yes'}">

      <j:forEach var="transformation" items="${transformations}">

        <!-- Display transformation info. Needs to be consumable by a GUI. -->
        
        <j:expr value="${transformation.beforeTransformation}"/>
        <echo/>
        <j:expr value="${transformation.afterTransformation}"/>
        <echo/>

        <i:ask
          question="Do you want to update this dependency?"
          answer="answer"
          default="yes"/>

        <j:if test="${answer == 'yes'}">

          <!-- Change the node. -->
          ${transformation.transform()}"/>

        </j:if>

      </j:forEach>

      <i:ask
        question="Would you like the update POM to be written out for the release?"
        answer="answer"
        default="yes"/>

      <j:if test="${answer == 'yes'}">
        <!-- Write out the pom. -->
        ${transformer.write()}
      </j:if>

    </j:if>
  </goal>

  <goal name="convert-snapshots-auto">

    <r:resolve-snapshots
      transformer="transformer"
      transformations="transformations"/>

      ${transformer.transformNodes()}
      ${transformer.write()}

  </goal>

  <goal name="increment-snapshot-version">
    <r:increment-snapshot-version
      transformer="transformer"
      transformations="transformations"/>
      ${transformer.transformNodes()}
      ${transformer.write()}
  </goal>

  <!--
   |
   | Validate the POM for a release. This consists of:
   |
   | 1) Making sure all SNAPSHOT version identifiers are converted.
   |
   -->
  <goal name="validate-pom-for-release">

    <j:useBean var="snapshots" class="java.util.ArrayList"/>

    <j:forEach var="dependency" items="${pom.dependencies}">
      <j:if test="${dependency.version == 'SNAPSHOT'}">
        <j:set var="dummy" value="${snapshots.add(dependency)}"/>
      </j:if>
    </j:forEach>

    <j:if test="${size(snapshots) > 0}">
      <echo/>
      <echo>The following dependencies have SNAPSHOT identifiers:</echo>
      <echo/>
      <j:forEach var="dependency" items="${snapshots}">
        <echo>${dependency.artifactId}</echo>
      </j:forEach>
      <fail message="">
      </fail>
    </j:if>

  </goal>

  <!--
   |
   | Release
   |
   -->
  <goal name="release">

    <attainGoal name="validate-pom-for-release"/>
    <attainGoal name="deploy:pom"/>

    <!--
     |
     | Build the binary and source distributions. We need to alter
     | the process so that we can build everything to check and then
     | deploy instead of having to build everything all the time.
     |
     -->

  </goal>

</project>
