Search code examples
regexxmlcommand-linephing

How to list hidden targets in Phing


My build.xml has 134 targets, most of which are hidden (hidden="true"). Is there a way to list all targets from the commandline? Target-definitions are sometimes split over multiple-lines, and I usually use double-quote-characters for properties. I'm running this on Debian, and kudos for sorting targets and/or also displaying descriptions. :-)

Examples:

    <target name="example1" hidden="false" />

    <target name="example3" hidden="true" />

    <target
        description="Ipsum lorem"
        hidden="true"
        name='example3'
    >
        <phingcall target="example1" />
    </target>

Solution

  • We can't do this with Phing, but we can within Phing. There's probably a cleaner, better way than this, but this works - assuming all other properties are wrapped in double quotes (i.e. it just passes example#3, above)

    <target name="list_all" hidden="false">
        <property name="command" value="
            cat ${phing.file.foo}
            | perl -pe 's|^\s*||g' 
            | perl -0pe 's|\n([^&lt;])| \1|gs'
            | grep '&lt;target'
            | perl -pe &quot;s|name='([^']*)'|name=\&quot;\1\&quot;|g&quot;
            | perl -pe 's|^&lt;target(\s?)||'
            | perl -pe 's|(.*)([ ]?)depends=&quot;([^&quot;]*)&quot;([ ]?)(.*)|\1 \2|g'
            | perl -pe 's|(.*)([ ]?)hidden=&quot;([^&quot;]*)&quot;([ ]?)(.*)|\1 \2|g'
            | perl -pe 's|.*description=&quot;([^&quot;]*).*name=&quot;([^&quot;]*).*|name=&quot;\2&quot; description=&quot;\1&quot;|g'
            | perl -pe 's|name=&quot;([^&quot;]*)&quot;|\1|g'
            | perl -pe 's|description=&quot;([^&quot;]*)&quot;|[\1]|g'
            | sort
            | uniq
        " override="true" />
        <exec command="${command}" passthru="true" />
    </target>
    

    What do those lines do?

    1) output the contents of build.xml. Here, I'm interested in my 'global' build.xml which is named 'foo';

    2) remove all leading whitespace from each line;

    3) remove line-breaks within each opening tag;

    4) filter for lines starting "<target";

    5) change single quote-marks on name-property to double;

    6, 7,8) remove leading "<target", and depends and hidden properties;

    9) move 'description' after 'name' on each line;

    10) remove 'name=' and its quote-marks;

    11) replace 'description=' and its quote-marks with square-brackets; and

    12, 13) sort & remove duplicates (if any)

    Sample output:

        example1
        example2
        example3 [Ipsum lorem]