Permalink
Browse files

Move general purpose phake/functions.php

is_tty() moved to term_colors.php
resolve_runfile() and load_runfile() moved to phake/Builder.php
fatal() moved to phake/Bin.php
  • Loading branch information...
1 parent 4e8cd8f commit 6360aee47493e87d44aaeb6064c717a81e11111d @clue clue committed with clue Aug 19, 2013
Showing with 54 additions and 58 deletions.
  1. +1 −2 composer.json
  2. +0 −1 lib/includes.php
  3. +21 −7 lib/phake/Bin.php
  4. +23 −0 lib/phake/Builder.php
  5. +0 −43 lib/phake/functions.php
  6. +9 −5 lib/term_colors.php
View
@@ -17,8 +17,7 @@
},
"files": [
"lib/functions.php",
- "lib/term_colors.php",
- "lib/phake/functions.php"
+ "lib/term_colors.php"
]
},
"bin": ["bin/phake"],
View
@@ -15,5 +15,4 @@
require_once __DIR__ . '/phake/Task.php';
require_once __DIR__ . '/phake/TaskCollisionException.php';
require_once __DIR__ . '/phake/TaskNotFoundException.php';
-require_once __DIR__ . '/phake/functions.php';
require_once __DIR__ . '/phake/Utils.php';
View
@@ -58,10 +58,13 @@ public function execute($args) {
$task_names = $cli_task_names;
}
+ $builder = builder();
+ /* @var $builder Builder */
+
//
// Locate runfile
if (!$runfile) {
- $runfile = resolve_runfile(getcwd());
+ $runfile = $builder->resolve_runfile(getcwd());
$directory = dirname($runfile);
if (!@chdir($directory)) {
@@ -71,13 +74,11 @@ public function execute($args) {
}
}
- load_runfile($runfile);
+ $builder->load_runfile($runfile);
//
// Go, go, go
-
- $application = builder()->get_application();
- /* @var $application Application */
+ $application = $builder->get_application();
$application->set_args($cli_args);
$application->reset();
@@ -99,9 +100,22 @@ public function execute($args) {
}
} catch (TaskNotFoundException $tnfe) {
- fatal($tnfe, "Don't know how to build task '$task_name'\n");
+ $this->fatal($tnfe, "Don't know how to build task '$task_name'\n", $trace);
} catch (Exception $e) {
- fatal($e);
+ $this->fatal($e, null, $trace);
+ }
+ }
+
+ private function fatal($exception, $message = null, $trace = false) {
+ echo "aborted!\n";
+ if (!$message) $message = $exception->getMessage();
+ if (!$message) $message = get_class($exception);
+ write(red($message), "\n\n");
+ if ($trace) {
+ echo $exception->getTraceAsString() . "\n";
+ } else {
+ echo "(See full trace by running task with --trace)\n";
}
+ die(1);
}
}
View
@@ -61,4 +61,27 @@ private function assign_description($thing) {
$this->description = null;
}
}
+
+ public function resolve_runfile($directory) {
+ $directory = rtrim($directory, '/') . '/';
+ $runfiles = array('Phakefile', 'Phakefile.php');
+ do {
+ foreach ($runfiles as $r) {
+ $candidate = $directory . $r;
+ if (file_exists($candidate)) {
+ return $candidate;
+ }
+ }
+ if ($directory == '/') {
+ throw new \Exception("No Phakefile found");
+ }
+ $directory = dirname($directory);
+ } while (true);
+ }
+
+ public function load_runfile($file) {
+ if (file_exists($file)) {
+ require $file;
+ }
+ }
}
View
@@ -1,43 +0,0 @@
-<?php
-
-namespace phake;
-
-function resolve_runfile($directory) {
- $runfiles = array('Phakefile', 'Phakefile.php');
- do {
- foreach ($runfiles as $r) {
- $candidate = $directory . '/' . $r;
- if (file_exists($candidate)) {
- return $candidate;
- }
- }
- if ($directory == '/') {
- throw new \Exception("No Phakefile found");
- }
- $directory = dirname($directory);
- } while (true);
-}
-
-function load_runfile($file) {
- if (file_exists($file)) {
- require $file;
- }
-}
-
-function fatal($exception, $message = null) {
- echo "aborted!\n";
- if (!$message) $message = $exception->getMessage();
- if (!$message) $message = get_class($exception);
- write(red($message), "\n\n");
- global $trace;
- if ($trace) {
- echo $exception->getTraceAsString() . "\n";
- } else {
- echo "(See full trace by running task with --trace)\n";
- }
- die(1);
-}
-
-function is_tty() {
- return function_exists('posix_isatty') ? posix_isatty(STDOUT) : false;
-}
View
@@ -2,7 +2,7 @@
/**
* Adds some message coloring to bash
* Usage inside tasks:
- *
+ *
* write( red('star'), green('leaf'), blue('sky'), yellow('stone'), 'or', bold('bolded text') );
*
* @see http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
@@ -25,18 +25,18 @@ function colorize($str, $color, $bold = false) {
'yellow' => '33',
'white' => '37'
);
-
+
$code = $colors[$color];
$bold = (int)$bold;
$bg = $color == 'black' ? "\033[47m" : '';
-
- return phake\is_tty() ?
+
+ return is_tty() ?
"\033[{$bold};{$code}m{$bg}{$str}\033[0m":
$str;
}
function bold($str) {
- return phake\is_tty() ?
+ return is_tty() ?
"\033[1m{$str}\033[2m" :
$str;
}
@@ -72,3 +72,7 @@ function purple($str, $bold = false) {
function black($str, $bold = false) {
return colorize($str, 'black', $bold);
}
+
+function is_tty() {
+ return function_exists('posix_isatty') ? posix_isatty(STDOUT) : false;
+}

0 comments on commit 6360aee

Please sign in to comment.