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

Bundle platform ruby version #9

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sweetchuck/robo-bundler",
"description": "Robo task wrapper for Bundler.",
"license": "GPL-2.0",
"license": "GPL-2.0-or-later",
"config": {
"bin-dir": "bin",
"sort-packages": true
Expand All @@ -10,16 +10,17 @@
"prefer-stable": true,
"require": {
"php": ">=7.1",
"consolidation/robo": "^1.0"
"consolidation/robo": "^1.0",
"icecave/semver": "^3.0"
},
"require-dev": {
"codeception/codeception": "^2.2",
"danielstjules/stringy": "^3.0",
"sweetchuck/codeception-module-robo-task-runner": "^0.0.1",
"sweetchuck/codeception-module-robo-task-runner": "^0.0",
"sweetchuck/git-hooks": "^0.0",
"sweetchuck/robo-git": "^0.0",
"sweetchuck/robo-phpcs": "^0.0",
"symfony/finder": "^3.2",
"symfony/finder": "^3.2 || ^4.0",
"webmozart/path-util": "^2.3"
},
"autoload": {
Expand Down
1,515 changes: 861 additions & 654 deletions composer.lock

Large diffs are not rendered by default.

36 changes: 32 additions & 4 deletions src/BundlerTaskLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,58 @@ trait BundlerTaskLoader
*/
protected function taskBundleCheck(array $options = []): CollectionBuilder
{
return $this->task(Task\BundleCheckTask::class, $options);
/** @var \Sweetchuck\Robo\Bundler\Task\BundleCheckTask $task */
$task = $this->task(Task\BundleCheckTask::class);
$task->setOptions($options);

return $task;
}

/**
* @return \Sweetchuck\Robo\Bundler\Task\BundleExecTask|\Robo\Collection\CollectionBuilder
*/
protected function taskBundleExec(array $options = []): CollectionBuilder
{
return $this->task(Task\BundleExecTask::class, $options);
/** @var \Sweetchuck\Robo\Bundler\Task\BundleExecTask $task */
$task = $this->task(Task\BundleExecTask::class);
$task->setOptions($options);

return $task;
}

/**
* @return \Sweetchuck\Robo\Bundler\Task\BundleInstallTask|\Robo\Collection\CollectionBuilder
*/
protected function taskBundleInstall(array $options = []): CollectionBuilder
{
return $this->task(Task\BundleInstallTask::class, $options);
/** @var \Sweetchuck\Robo\Bundler\Task\BundleInstallTask $task */
$task = $this->task(Task\BundleInstallTask::class);
$task->setOptions($options);

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
*/
protected function taskBundleShowPaths(array $options = []): CollectionBuilder
{
return $this->task(Task\BundleShowPathsTask::class, $options);
/** @var \Sweetchuck\Robo\Bundler\Task\BundleShowPathsTask $task */
$task = $this->task(Task\BundleShowPathsTask::class);
$task->setOptions($options);

return $task;
}
}
43 changes: 33 additions & 10 deletions src/Task/BaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,43 @@ 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
*/
protected $bundleExecutable = 'bundle';

protected function getBundleExecutable(): string
public function getBundleExecutable(): string
{
return $this->bundleExecutable;
}

/**
* @return $this
*/
protected function setBundleExecutable(string $value)
public function setBundleExecutable(string $value)
{
$this->bundleExecutable = $value;

Expand Down Expand Up @@ -217,14 +239,6 @@ public function setNoColor(?bool $value)

//endregion

/**
* {@inheritdoc}
*/
public function __construct(array $options = [])
{
$this->setOptions($options);
}

/**
* @return $this
*/
Expand All @@ -248,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 @@ -283,6 +301,11 @@ public function getCommand()

$cmdAsIs = [];

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Task/BundleCheckTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BundleCheckTask extends BaseTask
/**
* {@inheritdoc}
*/
protected $taskName = 'BundleCheck';
protected $taskName = 'Bundler - Check';

/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion src/Task/BundleExecTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BundleExecTask extends BaseTask
/**
* {@inheritdoc}
*/
protected $taskName = 'BundleExec';
protected $taskName = 'Bundler - Exec';

/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion src/Task/BundleInstallTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BundleInstallTask extends BaseTask
/**
* {@inheritdoc}
*/
protected $taskName = 'BundleInstall';
protected $taskName = 'Bundler - Install';

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

namespace Sweetchuck\Robo\Bundler\Task;

use Icecave\SemVer\Version as SemVerVersion;

class BundlePlatformRubyVersionTask extends BaseTask
{
/**
* {@inheritdoc}
*/
protected $taskName = 'Bundler - Platform - Ruby version';

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

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

/**
* {@inheritdoc}
*/
protected function runProcessOutputs()
{
parent::runProcessOutputs();
$this->assets['original'] = '';
$this->assets['full'] = '';

if ($this->actionExitCode === 0) {
list(, $versionOriginal) = explode(' ', trim($this->actionStdOutput), 2) + [1 => ''];
$versionFull = $this->convertRubyVersionPatchLevelToSemVerBuildMetaData($versionOriginal);
if (!SemVerVersion::isValid($versionFull)) {
// No ruby version specified.
return $this;
}

$version = SemVerVersion::parse($versionFull);
$this->assets['original'] = $versionOriginal;
$this->assets['full'] = $versionFull;
$this->assets['semVerVersion'] = $version;
$this->assets['major'] = $version->major();
$this->assets['minor'] = $version->minor();
$this->assets['patch'] = $version->patch();
$this->assets['preReleaseVersion'] = $version->preReleaseVersion();
$this->assets['buildMetaData'] = $version->buildMetaData();
$this->assets['base'] = sprintf('%d.%d.%d', $version->major(), $version->minor(), $version->patch());
}

return $this;
}

protected function convertRubyVersionPatchLevelToSemVerBuildMetaData(string $rubyVersion): string
{
$matches = [];
$pattern = '/^(?P<version>\d+\.\d+\.\d+)(?P<patch>p\d+)$/';
if (preg_match($pattern, $rubyVersion, $matches)) {
$rubyVersion = $matches['version'] . '+' . $matches['patch'];
}

return $rubyVersion;
}
}
32 changes: 32 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,36 @@ 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 = [
'original' => $data['bundlePlatformRubyVersion.original'],
'full' => $data['bundlePlatformRubyVersion.full'],
'base' => $data['bundlePlatformRubyVersion.base'],
'major' => $data['bundlePlatformRubyVersion.major'],
'minor' => $data['bundlePlatformRubyVersion.minor'],
'patch' => $data['bundlePlatformRubyVersion.patch'],
'buildMetaData' => $data['bundlePlatformRubyVersion.buildMetaData'],
];

$this->output()->write(Yaml::dump($versionParts));

return 0;
});
}

protected function dataDir(string $suffix = ''): string
{
return Path::join(__DIR__, '../../../_data/', $suffix);
Expand Down
33 changes: 33 additions & 0 deletions tests/acceptance/Task/BundlePlatformRubyVersionCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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, [
'original: 2.3.1p112',
'full: 2.3.1+p112',
'base: 2.3.1',
'major: 2',
'minor: 3',
'patch: 1',
'buildMetaData: p112',
'',
]),
$stdOutput
);
}
}
11 changes: 10 additions & 1 deletion 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 Expand Up @@ -90,7 +98,8 @@ public function casesGetCommand(): array
*/
public function testGetCommand(string $expected, array $options): void
{
$task = new BundleCheckTask($options);
$task = new BundleCheckTask();
$task->setOptions($options);
$this->tester->assertEquals($expected, $task->getCommand());
}
}
3 changes: 2 additions & 1 deletion tests/unit/Task/BundleExecTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function casesGetCommand(): array
*/
public function testGetCommand(string $expected, array $options): void
{
$task = new BundleExecTask($options);
$task = new BundleExecTask();
$task->setOptions($options);
$this->tester->assertEquals($expected, $task->getCommand());
}
}
Loading