-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic setup deptrac * added config options and documentation * updated readme * updated task option configuration
- Loading branch information
Showing
6 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Deptrac | ||
|
||
Follow the [installation instructions](https://github.com/sensiolabs-de/deptrac#installation) to add deptrac to your | ||
project. | ||
|
||
The Deptrac task will check for dependencies between the software layers of your project. It lives under the `deptrac` | ||
namespace and has following configurable parameters: | ||
|
||
|
||
```yaml | ||
# grumphp.yml | ||
parameters: | ||
tasks: | ||
deptrac: | ||
depfile: ~ | ||
formatter_graphviz: ~ | ||
formatter_graphviz_display: ~ | ||
formatter_graphviz_dump_image: ~ | ||
formatter_graphviz_dump_dot: ~ | ||
formatter_graphviz_dump_html: ~ | ||
``` | ||
**depfile** | ||
*Default: null* | ||
Set path to deptrac configuration file. Example: `/var/www/src/depfile.yml` | ||
|
||
**formatter_graphviz** | ||
|
||
*Default: false* | ||
|
||
Set to `true` to enable the graphviz formatter. | ||
|
||
**formatter_graphviz_display** | ||
|
||
*Default: false* | ||
|
||
Open the generated graphviz image. Set to `true` to activate. | ||
|
||
**formatter_graphviz_dump_image** | ||
|
||
*Default: null* | ||
|
||
Set path to a dumped png file. | ||
|
||
**formatter_graphviz_dump_dot** | ||
|
||
*Default: null* | ||
|
||
Set path to a dumped dot file. | ||
|
||
**formatter_graphviz_dump_html** | ||
|
||
*Default: null* | ||
|
||
Set path to a dumped html file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?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\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use GrumPHP\Task\Deptrac; | ||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Process\Process; | ||
|
||
class DeptracSpec extends ObjectBehavior | ||
{ | ||
function let(GrumPHP $grumPHP, ProcessBuilder $processBuilder, ProcessFormatterInterface $formatter) | ||
{ | ||
$grumPHP->getTaskConfiguration('deptrac')->willReturn([]); | ||
$this->beConstructedWith($grumPHP, $processBuilder, $formatter); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(Deptrac::class); | ||
} | ||
|
||
function it_should_have_a_name() | ||
{ | ||
$this->getName()->shouldBe('deptrac'); | ||
} | ||
|
||
function it_should_have_configurable_options() | ||
{ | ||
$options = $this->getConfigurableOptions(); | ||
$options->shouldBeAnInstanceOf(OptionsResolver::class); | ||
$options->getDefinedOptions()->shouldContain('depfile'); | ||
$options->getDefinedOptions()->shouldContain('formatter_graphviz'); | ||
$options->getDefinedOptions()->shouldContain('formatter_graphviz_display'); | ||
$options->getDefinedOptions()->shouldContain('formatter_graphviz_dump_image'); | ||
$options->getDefinedOptions()->shouldContain('formatter_graphviz_dump_dot'); | ||
$options->getDefinedOptions()->shouldContain('formatter_graphviz_dump_html'); | ||
} | ||
|
||
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('deptrac')->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('deptrac')->willReturn($arguments); | ||
$processBuilder->buildProcess($arguments)->willReturn($process); | ||
|
||
$process->run()->shouldBeCalled(); | ||
$process->isSuccessful()->willReturn(true); | ||
|
||
$context->getFiles()->willReturn(new FilesCollection([ | ||
new SplFileInfo('test1.php', '.', 'test2.php') | ||
])); | ||
|
||
$result = $this->run($context); | ||
$result->shouldBeAnInstanceOf(TaskResultInterface::class); | ||
$result->isPassed()->shouldBe(true); | ||
} | ||
|
||
function it_throws_an_exception_if_the_process_fails( | ||
ProcessBuilder $processBuilder, | ||
Process $process, | ||
ContextInterface $context | ||
) { | ||
$arguments = new ProcessArgumentsCollection(); | ||
$processBuilder->createArgumentsForCommand('deptrac')->willReturn($arguments); | ||
$processBuilder->buildProcess($arguments)->willReturn($process); | ||
|
||
$process->run()->shouldBeCalled(); | ||
$process->isSuccessful()->willReturn(false); | ||
|
||
$context->getFiles()->willReturn(new FilesCollection([ | ||
new SplFileInfo('test1.php', '.', 'test2.php') | ||
])); | ||
|
||
$result = $this->run($context); | ||
$result->shouldBeAnInstanceOf(TaskResultInterface::class); | ||
$result->isPassed()->shouldBe(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace GrumPHP\Task; | ||
|
||
use GrumPHP\Runner\TaskResult; | ||
use GrumPHP\Runner\TaskResultInterface; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Deptrac task | ||
*/ | ||
class Deptrac extends AbstractExternalTask | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'deptrac'; | ||
} | ||
|
||
/** | ||
* @return OptionsResolver | ||
*/ | ||
public function getConfigurableOptions() | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'depfile' => null, | ||
'formatter_graphviz' => false, | ||
'formatter_graphviz_display' => false, | ||
'formatter_graphviz_dump_image' => null, | ||
'formatter_graphviz_dump_dot' => null, | ||
'formatter_graphviz_dump_html' => null, | ||
]); | ||
|
||
$resolver->addAllowedTypes('depfile', ['null', 'string']); | ||
$resolver->addAllowedTypes('formatter_graphviz', ['bool']); | ||
$resolver->addAllowedTypes('formatter_graphviz_display', ['bool']); | ||
$resolver->addAllowedTypes('formatter_graphviz_dump_image', ['null', 'string']); | ||
$resolver->addAllowedTypes('formatter_graphviz_dump_dot', ['null', 'string']); | ||
$resolver->addAllowedTypes('formatter_graphviz_dump_html', ['null', 'string']); | ||
|
||
return $resolver; | ||
} | ||
|
||
/** | ||
* This methods specifies if a task can run in a specific context. | ||
* | ||
* @param ContextInterface $context | ||
* | ||
* @return bool | ||
*/ | ||
public function canRunInContext(ContextInterface $context) | ||
{ | ||
return ($context instanceof GitPreCommitContext || $context instanceof RunContext); | ||
} | ||
|
||
/** | ||
* @param ContextInterface $context | ||
* | ||
* @return TaskResultInterface | ||
*/ | ||
public function run(ContextInterface $context) | ||
{ | ||
$config = $this->getConfiguration(); | ||
|
||
$files = $context->getFiles()->name('*.php'); | ||
if (0 === count($files)) { | ||
return TaskResult::createSkipped($this, $context); | ||
} | ||
|
||
$arguments = $this->processBuilder->createArgumentsForCommand('deptrac'); | ||
$arguments->add('analyze'); | ||
$arguments->add('--formatter-graphviz=' . (int)$config['formatter_graphviz']); | ||
$arguments->addOptionalArgument('--formatter-graphviz-display=%s', $config['formatter_graphviz_display']); | ||
$arguments->addOptionalArgument('--formatter-graphviz-dump-image=%s', $config['formatter_graphviz_dump_image']); | ||
$arguments->addOptionalArgument('--formatter-graphviz-dump-dot=%s', $config['formatter_graphviz_dump_dot']); | ||
$arguments->addOptionalArgument('--formatter-graphviz-dump-html=%s', $config['formatter_graphviz_dump_html']); | ||
$arguments->addOptionalArgument('%s', $config['depfile']); | ||
|
||
$process = $this->processBuilder->buildProcess($arguments); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
return TaskResult::createFailed($this, $context, $this->formatter->format($process)); | ||
} | ||
|
||
return TaskResult::createPassed($this, $context); | ||
} | ||
} |