From b5d3754b2f856b49585a06e1ed407e050e87c3b7 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 2 Dec 2016 14:33:19 +0100 Subject: [PATCH] Updated stubs --- src/HotfixApplication.php | 2 +- src/stubs/bash.stub | 21 ++++++++- src/stubs/php.stub | 92 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/stubs/php.stub diff --git a/src/HotfixApplication.php b/src/HotfixApplication.php index 04c192d..42e59a4 100644 --- a/src/HotfixApplication.php +++ b/src/HotfixApplication.php @@ -8,7 +8,7 @@ class HotfixApplication extends Application { public function __construct() { - parent::__construct("Hotfix", APP_VERSION." (".PHAR_BUILD_DATE.")"); + parent::__construct("Hotfix", APP_VERSION.(defined('PHAR_BUILD_DATE')?" (".PHAR_BUILD_DATE.")":"")); $this->add(new Command\ApplyCommand()); $this->add(new Command\SignCommand()); } diff --git a/src/stubs/bash.stub b/src/stubs/bash.stub index 34ba3f4..e972cc9 100644 --- a/src/stubs/bash.stub +++ b/src/stubs/bash.stub @@ -7,11 +7,28 @@ function exec() { # &>$LOG EC=$? if [ $EC -gt 0 ]; then - echo -e "\e[31;1m[warn]\e[31;21m Command completed with exitcode $EC\e[0m" - # tail -n5 $LOG | awk '{ print " " $0 }' + echo -e "\e[31;1m[warn]\e[31;21m Command completed with exitcode $EC\e[0m" + # tail -n5 $LOG | awk '{ print " " $0 }' fi # rm $LOG } +function task() { + echo -e "\e[36;1m[exec]\e[36;21m $*\e[0m" + $@ > /dev/null & spinner +} function info() { echo -e "\e[32;1m[info]\e[32;21m $*\e[0m" } +function spinner() { + local pid=$! + local delay=0.5 + local spinstr='|/-\' + while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do + local temp=${spinstr#?} + printf "%c" $spinstr + local spinstr=$temp${spinstr%"$temp"} + sleep $delay + printf "\b" + done + printf " \b" +} diff --git a/src/stubs/php.stub b/src/stubs/php.stub new file mode 100644 index 0000000..d8d0c5e --- /dev/null +++ b/src/stubs/php.stub @@ -0,0 +1,92 @@ + +class console { + public static function info($fmt,...$arg) { + self::_log("info",$fmt,...$arg); + } + public static function debug($fmt,...$arg) { + self::_log("debug",$fmt,...$arg); + } + public static function notice($fmt,...$arg) { + self::_log("notice",$fmt,...$arg); + } + public static function warning($fmt,...$arg) { + self::_log("warning",$fmt,...$arg); + } + public static function error($fmt,...$arg) { + self::_log("error",$fmt,...$arg); + } + private static function _log($level,$fmt,...$arg) { + switch ($level) { + case "info": + $outfmt = "\e[92m\e[1m-¤- \e[21m%s\e[0m"; + break; + case "debug": + $outfmt = "\e[32m ¤ %s\e[0m"; + break; + case "notice": + $outfmt = "\e[33m\e[1m ~ \e[21m%s\e[0m"; + break; + case "warning": + $outfmt = "\e[93m\e[1m \e[21m%s\e[0m"; + break; + case "error": + $outfmt = "\e[91m\e[1m[#] \e[21m%s\e[0m"; + break; + default: + $outfmt = "%s"; + } + printf($outfmt, sprintf(rtrim($fmt)."\n", ...$arg)); + } + public function confirm($question, $defaultValue=false) + { + while (true) { + printf("\e[94;1m=?= \e[21m%s \e[36m[\e[96m%s\e[36m]? \e[0m", $question, $defaultValue?"Y/n":"y/N"); + $read = strtolower(trim(fgets(STDIN))); + if ($read == "y") { + return true; + } elseif ($read == "n") { + return false; + } else { + return $defaultValue; + } + } + } +} + +class http { + public static function download($url, $dest) { + $progress = new NoccyLabs\Downloader\BarProgress(); + $dl = new NoccyLabs\Downloader\Downloader($url, $dest, $progress); + $dl->start(); + } +} + + +class shell { + private static $chdir_path; + public static function chdir($path) { + self::$chdir_path = $path; + console::debug("Setting working directory to {$path}"); + } + public static function exec($command, ...$vars) { + $vars = array_map("escapeshellarg", $vars); + $exec = sprintf($command, ...$vars); + console::debug("| $ %s", $exec); + if (self::$chdir_path) { + $exec = sprintf("cd %s && %s", escapeshellarg(self::$chdir_path), $exec); + } + passthru($exec); + } + public static function execAndReturn($command, ...$vars) { + $vars = array_map("escapeshellarg", $vars); + $exec = sprintf($command, ...$vars); + $output = null; + $status = null; + console::debug("| $ %s", $exec); + exec($exec, $output, $status); + if (self::$chdir_path) { + $exec = sprintf("cd %s && %s", escapeshellarg(self::$chdir_path), $exec); + } + return $output; + } +}