Skip to content

Commit 17478df

Browse files
Added registerTask method and added names and descriptions to media tasks
Signed-off-by: Bastian Schwarz <bastian@codename-php.de>
1 parent 2f00814 commit 17478df

10 files changed

Lines changed: 161 additions & 26 deletions

File tree

src/functions/All.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
use Closure;
2121
use de\codenamephp\deployer\base\MissingConfigurationException;
22+
use de\codenamephp\deployer\base\task\iTaskWithDescription;
23+
use de\codenamephp\deployer\base\task\iTaskWithName;
2224
use Deployer\Deployer;
2325
use Deployer\Host\Host;
2426
use Deployer\Host\Localhost;
@@ -153,6 +155,11 @@ public function task(string $name, callable|array|\de\codenamephp\deployer\base\
153155
return \Deployer\task($name, $body);
154156
}
155157

158+
public function registerTask(iTaskWithName $task) : void {
159+
$registeredTask = $this->task($task->getName(), $task);
160+
if($task instanceof iTaskWithDescription) $registeredTask->desc($task->getDescription());
161+
}
162+
156163
public function upload(string $source, string $destination, array $config = []) : void {
157164
upload($source, $destination, $config);
158165
}

src/functions/iTask.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace de\codenamephp\deployer\base\functions;
1919

20+
use de\codenamephp\deployer\base\task\iTaskWithName;
2021
use Deployer\Task\Task;
2122

2223
/**
@@ -33,4 +34,13 @@ interface iTask {
3334
* @param \de\codenamephp\deployer\base\task\iTask|callable|array|null $body Callable task, array of other tasks names or nothing to get a defined tasks
3435
*/
3536
public function task(string $name, \de\codenamephp\deployer\base\task\iTask|callable|array|null $body = null) : Task;
37+
38+
/**
39+
* Registers a task with deployer. The task itself is used as body and the name is the identifier that the task is registered with. Implementations should
40+
* support the iTaskWithDescription interface and add the description as well if the task has the interface.
41+
*
42+
* @param iTaskWithName $task The task to register with deployer
43+
* @return void
44+
*/
45+
public function registerTask(iTaskWithName $task) : void;
3646
}

src/task/iTaskWithDescription.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\base\task;
19+
20+
/**
21+
* Interface to automatically add a description for the task when used with our custom registerTask() method. This is what you would add with the desc() method
22+
*/
23+
interface iTaskWithDescription extends iTask {
24+
25+
/**
26+
* Gets the description of the task that will be shown in the task list. This is what you would pass to the desc() function.
27+
*
28+
* @return string
29+
*/
30+
public function getDescription() : string;
31+
}

src/task/iTaskWithName.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\base\task;
19+
20+
/**
21+
* Interface that is needed when our custom registerTask() method is used. This is the name the task is registered with to deployer.
22+
*/
23+
interface iTaskWithName extends iTask {
24+
25+
/**
26+
* The name the task is registered with. This is what you would pass as the first parameter to task()
27+
*
28+
* @return string
29+
*/
30+
public function getName() : string;
31+
}

