Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add brunch support #239

Merged
merged 4 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ parameters:
ant: ~
atoum: ~
behat: ~
brunch: ~
clover_coverage: ~
codeception: ~
composer: ~
Expand Down
2 changes: 2 additions & 0 deletions doc/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ parameters:
ant: ~
atoum: ~
behat: ~
brunch: ~
clover_coverage: ~
codeception: ~
composer: ~
Expand Down Expand Up @@ -51,6 +52,7 @@ Every task has it's own default configuration. It is possible to overwrite the p
- [Ant](tasks/ant.md)
- [Atoum](tasks/atoum.md)
- [Behat](tasks/behat.md)
- [Brunch](tasks/brunch.md)
- [Clover Coverage](tasks/clover_coverage.md)
- [Codeception](tasks/codeception.md)
- [Composer](tasks/composer.md)
Expand Down
62 changes: 62 additions & 0 deletions doc/tasks/brunch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Brunch

The Brunch task will run your automated frontend tasks.
It lives under the `brunch` namespace and has following configurable parameters:

```yaml
# grumphp.yml
parameters:
tasks:
brunch:
task: build
env: production
jobs: 4
debug: false
triggered_by: [js, jsx, coffee, ts, less, sass, scss]
```

**brunch_file**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The brunch_file section is removed from the task.
Is there a reason why it is removed or can it be re-added?
If no, then this section in the documentation should be removed.

Copy link
Contributor Author

@ptondereau ptondereau Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brunch_file doesn't exist anymore since 2.0, i'll remove it.


*Default: null*

