<?xml version="1.0" encoding="UTF-8"?>

<project name="MailMimeParser" default="nothing">
    
    <property name="version-file" value="version.txt"/>
    <loadfile property="version" file="version.txt"/>
    <property name="release-type" value=""/>
    <property name="docs-dir" value="docs"/>
    <property name="phpdocs-dir" value="${docs-dir}/phpdocs"/>
    <property name="mdapi-dir" value="${docs-dir}/md-api"/>
    <property name="wiki-git" value="git@github.com:zbateson/MailMimeParser.wiki.git"/>
    
    <target name="nothing" hidden="true">
        <echo>
            Used for creating a release.  Default target does nothing.
            Call phing with "-l" to see available targets.
        </echo>
    </target>
    
    <target name="clone-docs" description="Clones documentation from remote">
        <echo>Cloning ${wiki-git} to ${mdapi-dir}</echo>
        <exec executable="git" logoutput="true">
            <arg value="clone"/>
            <arg value="${wiki-git}"/>
            <arg path="${mdapi-dir}"/>
        </exec>
    </target>
    
    <target name="push-docs" description="Pushes changed documentation to git remote">
        <echo>Executing "git add -A" on ${mdapi-dir}</echo>
        <exec executable="git" dir="${mdapi-dir}" logoutput="true">
            <arg value="add"/>
            <arg value="-A"/>
        </exec>
        <echo>git commit</echo>
        <exec executable="git" dir="${mdapi-dir}" logoutput="true">
            <arg value="commit"/>
            <arg value="-m"/>
            <arg value="API update for version: ${version}"/>
        </exec>
        <echo>git push</echo>
        <exec executable="git" dir="${mdapi-dir}" logoutput="true">
            <arg value="push"/>
        </exec>
    </target>
    
    <target name="generate-docs" description="Creates MD-formatted API documentation">
        
        <echo>Generating documentation for ${version}</echo>
        <mkdir dir="${phpdocs-dir}"/>
        <mkdir dir="${mdapi-dir}"/>
        
        <echo>Calling phpdoc and generating XML in ${phpdocs-dir}</echo>
        <exec executable="vendor/bin/phpdoc">
            <arg value="-d"/>
            <arg path="src"/>
            <arg value="-t"/>
            <arg path="${phpdocs-dir}"/>
            <arg value="--template=xml"/>
        </exec>
        <echo>Calling phpdocmd and generating MD in ${mdapi-dir}</echo>
        <exec executable="vendor/bin/phpdocmd">
            <arg value="--lt"/>
            <arg value="%c"/>
            <arg path="${phpdocs-dir}/structure.xml"/>
            <arg path="${mdapi-dir}"/>
        </exec>
    </target>
    
    <target name="cleanup-docs" description="Deletes documentation directories">
        <delete dir="${docs-dir}" quiet="true"/>
    </target>
    
    <target name="release" description="Bumps the version number, then updates documentation">
        <!--
            The following IfTask must be kept inside 'release'. Using phingcall and setting a property
            inside a different target sets the property within that target's scope only.
        -->
        <if>
            <equals arg1="${release-type}" arg2=""/>
            <then>
                <property name="release-type" value="Bugfix" override="true"/>
                <echo>Enter a release type (1 to 3) or default to 3.</echo>
                <echo>1. Major</echo>
                <echo>2. Minor</echo>
                <echo>3. Bugfix</echo>
                <input propertyname="release-type-index" defaultValue="3" promptChar=":">     Enter</input>
                <if>
                    <equals arg1="${release-type-index}" arg2="1"/>
                    <then>
                        <echo>Setting from ${release-type} to Major, no?</echo>
                        <property name="release-type" value="Major" override="true"/>
                        <echo>Now it is ${release-type}</echo>
                    </then>
                    <elseif>
                        <equals arg1="${release-type-index}" arg2="2"/>
                        <then>
                            <property name="release-type" value="Minor" override="true"/>
                        </then>
                    </elseif>
                </if>
            </then>
        </if>
        <echo>${release-type} Release - incrememting version number</echo>
        <version releasetype="${release-type}" file="version.txt" property="version"/>
        <phingcall target="cleanup-docs"/>
        <phingcall target="clone-docs"/>
        <phingcall target="generate-docs"/>
        <phingcall target="push-docs"/>
        <phingcall target="cleanup-docs"/>
    </target>
    
</project>
