Skip to content

Commit 119adc0

Browse files
committed
Merge pull request #34 from aequasi/miscExtension
[WIP] Adding MiscellaneousExtension
2 parents b1d0770 + f21bc70 commit 119adc0

File tree

6 files changed

+208
-3
lines changed

6 files changed

+208
-3
lines changed

.bldr.yml.dist

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,37 @@ bldr:
2323
description: Tests the MysqlExtension calls
2424
tasks:
2525
- mysql
26+
misc:
27+
description: Tests the MiscellaneousExtension calls
28+
tasks:
29+
- misc
2630

2731
tasks:
32+
misc:
33+
calls:
34+
-
35+
type: notify
36+
message: "Sleeping for 1 Second"
37+
-
38+
type: sleep
39+
seconds: 1
40+
-
41+
type: notify
42+
message: "Done sleeping for 1 Second"
43+
-
44+
type: notify
45+
message: "Stopping and Starting redis service"
46+
-
47+
type: service
48+
manager: launchctl # Using OS X
49+
service: homebrew.mxcl.redis
50+
method: stop
51+
-
52+
type: service
53+
manager: launchctl # Using OS X
54+
service: homebrew.mxcl.redis
55+
method: start
56+
2857
mysql:
2958
calls:
3059
-
@@ -69,6 +98,9 @@ bldr:
6998
prepare:
7099
description: 'Cleans up old builds and prepares the new one'
71100
calls:
101+
-
102+
type: sleep
103+
seconds: 5
72104
-
73105
type: filesystem:remove
74106
files: [build/coverage, build/logs]

src/DependencyInjection/ContainerBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class ContainerBuilder extends BaseContainerBuilder
2828
{
2929
/**
30-
* @param ParameterBagInterface $parameterBag
30+
* @param ParameterBagInterface|null $parameterBag
3131
*/
3232
public function __construct(ParameterBagInterface $parameterBag = null)
3333
{
@@ -65,7 +65,8 @@ private function getCoreExtensions()
6565
new Extension\Notify\DependencyInjection\NotifyExtension(),
6666
new Extension\Watch\DependencyInjection\WatchExtension(),
6767
new Extension\Database\DependencyInjection\DatabaseExtension(),
68-
new Extension\Database\DependencyInjection\MysqlExtension()
68+
new Extension\Database\DependencyInjection\MysqlExtension(),
69+
new Extension\Miscellaneous\DependencyInjection\MiscellaneousExtension(),
6970
];
7071

7172
return $extensions;

src/Extension/Execute/Call/ExecuteCall.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function run()
6565
);
6666
}
6767

68-
if ($this->getOutput()->isVerbose()) {
68+
if ($this->getOutput()->isVerbose() || $this->getOption('dry_run')) {
6969
$this->getOutput()->writeln($process->getCommandLine());
7070
}
7171

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Extension\Miscellaneous\Call;
13+
14+
use Bldr\Extension\Execute\Call\ExecuteCall;
15+
16+
/**
17+
* @author Aaron Scherer <aequasi@gmail.com>
18+
*/
19+
class ServiceCall extends ExecuteCall
20+
{
21+
private static $MANAGERS = ['service', 'init.d', 'launchctl'];
22+
23+
/**
24+
* {@inheritDoc}
25+
*/
26+
public function configure()
27+
{
28+
$this->setName('service')
29+
->setDescription('Used to stop/start/restart services')
30+
->addOption(
31+
'manager',
32+
true,
33+
sprintf(
34+
'Method used to manage service: %s',
35+
implode(', ', static::$MANAGERS)
36+
),
37+
'service'
38+
)
39+
->addOption('service', true, 'Service to manage')
40+
->addOption('method', true, 'Method to run on the service manager. <restart>', 'restart')
41+
->addOption('sudo', true, 'Run as sudo?', false)
42+
->addOption('dry_run', true, 'If set, will not run command', false);
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
* @throws \Exception
48+
*/
49+
public function run()
50+
{
51+
$this->addOption('executable', true)
52+
->addOption('arguments', true);
53+
54+
$arguments = [];
55+
$sudo = $this->getOption('sudo');
56+
$manager = $this->getOption('manager');
57+
switch ($manager) {
58+
case 'service':
59+
$executable = $manager;
60+
if ($sudo) {
61+
$executable = 'sudo';
62+
$arguments[] = $manager;
63+
}
64+
$arguments[] = $this->getOption('service');
65+
$arguments[] = $this->getOption('method');
66+
break;
67+
case 'init.d':
68+
$executable = '/etc/init.d/'.$this->getOption('service');
69+
if ($sudo) {
70+
$executable = 'sudo';
71+
$arguments[] = $manager;
72+
}
73+
$arguments[] = $this->getOption('method');
74+
break;
75+
case 'launchctl':
76+
$executable = $manager;
77+
if ($sudo) {
78+
$executable = 'sudo';
79+
$arguments[] = $manager;
80+
}
81+
$arguments[] = $this->getOption('method');
82+
$arguments[] = $this->getOption('service');
83+
break;
84+
default:
85+
throw new \Exception(
86+
$manager.' is not a valid manager for services. Feel free to '.
87+
'create a pull request, if you think it should be.'
88+
);
89+
}
90+
91+
$this->setOption('executable', $executable);
92+
$this->setOption('arguments', $arguments);
93+
94+
return parent::run();
95+
}
96+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Extension\Miscellaneous\Call;
13+
14+
use Bldr\Call\AbstractCall;
15+
16+
/**
17+
* @author Aaron Scherer <aequasi@gmail.com>
18+
*/
19+
class SleepCall extends AbstractCall
20+
{
21+
/**
22+
* {@inheritDoc}
23+
*/
24+
public function configure()
25+
{
26+
$this->setName('sleep')
27+
->setDescription('Sleep for the given amount of time')
28+
->addOption('seconds', true, 'Milliseconds to sleep for.');
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
public function run()
35+
{
36+
sleep($this->getOption('seconds'));
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Extension\Miscellaneous\DependencyInjection;
13+
14+
use Bldr\DependencyInjection\AbstractExtension;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
18+
/**
19+
* @author Aaron Scherer <aequasi@gmail.com>
20+
*/
21+
class MiscellaneousExtension extends AbstractExtension
22+
{
23+
/**
24+
* {@inheritDoc}
25+
*/
26+
public function load(array $config, ContainerBuilder $container)
27+
{
28+
$services = [
29+
'bldr_miscellaneous.sleep' => 'Bldr\Extension\Miscellaneous\Call\SleepCall',
30+
'bldr_miscellaneous.service' => 'Bldr\Extension\Miscellaneous\Call\ServiceCall',
31+
];
32+
33+
foreach ($services as $name => $class) {
34+
$container->setDefinition($name, new Definition($class))
35+
->addTag('bldr');
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)