Passed
Push — master ( 87c3e1...f9d113 )
by Caen
03:37 queued 13s
created

BuildTask::writeln()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\BuildTasks;
6
7
use Throwable;
8
use Hyde\Hyde;
9
use Illuminate\Console\OutputStyle;
10
use Symfony\Component\Console\Command\Command;
11
use Hyde\Framework\Concerns\TracksExecutionTime;
12
use Illuminate\Console\Concerns\InteractsWithIO;
13
14
use function str_replace;
15
use function sprintf;
16
17
abstract class BuildTask
18
{
19
    use InteractsWithIO;
20
    use TracksExecutionTime;
21
22
    /** @var string The message that will be displayed when the task is run. */
23
    protected static string $message = 'Running generic build task';
24
25
    protected int $exitCode = Command::SUCCESS;
26
27
    /** @var \Illuminate\Console\OutputStyle|null */
28
    protected $output;
29
30
    abstract public function handle(): void;
31
32
    /**
33
     * This method is called by the BuildTaskService. It will run the task using the handle method,
34
     * as well as write output to the console, and handle any exceptions that may occur.
35
     *
36
     * @return int The exit code of the task. This can be used when calling a task directly from a command.
37
     */
38
    public function run(?OutputStyle $output = null): int
39
    {
40
        $this->startClock();
41
42
        if ($output && ! $this->output) {
43
            $this->setOutput($output);
44
        }
45
46
        $this->printStartMessage();
47
48
        try {
49
            $this->handle();
50
            $this->printFinishMessage();
51
        } catch (Throwable $exception) {
52
            if ($exception instanceof BuildTaskSkippedException) {
53
                $this->writeln('<bg=yellow>Skipped</>');
54
                $this->writeln("<fg=gray> > {$exception->getMessage()}</>");
55
            } else {
56
                $this->writeln('<error>Failed</error>');
57
                $this->writeln("<error>{$exception->getMessage()}</error>");
58
            }
59
60
            $this->exitCode = $exception->getCode();
61
        }
62
63
        $this->write("\n");
64
65
        return $this->exitCode;
66
    }
67
68
    public function printStartMessage(): void
69
    {
70
        $this->write("<comment>{$this->getMessage()}...</comment> ");
71
    }
72
73
    public function printFinishMessage(): void
74
    {
75
        $this->writeln('<fg=gray>Done in '.$this->getExecutionTimeString().'</>');
76
    }
77
78
    public function getMessage(): string
79
    {
80
        return static::$message;
81
    }
82
83
    public function write(string $message): void
84
    {
85
        $this->output?->write($message);
86
    }
87
88
    public function writeln(string $message): void
89
    {
90
        $this->output?->writeln($message);
91
    }
92
93
    /**
94
     * Write a fluent message to the output that the task is skipping and halt the execution.
95
     *
96
     * @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException
97
     */
98
    public function skip(string $reason = 'Task was skipped'): void
99
    {
100
        throw new BuildTaskSkippedException($reason);
101
    }
102
103
    /** Write a fluent message to the output that the task created the specified file. */
104
    public function createdSiteFile(string $path): static
105
    {
106
        $this->write(sprintf(
107
            "\n > Created <info>%s</info>",
108
            str_replace('\\', '/', Hyde::pathToRelative($path))
109
        ));
110
111
        return $this;
112
    }
113
114
    /** Write a fluent message to the output with the execution time of the task. */
115
    public function withExecutionTime(): static
116
    {
117
        $this->write(" in {$this->getExecutionTimeString()}");
118
119
        return $this;
120
    }
121
}
122