前几天,我需要将一个Drupal项目从我的源文件夹复制到另一个文件夹,因此,我决定手动创建一个Phing脚本,而不是手动复制这些文件,而不是手动复制这些文件。此Phing脚本会将您的Drupal项目导出到另一个目录,更改数据库凭据并创建该项目的zip和tar文件。首先要做的是创建一个属性,这是该文件的内容。
drupal.destination.database.host=externalhost drupal.destination.database.database= drupaldatabase drupal.destination.database.password=password drupal.destination.database.username=username drupal.destination.database.prefix=dru_ drupal.destination.database.url=mysql://${drupal.destination.database.username}:${drupal.destination.database.password}@${drupal.destination.database.host}/${drupal.destination.database.database} drupal.source.directory=D:/drupal/ drupal.source.name=MyDrupalProject drupal.destination.directory=D:/${drupal.source.name}/ drupal.destination.compressedfile=${drupal.source.name}
只需将此文件另存为build.properties并将属性更改为所需的设置即可。
的构建文件部分包含一个文件集,该文件集包含所有需要的文件,然后包含一些用于处理这些文件的目标。那些曾经使用过Phing和Drupal的人会注意到,我们包含了很多文件,但缺少一个名为settings.php的文件。这是故意的,因为尝试将build.properties文件中的参数匹配到文件中的数据库设置settings.php会很棘手。因此,我们不必输入多个数据库参数,而只需使用目标参数,default.settings.php文件(我们将知道其格式)和少量正则表达式替换即可settings.php为目标创建一个新文件。这是构建文件的内容。
<?xml version="1.0"?> <project name="DrupalExport" default="main"> <!-- Include properties file. --> <property file="build.properties" /> <fileset dir="${drupal.source.directory}" id="DrupalFiles"> <include name="cron.php" /> <include name="index.php" /> <include name="install.php" /> <include name="robots.txt" /> <include name="update.php" /> <include name="xmlrpc.php" /> <include name=".htaccess" /> <include name="scripts/**" /> <include name="themes/**" /> <include name="misc/**" /> <include name="modules/**" /> <include name="profiles/**" /> <include name="includes/**" /> <include name="sites/all/**" /> <include name="sites/default/files/**" /> <include name="sites/default/default.settings.php" /> </fileset> <target name="prepare"> <!-- Reset the permissions on thesetttings.phpfile so we can delete it. --> <exec command="php -r "var_dump(chmod('${drupal.destination.directory}sites/default/settings.php', 0777));"" /> <!-- Delete the destination directory. --> <delete dir="${drupal.destination.directory}" /> <echo>Prepare complete.</echo> </target> <target name="copyFiles" depends="prepare"> <copy todir="${drupal.destination.directory}" overwrite="false" tstamp="false"> <fileset refid="DrupalFiles" /> </copy> <echo>copyFiles complete</echo> </target> <target name="drupalDatabaseConnection" depends="copyFiles"> <copy file="${drupal.source.directory}/sites/default/default.settings.php" tofile="${drupal.destination.directory}/sites/default/settings.php" overwrite="true"> <filterchain> <replaceregexp> <!-- Add some rules to replace settings with real values. --> <regexp pattern="\$db_url = 'mysql:\/\/username:password\@localhost\/databasename';" replace="\$db_url = '${drupal.destination.database.url}';" ignoreCase="true"/> <regexp pattern="\$db_prefix = '';" replace="\$db_prefix = '${drupal.destination.database.prefix}';" ignoreCase="true"/> </replaceregexp> </filterchain> </copy> <!-- Makesettings.phpreadonly --> <exec command="php -r "var_dump(chmod('${drupal.destination.directory}sites/default/settings.php', 0444));"" /> <echo>drupalDatabaseConnection complete</echo> </target> <target name="zipFiles" depends="drupalDatabaseConnection"> <fileset dir="${drupal.destination.directory}" id="DestinationFiles"> <include name="**" /> </fileset> <!-- Delete original file. --> <delete file="${drupal.destination.compressedfile}.zip" /> <zip destfile="${drupal.destination.compressedfile}.zip"> <fileset refid="DestinationFiles" /> </zip> <echo>zipFiles complete</echo> </target> <target name="tarFiles"> <fileset dir="${drupal.destination.directory}" id="DestinationFiles"> <include name="**" /> </fileset> <!-- Delete original file. --> <delete file="${drupal.destination.compressedfile}.tar" /> <tar destfile="${drupal.destination.compressedfile}.tar" compression="gzip"> <fileset refid="DestinationFiles" /> </tar> <echo>tarFiles complete</echo> </target> <target name="main"> <phingcall target="copyFiles" /> <phingcall target="zipFiles" /> <phingcall target="tarFiles" /> <echo>Export finished!</echo> </target> </project>
要运行此脚本,请将此xml保存到名为drupalexport.xml的文件中,请使用以下命令来运行它。
phing -f drupalexport.xml
现在,您将拥有一个包含您的Drupal项目的目录,以及一个包含上面目录中的相同文件的zip和tar文件。