<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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 name="org.apache.easyant.plugins#default-version-strategy" xmlns:ivy="antlib:org.apache.ivy.ant"
    xmlns:ac="antlib:net.sf.antcontrib" xmlns:ea="antlib:org.apache.easyant">
    <ea:core-version requiredrevision="[0.9,+]" />

    <ea:plugin module="ivy-publication" revision="0.9" />

    <target name="default-version-strategy:init">
        <ea:parameter property="release.resolver" default="shared"
            description="the name of the resolver use for release publication" />
        <ea:parameter property="shared.resolver" default="shared" description="the name of the share resolver" />
        <ea:parameter property="project.ivy.instance" default="project.ivy.instance" description="ivy instance name" />
        <ea:parameter property="project.ivy.file" required="true"
            description="ivy file (use to resolve dependencies for the project)" />
        <ea:parameter property="default.build.number" default="1" description="default project version" />
        <ea:parameter property="revision.separator" default="-dev-build" description="separator for version" />
        <!-- is project ivy settings configured ? -->
        <ea:parameter property="project.ivy.settings.file" description="the ivysettings file used by your project" />
        <ea:parameter property="project.ivy.settings.url" description="the ivysettings url used by your project" />

        <ea:parameter property="fail.publication.if.no.revision" default="true"
            description="fail build if current module has no revision in 'info' tag of the module.ivy" />
        <condition property="is.project.ivy.settings.configured">
            <isreference refid="${project.ivy.instance}" />
        </condition>
        <fail unless="is.project.ivy.settings.configured"
            message="project ivy instance is not configured, at least one of the following property must be set : project.ivy.settings.file, project.ivy.settings.url" />
    </target>

    <macrodef name="check-version-number">
        <sequential>
            <property name="pub.no.revision.message"
                value="It seems that you did not specify the revision number of your project, we strongly recommend you to set it through the 'info' tag in your module ivy before any publication" />
            <ac:if>
                <contains string="${ivy.revision}" substring="working@" />
                <ac:then>
                    <ac:if>
                        <istrue value="${fail.publication.if.no.revision}" />
                        <ac:then>
                            <fail message="${pub.no.revision.message}" />
                        </ac:then>
                        <ac:else>
                            <echo message="Warning : ${pub.no.revision.message}" level="warning" />
                        </ac:else>
                    </ac:if>
                </ac:then>
            </ac:if>
        </sequential>
    </macrodef>

    <!-- version numbers -->
    <target name="default-version-strategy:local" depends="default-version-strategy:init" extensionOf="ivy-publication:generate-local-version">
        <check-version-number />
        <tstamp>
            <format property="now" pattern="yyyyMMddHHmmss" />
        </tstamp>
        <property name="version" value="${ivy.revision}-local-${now}" />
    </target>

    <target name="default-version-strategy:shared" depends="default-version-strategy:init" unless="ivy.new.revision"
        extensionOf="ivy-publication:generate-shared-version">
        <ea:checkresolver resolver="shared.resolver" settingsRef="${project.ivy.instance}"
            description="name of the repository used to publish shared integration versions (snapshots)" />
        <check-version-number />
        <ivy:buildnumber organisation="${ivy.organisation}" module="${ivy.module}" revision="${ivy.revision}"
            defaultBuildNumber="${default.build.number}" revSep="${revision.separator}" settingsRef="${project.ivy.instance}"
            resolver="${shared.resolver}" />
        <property name="version" value="${ivy.new.revision}" />
    </target>

    <target name="default-version-strategy:release" depends="default-version-strategy:init" unless="ivy.new.revision"
        extensionOf="ivy-publication:generate-release-version">
        <check-version-number />
        <property name="version" value="${ivy.revision}" />
    </target>
</project>