Phing update version number in XML manifest

87 Views Asked by At

I need to add the ability to a phing build to:

  1. Parse an existing xml file within the project area to get an existing build number (in format 1.2.3)
  2. Ask the user what type of 'change' this is (i.e. major, minor, fix)
  3. Based on the response of the user at the time of run, upgrade the respective digit from the build number (if major increase 1 by 1; if minor increase 2 by 1; if fix increase 3 by 1)
  4. Store the build number back into the original xml file
  5. Have the new build number available for use when naming a zip file (later in the build).

Wondering if anyone already has a phing build file that does something like this or if you happen to know what phing tasks might help with these steps?

1

There are 1 best solutions below

0
Siad Ardroumli On

As a starting point you could do it without overhead using the version task (it uses a property file to store the version information) or with some more effort from a xml file.

The following example build script (documentation links can be found in the description attributes) contains both ways.

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

<project name="version test"
         default="help"
         phingVersion="3.0"
         description="https://stackoverflow.com/questions/68584221/phing-update-version-number-in-xml-manifest"
>

    <target name="help" description="usage help">
        <echo>Usage:</echo>
        <echo>bin/phing xml-file-based-workflow</echo>
        <echo>bin/phing property-file-based-workflow</echo>
    </target>

    <target name="xml-file-based-workflow"
            description="version handling with xml file"
            depends="user-input,handle-xml-version,use-version"
    />

    <target name="property-file-based-workflow"
            description="version handling with property file"
            depends="user-input,handle-property-version,use-version"
    />

    <target name="user-input"
            description="https://www.phing.info/guide/hlhtml/#InputTask"
            hidden="true"
    >
        <input message="what is your release type?" propertyName="release.type" defaultValue="Bugfix"/>
    </target>

    <target name="handle-property-version"
            description="https://www.phing.info/guide/hlhtml/#VersionTask"
            hidden="true"
    >
        <version releasetype="${release.type}" file="VERSION.txt" property="version.number"/>
    </target>

    <target name="handle-xml-version"
            description="
                https://www.phing.info/guide/hlhtml/#XmlPropertyTask
                https://www.phing.info/guide/hlhtml/#EchoPropertiesTask
                https://www.phing.info/guide/hlhtml/#VersionTask
                https://www.phing.info/guide/hlhtml/#DeleteTask
                https://www.phing.info/guide/hlhtml/#EchoXMLTask
                    "
            hidden="true"
    >
        <xmlproperty file="VERSION.xml" />
        <echoproperties destfile="VERSION.txt" regex="/version\.number/"/>
        <version releasetype="${release.type}" file="VERSION.txt" property="version.number"/>
        <delete file="VERSION.txt"/>
        <echoxml file="VERSION.xml">
            <version>
                <number>${version.number}</number>
            </version>
        </echoxml>
    </target>

    <target name="use-version"
            description="https://www.phing.info/guide/hlhtml/#EchoTask"
            hidden="true"
    >
        <echo message="${version.number}" />
    </target>

</project>