如何开发一种替代重复代码块的方法

时间:2015-12-17 13:39:01

标签: ant

我是蚂蚁的新手,所以我无法找到让我的文件更优雅的方法。我相信有一种方法可以将重复的代码块替换为我的构建。所以这里是构建文件:

<project basedir="../../../" name="do-report" default="zip-all">
    <xmlproperty keeproot="false" file="implementation/xml/ant/properties.xml"/>
    <!--    -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${infrastructure-base-dir}/apache-ant-1.9.6/lib/ant-contrib-0.3.jar"/>
        </classpath>
    </taskdef>
    <!--    -->
    <target name="clean">
        <delete dir="${dita-odt.path.odt-unzipped-base-dir}" includeemptydirs="true" failonerror="no"/>
        <delete dir="examples/intermediate/odt-files" includeemptydirs="true" failonerror="no"/>
    </target>
    <!--    -->
    <target name="unzip-writing-odt-file" depends="clean">
        <unzip src="${dita-odt.path.writing-odt}" dest="${dita-odt.path.writing-odt-unzipped}"/>
    </target>
    <!--    -->
    <target name="extract-common-paths" depends="unzip-writing-odt-file">
        <foreach target="copy-text-path" param="file">
            <path>
                <fileset dir="${dita-odt.path.text-xml-base-dir}">
                    <include name="**/content.xml"/>
                </fileset>
            </path>
        </foreach>
    </target>
    <!--    -->
    <target name="copy-text-path" description="copy text-xml path relative to text-xml-base-dir">
        <dirname property="text-absolute-dir-path" file="${file}"/>
        <property name="absolute-path-text-base-dir" location="${dita-odt.path.text-xml-base-dir}"/>
        <pathconvert property="common-path" dirsep="/">
            <path location="${text-absolute-dir-path}"/>
            <map from="${absolute-path-text-base-dir}/" to=""/>
        </pathconvert>
        <antcall target="copy-writing-unzipped">
            <param name="common-path" value="${common-path}"/>
        </antcall>
    </target>
    <!--    -->
    <target name="copy-writing-unzipped">
        <echo>${common-path}</echo>
        <copy todir="${dita-odt.path.odt-unzipped-base-dir}/${common-path}">
            <fileset dir="${dita-odt.path.writing-odt-unzipped}">
                <include name="**/*"/>
            </fileset>
        </copy>
    </target>
    <!--   -->
    <target name="transform-all" depends="extract-common-paths">
        <foreach target="transform" param="file">
            <path>
                <fileset dir="${dita-odt.path.text-xml-base-dir}">
                    <include name="**/content.xml"/>
                </fileset>
            </path>
        </foreach>
    </target>
    <!--   -->
    <target name="transform">
        <basename property="file-base-name" file="${file}"/>
        <dirname property="file-dir-absolute-path" file="${file}"/>
        <property name="text-xml-base-dir-absolute-path" location="${dita-odt.path.text-xml-base-dir}"/>
        <pathconvert property="common-path" dirsep="/">
            <path location="${file-dir-absolute-path}"/>
            <map from="${text-xml-base-dir-absolute-path}/" to=""/>
        </pathconvert>
        <!--Substitutes backslashes with forword slashes. Basedir is a reserved property that returns absolute path with separator symbols of the current OS.-->
        <pathconvert dirsep="/" property="base-dir-unix">
            <path location="${basedir}"/>
        </pathconvert>
        <echo>TRANSFORM TO: ${dita-odt.path.odt-unzipped-base-dir}/${common-path}/${file-base-name}</echo>
        <xslt in="${file}" out="${dita-odt.path.odt-unzipped-base-dir}/${common-path}/${file-base-name}" style="${dita-odt.path.text-odt-xsl}" extension=".xml" force="true">
            <param name="dir-path-styles-xml" expression="${dita-odt.path.odt-unzipped-base-dir}/${common-path}"/>
            <param name="project-base-dir-absolute-path" expression="${base-dir-unix}"/>
            <classpath location="${infrastructure-base-dir}/${dita-odt.text-odt-xsl.processor}"/>
        </xslt>
    </target>
    <!--   -->
    <target name="zip-all" depends="transform-all" description="Turns all unzipped text folders into ODT files">
        <foreach target="zip-odt" param="file">
            <path>
                <fileset dir="${dita-odt.path.odt-unzipped-base-dir}" includes="**/content.xml" excludes="writing/**"/>
            </path>
        </foreach>
    </target>
    <!--    -->
    <target name="zip-odt">
        <basename property="file-base-name" file="${file}"/>
        <dirname property="file-dir-absolute-path" file="${file}"/>
        <!--This property will be used to provided name for the produced ODT file. The document will have the same name as the folder that contains it.-->
        <basename property="odt-doc-name" file="${file-dir-absolute-path}.odt"/>
        <property name="odt-unzipped-base-dir-absolute-path" location="${dita-odt.path.odt-unzipped-base-dir}"/>
        <pathconvert property="common-path" dirsep="/">
            <path location="${file-dir-absolute-path}"/>
            <map from="${odt-unzipped-base-dir-absolute-path}/" to=""/>
        </pathconvert>
        <echo>COMMON PATH: ${common-path}</echo>
        <zip destfile="examples/intermediate/odt-files/${common-path}/${odt-doc-name}" basedir="${dita-odt.path.odt-unzipped-base-dir}/${common-path}" update="true"/>
    </target>
    <!--    -->
