Phing
PHP版ant。情報があんまりないなぁ…。Symfonyで使われてるみたいだけど。
ユーザガイド
http://phing.info/docs/guide/current/
インストール
% pear channel-discover pear.phing.info % pear install phing/phing
サンプル
例)temp,logディレクトリを作成して、777にする
build.xml
<?xml version="1.0"?>
<project name="myproject" default="install" basedir=".">
<target name="install">
<echo msg="Making directory" />
<mkdir dir="temp" />
<mkdir dir="log" />
<chmod file="temp" mode="0777" />
<chmod file="log" mode="0777" />
</target>
</project>
build.xmlがあるディレクトリでコマンドを実行
% phing
buildfile
http://phing.info/docs/guide/current/chapters/GettingStarted.html#XMLAndPhing
<?xml version="1.0"?>
<project name="FooBar" default="dist" basedir=".">
<target name="prepare">
<echo msg="Making directory ./build" />
<mkdir dir="./build" />
</target>
<target name="build" depends="prepare">
<echo msg="Copying files to build directory..." />
<echo msg="Copying ./about.php to ./build directory..." />
<copy file="./about.php" tofile="./build/about.php" />
<echo msg="Copying ./browsers.php to ./build directory..." />
<copy file="./browsers.php" tofile="./build/browsers.php" />
<echo msg="Copying ./contact.php to ./build directory..." />
<copy file="./contact.php" tofile="./build/contact.php" />
</target>
<target name="dist" depends="build">
<echo msg="Creating archive..." />
<tar destfile="./build/build.tar.gz" compression="gzip">
<fileset dir="./build">
<include name="*" />
</fileset>
</tar>
<echo msg="Files copied and compressed in build directory OK!" />
</target>
</project>
タスク
CapsuleTask
テンプレートを使ってファイル生成する。例えば自分でscaffoldみたいなことが作れる。CapsuleというのはPhingが用意した素のPHPによるテンプレートエンジン。テンプレート内で<?php echo $var ?>とかやって変数出力する。素のPHPだからテンプレート内でコードも書けるので手軽。同じようなものにSmartyTaskがある。
指定しなければならないプロパティ
- templatePath
- controlTemplate(テンプレートファイル名)
- outputDirectory
- outputFile
- contextProperties(assignする変数をproperties形式で書かれたファイル名)
svn
この4つが使える。
SvnCheckoutTask
SvnUpdateTask
SvnLastRevisionTask
SvnExportTask
checkoutするならこう
<svncheckout svnPath="svn" // svnクライアントのパス toDir="library" repositoryUrl="http://server/repository/url" />
Zend_Config, Zend_DBを使ってmigrateするタスク
lib/DbMigrateTask.php
>|php|
<?php
include_once 'phing/Task.php';
class DbMigrateTask extends Task{
protected $connectorFile;
/**
* @var Zend_Db_Adapter_Abstract
*/
private $db;
/**
* @var FileSet
*/
private $filesets;
public function addFileset(FileSet $set) {
$this->filesets[] = $set;
}
function main(){
require_once('Zend/Config/Ini.php');
$config = new Zend_Config_Ini($this->connectorFile);
require_once('Zend/Db.php');
$this->db = Zend_Db::factory($config->database);
// deal with the filesets
foreach($this->filesets as $fs) {
$ds = $fs->getDirectoryScanner($this->project);
$srcDir = $fs->getDir($this->project);
$srcFiles = $ds->getIncludedFiles();
$this->run_sql($srcDir, $srcFiles);
}
}
function run_sql($dir, $files){
foreach ($files as $file){
$path = $dir . DIRECTORY_SEPARATOR . $file;
$this->log("load $path");
$sql = trim(file_get_contents($path));
$sqls = split('\s*;\s*', $sql);
foreach ($sqls as $s){
$manip = '';
$table_name = '';
if (!$s) continue;
if (preg_match('/create table(?: if not exists)? `(\w+)`/i', $s, $match)){
$manip = 'create table';
$table_name = $match[1];
}else if (preg_match('/alter table `(\w+)`/i', $s, $match)){
$manip = 'alter table';
$table_name = $match[1];
}
$this->db->query($s);
$this->log("$manip $table_name OK");
}
}
}
function setConnectorFile($file){
$this->connectorFile = $file;
}
}
?>
||<
build.xml
>|xml|
<project name="myproject" default="install" basedir=".">
<taskdef name="dbmigrate" classname="mindia.task.DbMigrateTask" classpath="lib" />
<target name="install">
<dbmigrate connectorFile="config/server.ini">
<fileset dir="application/db">
<include name="*.sql" />
</fileset>
</dbmigrate>
</target>
</project>
||<
config/server.ini
[database]
adapter = Pdo_MySQL
params.engine = mysql
params.username = akkun
params.password = password
params.host = localhost
params.dbname = mydatabase
後はapplication/dbディレクトリに実行したいsqlファイルをいれてphing!