src/task/media/Copy.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
use de\codenamephp\deployer\base\MissingConfigurationException;
2727
use de\codenamephp\deployer\base\ssh\client\iClient;
2828
use de\codenamephp\deployer\base\ssh\client\StaticProxy;
29-
use de\codenamephp\deployer\base\task\iTask;
29+
use de\codenamephp\deployer\base\task\iTaskWithDescription;
30+
use de\codenamephp\deployer\base\task\iTaskWithName;
3031
use de\codenamephp\deployer\base\transferable\iTransferable;
3132
use de\codenamephp\deployer\base\UnsafeOperationException;
3233
use Deployer\Exception\RunException;
@@ -37,9 +38,10 @@
3738
*
3839
* @psalm-suppress PropertyNotSetInConstructor see https://github.com/vimeo/psalm/issues/4393
3940
*/
40-
final class Copy implements iTask {
41+
final class Copy implements iTaskWithName, iTaskWithDescription {
4142

4243
public const OPTION_SOURCE_HOST = 'cpd:media:copy:sourceHost';
44+
public const NAME = 'media:copy';
4345

4446
/**
4547
* @var array<iTransferable>
@@ -65,16 +67,8 @@ public function __construct(array $transferables,
6567
);
6668
}
6769

68-
/**
69-
* @return iTransferable[]
70-
*/
71-
public function getTransferables() : array {
72-
return $this->transferables;
73-
}
74-
75-
public function setTransferables(iTransferable ...$transferables) : Copy {
76-
$this->transferables = $transferables;
77-
return $this;
70+
public function getDescription() : string {
71+
return 'Copies the media from one remote to another. The copy is done using rsync directly between the hosts.';
7872
}
7973

8074
/**
@@ -109,4 +103,20 @@ public function __invoke() : void {
109103
$this->deployerFunctions->runLocally(sprintf('%s "%s"', $sshCommand, sprintf($rsyncCommand, implode(' ', $transferable->getConfig()['options'] ?? []), $sourcePath, $targetPath)));
110104
}
111105
}
106+
107+
public function getName() : string {
108+
return self::NAME;
109+
}
110+
111+
/**
112+
* @return iTransferable[]
113+
*/
114+
public function getTransferables() : array {
115+
return $this->transferables;
116+
}
117+
118+
public function setTransferables(iTransferable ...$transferables) : Copy {
119+
$this->transferables = $transferables;
120+
return $this;
121+
}
112122
}

src/task/media/Pull.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
use de\codenamephp\deployer\base\functions\All;
2121
use de\codenamephp\deployer\base\functions\iDownload;
22-
use de\codenamephp\deployer\base\task\iTask;
22+
use de\codenamephp\deployer\base\task\iTaskWithDescription;
23+
use de\codenamephp\deployer\base\task\iTaskWithName;
2324
use de\codenamephp\deployer\base\transferable\iTransferable;
2425
use Deployer\Exception\RunException;
2526

@@ -28,7 +29,9 @@
2829
*
2930
* @psalm-suppress PropertyNotSetInConstructor see https://github.com/vimeo/psalm/issues/4393
3031
*/
31-
final class Pull implements iTask {
32+
final class Pull implements iTaskWithName, iTaskWithDescription {
33+
34+
public const NAME = 'media:pull';
3235

3336
/**
3437
* @var array<iTransferable>
@@ -43,16 +46,8 @@ public function __construct(array $transferables, public iDownload $deployerFunc
4346
$this->setTransferables(...$transferables);
4447
}
4548

46-
/**
47-
* @return iTransferable[]
48-
*/
49-
public function getTransferables() : array {
50-
return $this->transferables;
51-
}
52-
53-
public function setTransferables(iTransferable ...$transferables) : Pull {
54-
$this->transferables = $transferables;
55-
return $this;
49+
public function getDescription() : string {
50+
return 'Pull the media from remote to local.';
5651
}
5752

5853
/**
@@ -66,4 +61,20 @@ public function __invoke() : void {
6661
$this->deployerFunctions->download($transferable->getRemotePath(), $transferable->getLocalPath(), $transferable->getConfig());
6762
}
6863
}
64+
65+
public function getName() : string {
66+
return self::NAME;
67+
}
68+
69+
/**
70+
* @return iTransferable[]
71+
*/
72+
public function getTransferables() : array {
73+
return $this->transferables;
74+
}
75+
76+
public function setTransferables(iTransferable ...$transferables) : Pull {
77+
$this->transferables = $transferables;
78+
return $this;
79+
}
6980
}

src/task/media/Push.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
use de\codenamephp\deployer\base\hostCheck\DoNotRunOnProduction;
2323
use de\codenamephp\deployer\base\hostCheck\iHostCheck;
2424
use de\codenamephp\deployer\base\hostCheck\SkippableByOption;
25-
use de\codenamephp\deployer\base\task\iTask;
25+
use de\codenamephp\deployer\base\task\iTaskWithDescription;
26+
use de\codenamephp\deployer\base\task\iTaskWithName;
2627
use de\codenamephp\deployer\base\transferable\iTransferable;
2728
use de\codenamephp\deployer\base\UnsafeOperationException;
2829
use Deployer\Exception\RunException;
@@ -32,7 +33,9 @@
3233
*
3334
* @psalm-suppress PropertyNotSetInConstructor see https://github.com/vimeo/psalm/issues/4393
3435
*/
35-
final class Push implements iTask {
36+
final class Push implements iTaskWithName, iTaskWithDescription {
37+
38+
public const NAME = 'media:push';
3639

3740
/**
3841
* @var array<iTransferable>
@@ -62,6 +65,14 @@ public function setTransferables(iTransferable ...$transferables) : Push {
6265
return $this;
6366
}
6467

68+
public function getDescription() : string {
69+
return 'Pushes media from local to remote.';
70+
}
71+
72+
public function getName() : string {
73+
return self::NAME;
74+
}
75+
6576
/**
6677
* Iterates over all transferables and passes the localPath, the remotePath and the config to the upload function. If the stage is production an
6778
* UnsafeOperationException is thrown since we don't want to push to production.

test/task/media/CopyTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,12 @@ public function test__invoke() : void {
116116

117117
$this->sut->__invoke();
118118
}
119+
120+
public function testGetName() : void {
121+
self::assertEquals(Copy::NAME, $this->sut->getName());
122+
}
123+
124+
public function testGetDescription() : void {
125+
self::assertEquals('Copies the media from one remote to another. The copy is done using rsync directly between the hosts.', $this->sut->getDescription());
126+
}
119127
}

test/task/media/PullTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ public function test__invoke() : void {
7272

7373
$this->sut->__invoke();
7474
}
75+
76+
public function testGetName() : void {
77+
self::assertEquals(Pull::NAME, $this->sut->getName());
78+
}
79+
80+
public function testGetDescription() : void {
81+
self::assertEquals('Pull the media from remote to local.', $this->sut->getDescription());
82+
}
7583
}

test/task/media/PushTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,12 @@ public function test__invoke() : void {
7171

7272
$this->sut->__invoke();
7373
}
74+
75+
public function testGetName() : void {
76+
self::assertEquals(Push::NAME, $this->sut->getName());
77+
}
78+
79+
public function testGetDescription() : void {
80+
self::assertEquals('Pushes media from local to remote.', $this->sut->getDescription());
81+
}
7482
}

0 commit comments

Comments
 (0)