16

有谁知道如何使用 Phing 查找和替换文件中的文本?

4

6 回答 6

30

如果您不想复制文件而只是替换文件所在的当前文件夹中的字符串,请执行自反任务

<reflexive>
    <fileset dir=".">
        <include pattern="*.js" />
    </fileset>
    <filterchain>
        <replaceregexp>
            <regexp pattern="SEARCH" replace="REPLACEMENT"/>
        </replaceregexp>
    </filterchain>
</reflexive>
于 2011-11-08T09:20:36.280 回答
25

您可以使用过滤器替换文件内的文本。过滤器用于其他文件操作任务,例如复制。

我相信过滤器背后的主要思想是,您可以使用带有标记而不是实际值的模板文件,然后将标记替换为复制过程的一部分。

快速示例:将数据库配置模板文件存储在模板目录中。然后使用以下命令将其复制到目标配置文件:

<copy file="templates/database.config.php.tpl" tofile="config/database.config.php" overwrite="true">
                <filterchain>
                    <replacetokens begintoken="%%" endtoken="%%">
                        <!-- MySQL TOKENS -->
                        <token key="dbname" value="${db.mysql.dbname}" />
                        <token key="dbhost" value="${db.mysql.host}" />
                        <token key="dbport" value="${db.mysql.port}" />
                        <token key="dbuser" value="${db.mysql.username}" />
                        <token key="dbpassword" value="${db.mysql.password}" />
                    </replacetokens>
                </filterchain>
            </copy>

还有很多其他可用的过滤器(例如正则表达式搜索和替换)。在文档中查看有关过滤器的更多信息:http: //phing.info/docs/guide/stable/chapters/appendixes/AppendixD2-CoreFilters.html

于 2011-02-06T15:36:55.260 回答
8

我一直在寻找同样的东西,我发现存在一个名为ExpandProperties的过滤器,它允许替换复制文件中的属性。例如,我在 apache 虚拟主机模板中使用它:

<target name="apache-config" description="Generates apache configuration">
    <!-- Default value for Debian/Ubuntu -->
    <property name="apache.vhost.dir" value="/etc/apache2/sites-available" override="false"/>
    <copy file="${application.startdir}/docs/vhost.conf.tpl" todir="${apache.vhost.dir}" overwrite="true">
        <filterchain>
            <expandproperties/>
        </filterchain>
    </copy>
    <echo message="Apache virtual host configuration copied, reload apache to activate it"/>
</target>

并在模板文件中

<VirtualHost *:80>
   DocumentRoot "${application.startdir}/public"
   ServerName ${apache.default.host}

   <Directory "${application.startdir}/public">
       Options Indexes MultiViews FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

</VirtualHost>

通过这种方式,您无需显式列出要替换的所有标记,非常有用...

于 2011-07-19T11:34:09.733 回答
2

我在我的 phing build.xml 文件中使用它

<exec command="find ./ -type f -name '*.php' | xargs sed -i 's|x--Jversion--x|${jversion}|g'" dir="${targetdir}/_package/${extname}.${package.version}" /> 
于 2012-03-29T09:25:25.117 回答
1

使用“传统”工具实现这一目标的最简单方法是sed

sed -i 's/old/new/g'  myfile.txt

如果它是基于蚂蚁的,那么这应该会有所帮助:http ://ant.apache.org/manual/Tasks/replace.html

最简单的形式是<replace file="myfile.html" token="OLD" value="NEW"/>.

如果您真的需要它,您可以使用 ant 运行外部工具,如http://ant.apache.org/manual/Tasks/exec.html中所述,这意味着您可以调用 sed 从蚂蚁有类似的东西:

 <exec executable="sed">
   <arg value="s/old/new/g" />
   <arg value="$MY_FILE" />
 </exec>
于 2011-02-05T15:56:09.820 回答
-1

Acme 给出的答案是正确的。如果您尝试将文件复制到自身以对其进行修改,则会大喊大叫,告诉您无法自行复制。

<reflexive file="./app/config/config.yml" tofile="./app/config/config.yml">
    <filterchain>
    <replacetokens begintoken="__" endtoken="__">
        <token key="BUILD_VERSION" value="Replace Value" />
    </replacetokens>
    </filterchain>
</reflexive>

这对我很有效。

于 2017-01-19T13:45:09.053 回答