Skip to content

Commit 7383934

Browse files
Add label functionality
You can provide actions with labels to enhance the output. Suggested in issue #212
1 parent cc2c80b commit 7383934

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

src/Config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Config
3131
public const SETTING_GIT_DIR = 'git-directory';
3232
public const SETTING_INCLUDES = 'includes';
3333
public const SETTING_INCLUDES_LEVEL = 'includes-level';
34+
public const SETTING_LABEL = 'label';
3435
public const SETTING_RUN_EXEC = 'run-exec';
3536
public const SETTING_RUN_MODE = 'run-mode';
3637
public const SETTING_RUN_PATH = 'run-path';

src/Config/Action.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,37 @@ class Action
2828
*
2929
* @var string
3030
*/
31-
private $action;
31+
private string $action;
3232

3333
/**
3434
* Map of options name => value
3535
*
3636
* @var \CaptainHook\App\Config\Options
3737
*/
38-
private $options;
38+
private Options $options;
3939

4040
/**
4141
* List of action conditions
4242
*
4343
* @var \CaptainHook\App\Config\Condition[]
4444
*/
45-
private $conditions = [];
45+
private array $conditions = [];
4646

4747
/**
4848
* Action settings
4949
*
5050
* @var array<string, mixed>
5151
*/
52-
private $settings = [];
52+
private array $settings = [];
5353

5454
/**
5555
* List of available settings
5656
*
5757
* @var string[]
5858
*/
59-
private static $availableSettings = [
60-
Config::SETTING_ALLOW_FAILURE
59+
private static array $availableSettings = [
60+
Config::SETTING_ALLOW_FAILURE,
61+
Config::SETTING_LABEL
6162
];
6263

6364
/**
@@ -108,7 +109,7 @@ private function setupSettings(array $settings): void
108109
{
109110
foreach (self::$availableSettings as $setting) {
110111
if (isset($settings[$setting])) {
111-
$this->settings[Config::SETTING_ALLOW_FAILURE] = $settings[$setting];
112+
$this->settings[$setting] = $settings[$setting];
112113
}
113114
}
114115
}
@@ -124,6 +125,16 @@ public function isFailureAllowed(bool $default = false): bool
124125
return (bool) ($this->settings[Config::SETTING_ALLOW_FAILURE] ?? $default);
125126
}
126127

128+
/**
129+
* Return the label or the action if no label is set
130+
*
131+
* @return string
132+
*/
133+
public function getLabel(): string
134+
{
135+
return (string) ($this->settings[Config::SETTING_LABEL] ?? $this->getAction());
136+
}
137+
127138
/**
128139
* Action getter
129140
*

src/Runner/Hook.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,12 @@ private function executeFailAfterAllActions(array $actions): void
304304
private function handleAction(Config\Action $action): void
305305
{
306306
if ($this->shouldSkipActions()) {
307-
$this->io->write(
308-
$this->formatActionOutput($action->getAction()) . ': <comment>deactivated</comment>',
309-
true
310-
);
307+
$this->io->write($this->formatActionOutput($action->getLabel()) . ': <comment>deactivated</comment>');
311308
return;
312309
}
313310

314311
$this->io->write(
315-
' - <fg=blue>' . $this->formatActionOutput($action->getAction()) . '</> : ',
312+
' - <fg=blue>' . $this->formatActionOutput($action->getLabel()) . '</> : ',
316313
$this->io->isVerbose()
317314
);
318315

tests/unit/Config/ActionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ public function testGetAction(): void
6262
$this->assertEquals('\\Foo\\Bar', $action->getAction());
6363
}
6464

65+
/**
66+
* Tests Action::getLabel
67+
*/
68+
public function testGetLabel(): void
69+
{
70+
$action = new Action('\\Foo\\Bar', [], [], ['label' => 'My label']);
71+
72+
$this->assertEquals('My label', $action->getLabel());
73+
}
74+
75+
/**
76+
* Tests Action::getLabel
77+
*/
78+
public function testGetLabelEmpty(): void
79+
{
80+
$action = new Action('\\Foo\\Bar');
81+
82+
$this->assertEquals('\\Foo\\Bar', $action->getLabel());
83+
}
84+
6585
/**
6686
* Tests Action::getOptions
6787
*/

0 commit comments

Comments
 (0)