Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| BuildTask | |
100.00% |
28 / 28 |
|
100.00% |
9 / 9 |
13 | |
100.00% |
1 / 1 |
| handle | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| run | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
| printStartMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| printFinishMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| write | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| writeln | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| skip | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createdSiteFile | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| withExecutionTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
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->write("<bg=yellow>Skipped</>\n"); |
| 54 | $this->write("<fg=gray> > {$exception->getMessage()}</>"); |
| 55 | } else { |
| 56 | $this->write("<error>Failed</error>\n"); |
| 57 | $this->write("<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 | } |