Skip to content

Commit 3bf681f

Browse files
committed
Merge pull request #39 from aequasi/finder
This PR was merged into bldr-io:master branch. Discussion ---------- Migrating to using Symfony Finder Component |Q |A | |--- |---| |Bug Fix? |n | |New Feature? |y | |BC Breaks? |y | |Deprecations?|n | |Tests Pass? |n | |Fixed Tickets| | |License |MIT| |Doc PR | | Commits ------- 7faaca8 Switching to using finder aequasi 486d70b Updating box aequasi 9a74594 Fixing spacing aequasi 343cff8 Removing useless functions aequasi 9e96b7c Adding use statement aequasi 86f0efd Adding use statement aequasi
2 parents 1c1f92b + 86f0efd commit 3bf681f

File tree

7 files changed

+149
-87
lines changed

7 files changed

+149
-87
lines changed

.bldr.yml.dist

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ bldr:
1313
watch:
1414
description: "Watch Profile"
1515
tasks:
16-
- test
1716
- watch
1817
server:
1918
description: Starts a server and waits for a bit. Then kills it.
@@ -93,8 +92,9 @@ bldr:
9392
calls:
9493
-
9594
type: watch
96-
files: [src/**.php, tests/**.php]
97-
task: test
95+
profile: default
96+
src:
97+
- { path: [src, tests], files: *.php, recursive: true }
9898
prepare:
9999
description: 'Cleans up old builds and prepares the new one'
100100
calls:
@@ -126,7 +126,9 @@ bldr:
126126
-
127127
type: apply
128128
failOnError: true
129-
fileset: [src/*.php, src/**/*.php]
129+
src:
130+
- { path: [src, tests], files: *.php, recursive: true }
131+
output: /dev/null
130132
executable: php
131133
arguments: [-l]
132134

@@ -162,3 +164,10 @@ bldr:
162164
arguments:
163165
- bin/phpunit
164166
- --coverage-text=php://stdout
167+
build:
168+
description: Builds the Box
169+
calls:
170+
-
171+
type: exec
172+
executable: bin/box
173+
arguments: [build]

bin/bldr

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,26 @@ ini_set('display_errors', 1);
1414
* with this source code in the file LICENSE.
1515
*/
1616

