如何开发替代重复代码块的方法 [英] How to develope a method to substitute repetitive code block

查看:14
本文介绍了如何开发替代重复代码块的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ant 的新手,所以我找不到一种方法来使我的 buld 文件更优雅一些.我相信有一种方法可以将重复的代码块替换到我的构建中.所以这里是构建文件:

I am new in ant, so I wasn't able to find an approach to make my buld file a bit more elegant. I believe there is an approach to substitute repetitive block of code into my build. So here is build file:

<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>

所以这部分脚本的作用几乎相同,但在项目中的几乎所有目标之间共享:

So this part of the script does pretty much the same, but shared among almost all the target in the 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,但据我所知它不返回,只接受一些属性形式的参数.无论如何,任何帮助将不胜感激.

This part does nothing but to obtain part of a path. For example if ${file} stands for /folder/subfolder1/subfolder2 then take the path after /folder namely subfolder1/subfolder2 and assign it to a property. I this case that property is named common-path that holds same path for all the target. I examined MacroDef Task, but as far as I understand it doesn't return, only accepts some parameters in form of attributes. Anyway, any help would be much appreciated.

推荐答案

您在考虑 以减少重复代码方面走在正确的轨道上.

You are on the right track in considering <macrodef> to reduce repetitive code.

虽然 确实不返回任何内容,但可以为 指定要设置的属性的名称.例如...

While it's true that <macrodef> doesn't return anything, <macrodef> can be given the name of a property to set. For example...

<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!

在这个例子中, 的调用者告诉 macrodef 在 say-hi-to-riko 属性中返回"它的结果.

In this example, the caller of <my-hello> tells the macrodef to "return" its results in the say-hi-to-riko property.

知道这一点,脚本中的几个 可以转换为设置属性的 ......

Knowing this, several of the <target>s in your script can be converted to <macrodef>s that set properties...

<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>

一般来说,调用 s 比直接调用 s 更好.在上面的例子中, 被替换为 因为 让我们调用 宏定义>s.

In general, it's better to prefer calling <macrodef>s over calling <target>s directly. In the above example, <foreach> is replaced with <for> because <for> lets us call <macrodef>s.

输出

 [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.

这篇关于如何开发替代重复代码块的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