Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Add task BundlePlatformRubyVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweetchuck committed Mar 15, 2018
1 parent 20df9a3 commit 71c634b
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/BundlerTaskLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
31 changes: 31 additions & 0 deletions src/Task/BaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -275,6 +301,11 @@ public function getCommand()

$cmdAsIs = [];

if ($this->getRubyExecutable()) {
$cmdPattern[] = '%s';
$cmdArgs[] = escapeshellcmd($this->getRubyExecutable());
}

$cmdPattern[] = '%s';
$cmdArgs[] = escapeshellcmd($this->getBundleExecutable());

Expand Down
48 changes: 48 additions & 0 deletions src/Task/BundlePlatformRubyVersionTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Sweetchuck\Robo\Bundler\Task;

use Sweetchuck\Robo\Bundler\Utils;

class BundlePlatformRubyVersionTask extends BaseTask
{
/**
* {@inheritdoc}
*/
protected $taskName = 'BundlePlatformRubyVersion';

/**
* {@inheritdoc}
*/
protected $action = 'platform';

/**
* {@inheritdoc}
*/
protected function getCommandOptions(): array
{
return [
'ruby' => [
'type' => 'flag',
'value' => true,
],
] + parent::getCommandOptions();
}

/**
* {@inheritdoc}
*/
protected function runProcessOutputs()
{
$this->assets['full'] = '';
if ($this->actionExitCode === 0) {
$parts = explode(' ', trim($this->actionStdOutput), 2) + [1 => ''];
$rubyVersion = Utils::parseRubyVersion($parts[1]);
foreach ($rubyVersion as $key => $value) {
$this->assets[$key] = $value;
}
}

return $this;
}
}
23 changes: 23 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,27 @@ public static function filterEnabled(array $items): array
array_keys($items, true, true)
: $items;
}

public static function parseRubyVersion(string $version): array
{
$pattern = '/^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<fix>\d+)(p(?P<patch>\d+)){0,1}$/';
$versionParts = [
'full' => $version,
'base' => '',
'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;
$versionParts['base'] = "{$versionParts['major']}.{$versionParts['minor']}.{$versionParts['fix']}";
}

return $versionParts;
}
}
31 changes: 31 additions & 0 deletions tests/_support/Helper/RoboFiles/BundleRoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,6 +56,35 @@ public function showPaths(): TaskInterface
->setBundleGemFile($this->dataDir('Gemfile.success.rb'));
}

/**
* @command bundle:platform:ruby-version
*/
public function bundlePlatformRubyVersion()
{
return $this
->collectionBuilder()
->addTask(
$this
->taskBundlePlatformRubyVersion()
->setAssetNamePrefix('bundlePlatformRubyVersion.')
->setBundleGemFile($this->dataDir('Gemfile.success.rb'))
)
->addCode(function (RoboStateData $data) {
$versionParts = [
'full' => $data['bundlePlatformRubyVersion.full'],
'base' => $data['bundlePlatformRubyVersion.base'],
'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);
Expand Down
32 changes: 32 additions & 0 deletions tests/acceptance/Task/BundlePlatformRubyVersionCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Sweetchuck\Robo\Bundler\Tests\Acceptance\Task;

use Sweetchuck\Robo\Bundler\Test\AcceptanceTester;
use Sweetchuck\Robo\Bundler\Test\Helper\RoboFiles\BundleRoboFile;

class BundlePlatformRubyVersionCest
{
public function runInstallSuccess(AcceptanceTester $I)
{
$id = 'bundle:platform:ruby-version';
$I->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',
'base: 2.3.1',
'major: 2',
'minor: 3',
'fix: 1',
'patch: 112',
'',
]),
$stdOutput
);
}
}
8 changes: 8 additions & 0 deletions tests/unit/Task/BundleCheckTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand Down

0 comments on commit 71c634b

Please sign in to comment.