From 368a402354c0008acc2a0aebe12f151d006601bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Andor?= Date: Wed, 14 Mar 2018 20:23:18 +0100 Subject: [PATCH] Add task BundlePlatformRubyVersion --- src/BundlerTaskLoader.php | 12 +++++ src/Task/BaseTask.php | 31 ++++++++++++ src/Task/BundlePlatformRubyVersionTask.php | 48 +++++++++++++++++++ src/Utils.php | 21 ++++++++ .../Helper/RoboFiles/BundleRoboFile.php | 29 +++++++++++ .../Task/BundlePlatformRubyVersionCest.php | 31 ++++++++++++ tests/unit/Task/BundleCheckTaskTest.php | 8 ++++ 7 files changed, 180 insertions(+) create mode 100644 src/Task/BundlePlatformRubyVersionTask.php create mode 100644 tests/acceptance/Task/BundlePlatformRubyVersionCest.php diff --git a/src/BundlerTaskLoader.php b/src/BundlerTaskLoader.php index acf19a8..853b353 100644 --- a/src/BundlerTaskLoader.php +++ b/src/BundlerTaskLoader.php @@ -42,6 +42,18 @@ protected function taskBundleInstall(array $options = []): CollectionBuilder return $task; } + /** + * @return \Sweetchuck\Robo\Bundler\Task\BundlePlatformRubyVersionTask|\Robo\Collection\CollectionBuilder + */ + protected function taskBundlePlatformRubyVersion(array $options = []): CollectionBuilder + { + /** @var \Sweetchuck\Robo\Bundler\Task\BundlePlatformRubyVersionTask $task */ + $task = $this->task(Task\BundlePlatformRubyVersionTask::class); + $task->setOptions($options); + + return $task; + } + /** * @return \Sweetchuck\Robo\Bundler\Task\BundleShowPathsTask|\Robo\Collection\CollectionBuilder */ diff --git a/src/Task/BaseTask.php b/src/Task/BaseTask.php index 1dcd500..9ab0ad0 100644 --- a/src/Task/BaseTask.php +++ b/src/Task/BaseTask.php @@ -149,6 +149,28 @@ public function setGemFile(string $gemFile) } //endregion + // region rubyExecutable + /** + * @var string + */ + protected $rubyExecutable = ''; + + public function getRubyExecutable(): string + { + return $this->rubyExecutable; + } + + /** + * @return $this + */ + public function setRubyExecutable(string $value) + { + $this->rubyExecutable = $value; + + return $this; + } + // endregion + //region Option - bundleExecutable. /** * @var string @@ -240,6 +262,10 @@ public function setOptions(array $option) $this->setGemFile($value); break; + case 'rubyExecutable': + $this->setRubyExecutable($value); + break; + case 'bundleExecutable': $this->setBundleExecutable($value); break; @@ -275,6 +301,11 @@ public function getCommand() $cmdAsIs = []; + if ($this->getRubyExecutable()) { + $cmdPattern[] = '%s'; + $cmdArgs[] = escapeshellcmd($this->getRubyExecutable()); + } + $cmdPattern[] = '%s'; $cmdArgs[] = escapeshellcmd($this->getBundleExecutable()); diff --git a/src/Task/BundlePlatformRubyVersionTask.php b/src/Task/BundlePlatformRubyVersionTask.php new file mode 100644 index 0000000..34d598f --- /dev/null +++ b/src/Task/BundlePlatformRubyVersionTask.php @@ -0,0 +1,48 @@ + [ + 'type' => 'flag', + 'value' => true, + ], + ] + parent::getCommandOptions(); + } + + /** + * {@inheritdoc} + */ + protected function runProcessOutputs() + { + $this->assets['bundlePlatformRubyVersion.full'] = ''; + if ($this->actionExitCode === 0) { + $parts = explode(' ', trim($this->actionStdOutput), 2) + [1 => '']; + $rubyVersion = Utils::parseRubyVersion($parts[1]); + foreach ($rubyVersion as $key => $value) { + $this->assets["bundlePlatformRubyVersion.{$key}"] = $value; + } + } + + return $this; + } +} diff --git a/src/Utils.php b/src/Utils.php index 8faf73c..51d6121 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -10,4 +10,25 @@ public static function filterEnabled(array $items): array array_keys($items, true, true) : $items; } + + public static function parseRubyVersion(string $version): array + { + $pattern = '/^(?P\d+)\.(?P\d+)\.(?P\d+)(p(?P\d+)){0,1}$/'; + $versionParts = [ + 'full' => $version, + 'major' => '', + 'minor' => '', + 'fix' => '', + 'patch' => '', + ]; + $matches = []; + if (preg_match($pattern, $version, $matches)) { + $versionParts['major'] = (int) $matches['major']; + $versionParts['minor'] = (int) $matches['minor']; + $versionParts['fix'] = (int) $matches['fix']; + $versionParts['patch'] = $matches['patch'] ? (int) $matches['patch'] : null; + } + + return $versionParts; + } } diff --git a/tests/_support/Helper/RoboFiles/BundleRoboFile.php b/tests/_support/Helper/RoboFiles/BundleRoboFile.php index b7d23d6..5a2fdd4 100644 --- a/tests/_support/Helper/RoboFiles/BundleRoboFile.php +++ b/tests/_support/Helper/RoboFiles/BundleRoboFile.php @@ -5,7 +5,9 @@ use Sweetchuck\Robo\Bundler\BundlerTaskLoader; use Sweetchuck\Robo\Bundler\Test\Helper\Dummy\DummyCommand as DummyCommand; use Robo\Contract\TaskInterface; +use Robo\State\Data as RoboStateData; use Robo\Tasks; +use Symfony\Component\Yaml\Yaml; use Webmozart\PathUtil\Path; class BundleRoboFile extends Tasks @@ -54,6 +56,33 @@ public function showPaths(): TaskInterface ->setBundleGemFile($this->dataDir('Gemfile.success.rb')); } + /** + * @command bundle:platform:ruby-version + */ + public function bundlePlatformRubyVersion() + { + return $this + ->collectionBuilder() + ->addTask( + $this + ->taskBundlePlatformRubyVersion() + ->setBundleGemFile($this->dataDir('Gemfile.success.rb')) + ) + ->addCode(function (RoboStateData $data) { + $versionParts = [ + 'full' => $data['bundlePlatformRubyVersion.full'], + 'major' => $data['bundlePlatformRubyVersion.major'], + 'minor' => $data['bundlePlatformRubyVersion.minor'], + 'fix' => $data['bundlePlatformRubyVersion.fix'], + 'patch' => $data['bundlePlatformRubyVersion.patch'], + ]; + + $this->output()->write(Yaml::dump($versionParts)); + + return 0; + }); + } + protected function dataDir(string $suffix = ''): string { return Path::join(__DIR__, '../../../_data/', $suffix); diff --git a/tests/acceptance/Task/BundlePlatformRubyVersionCest.php b/tests/acceptance/Task/BundlePlatformRubyVersionCest.php new file mode 100644 index 0000000..2cad519 --- /dev/null +++ b/tests/acceptance/Task/BundlePlatformRubyVersionCest.php @@ -0,0 +1,31 @@ +runRoboTask($id, BundleRoboFile::class, 'bundle:platform:ruby-version'); + + $exitCode = $I->getRoboTaskExitCode($id); + $stdOutput = $I->getRoboTaskStdOutput($id); + + $I->assertEquals(0, $exitCode); + $I->assertEquals( + implode(PHP_EOL, [ + 'full: 2.3.1p112', + 'major: 2', + 'minor: 3', + 'fix: 1', + 'patch: 112', + '', + ]), + $stdOutput + ); + } +} diff --git a/tests/unit/Task/BundleCheckTaskTest.php b/tests/unit/Task/BundleCheckTaskTest.php index fb11f14..c78bf7c 100644 --- a/tests/unit/Task/BundleCheckTaskTest.php +++ b/tests/unit/Task/BundleCheckTaskTest.php @@ -38,6 +38,14 @@ public function casesGetCommand(): array 'gemFile' => 'myGemfile', ], ], + 'Gemfile & rubyExecutable & bundleExecutable' => [ + "BUNDLE_GEMFILE='../myGemfile' my-ruby my-bundle check", + [ + 'bundleGemFile' => '../myGemfile', + 'rubyExecutable' => 'my-ruby', + 'bundleExecutable' => 'my-bundle', + ], + ], 'bundleExecutable' => [ 'my-bundle check', [