<?xml version="1.0"?>
<!-- 
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 -->


<project
  xmlns:plugin="plugin"
  xmlns:ant="jelly:ant"
  xmlns:j="jelly:core"
  xmlns:maven="jelly:maven"
  xmlns:u="jelly:util"
  xmlns:define="jelly:define"
  xmlns:deploy="deploy"
  xmlns:x="jelly:xml">
  
  <goal name="plugin"
    description="Build a plugin jar and install to the maven.repo.local">

    <!-- Plugins are just a jar. -->
    <attainGoal name="jar:jar"/>

    <copy file="${maven.build.dir}/${maven.final.name}.jar"
      todir="${maven.repo.local}/${pom.groupId}/plugins" />

  </goal>

  <goal name="plugin:install" prereqs="plugin"
    description="Install the plugin jar, prepare Maven to expand it locally and clear caches">

    <plugin:uninstall name="${pom.artifactId}" />
    <!-- Don't clear the cache. RC3 will do this if we happened to clean up an additional version -->

    <copy file="${maven.build.dir}/${maven.final.name}.jar"
      todir="${maven.home}/plugins"/>
  </goal>

  <goal name="plugin:install-now" prereqs="plugin"
    description="Build a plugin and load it into the currently running instance of Maven.">
    <!-- The cache should remain intact through these operations -->
    <plugin:uninstall name="${pom.artifactId}" />
    <maven:uninstallPlugin artifactId="${pom.artifactId}" />
    <maven:installPlugin file="${maven.build.dir}/${maven.final.name}.jar" cache="true" />
  </goal>

  <goal name="plugin:uninstall" 
    description="Uninstall all versions of the plugin">

    <plugin:uninstall name="${pom.artifactId}" />
    <plugin:clearCache />
  </goal>

  <goal name="plugin:uninstall-now" prereqs="plugin:uninstall"
    description="Uninstall all versions of the plugin, including those in the currently running instance of Maven">
    <maven:uninstallPlugin artifactId="${pom.artifactId}" />
  </goal>

  <goal name="plugin:deploy" prereqs="plugin"
    description="Install a specific unpacked plugin but no new jar">

    <mkdir dir="${maven.plugin.unpacked.dir}/${maven.final.name}"/>
    <unzip src="${maven.build.dir}/${maven.final.name}.jar"
      dest="${maven.plugin.unpacked.dir}/${maven.final.name}"/>

    <echo message="" >
  Warning: Contents of ${maven.home}/plugins/${maven.final.name}.jar does not match
  the unpacked contents of ${maven.plugin.unpacked.dir}/${maven.final.name} directory.
    </echo>

  </goal>


  <!-- generate documentation -->

  <!-- generate docs that are usually missing -->
  <goal name="plugin:generate-docs"
    description="Generate navigation, goals and properties docs">
    <attainGoal name="plugin:generate-navigation" />
    <attainGoal name="plugin:generate-goals" />
    <attainGoal name="plugin:generate-properties" />
  </goal>

  <!-- generate a skeletal navigation.xml for the plugin specified
       by the 'plugin' variable -->
  <goal name="plugin:generate-navigation"
    description="Generate navigation.xml for the plugin">

    <j:set var="skip" value="false" />
    <j:set var="fileName">${maven.docs.src}/navigation.xml</j:set>
    <u:available file="${fileName}">
      <echo>Skipping file as '${fileName}' already exists</echo>
      <j:set var="skip" value="true" />
    </u:available>

    <j:if test="${!skip}">
      <mkdir dir="${maven.docs.src}" />
      <echo>Generating file '${fileName}'</echo>
      <j:file name="${fileName}" prettyPrint="true">
        <j:import file="${plugin.resources}/templates/navigation.jelly" inherit="true"/>
      </j:file>
    </j:if>
  </goal>

  <!-- generate a skeletal goals.xml for the plugin specified -->
  <goal name="plugin:generate-goals"
    description="Generate goals.xml for the plugin">

    <j:set var="skip" value="false" />
    <j:set var="fileName">${maven.docs.src}/goals.xml</j:set>
    <u:available file="${fileName}">
      <echo>Skipping file as '${fileName}' already exists</echo>
      <j:set var="skip" value="true" />
    </u:available>

    <j:if test="${!skip}">
      <j:set var="pluginScript" value="false" />
      <u:available file="${basedir}/plugin.jelly">
        <j:set var="pluginScript" value="true" />
      </u:available>

      <mkdir dir="${maven.docs.src}" />

      <j:if test="${!pluginScript}">
        <!-- no plugin.jelly - generate a file for empty goals -->
        <echo>Generating 'empty' goals file '${fileName}'</echo>
        <j:set var="template" value="${plugin.resources}/templates/goals-empty.jelly" />
      </j:if>

      <j:if test="${pluginScript}">
        <!-- generate docs based on plugin.jelly -->
        <u:file name="${basedir}/plugin.jelly" var="script" />
        <x:parse xml="${script}" var="def"/>
        <echo>Generating file '${fileName}'</echo>
        <j:set var="template" value="${plugin.resources}/templates/goals.jelly"/>
      </j:if>

      <j:file name="${fileName}" prettyPrint="true">
        <j:import file="${template}" inherit="true"/>
      </j:file>

    </j:if>
  </goal>

  <!-- generate properties documentation -->
  <goal name="plugin:generate-properties"
    description="Generate properties.xml for the plugin">


    <j:set var="skip" value="false" />
    <j:set var="fileName">${maven.docs.src}/properties.xml</j:set>
    <u:available file="${fileName}">
      <echo>Skipping file as '${fileName}' already exists</echo>
      <j:set var="skip" value="true" />
    </u:available>

    <j:if test="${!skip}">
      <j:set var="props" value="false"/>
      <j:set var="propsFileName">${basedir}/plugin.properties</j:set>
      <u:available file="${propsFileName}">
        <j:set var="props" value="true" />
      </u:available>

      <j:if test="${!props}">
        <echo>Generating 'empty' properties file '${fileName}'</echo>
        <j:set var="template" value="${plugin.resources}/templates/properties-empty.jelly"/>
      </j:if>

      <j:if test="${props}">
        <echo>Generating file '${fileName}'</echo>
        <j:scope>
          <u:properties file="${propsFileName}" />
          <j:set var="vars" value="${context.variables}" scope="parent"/>
        </j:scope>
        <j:set var="template" value="${plugin.resources}/templates/properties.jelly"/>
      </j:if>

      <mkdir dir="${maven.docs.src}" />

      <j:file name="${fileName}" prettyPrint="true">
        <j:import file="${template}" inherit="true"/>
      </j:file>

    </j:if>
  </goal>
  
  <goal name="plugin:download" description="download and install a plugin from a remote repo" prereqs="plugin:download-artifact">
    <plugin:uninstall name="${artifactId}" />
    <u:file var="localPlugin" name="${maven.home}/plugins/${artifactId}-${version}.jar" />
    <ant:copy file="${localPluginFile}" tofile="${localPlugin}" />      
  </goal>

  <!-- download a plugin from a remote repo -->
  <goal name="plugin:download-artifact" description="download a plugin from a remote repo">
    <maven:param-check value="${artifactId}" fail="true" message="'artifactId' must be specified"/>
    <maven:param-check value="${groupId}" fail="true" message="'groupId' must be specified"/>
    <maven:param-check value="${version}" fail="true" message="'version' must be specified"/>
    
    <u:file var="localPluginFile" 
      name="${maven.repo.local}/${groupId}/plugins/${artifactId}-${version}.jar" />        
    <j:if test="${!localPluginFile.exists()}">
      <ant:mkdir dir="${maven.repo.local}/${groupId}/plugins" />
      <j:set var="repoList">${maven.repo.remote}</j:set>
      <u:tokenize var="repos" delim=",">${repoList.trim()}</u:tokenize>
    
      <j:forEach var="repo" items="${repos}">
        <echo>repo is '${repo}'</echo>
        <j:set var="remoteFile"
          value="${repo}/${groupId}/plugins/${artifactId}-${version}.jar" />
        <echo>trying to download ${remoteFile}</echo>
        <j:catch var="ex">
          <j:invokeStatic var="dummy" method="getFile" 
            className="org.apache.maven.util.HttpUtils">
            <j:arg type="java.lang.String" value="${remoteFile}" />
            <j:arg type="java.io.File" value="${localPluginFile}"/>
            <j:arg type="boolean" value="false"/>
            <j:arg type="boolean" value="true"/>
            <j:arg type="java.lang.String" value="${maven.proxy.host}" />
            <j:arg type="java.lang.String" value="${maven.proxy.port}" />
            <j:arg type="java.lang.String" value="${maven.proxy.username}" />
            <j:arg type="java.lang.String" value="${maven.proxy.password}" />
            <j:arg type="boolean" value="false"/>
          </j:invokeStatic>
        </j:catch>
        <j:break test="${localPluginFile.exists()}"/>
      </j:forEach>
    </j:if>
    
    <j:set var="downloaded" value="${localPluginFile.exists()}"/>
    <j:if test="${!downloaded}">
      <ant:fail message="Unable to find plug-in" />    
    </j:if>
  </goal>

  <!--
     ========================================================================
       Goals and tags used for testing Maven plugins.
     ========================================================================
  -->
    
  <!-- test a plugin -->
  <goal name="plugin:test">
    <u:available file="${basedir}/src/plugin-test/project.xml">
      <maven:maven
        descriptor="${basedir}/src/plugin-test/project.xml"
        goals="testPlugin"
        ignoreFailures="false" />
    </u:available>
  </goal>

  <goal name="plugin:repository-deploy" prereqs="plugin">
    <maven:makeRelativePath separator="/" basedir="${basedir}" path="${maven.build.dir}/${maven.final.name}.jar" var="artifactFilename" />
    <deploy:artifact
      artifact="${artifactFilename}"
      type="plugins"
      assureDirectoryCommand="mkdir -p"
      siteCommand="cd @deployDirectory@; chmod g+w ${maven.final.name}.jar; chgrp ${maven.remote.group} ${maven.final.name}.jar"
    />
  </goal>

  <define:taglib uri="plugin">
    <define:tag name="uninstall">
      <ant:delete verbose="false" failonerror="false">
        <ant:fileset dir="${maven.home}/plugins">
          <ant:include name="${name}-*.jar" />
        </ant:fileset>
      </ant:delete>

      <ant:delete includeEmptyDirs="true" verbose="false" failonerror="false">
        <ant:fileset dir="${maven.plugin.unpacked.dir}">
          <ant:include name="${name}-*/**" />
          <ant:include name="${name}-*.jar" />
        </ant:fileset>
      </ant:delete>
    </define:tag>

    <define:tag name="clearCache">
      <ant:delete includeEmptyDirs="true" verbose="false" failonerror="false">
        <ant:fileset dir="${maven.plugin.unpacked.dir}">
          <ant:include name="*.cache"/>
          <ant:include name="**/.processed" />
        </ant:fileset>
      </ant:delete>
    </define:tag>
  </define:taglib>
    
  <define:taglib uri="assert">
    <define:tag name="assertFileExists">
      <!-- @file : Full file path -->
      <!-- @msg  : optional message to be displayed -->
      <u:file var="fileAsFile" name="${file}"/>
      <j:if test="${!(fileAsFile.exists())}">
        <ant:fail>${file} does not exist.${msg}</ant:fail>
      </j:if>      
    </define:tag>
    <define:tag name="assertEquals">
      <!-- @expected  : the expected value -->
      <!-- @actual    : the actual value   -->
      <!-- @msg       : optional message to be displayed -->
      <j:if test="${not(expected.equals(value))}">
        <ant:fail>Expected [${expected}] but got [${value}].${msg}</ant:fail>
      </j:if>
    </define:tag>
  </define:taglib>

</project>
