From b4a58f8e6c4990fa0d00f3fae22c96864d9cff72 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Fri, 11 Jan 2019 15:54:36 +0300 Subject: quickbuilds --- build/.build.json | 17 ++++ build/build.php | 39 +++++++ build/docker/Dockerfile | 8 ++ build/docker/compile.sh | 9 ++ build/main.go | 14 +++ build/src/Builder.php | 237 +++++++++++++++++++++++++++++++++++++++++++ quickbuild/.build.json | 17 ---- quickbuild/build.php | 39 ------- quickbuild/docker/Dockerfile | 8 -- quickbuild/docker/compile.sh | 9 -- quickbuild/main.go | 14 --- quickbuild/src/Builder.php | 237 ------------------------------------------- 12 files changed, 324 insertions(+), 324 deletions(-) create mode 100644 build/.build.json create mode 100644 build/build.php create mode 100644 build/docker/Dockerfile create mode 100644 build/docker/compile.sh create mode 100644 build/main.go create mode 100644 build/src/Builder.php delete mode 100644 quickbuild/.build.json delete mode 100644 quickbuild/build.php delete mode 100644 quickbuild/docker/Dockerfile delete mode 100644 quickbuild/docker/compile.sh delete mode 100644 quickbuild/main.go delete mode 100644 quickbuild/src/Builder.php diff --git a/build/.build.json b/build/.build.json new file mode 100644 index 00000000..74b83cea --- /dev/null +++ b/build/.build.json @@ -0,0 +1,17 @@ +{ + "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/build/build.php b/build/build.php new file mode 100644 index 00000000..63bd1460 --- /dev/null +++ b/build/build.php @@ -0,0 +1,39 @@ +RoadRunner specifically for you (version: %s)...\n", + $version +); + +$builder = Builder::loadConfig($config); +if ($builder == null) { + Builder::cprintf("Unable to load config: %s\n", $config); + return; +} + +$errors = $builder->configErrors(); +if (!empty($errors)) { + Builder::cprintf("Found configuration errors:\n"); + foreach ($errors as $error) { + Builder::cprintf("- %s\n", $error); + } + + return; +} + +// Start build +$builder->build(getcwd(), __DIR__ . '/main.go', 'rr', $version); \ No newline at end of file diff --git a/build/docker/Dockerfile b/build/docker/Dockerfile new file mode 100644 index 00000000..11fb5abb --- /dev/null +++ b/build/docker/Dockerfile @@ -0,0 +1,8 @@ +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/build/docker/compile.sh b/build/docker/compile.sh new file mode 100644 index 00000000..0c85124f --- /dev/null +++ b/build/docker/compile.sh @@ -0,0 +1,9 @@ +#!/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/build/main.go b/build/main.go new file mode 100644 index 00000000..a065fe27 --- /dev/null +++ b/build/main.go @@ -0,0 +1,14 @@ +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/build/src/Builder.php b/build/src/Builder.php new file mode 100644 index 00000000..5568d725 --- /dev/null +++ b/build/src/Builder.php @@ -0,0 +1,237 @@ + "\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("%s\n", $command); + + // run the build + $this->run($command, true); + + if (!file_exists($directory . '/' . $output)) { + self::cprintf("Build has failed!"); + return; + } + + self::cprintf("Build complete!\n"); + $this->run($directory . '/' . $output, false); + } + + /** + * @param string $command + * @param bool $shadow + */ + protected function run(string $command, bool $shadow = false) + { + $shadow && self::cprintf(""); + passthru($command); + $shadow && self::cprintf(""); + } + + /** + * @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, 'win') !== false) { + return 'windows'; + } + + if (strpos($os, 'darwin') !== false) { + return 'darwin'; + } + + 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; + } + } +} \ No newline at end of file diff --git a/quickbuild/.build.json b/quickbuild/.build.json deleted file mode 100644 index 74b83cea..00000000 --- a/quickbuild/.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/quickbuild/build.php b/quickbuild/build.php deleted file mode 100644 index 63bd1460..00000000 --- a/quickbuild/build.php +++ /dev/null @@ -1,39 +0,0 @@ -RoadRunner specifically for you (version: %s)...\n", - $version -); - -$builder = Builder::loadConfig($config); -if ($builder == null) { - Builder::cprintf("Unable to load config: %s\n", $config); - return; -} - -$errors = $builder->configErrors(); -if (!empty($errors)) { - Builder::cprintf("Found configuration errors:\n"); - foreach ($errors as $error) { - Builder::cprintf("- %s\n", $error); - } - - return; -} - -// Start build -$builder->build(getcwd(), __DIR__ . '/main.go', 'rr', $version); \ No newline at end of file diff --git a/quickbuild/docker/Dockerfile b/quickbuild/docker/Dockerfile deleted file mode 100644 index 11fb5abb..00000000 --- a/quickbuild/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/quickbuild/docker/compile.sh b/quickbuild/docker/compile.sh deleted file mode 100644 index 0c85124f..00000000 --- a/quickbuild/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/quickbuild/main.go b/quickbuild/main.go deleted file mode 100644 index a065fe27..00000000 --- a/quickbuild/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/quickbuild/src/Builder.php b/quickbuild/src/Builder.php deleted file mode 100644 index 5568d725..00000000 --- a/quickbuild/src/Builder.php +++ /dev/null @@ -1,237 +0,0 @@ - "\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("%s\n", $command); - - // run the build - $this->run($command, true); - - if (!file_exists($directory . '/' . $output)) { - self::cprintf("Build has failed!"); - return; - } - - self::cprintf("Build complete!\n"); - $this->run($directory . '/' . $output, false); - } - - /** - * @param string $command - * @param bool $shadow - */ - protected function run(string $command, bool $shadow = false) - { - $shadow && self::cprintf(""); - passthru($command); - $shadow && self::cprintf(""); - } - - /** - * @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, 'win') !== false) { - return 'windows'; - } - - if (strpos($os, 'darwin') !== false) { - return 'darwin'; - } - - 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; - } - } -} \ No newline at end of file -- cgit v1.2.3