Skip to content

Commit

Permalink
big changes to how Pomander is booted. Not dependent on calling the
Browse files Browse the repository at this point in the history
phake bin stub anymore.

added help and version options. closes #23
  • Loading branch information
tamagokun committed Nov 2, 2013
1 parent eef136e commit f527742
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.DS_Store
vendor
deploy
42 changes: 0 additions & 42 deletions Phakefile

This file was deleted.

26 changes: 20 additions & 6 deletions bin/pom
@@ -1,10 +1,24 @@
#!/usr/bin/env php
<?php
// pass arguments to phake

if(!class_exists("\\Pomander"))
{
try {
$dir = dirname(__DIR__)."/vendor";
if(!file_exists($dir)) $dir = dirname(dirname(dirname(__DIR__)));
require_once "{$dir}/autoload.php";
}catch(Exception $e)
{
echo "Failed to load Pomander library. Is pom installed in the proper location?\n";
}
}

\Pomander::version();
array_shift($GLOBALS['argv']);
$arg_list = implode(' ', $GLOBALS['argv']);
$file = dirname(__DIR__).'/Phakefile';
$phake_path = dirname(dirname(dirname(__DIR__))).'/bin/phake';
$phake = file_exists($phake_path)? $phake_path : "phake";
passthru("$phake -f $file $arg_list", $status);

$cli = new \Pomander\Cli();
set_error_handler(array($cli, 'error_handler'));
set_exception_handler(array($cli, 'exception_handler'));

$status = $cli->exec($GLOBALS['argv']);
exit($status);
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -13,8 +13,8 @@
}
],
"require": {
"jaz303/phake": "*",
"phpseclib/phpseclib": "*"
"jaz303/phake": "@dev",
"phpseclib/phpseclib": "@stable"
},
"autoload": {
"psr-0": {
Expand Down
34 changes: 22 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 6 additions & 31 deletions lib/Pomander.php
Expand Up @@ -4,41 +4,15 @@

class Pomander
{
public static function resolve_runfile($directory)
public static function version()
{
$runfiles = array('Phakefile','Phakefile.php','Pomfile','Pomfile.php');
do
{
foreach($runfiles as $r)
{
$candidate = $directory.'/'.$r;
if(file_exists($candidate)) return $candidate;
}
if($directory == '/') return false;
$directory = dirname($directory);
} while (true);
return array(0,3,9);
}
}

// set default date
if(function_exists('date_default_timezone_set')) date_default_timezone_set('UTC');

set_error_handler(function($errno,$errstr,$errfile,$errline) {
puts("aborted!");
puts("$errstr\n");
if($errno <= 0) $errno = 1;
global $trace;
if($trace)
{
$exception = new \ErrorException($errstr, 0, $errno, $errfile, $errline);
puts($exception->getTraceAsString());
}else
{
puts("(See full trace by running task with --trace)");
}
exit($errno);
});

//utils
function info($status,$msg)
{
Expand Down Expand Up @@ -68,9 +42,10 @@ function puts($text) { echo $text.PHP_EOL; }

function home()
{
if(!isset(builder()->get_application()->home))
builder()->get_application()->home = trim(shell_exec("cd && pwd"),"\r\n");
return builder()->get_application()->home;
$app = builder()->get_application();
if(!isset($app->home))
$app->home = trim(shell_exec("cd && pwd"),"\r\n");
return $app->home;
}

function run()
Expand Down
3 changes: 2 additions & 1 deletion lib/Pomander/Builder.php
Expand Up @@ -51,7 +51,8 @@ public function run($first = true)
$app->env->multi_role_support("db",$app);
});

require dirname(__DIR__).'/tasks/default.php';
$tasks = glob(dirname(__DIR__).'/tasks/*.php');
foreach($tasks as $task) require $task;
if($first) $this->inject_default($app);
}

Expand Down
161 changes: 161 additions & 0 deletions lib/Pomander/Cli.php
@@ -0,0 +1,161 @@
<?php
namespace Pomander;

use phake\Application;

class Cli
{
public $action = "invoke";
public $trace = false;
public $app;

public function exec($args)
{
try
{
$parser = new OptionParser($args);
// handle cli options
foreach ($parser->get_options() as $option => $value) $this->handle_option($option);

$task_args = array();
$tasks = array();

// handle tasks and task vars
foreach($parser->get_non_options() as $option)
{
if(strpos($option, '=') === false) $tasks[] = $option;
else $task_args[] = $option;
}

$this->app = new Application();
$this->app->set_args(\phake\Utils::parse_args($task_args));
$this->app->top_level_tasks = count($tasks) ? $tasks : array('default');
$this->app->dir = dirname(__DIR__);

\phake\Builder::$global = new \phake\Builder($this->app);

// load Pomander/Pomfile
$runfile = $this->resolve_runfile(getcwd());
if(!$runfile)
{
$pom = new \Pomander\Builder();
$pom->run();
}else
{
$directory = dirname($runfile);
if(!@chdir($directory))
throw new \Exception("Couldn't change to directory '$directory'");
else
echo "(in $directory)\n";
require $runfile;
}

if($this->action == "list")
{
$this->list_tasks();
return 0;
}

$this->app->reset();

switch($this->action)
{
case 'list':
$this->list_tasks();
break;
case 'invoke':
foreach($tasks as $task_name) $this->app->invoke($task_name);
break;
}
}catch(\phake\TaskNotFoundException $tnfe)
{
$this->fatal($tnfe, "Don't know how to build task '$task_name'\n");
}catch(\Exception $e)
{
$this->fatal($e, null);
}
}

public function error_handler($errno, $errstr, $errfile, $errline)
{
$exception = new \ErrorException($errstr, 0, $errno, $errfile, $errline);
return $this->fatal($exception, $errstr, $errno);
}

public function exception_handler(\Exception $e)
{
return $this->fatal($e);
}

protected function handle_option($option)
{
switch($option)
{
case "t":
case "trace":
$this->trace = true;
break;
case "T":
case "tasks":
$this->action = "list";
break;
case "V":
case "version":
echo "Pomander ".implode(".", \Pomander::version())."\n";
exit;
break;
case "h":
case "H":
case "help":
echo "Usage:\n";
echo "pom {options} tasks...\n\n";
echo "Options:\n";
echo " -T, --tasks Display the available tasks.\n";
echo " -t, --trace Turn on invoke/execute tracing, enable full backtrace.\n";
echo " -V, --version Display the program version.\n";
echo " -h, -H, --help Display the help message.\n";
exit;
break;
default:
throw new \Exception("Unknown command line option '$option'");
break;
}
}

protected function fatal($exception, $message = null, $status = 1)
{
puts("aborted!");
if(!$message) $message = $exception->getMessage();
if(!$message) $message = get_class($exception);
puts("$message\n");
if($this->trace)
puts($exception->getTraceAsString());
else
puts("(See full trace by running task with --trace)");
exit($status > 0 ? $status : 1);
}

protected function list_tasks()
{
$task_list = $this->app->get_task_list();
if(!count($task_list)) return;
$max = max(array_map('strlen', array_keys($task_list)));
foreach($task_list as $name => $desc)
echo str_pad($name, $max + 4) . $desc . "\n";
}

protected function resolve_runfile($directory)
{
$runfiles = array('Phakefile','Phakefile.php','Pomfile','Pomfile.php');
do
{
foreach($runfiles as $r)
{
$candidate = $directory.'/'.$r;
if(file_exists($candidate)) return $candidate;
}
if($directory == '/') return false;
$directory = dirname($directory);
} while (true);
}
}

0 comments on commit f527742

Please sign in to comment.