/** * Main method. * * @return int */ function main() { $operation = 0; $location = null; $updateServer = Updater::DEFAULT_UPDATE_SERVER; while (false !== ($c = $this->getOption($v))) { switch ($c) { case '__ambiguous': $this->resolveOptionAmbiguity($v); break; case 'f': $operation = static::OPERATION_FETCH | Updater::FORMAT_PHAR; break; case 'z': $operation = static::OPERATION_FETCH | Updater::FORMAT_ZIP; break; case 'a': $operation = static::OPERATION_APPLY; $location = $v; break; case 's': $updateServer = $v; break; case 'h': case '?': default: return $this->usage(); break; } } $updateServer = rtrim($updateServer, '/') . '/'; if (0 !== (static::OPERATION_FETCH & $operation)) { $updatesDotJson = Updater::getUpdateUrl($updateServer); $versions = @file_get_contents($updatesDotJson); if (empty($versions)) { throw new Exception\Console('Oh no! We are not able to check if a new version exists… ' . 'Contact us at http://sabre.io/ ' . '(tried URL %s).', 0, $updatesDotJson); } $versions = json_decode($versions, true); /** * Expected format: * { * "1.0.1": { * "phar": "https://…", * "zip" : "https://…" * }, * "1.0.0": { * "phar": "https://…", * "zip" : "https://…" * }, * … * } */ $versionsToFetch = Updater::filterVersions($versions, Version::VERSION, $operation); $windowWidth = Window::getSize()['x']; $progress = function ($percent) use($windowWidth) { Cursor::clear('↔'); $message = 'Downloading… '; $barWidth = $windowWidth - mb_strlen($message); if ($percent <= 0) { $color = '#c74844'; } elseif ($percent <= 25) { $color = '#cb9a3d'; } elseif ($percent <= 50) { $color = '#dcb11e'; } elseif ($percent <= 75) { $color = '#aed633'; } else { $color = '#54b455'; } echo $message; Cursor::colorize('foreground(' . $color . ') background(' . $color . ')'); echo str_repeat('|', $percent * $barWidth / 100); Cursor::colorize('normal'); }; foreach ($versionsToFetch as $versionNumber => $versionUrl) { list(, $versionUrlBasename) = Uri\split($versionUrl); $fileIn = new File\Read($versionUrl, File::MODE_READ, null, true); $fileOut = new File\Write(SABRE_KATANA_PREFIX . '/data/share/update/' . $versionUrlBasename); echo "\n", 'Fetch version ', $versionNumber, ' from ', $versionUrl, "\n", 'Waiting…', "\n"; $fileIn->on('connect', function () { Cursor::clear('↔'); echo 'Downloading… '; }); $fileIn->on('progress', function (Core\Event\Bucket $bucket) use($progress) { static $previousPercent = 0; $data = $bucket->getData(); $current = $data['transferred']; $max = $data['max']; $percent = $current * 100 / $max; $delta = $percent - $previousPercent; if (1 <= $delta) { $previousPercent = $percent; $progress($percent); } }); $fileIn->open(); $fileOut->writeAll($fileIn->readAll()); echo "\n", 'Fetched at ', $fileOut->getStreamName(), '.', "\n"; } return 0; } elseif (static::OPERATION_APPLY === $operation) { if (false === file_exists($location)) { throw new Exception\Console('Update %s is not found.', 1, $location); } $processus = new Console\Processus(Core::getPHPBinary(), [$location, '--extract' => SABRE_KATANA_PREFIX, '--overwrite']); $processus->on('input', function () { return false; }); $processus->on('output', function (Core\Event\Bucket $bucket) { echo $bucket->getData()['line'], "\n"; }); $processus->run(); if (true === $processus->isSuccessful()) { echo 'sabre/katana updated!', "\n"; } else { echo 'Something wrong happened!', "\n"; } return $processus->getExitCode(); } else { return $this->usage(); } }
/** * The entry method. * * @return int */ public function main() { $directories = []; $clean = false; $lang = 'En'; while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'd': foreach ($this->parser->parseSpecialValue($v) as $directory) { $directory = realpath($directory); if (false === is_dir($directory)) { throw new Console\Exception('Directory %s does not exist.', 0, $directory); } $directories[] = $directory; } break; case 'c': $clean = true; break; case 'l': $lang = ucfirst(strtolower($v)); break; case '__ambiguous': $this->resolveOptionAmbiguity($v); break; case 'h': case '?': default: return $this->usage(); } } $workspace = resolve('hoa://Library/Devtools/Resource/Documentation') . DS . 'HackBook.output'; if (true === $clean) { if (true === is_dir($workspace)) { $directory = new File\Directory($workspace); $directory->delete(); unset($directory); } return; } clearstatcache(true); $workspace .= DS . $lang; if (false === is_dir($workspace)) { File\Directory::create($workspace); } Console\Cursor::colorize('foreground(yellow)'); echo 'Selected language: ', $lang, '.', "\n\n"; Console\Cursor::colorize('normal'); require_once 'hoa://Library/Devtools/Resource/Documentation/Router.php'; // $router is defined. $finder = new File\Finder(); foreach ($directories as $location) { $_location = $location . DS . 'Documentation' . DS . $lang; if (false === is_dir($_location)) { throw new Console\Exception('Directory %s does not contain documentation.', 1, $location); } $finder->in($_location); } foreach (resolve('hoa://Library', true, true) as $location) { $libraryFinder = new File\Finder(); $libraryFinder->in($location)->directories()->maxDepth(1); foreach ($libraryFinder as $_location) { $_location = $_location->getPathName() . DS . 'Documentation' . DS . $lang; if (true === is_dir($_location)) { $finder->in($_location); } } } $vendors = []; foreach ($finder as $entry) { $path = dirname(dirname($entry->getPath())); $vendor = ucfirst(strtolower(basename(dirname($path)))); $library = ucfirst(strtolower(basename($path))); if (!isset($vendors[$vendor])) { $vendors[$vendor] = []; } $vendors[$vendor][$library] = ['library' => $library, 'vendor' => $vendor, 'fullname' => $vendor . '\\' . $library]; } foreach ($vendors as $vendor => &$libraries) { $libraries = array_values($libraries); } $layout = new File\Read('hoa://Library/Devtools/Resource/Documentation/Layout.xyl'); $xyl = new Xyl($layout, new File\Write($workspace . '/index.html'), new Xyl\Interpreter\Html(), $router); $xyl->setTheme(''); $data = $xyl->getData(); foreach ($vendors as $vendor => $libraries) { $data->vendors->vendor = ['name' => $vendor, 'library' => $libraries]; } $xyl->addOverlay('hoa://Library/Devtools/Resource/Documentation/Index.xyl'); $xyl->render(); echo 'Generate', "\t"; Console\Cursor::colorize('foreground(green)'); echo 'index.html'; Console\Cursor::colorize('normal'); echo '.', "\n"; $xyl = null; foreach ($vendors as $vendor => $libraries) { File\Directory::create($workspace . dirname($router->unroute('full', ['vendor' => $libraries[0]['vendor'], 'chapter' => $libraries[0]['library']]))); foreach ($libraries as $library) { $in = 'hoa://Library/' . $library['library'] . '/Documentation/' . $lang . '/Index.xyl'; $out = $workspace . $router->unroute('full', ['vendor' => $library['vendor'], 'chapter' => $library['library']]); if (true === file_exists($out) && filemtime($in) <= filemtime($out)) { echo 'Skip', "\t\t"; Console\Cursor::colorize('foreground(green)'); echo $library['fullname']; Console\Cursor::colorize('normal'); echo '.', "\n"; continue; } $out = new File\Write($out); $out->truncate(0); if (null === $xyl) { $xyl = new Xyl($layout, $out, new Xyl\Interpreter\Html(), $router); $xyl->setTheme(''); $xyl->addOverlay('hoa://Library/Devtools/Resource/Documentation/Chapter.xyl'); } else { $xyl->setOutputStream(new File\Write($out)); } $xyl->addOverlay($in); $xyl->getData()->name[0] = $library['fullname']; $xyl->getData()->library[0] = $library['library']; try { $xyl->render(); } catch (\Exception $e) { echo $e->getMessage(), "\n"; } $xyl->removeOverlay($in); echo 'Generate', "\t"; Console\Cursor::colorize('foreground(green)'); echo $library['fullname']; Console\Cursor::colorize('normal'); echo '.', "\n"; } } echo "\n", 'Open file://', $workspace, '/index.html', '.', "\n"; return; }
*/ define('SABRE_KATANA', true); /** * Current version. */ define('SABRE_KATANA_VERSION', '0.1.1'); /** * Define the prefix. */ define('SABRE_KATANA_PREFIX', __DIR__); /** * Handle exceptions and errors. */ Core\Core::enableExceptionHandler(true); Core\Core::enableErrorHandler(true); /** * Log all exceptions. */ event('hoa://Event/Exception')->attach(function (Core\Event\Bucket $bucket) { $exception = $bucket->getData(); $filename = date('Ymd') . '.exceptions.log'; $file = new File\Write('katana://data/log/' . $filename); $exceptionFile = $exception->getFile(); $prefixLength = strlen(SABRE_KATANA_PREFIX); if (SABRE_KATANA_PREFIX === substr($exceptionFile, 0, $prefixLength)) { $exceptionFile = substr($exceptionFile, $prefixLength + 1); } $file->writeAll(sprintf('[%s] "%s" %s:%d' . "\n", date('c'), $exception->getMessage(), $exceptionFile, $exception->getLine())); $file->close(); return; });