17-
function glob_r($pattern, $flags = 0)
18-
{
19-
$files = glob($pattern, $flags);
17+
$autoloaders = [
18+
getcwd().'/vendor/autoload.php',
19+
__DIR__.'/../vendor/autoload.php',
20+
__DIR__.'/../../../autoload.php'
21+
];
2022

21-
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
22-
$files = array_merge($files, glob_r($dir . '/' . basename($pattern), $flags));
23-
}
24-
return $files;
25-
}
26-
27-
function glob_recursive($pattern, $flags = 0)
28-
{
29-
if (strpos($pattern, '**') !== false) {
30-
return glob_r($pattern, $flags);
31-
}
32-
33-
return glob($pattern, $flags);
34-
}
35-
36-
if (__DIR__ !== getcwd()) {
37-
$paths[] = getcwd() . '/vendor';
38-
}
39-
40-
$paths[] = __DIR__ . '/../vendor';
41-
$paths[] = __DIR__ . '/../../..';
42-
43-
$autoloader = false;
4423
$loaded = [];
45-
foreach ($paths as $path) {
46-
$filePath = $path . '/autoload.php';
47-
if (file_exists($filePath)) {
48-
@$fileHash = sha1_file($filePath);
49-
if(!in_array($fileHash, $loaded)) {
50-
$autoloader = true;
51-
require $filePath;
24+
foreach ($autoloaders as $autoloader) {
25+
if (file_exists($autoloader)) {
26+
@$fileHash = sha1_file($autoloader);
27+
echo $fileHash."\n";
28+
if (!in_array($fileHash, $loaded)) {
29+
require $autoloader;
5230
$loaded[] = $fileHash;
5331
}
5432
}
5533
}
5634

57-
if (false === $autoloader) {
58-
echo "Cannot find an autoload.php file, have you executed composer install command?\n";
35+
if (!sizeof($loaded)) {
36+
echo "Cannot find an autoloader file, have you executed the composer install command?\n";
5937
exit(1);
6038
}
6139

bldr.phar

1.18 KB
Binary file not shown.

docs/extensions.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ This extension lets you run ``exec`` and ``apply`` tasks.
3838
-
3939
task: apply
4040
executeable: php
41-
fileset: [src/*.php, src/*/*.php]
41+
output: /dev/null
42+
src:
43+
- { path: [src, tests], files: *.php, recursive: true } # Checks src and tests directories for *.php files recursively
4244
arguments: [-l]
4345
4446
`Filesystem Extension`_ (Official)
@@ -110,13 +112,17 @@ This extension lets you run the ``watch`` commands. It will let you watch the fi
110112
calls:
111113
-
112114
task: watch
113-
files: [src/*.php, src/**/*.php]
115+
src:
116+
- { path: [src, tests], files: *.php, recursive: true } # Checks src and tests directories for *.php files recursively
117+
- { path: vendor/, files: [*.php, *.yml], recursive: true } # Checks vendor/ directory for *.php and *.yml files recursively
114118
profile: someProfile
115119
sample2:
116120
calls:
117121
-
118122
task: watch
119-
files: [src/*.php, src/**/*.php]
123+
src:
124+
- { path: [src, tests], files: *.php, recursive: true } # Checks src and tests directories for *.php files recursively
125+
- { files: *.yml } # Checks current directory, non-recursively
120126
task: someTask
121127
122128
@@ -138,8 +144,8 @@ This extension lets you run symfony console commands quicker. Needs work... I wa
138144
- cache:clear
139145
140146
141-
.. _Execute Extension: https://github.com/bldr-io/bldr/tree/master/src/Extension
142-
.. _Filesystem Extension: https://github.com/bldr-io/bldr/tree/master/src/Filesystem
143-
.. _Notify Extension: https://github.com/bldr-io/bldr/tree/master/src/Notify
144-
.. _Watch Extension: https://github.com/bldr-io/bldr/tree/master/src/Watch
147+
.. _Execute Extension: https://github.com/bldr-io/bldr/tree/master/src/Extension/Execute
148+
.. _Filesystem Extension: https://github.com/bldr-io/bldr/tree/master/src/Extension/Filesystem
149+
.. _Notify Extension: https://github.com/bldr-io/bldr/tree/master/src/Extension/Notify
150+
.. _Watch Extension: https://github.com/bldr-io/bldr/tree/master/src/Extension/Watch
145151
.. _Symfony Extension: https://www.github.com/bldr-io/bldr-symfony/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* This file is part of bldr
5+
*
6+
* (c) Aaron Scherer <aequasi@gmail.com>
7+
*
8+
* This source file is subject to the license that is bundled
9+
* with this source code in the file LICENSE
10+
*/
11+
12+
namespace Bldr\Call\Traits;
13+
14+
use Symfony\Component\Finder\Finder;
15+
use \Symfony\Component\Finder\SplFileInfo;
16+
17+
/**
18+
* @author Aaron Scherer <aequasi@gmail.com>
19+
*/
20+
trait FinderAwareTrait
21+
{
22+
/**
23+
* Finds all the files for the given config
24+
*
25+
* @param array $source
26+
*
27+
* @return string[]
28+
*
29+
* @throws \Exception
30+
*/
31+
protected function getFiles(array $source)
32+
{
33+
$fileSet = [];
34+
foreach ($source as $set) {
35+
if (!array_key_exists('files', $set)) {
36+
throw new \Exception("`src` must have a `files` option");
37+
}
38+
39+
if (!array_key_exists('path', $set)) {
40+
$set['path'] = getcwd();
41+
}
42+
43+
if (!array_key_exists('recursive', $set)) {
44+
$set['recursive'] = false;
45+
}
46+
47+
$paths = is_array($set['path']) ? $set['path'] : [$set['path']];
48+
$files = is_array($set['files']) ? $set['files'] : [$set['files']];
49+
foreach ($paths as $path) {
50+
foreach ($files as $file) {
51+
$finder = new Finder();
52+
$finder->files()->in($path)->name($file);
53+
if (!$set['recursive']) {
54+
$finder->depth('== 0');
55+
}
56+
57+
$fileSet = $this->appendFileSet($finder, $fileSet);
58+
}
59+
}
60+
}
61+
62+
return $fileSet;
63+
}
64+
65+
/**
66+
* @param Finder $finder
67+
* @param array $fileSet
68+
*
69+
* @return array
70+
*/
71+
protected function appendFileSet(Finder $finder, array $fileSet)
72+
{
73+
foreach ($finder as $file) {
74+
/** @var SplFileInfo $file */
75+
$fileSet[] = $file->getRealPath();
76+
}
77+
78+
return $fileSet;
79+
}
80+
}

src/Extension/Execute/Call/ApplyCall.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,48 @@
1111

1212
namespace Bldr\Extension\Execute\Call;
1313

14+
use Bldr\Call\Traits\FinderAwareTrait;
1415
use Symfony\Component\Console\Helper\FormatterHelper;
1516

1617
/**
1718
* @author Aaron Scherer <aequasi@gmail.com>
1819
*/
1920
class ApplyCall extends ExecuteCall
2021
{
22+
use FinderAwareTrait;
23+
2124
/**
2225
* @var array $files
2326
*/
2427
private $files;
2528

29+
/**
30+
* {@inheritDoc}
31+
*/
2632
public function configure()
2733
{
2834
parent::configure();
2935
$this->setName('apply')
30-
->addOption('fileset', true, 'The fileset to run the executable on');
36+
->addOption('src', true, 'Source to run the apply on');
3137
}
3238

3339
/**
3440
* {@inheritDoc}
3541
*/
3642
public function run()
3743
{
38-
$this->setFileset($this->getOption('fileset'));
39-
4044
/** @var FormatterHelper $formatter */
4145
$formatter = $this->getHelperSet()->get('formatter');
4246

4347
$this->getOutput()->writeln($formatter->formatSection($this->getTask()->getName(), 'Starting'));
4448

4549
$arguments = $this->getOption('arguments');
46-
foreach ($this->files as $file) {
50+
$files = $this->getFiles($this->getOption('src'));
51+
foreach ($files as $file) {
4752
$args = $arguments;
4853
$args[] = $file;
4954
$this->setOption('arguments', $args);
5055
parent::run();
5156
}
5257
}
53-
54-
/**
55-
* @param string $fileset
56-
*/
57-
public function setFileset($fileset)
58-
{
59-
$fileOption = $this->getOption('fileset');
60-
if (is_array($fileOption)) {
61-
$files = [];
62-
foreach ($fileOption as $file) {
63-
$files = array_merge($files, glob_recursive(getcwd() . '/' . $file));
64-
}
65-
} else {
66-
$files = glob_recursive(getcwd() . '/' . $fileOption);
67-
}
68-
69-
$this->files = $files;
70-
}
7158
}

src/Extension/Watch/Call/WatchCall.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
namespace Bldr\Extension\Watch\Call;
1313

1414
use Bldr\Call\AbstractCall;
15+
use Bldr\Call\Traits\FinderAwareTrait;
1516
use Bldr\Model\Task;
1617
use Bldr\Registry\TaskRegistry;
18+
use Symfony\Component\Finder\Finder;
19+
use Symfony\Component\Finder\SplFileInfo;
1720

1821
/**
1922
* @author Aaron Scherer <aaron@undergroundelephant.com>
2023
*/
2124
class WatchCall extends AbstractCall
2225
{
26+
use FinderAwareTrait;
27+
2328
/**
2429
* @var TaskRegistry $tasks
2530
*/
@@ -43,7 +48,7 @@ public function configure()
4348
{
4449
$this->setName('watch')
4550
->setDescription('Watches the filesystem for changes')
46-
->addOption('files', true, 'Files to watch')
51+
->addOption('src', true, 'Source to watch')
4752
->addOption('profile', false, 'Profile to run on filesystem change')
4853
->addOption('task', false, 'Task to run on filesystem change');
4954
}
@@ -61,21 +66,16 @@ public function run()
6166
throw new \Exception("Watch must have either a task, or a profile");
6267
}
6368

64-
$fileOption = $this->getOption('files');
65-
if (is_array($fileOption)) {
66-
$files = [];
67-
foreach ($fileOption as $file) {
68-
$files = array_merge($files, glob_recursive(getcwd() . '/' . $file));
69-
}
70-
} else {
71-
$files = glob_recursive(getcwd() . '/' . $fileOption);
69+
$source = $this->getOption('src');
70+
if (!is_array($source)) {
71+
throw new \Exception("`src` must be an array");
7272
}
7373

74-
$this->watchForChanges($files);
74+
$this->watchForChanges($this->getFiles($source));
7575
}
7676

7777
/**
78-
* @param string[] $files
78+
* @param SplFileInfo[] $files
7979
*/
8080
private function watchForChanges(array $files)
8181
{
@@ -84,11 +84,15 @@ private function watchForChanges(array $files)
8484

8585
$previously = [];
8686
while (true) {
87-
foreach ($files as $name) {
88-
if ($this->checkFile($name, $previously)) {
87+
foreach ($files as $file) {
88+
/** @var SplFileInfo $file */
89+
if ($this->checkFile($file->getRealPath(), $previously)) {
8990
$this->getOutput()
9091
->writeln(
91-
sprintf("<info>>>>></info> <comment>The %s file changed.</comment>", $name)
92+
sprintf(
93+
"<info>>>>></info> <comment>The following file changed:</comment> <info>%s</info>",
94+
$file->getPathname()
95+
)
9296
);
9397

9498
$this->getTasks();
@@ -147,12 +151,10 @@ public function fetchTasks($profileName)
147151
public function buildTasks($names)
148152
{
149153
foreach ($names as $name) {
150-
$taskInfo = $this->config['tasks'][$name];
151-
$taskInfo['failOnError'] = false;
152-
$description = isset($taskInfo['description']) ? $taskInfo['description'] : "";
154+
$taskInfo = $this->config['tasks'][$name];
155+
$description = isset($taskInfo['description']) ? $taskInfo['description'] : "";
153156

154-
$task = new Task($name, $description, $taskInfo['calls']);
155-
$this->tasks->addTask($task);
157+
$this->tasks->addTask(new Task($name, $description, false, $taskInfo['calls']));
156158
}
157159
}
158160
}

0 commit comments

Comments
 (0)