Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ant-how can i return value from macrodef?

I need to compare two string args, from which I used to get one arg as runtime input (e.g. platform=windows,ios,mac) and another one has list of values, defined under build.properties (e.g. project.supportedplatforms=windows,mac). If the condition matches, then it should return "true" else "fail" from one macrodef to some target.

<for list="${platform}" param="platformparam" trim="true">
  <sequential>
    <if>
      <isItemExists retToProp="@{platformparam}" />
      <then>
        <antcall target="package.@{platformParam}" />
      </then>
    </if>
  </sequential>
</for>

<macrodef name="isItemExists">
  <attribute name="retToProp" />
  <property name="itemtosearch" value="@{retToProp}" />
  <for list="${project.supportedplatforms}" param="listparam" trim="true">
    <if>
      <equals arg1="@{listparam}" arg2="@{platformparam}" />
      <then>
        <!-- return true -->
      </then>
      <else>
        <!-- return false -->
      </else>
    </if>
  </for>
</macrodef>

When ${platforms} and ${project.supportedplatforms} having same value it should call the specified target. But in this snippets, the macrodef-for loop will executes for n times and at last what the value is assigned for @{returnproperty}, is going to throw for target "build", if it happens like this with valid input, it will not do my stuff, because for loop will execute in sequential manner. (e.g. platforms=windows,mac,android, project.supportedplatforms=ios,android,windows, if my list looks like this means, is there any possible way to get my result).

<for list="${platforms}" param="platformparam" trim="true">
  <sequential>
    <isItemExists returnProperty="returnProp" platforms="@{platformparam}" />
    <if>
      <equals arg1="${returnProp}" arg2="true" />
      <then>
        <antcall target="package.@{platformparam}" />
      </then>
    </if>
  </sequential>
</for>

<macrodef name="isItemExists">
  <attribute name="platform" />
  <attribute name="returnProperty" />
  <sequential>
    <var name="@{returnProperty}" unset="true" />
    <for list="${project.supportedplatforms}" param="listparam" trim="true">
      <if>
        <equals arg1="@{listparam}" arg2="@{platform}" />
        <then>
          <property name="@{returnProperty}" value="true" />
        </then>
        <else>
          <property name="@{returnProperty}" value="false" />
        </else>
      </if>
  </sequential>
</macrodef>
like image 260
jass Avatar asked Oct 31 '22 22:10

jass


2 Answers

<target name="some-test-target">
        <for list="${platform}" param="platformparam" trim="true">
            <sequential>
                <isItemExists platform="@{platformparam}" returnProperty="returnProp" />
                <if>
                    <equals arg1="${returnProp}" arg2="true"/>
                    <then>
                        <antcall target="package.@{platformparam}"/>
                    </then>
                </if>
            </sequential>
        </for>
</target>

Use sequential task for running for ant-contrib task like:

<macrodef name="isItemExists">
        <attribute name="platform"/>
        <attribute name="returnProperty"/>
        <sequential>
            <var name="@{returnProperty}" unset="true"/>
            <for list="${project.supportedplatforms}" param="listparam" trim="true">
                <sequential>
                <if>
                    <equals arg1="@{listparam}" arg2="@{platform}"/>
                    <then>
                        <property name="@{returnProperty}" value="true"/>
                    </then>
                    <else>
                        <property name="@{returnProperty}" value="false"/>
                    </else>
                </if>
                </sequential>
            </for>
        </sequential>
</macrodef>
like image 161
user3584056 Avatar answered Jan 04 '23 15:01

user3584056


I have an example that returns the time from a call to <tstamp>

Hope this helps!

<macrodef name="gettime">
    <attribute name="time-stamp" default=""/>
    <sequential>
        <tstamp>
            <format property="time-stamp" pattern="yyyyMMdd_HHmmss" />
        </tstamp>
    </sequential>   
</macrodef>

<target name="test-time">
    <gettime/>
    <echo message="${time-stamp}" />
</target>
like image 30
EllaVader Avatar answered Jan 04 '23 15:01

EllaVader