diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/bin/roadrunner | 52 | ||||
-rw-r--r-- | src/qbuild/.build.json | 17 | ||||
-rw-r--r-- | src/qbuild/docker/Dockerfile | 8 | ||||
-rw-r--r-- | src/qbuild/docker/compile.sh | 9 | ||||
-rw-r--r-- | src/qbuild/main.go | 14 | ||||
-rw-r--r-- | src/qbuild/rr-build | 40 | ||||
-rw-r--r-- | src/qbuild/src/Builder.php | 237 |
7 files changed, 33 insertions, 344 deletions
diff --git a/src/bin/roadrunner b/src/bin/roadrunner index 6a553135..cf4317d7 100755 --- a/src/bin/roadrunner +++ b/src/bin/roadrunner @@ -1,13 +1,18 @@ #!/usr/bin/env php <?php -/* +/** * RoadRunner * High-performance PHP process supervisor and load balancer written in Go * * This file responsive for cli commands */ +declare(strict_types=1); -foreach ([__DIR__ . '/../../../../autoload.php', __DIR__ . '/../../vendor/autoload.php', __DIR__ . '/vendor/autoload.php'] as $file) { +foreach ([ + __DIR__ . '/../../../../autoload.php', + __DIR__ . '/../../vendor/autoload.php', + __DIR__ . '/vendor/autoload.php' + ] as $file) { if (file_exists($file)) { define('RR_COMPOSER_INSTALL', $file); @@ -28,6 +33,16 @@ if (!defined('RR_COMPOSER_INSTALL')) { die(1); } +if (!class_exists('ZipArchive')) { + fwrite(STDERR, 'Extension `php-zip` is required.' . PHP_EOL); + die(1); +} + +if (!function_exists('curl_init')) { + fwrite(STDERR, 'Extension `php-curl` is required.' . PHP_EOL); + die(1); +} + require RR_COMPOSER_INSTALL; use Symfony\Component\Console\Application; @@ -45,7 +60,7 @@ class RoadRunnerCLIHelper * @return string Version of RoadRunner * @throws Exception */ - public static function getVersion() + public static function getVersion(): string { $file = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'build.sh'; $fileResource = fopen($file, 'r') or die(1); @@ -65,7 +80,7 @@ class RoadRunnerCLIHelper * * @return string OS Type */ - public static function getOsType() + public static function getOSType(): string { switch (PHP_OS) { case 'Darwin': @@ -91,11 +106,10 @@ class RoadRunnerCLIHelper */ public static function getBinaryDownloadUrl() { - return 'https://github.com/spiral/roadrunner/releases/download/v' . static::getVersion() . '/roadrunner-' . static::getVersion() . '-' . static::getOsType() . '-amd64.zip'; + return 'https://github.com/spiral/roadrunner/releases/download/v' . static::getVersion() . '/roadrunner-' . static::getVersion() . '-' . static::getOSType() . '-amd64.zip'; } } - (new Application('RoadRunner', RoadRunnerCLIHelper::getVersion())) ->register('update-binaries') ->setDescription("Install or update RoadRunner binaries in specified folder (current folder by default)") @@ -115,7 +129,7 @@ class RoadRunnerCLIHelper } } - $output->writeln('<info>Downloading RoadRunner archive for ' . RoadRunnerCLIHelper::getOsType() . '</info>'); + $output->writeln('<info>Downloading RoadRunner archive for ' . RoadRunnerCLIHelper::getOSType() . '</info>'); $progressBar = new ProgressBar($output); $progressBar->setFormat('verbose'); @@ -129,14 +143,15 @@ class RoadRunnerCLIHelper curl_setopt($curlResource, CURLOPT_BINARYTRANSFER, true); curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlResource, CURLOPT_FILE, $zipFile); - curl_setopt($curlResource, CURLOPT_PROGRESSFUNCTION, function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($progressBar) { - if ($download_size == 0) { - return; - } - $progressBar->setFormat('[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% ' . intval($download_size / 1024) . 'KB'); - $progressBar->setMaxSteps($download_size); - $progressBar->setProgress($downloaded); - }); + curl_setopt($curlResource, CURLOPT_PROGRESSFUNCTION, + function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use ($progressBar) { + if ($download_size == 0) { + return; + } + $progressBar->setFormat('[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% ' . intval($download_size / 1024) . 'KB'); + $progressBar->setMaxSteps($download_size); + $progressBar->setProgress($downloaded); + }); curl_setopt($curlResource, CURLOPT_NOPROGRESS, false); // needed to make progress function work curl_setopt($curlResource, CURLOPT_HEADER, 0); curl_exec($curlResource); @@ -150,7 +165,7 @@ class RoadRunnerCLIHelper $zipArchive = new ZipArchive(); $zipArchive->open($zipFileName); - $fileStreamFromZip = $zipArchive->getStream('roadrunner-' . RoadRunnerCLIHelper::getVersion() . '-' . RoadRunnerCLIHelper::getOsType() . '-amd64/rr'); + $fileStreamFromZip = $zipArchive->getStream('roadrunner-' . RoadRunnerCLIHelper::getVersion() . '-' . RoadRunnerCLIHelper::getOSType() . '-amd64/rr'); $finalFileResource = fopen($finalFile, 'w'); if (!$fileStreamFromZip) { @@ -184,10 +199,9 @@ class RoadRunnerCLIHelper return; } } - copy(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.rr.yaml', $input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml'); + copy(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.rr.yaml', + $input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml'); $output->writeln('<info>Config file created!</info>'); }) ->getApplication() ->run(); - - diff --git a/src/qbuild/.build.json b/src/qbuild/.build.json deleted file mode 100644 index 74b83cea..00000000 --- a/src/qbuild/.build.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "packages": [ - "github.com/spiral/roadrunner/service/env", - "github.com/spiral/roadrunner/service/http", - "github.com/spiral/roadrunner/service/rpc", - "github.com/spiral/roadrunner/service/static" - ], - "commands": [ - "github.com/spiral/roadrunner/cmd/rr/http" - ], - "register": [ - "rr.Container.Register(env.ID, &env.Service{})", - "rr.Container.Register(rpc.ID, &rpc.Service{})", - "rr.Container.Register(http.ID, &http.Service{})", - "rr.Container.Register(static.ID, &static.Service{})" - ] -}
\ No newline at end of file diff --git a/src/qbuild/docker/Dockerfile b/src/qbuild/docker/Dockerfile deleted file mode 100644 index 11fb5abb..00000000 --- a/src/qbuild/docker/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -FROM golang:latest - -ENV CGO_ENABLED=0 -ENV GO111MODULE=on - -WORKDIR /go/src/rr - -COPY compile.sh /go/src/rr/
\ No newline at end of file diff --git a/src/qbuild/docker/compile.sh b/src/qbuild/docker/compile.sh deleted file mode 100644 index 0c85124f..00000000 --- a/src/qbuild/docker/compile.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -LDFLAGS="$LDFLAGS -X github.com/spiral/roadrunner/cmd/rr/cmd.Version=${RR_VERSION}" -LDFLAGS="$LDFLAGS -X github.com/spiral/roadrunner/cmd/rr/cmd.BuildTime=$(date +%FT%T%z)" - -# Verify all external modules -go mod init - -# Build the binary -CGO_ENABLED=0 go build -v -ldflags "$LDFLAGS -extldflags '-static'" -o "rr"
\ No newline at end of file diff --git a/src/qbuild/main.go b/src/qbuild/main.go deleted file mode 100644 index a065fe27..00000000 --- a/src/qbuild/main.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "github.com/sirupsen/logrus" - rr "github.com/spiral/roadrunner/cmd/rr/cmd" - // -packages- // - // -commands- // -) - -func main() { - // -register- // - rr.Logger.Formatter = &logrus.TextFormatter{ForceColors: true} - rr.Execute() -} diff --git a/src/qbuild/rr-build b/src/qbuild/rr-build deleted file mode 100644 index 7918f4f8..00000000 --- a/src/qbuild/rr-build +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env php -<?php -/** - * Automatic roadrunner builds. - */ - -use Spiral\RoadRunner\QuickBuild\Builder; - -require_once "src/Builder.php"; - -// load build config -$version = $argv[1] ?? "1.3.1"; - -// load build config -$config = $argv[2] ?? __DIR__ . "/.build.json"; - -// Greeting! -Builder::cprintf( - "Building <green>RoadRunner</reset> specifically for you (version: <white>%s</reset>)...\n", - $version -); - -$builder = Builder::loadConfig($config); -if ($builder == null) { - Builder::cprintf("<red>Unable to load config:</reset> %s\n", $config); - return; -} - -$errors = $builder->configErrors(); -if (!empty($errors)) { - Builder::cprintf("<yellow>Found configuration errors:</reset>\n"); - foreach ($errors as $error) { - Builder::cprintf("- <red>%s</reset>\n", $error); - } - - return; -} - -// Start build -$builder->build(getcwd(), __DIR__ . '/main.go', 'rr', $version);
\ No newline at end of file diff --git a/src/qbuild/src/Builder.php b/src/qbuild/src/Builder.php deleted file mode 100644 index 4b441094..00000000 --- a/src/qbuild/src/Builder.php +++ /dev/null @@ -1,237 +0,0 @@ -<?php -declare(strict_types=1); -/** - * RoadRunner. - * - * @license MIT - * @author Anton Titov (Wolfy-J) - */ - -namespace Spiral\RoadRunner\QuickBuild; - -final class Builder -{ - const DOCKER = 'spiralscout/rr-build'; - - /** - * Coloring. - * - * @var array - */ - protected static $colors = [ - "reset" => "\e[0m", - "white" => "\033[1;38m", - "red" => "\033[0;31m", - "green" => "\033[0;32m", - "yellow" => "\033[1;93m", - "gray" => "\033[0;90m" - ]; - - /** @var array */ - private $config; - - /** - * @param array $config - */ - protected function __construct(array $config) - { - $this->config = $config; - } - - /** - * Validate the build configuration. - * - * @return array - */ - public function configErrors(): array - { - $errors = []; - if (!isset($this->config["commands"])) { - $errors[] = "Directive 'commands' missing"; - } - - if (!isset($this->config["packages"])) { - $errors[] = "Directive 'packages' missing"; - } - - if (!isset($this->config["register"])) { - $errors[] = "Directive 'register' missing"; - } - - return $errors; - } - - /** - * Build the application. - * - * @param string $directory - * @param string $template - * @param string $output - * @param string $version - */ - public function build(string $directory, string $template, string $output, string $version) - { - $filename = $directory . "/main.go"; - $output = $output . ($this->getOS() == 'windows' ? '.exe' : ''); - - // step 1, generate template - $this->generate($template, $filename); - - $command = sprintf( - 'docker run --rm -v "%s":/mnt -e RR_VERSION=%s -e GOARCH=amd64 -e GOOS=%s %s /bin/bash -c "mv /mnt/main.go main.go; bash compile.sh; cp rr /mnt/%s;"', - $directory, - $version, - $this->getOS(), - self::DOCKER, - $output - ); - - self::cprintf("<yellow>%s</reset>\n", $command); - - // run the build - $this->run($command, true); - - if (!file_exists($directory . '/' . $output)) { - self::cprintf("<red>Build has failed!</reset>"); - return; - } - - self::cprintf("<green>Build complete!</reset>\n"); - $this->run($directory . '/' . $output, false); - } - - /** - * @param string $command - * @param bool $shadow - */ - protected function run(string $command, bool $shadow = false) - { - $shadow && self::cprintf("<gray>"); - passthru($command); - $shadow && self::cprintf("</reset>"); - } - - /** - * @param string $template - * @param string $filename - */ - protected function generate(string $template, string $filename) - { - $body = file_get_contents($template); - - $replace = [ - '// -packages- //' => '"' . join("\"\n\"", $this->config['packages']) . '"', - '// -commands- //' => '_ "' . join("\"\n_ \"", $this->config['commands']) . '"', - '// -register- //' => join("\n", $this->config['register']) - ]; - - // compile the template - $result = str_replace(array_keys($replace), array_values($replace), $body); - file_put_contents($filename, $result); - } - - /** - * @return string - */ - protected function getOS(): string - { - $os = strtolower(PHP_OS); - - if (strpos($os, 'darwin') !== false) { - return 'darwin'; - } - - if (strpos($os, 'win') !== false) { - return 'windows'; - } - - return "linux"; - } - - /** - * Create new builder using given config. - * - * @param string $config - * @return Builder|null - */ - public static function loadConfig(string $config): ?Builder - { - if (!file_exists($config)) { - return null; - } - - $configData = json_decode(file_get_contents($config), true); - if (!is_array($configData)) { - return null; - } - - return new Builder($configData); - } - - /** - * Make colored output. - * - * @param string $format - * @param mixed ...$args - */ - public static function cprintf(string $format, ...$args) - { - if (self::isColorsSupported()) { - $format = preg_replace_callback("/<\/?([^>]+)>/", function ($value) { - return self::$colors[$value[1]]; - }, $format); - } else { - $format = preg_replace("/<[^>]+>/", "", $format); - } - - echo sprintf($format, ...$args); - } - - /** - * @return bool - */ - public static function isWindows(): bool - { - return \DIRECTORY_SEPARATOR === '\\'; - } - - /** - * Returns true if the STDOUT supports colorization. - * - * @codeCoverageIgnore - * @link https://github.com/symfony/Console/blob/master/Output/StreamOutput.php#L94 - * @param mixed $stream - * @return bool - */ - public static function isColorsSupported($stream = STDOUT): bool - { - if ('Hyper' === getenv('TERM_PROGRAM')) { - return true; - } - - try { - if (\DIRECTORY_SEPARATOR === '\\') { - return ( - function_exists('sapi_windows_vt100_support') - && @sapi_windows_vt100_support($stream) - ) || getenv('ANSICON') !== false - || getenv('ConEmuANSI') == 'ON' - || getenv('TERM') == 'xterm'; - } - - if (\function_exists('stream_isatty')) { - return (bool)@stream_isatty($stream); - } - - if (\function_exists('posix_isatty')) { - return (bool)@posix_isatty($stream); - } - - $stat = @fstat($stream); - // Check if formatted mode is S_IFCHR - return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; - } catch (\Throwable $e) { - return false; - } - } -} |