<?php
/**
 * Ethna_Plugin_Handle_AddPlugin.php
 * 
 * @author BoBpp < bobpp@users.sourceforge.jp >
 * @package projectBop.Ethna
 */

/**
 * プラグイン作成コマンド
 * 
 * @access public
 * @author BoBpp < bobpp@users.sourceforge.jp >
 * @package projectBop.Ethna
 */
class Ethna_Plugin_Handle_AddPlugin extends Ethna_Plugin_Handle
{
	function getDescription()
	{
		return "add new Plugin to project:\n    {$this->id} [type] [name]\n";
	}
	
	function usage()
	{
		printf("usage: \nethna %s [type] [name]", $this->id);
	}
	
	function perform()
	{
		// 引数を設定する
		$r = $this->_validateArgList();
		if (Ethna::isError($r)) {
			return $r;
		}
		list($type, $name, $proj_dir) = $r;
		
		// generate
		$generator =& new Ethna_Generator();
		$r =& $generator->generate('PluginFile', $type, $name, $proj_dir);
		if (Ethna::isError($r)) {
			return $r;
		}
		
		return true;
	}
	
	/**
	 * 引数リストを取得する
	 * 
	 */
	function _validateArgList()
	{
		$args = count($this->arg_list);
		if ($args < 2) {
			return Ethna::raiseError('too few arguments', 'usage');
		} else if ($args > 3) {
			return Ethna::raiseError('too many arguments', 'usage');
		} else if ($args == 2) {
			$list = $this->arg_list;
			$list[] = getcwd();
		} else {
			$list = $this->arg_list;
		}
		
		return $list;
	}
}
?>
