diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 2d9607ff..9ba46fad 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -17,11 +17,17 @@ class Builder protected $verbose = false; protected $plugins = array(); protected $build; + protected $logCallback; - public function __construct(Build $build) + public function __construct(Build $build, $logCallback = null) { $this->build = $build; $this->store = Store\Factory::getStore('Build'); + + if(!is_null($logCallback) && is_callable($logCallback)) + { + $this->logCallback = $logCallback; + } } public function execute() @@ -87,24 +93,32 @@ class Builder protected function log($message, $prefix = '') { + if(is_array($message)) { - $message = array_map(function($item) use ($prefix) + $cb = $this->logCallback; + + $message = array_map(function($item) use ($cb, $prefix) { - return $prefix . $item; - }, $message); + if(is_callable($cb)) + { + $cb($prefix . $item); + } - $message = implode(PHP_EOL, $message); - - $this->log .= $message; - print $message . PHP_EOL; + $this->log .= $prefix . $item . PHP_EOL; + }, $message); } else { - $message = $prefix . $message . PHP_EOL; + $message = $prefix . $message; - $this->log .= $message; - print $message; + $this->log .= $message . PHP_EOL; + + if(isset($this->logCallback) && is_callable($this->logCallback)) + { + $cb = $this->logCallback; + $cb($message); + } } $this->build->setLog($this->log); diff --git a/PHPCI/Command/GenerateCommand.php b/PHPCI/Command/GenerateCommand.php new file mode 100644 index 00000000..52b12198 --- /dev/null +++ b/PHPCI/Command/GenerateCommand.php @@ -0,0 +1,26 @@ +setName('phpci:generate') + ->setDescription('Generate models and stores from the database.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $gen = new \b8\Database\CodeGenerator(\b8\Database::getConnection(), 'PHPCI', PHPCI_DIR . '/PHPCI/'); + $gen->generateModels(); + $gen->generateStores(); + } +} \ No newline at end of file diff --git a/PHPCI/Command/InstallCommand.php b/PHPCI/Command/InstallCommand.php new file mode 100644 index 00000000..2a2cf7c2 --- /dev/null +++ b/PHPCI/Command/InstallCommand.php @@ -0,0 +1,114 @@ +setName('phpci:install') + ->setDescription('Install PHPCI.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $dbHost = $this->ask('Enter your MySQL host: '); + $dbName = $this->ask('Enter the database name PHPCI should use: '); + $dbUser = $this->ask('Enter your MySQL username: '); + $dbPass = $this->ask('Enter your MySQL password: ', true); + $ciUrl = $this->ask('Your PHPCI URL (without trailing slash): ', true); + $ghId = $this->ask('(Optional) Github Application ID: ', true); + $ghSecret = $this->ask('(Optional) Github Application Secret: ', true); + + $cmd = 'mysql -u' . $dbUser . (!empty($dbPass) ? ' -p' . $dbPass : '') . ' -h' . $dbHost . ' -e "CREATE DATABASE IF NOT EXISTS ' . $dbName . '"'; + shell_exec($cmd); + + $str = "set('install_url', '{$ciUrl}'); +"; + + if(!empty($ghId) && !empty($ghSecret)) + { + $str .= PHP_EOL . "\$registry->set('github_app', array('id' => '{$ghId}', 'secret' => '{$ghSecret}'));" . PHP_EOL; + } + + + file_put_contents(PHPCI_DIR . 'config.php', $str); + + if(!file_exists(PHPCI_DIR . 'composer.phar')) + { + print 'INSTALLING: Composer' . PHP_EOL; + file_put_contents(PHPCI_DIR . 'composerinstaller.php', file_get_contents('https://getcomposer.org/installer')); + shell_exec('php ' . PHPCI_DIR . 'composerinstaller.php'); + unlink(PHPCI_DIR . 'composerinstaller.php'); + } + + print 'RUNNING: Composer' . PHP_EOL; + shell_exec('php '.PHPCI_DIR.'composer.phar install'); + + + require(PHPCI_DIR . 'bootstrap.php'); + + $gen = new \b8\Database\Generator(\b8\Database::getConnection(), 'PHPCI', './PHPCI/Model/Base/'); + $gen->generate(); + + $adminEmail = $this->ask('Enter your email address: '); + $adminPass = $this->ask('Enter your desired admin password: '); + $adminName = $this->ask('Enter your name: '); + + try + { + $user = new \PHPCI\Model\User(); + $user->setEmail($adminEmail); + $user->setName($adminName); + $user->setIsAdmin(1); + $user->setHash(password_hash($adminPass, PASSWORD_DEFAULT)); + + $store = \b8\Store\Factory::getStore('User'); + $store->save($user); + + print 'User account created!' . PHP_EOL; + } + catch(\Exception $ex) + { + print 'There was a problem creating your account. :(' . PHP_EOL; + print $ex->getMessage(); + } + } + + function ask($question, $emptyOk = false) + { + print $question . ' '; + + $rtn = ''; + $fp = fopen('php://stdin', 'r'); + $rtn = fgets($fp); + fclose($fp); + + $rtn = trim($rtn); + + if(!$emptyOk && empty($rtn)) + { + $rtn = $this->ask($question, $emptyOk); + } + + return $rtn; + } +} \ No newline at end of file diff --git a/PHPCI/Command/RunCommand.php b/PHPCI/Command/RunCommand.php new file mode 100644 index 00000000..94334269 --- /dev/null +++ b/PHPCI/Command/RunCommand.php @@ -0,0 +1,46 @@ +setName('phpci:run-builds') + ->setDescription('Run all pending PHPCI builds.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->output = $output; + + $store = Factory::getStore('Build'); + $result = $store->getByStatus(0); + + foreach($result['items'] as $build) + { + if ($input->getOption('verbose')) { + $builder = new Builder($build, array($this, 'logCallback')); + } + else { + $builder = new Builder($build); + } + + $builder->execute(); + } + } + + public function logCallback($log) + { + $this->output->writeln($log); + } +} \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php index 50cae0a4..1fcf3598 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -1,5 +1,7 @@ set('app_namespace', 'PHPCI'); b8\Registry::getInstance()->set('DefaultController', 'Index'); diff --git a/console b/console new file mode 100755 index 00000000..cae511bd --- /dev/null +++ b/console @@ -0,0 +1,17 @@ +#!/usr/bin/env php +add(new RunCommand); +$application->add(new InstallCommand); +$application->add(new GenerateCommand); +$application->run(); diff --git a/cron.php b/cron.php deleted file mode 100644 index 80dbe7d8..00000000 --- a/cron.php +++ /dev/null @@ -1,18 +0,0 @@ -getByStatus(0); - -foreach($result['items'] as $build) -{ - $builder = new PHPCI\Builder($build); - $builder->execute(); -} \ No newline at end of file diff --git a/generate.php b/generate.php deleted file mode 100644 index d3e791dc..00000000 --- a/generate.php +++ /dev/null @@ -1,7 +0,0 @@ -generateModels(); -$gen->generateStores(); \ No newline at end of file diff --git a/install.php b/install.php deleted file mode 100644 index 52da6899..00000000 --- a/install.php +++ /dev/null @@ -1,95 +0,0 @@ -set('install_url', '{$ciUrl}'); -"; - -if(!empty($ghId) && !empty($ghSecret)) -{ - $str .= PHP_EOL . "\$registry->set('github_app', array('id' => '{$ghId}', 'secret' => '{$ghSecret}'));" . PHP_EOL; -} - - -file_put_contents('./config.php', $str); - -if(!file_exists('./composer.phar')) -{ - print 'INSTALLING: Composer' . PHP_EOL; - file_put_contents('./composerinstaller.php', file_get_contents('https://getcomposer.org/installer')); - shell_exec('php composerinstaller.php'); - unlink('./composerinstaller.php'); -} - -print 'RUNNING: Composer' . PHP_EOL; -shell_exec('php composer.phar install'); - - -require_once('bootstrap.php'); - -$gen = new b8\Database\Generator(b8\Database::getConnection(), 'PHPCI', './PHPCI/Model/Base/'); -$gen->generate(); - -$adminEmail = ask('Enter your email address: '); -$adminPass = ask('Enter your desired admin password: '); -$adminName = ask('Enter your name: '); - -try -{ - $user = new PHPCI\Model\User(); - $user->setEmail($adminEmail); - $user->setName($adminName); - $user->setIsAdmin(1); - $user->setHash(password_hash($adminPass, PASSWORD_DEFAULT)); - - $store = b8\Store\Factory::getStore('User'); - $store->save($user); - - print 'User account created!' . PHP_EOL; -} -catch(Exception $ex) -{ - print 'There was a problem creating your account. :(' . PHP_EOL; - print $ex->getMessage(); -} - - -function ask($question, $emptyOk = false) -{ - print $question . ' '; - - $rtn = ''; - $fp = fopen('php://stdin', 'r'); - $rtn = fgets($fp); - fclose($fp); - - $rtn = trim($rtn); - - if(!$emptyOk && empty($rtn)) - { - $rtn = ask($question, $emptyOk); - } - - return $rtn; -} \ No newline at end of file