If your `brunch-config.js file is located at an exotic location, you can specify your custom brunch file location with this option.
This option is set to `null` by default.
This means that `brunch-config.js` is automatically loaded if the file exists in the current directory.


**task**

*Default: build*

This option specifies which Brunch task you want to run.
This option is set to `build` by default.
This means that brunch will run the `build` task.
Note that this task should be used to compile your assets.
It is also possible to alter code during commit, but this is surely **NOT** recommended!

**env**

*Default: production*

This option specifies in which format you want to compile your assets.
E.g: `--env production`. You can specify the env you set up in your brunch config file.

**jobs**

*Default: 4*

This option enables experimental multi-process support. May improve compilation speed of large projects. Try different WORKERS amount to see which one works best for your system.

**debug**

*Default: false*

It enables verbose debug output.

**triggered_by**

*Default: [js, jsx, coffee, ts, less, sass, scss]*

This option will specify which file extensions will trigger the brunch task.
By default Brunch will be triggered by altering a front-end file.
You can overwrite this option to whatever file you want to use!
9 changes: 9 additions & 0 deletions resources/config/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ services:
tags:
- {name: grumphp.task, config: behat}

task.brunch:
class: GrumPHP\Task\Brunch
arguments:
- '@config'
- '@process_builder'
- '@formatter.raw_process'
tags:
- {name: grumphp.task, config: brunch}

task.clovercoverage:
class: GrumPHP\Task\CloverCoverage
arguments:
Expand Down
113 changes: 113 additions & 0 deletions spec/GrumPHP/Task/BrunchSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace spec\GrumPHP\Task;

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Collection\ProcessArgumentsCollection;
use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Process\ProcessBuilder;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Brunch;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Process\Process;

/**
* Class BrunchSpec
*/
class BrunchSpec extends ObjectBehavior
{
function let(GrumPHP $grumPHP, ProcessBuilder $processBuilder, ProcessFormatterInterface $formatter)
{
$grumPHP->getTaskConfiguration('brunch')->willReturn([]);
$this->beConstructedWith($grumPHP, $processBuilder, $formatter);
}

function it_is_initializable()
{
$this->shouldHaveType(Brunch::class);
}

function it_should_have_a_name()
{
$this->getName()->shouldBe('brunch');
}

function it_should_have_configurable_options()
{
$options = $this->getConfigurableOptions();
$options->shouldBeAnInstanceOf(OptionsResolver::class);
$options->getDefinedOptions()->shouldContain('task');
$options->getDefinedOptions()->shouldContain('env');
$options->getDefinedOptions()->shouldContain('jobs');
$options->getDefinedOptions()->shouldContain('debug');
$options->getDefinedOptions()->shouldContain('triggered_by');
}

function it_should_run_in_git_pre_commit_context(GitPreCommitContext $context)
{
$this->canRunInContext($context)->shouldReturn(true);
}

function it_should_run_in_run_context(RunContext $context)
{
$this->canRunInContext($context)->shouldReturn(true);
}

function it_does_not_do_anything_if_there_are_no_files(ProcessBuilder $processBuilder, ContextInterface $context)
{
$processBuilder->createArgumentsForCommand('brunch')->shouldNotBeCalled();
$processBuilder->buildProcess()->shouldNotBeCalled();
$context->getFiles()->willReturn(new FilesCollection());

$result = $this->run($context);
$result->shouldBeAnInstanceOf(TaskResultInterface::class);
$result->getResultCode()->shouldBe(TaskResult::SKIPPED);
}

function it_runs_the_suite(ProcessBuilder $processBuilder, Process $process, ContextInterface $context)
{
$arguments = new ProcessArgumentsCollection();
$processBuilder->createArgumentsForCommand('brunch')->willReturn($arguments);
$processBuilder->buildProcess($arguments)->willReturn($process);

$process->run()->shouldBeCalled();
$process->isSuccessful()->willReturn(true);

$context->getFiles()->willReturn(new FilesCollection([
new SplFileInfo('test.js', '.', 'test.js')
]));

$result = $this->run($context);
$result->shouldBeAnInstanceOf(TaskResultInterface::class);
$result->isPassed()->shouldBe(true);
}

function it_throws_exception_if_the_process_fails(
ProcessBuilder $processBuilder,
Process $process,
ContextInterface $context
) {
$arguments = new ProcessArgumentsCollection();
$processBuilder->createArgumentsForCommand('brunch')->willReturn($arguments);
$processBuilder->buildProcess($arguments)->willReturn($process);

$process->run()->shouldBeCalled();
$process->isSuccessful()->willReturn(false);

$context->getFiles()->willReturn(new FilesCollection([
new SplFileInfo('test.js', '.', 'test.js')
]));

$result = $this->run($context);
$result->shouldBeAnInstanceOf(TaskResultInterface::class);
$result->isPassed()->shouldBe(false);
}
}
81 changes: 81 additions & 0 deletions src/GrumPHP/Task/Brunch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace GrumPHP\Task;

use GrumPHP\Runner\TaskResult;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Brunch task
*/
class Brunch extends AbstractExternalTask
{
/**
* @return string
*/
public function getName()
{
return 'brunch';
}

/**
* @return OptionsResolver
*/
public function getConfigurableOptions()
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'task' => 'build',
'env' => 'production',
'jobs' => 4,
'debug' => false,
'triggered_by' => ['js', 'jsx', 'coffee', 'ts', 'less', 'sass', 'scss']
]);

$resolver->addAllowedTypes('task', ['string']);
$resolver->addAllowedTypes('env', ['string']);
$resolver->addAllowedTypes('jobs', ['int']);
$resolver->addAllowedTypes('debug', ['bool']);
$resolver->addAllowedTypes('triggered_by', ['array']);

return $resolver;
}

/**
* {@inheritdoc}
*/
public function canRunInContext(ContextInterface $context)
{
return ($context instanceof GitPreCommitContext || $context instanceof RunContext);
}

/**
* {@inheritdoc}
*/
public function run(ContextInterface $context)
{
$config = $this->getConfiguration();
$files = $context->getFiles()->extensions($config['triggered_by']);
if (0 === count($files)) {
return TaskResult::createSkipped($this, $context);
}

$arguments = $this->processBuilder->createArgumentsForCommand('brunch');
$arguments->addRequiredArgument('%s', $config['task']);
$arguments->addOptionalArgument('--env %s', $config['env']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in the escaped CLI argument '--env production', shouldn't this be 2 separate arguments like this: '--env' 'production'?
If so, you could use the $arguments->addOptionalArgumentWithSeparatedValue() method.

Copy link
Contributor Author

@ptondereau ptondereau Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should result to $ brunch build --env production --> --env 'production'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I guess you should use addOptionalArgumentWithSeparatedValue() instead.

$arguments->addOptionalArgument('--jobs %s', $config['jobs']);
$arguments->addOptionalArgument('--debug', $config['debug']);

$process = $this->processBuilder->buildProcess($arguments);
$process->run();

if (!$process->isSuccessful()) {
return TaskResult::createFailed($this, $context, $this->formatter->format($process));
}

return TaskResult::createPassed($this, $context);
}
}