</project>

因此脚本的这部分几乎完全相同,但在项目中的几乎所有目标中共享:

        <dirname property="file-dir-absolute-path" file="${file}"/>
        <property name="text-xml-base-dir-absolute-path" location="${dita-odt.path.text-xml-base-dir}"/>
        <pathconvert property="common-path" dirsep="/">
            <path location="${file-dir-absolute-path}"/>
            <map from="${text-xml-base-dir-absolute-path}/" to=""/>
        </pathconvert>

这部分除了获取路径的一部分外什么都不做。例如,如果${file}代表/folder/subfolder1/subfolder2,则取/folder之后的路径subfolder1/subfolder2并将其分配给属性。在这种情况下,该属性名为common-path,它为所有目标保存相同的路径。我检查了MacroDef Task,但据我所知,它并没有返回,只接受属性形式的一些参数。无论如何,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您正在考虑<macrodef>以减少重复代码。

虽然<macrodef>确实没有返回任何内容,但<macrodef>可以被赋予要设置的属性的名称。例如......

<macrodef name="my-hello">
    <attribute name="person"/>
    <attribute name="output-property"/>
    <sequential>
        <property name="@{output-property}" value="Hello, @{person}!"/>
    </sequential>
</macrodef>

<my-hello person="Riko" output-property="say-hi-to-riko"/>
<echo>my-hello said: ${say-hi-to-riko}</echo>

...输出...

[echo] my-hello said: Hello, Riko!

在此示例中,<my-hello>的调用者告诉macrodef将其结果“返回”say-hi-to-riko属性。

了解这一点,脚本中的几个<target>可以转换为设置属性的<macrodef> ...

<project name="ant-macrodef-pathconvert" default="extract-common-paths">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    <property name="dita-odt.path.text-xml-base-dir" value="C:\temp\dita-odt"/>
    <macrodef name="my-pathconvert">
        <attribute name="file"/>
        <attribute name="common-path-property"/>
        <sequential>
            <!-- <local> allows multiple calls to a macrodef. -->
            <local name="file-dir-absolute-path"/>
            <echo>In my-pathconvert for @{file}</echo>
            <dirname property="file-dir-absolute-path" file="@{file}"/>
            <property name="text-xml-base-dir-absolute-path"
                location="${dita-odt.path.text-xml-base-dir}"/>
            <pathconvert property="@{common-path-property}" dirsep="/">
                <path location="${file-dir-absolute-path}"/>
                <map from="${file-dir-absolute-path}/" to=""/>
            </pathconvert>
        </sequential>
    </macrodef>
    <macrodef name="copy-text-path"
        description="copy text-xml path relative to text-xml-base-dir">
        <attribute name="file"/>
        <sequential>
            <local name="common-path"/>
            <echo>In copy-text-path for @{file}</echo>
            <my-pathconvert file="@{file}" common-path-property="common-path"/>
            <copy-writing-unzipped common-path="${common-path}"/>
        </sequential>
    </macrodef>
    <macrodef name="copy-writing-unzipped">
        <attribute name="common-path"/>
        <sequential>
            <echo>In copy-writing-unzipped for @{common-path}</echo>
            <echo>copy task goes here.</echo>
        </sequential>
    </macrodef>
    <target name="extract-common-paths">
        <for param="file">
            <path>
                <fileset dir="${dita-odt.path.text-xml-base-dir}">
                    <include name="**/content.xml"/>
                </fileset>
            </path>
            <sequential>
                <copy-text-path file="@{file}"/>
            </sequential>
        </for>
    </target>
</project>

一般情况下,最好是直接调用<macrodef>来调用<target> s。在上面的示例中,<foreach>已替换为<for>,因为<for>允许我们调用<macrodef>

输出

 [echo] In copy-text-path for C:\temp\dita-odt\dir1\content.xml
 [echo] In my-pathconvert for C:\temp\dita-odt\dir1\content.xml
 [echo] In copy-writing-unzipped for C:/temp/dita-odt/dir1
 [echo] copy task goes here.
 [echo] In copy-text-path for C:\temp\dita-odt\dir2\content.xml
 [echo] In my-pathconvert for C:\temp\dita-odt\dir2\content.xml
 [echo] In copy-writing-unzipped for C:/temp/dita-odt/dir2
 [echo] copy task goes here.