|
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 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
|
|
|
public function newLine(int $count = 1): void |
|
94
|
|
|
{ |
|
95
|
|
|
$this->output?->newLine($count); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Write a fluent message to the output that the task is skipping and halt the execution. |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function skip(string $reason = 'Task was skipped'): void |
|
104
|
|
|
{ |
|
105
|
|
|
throw new BuildTaskSkippedException($reason); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** Write a fluent message to the output that the task created the specified file. */ |
|
109
|
|
|
public function createdSiteFile(string $path): static |
|
110
|
|
|
{ |
|
111
|
|
|
$this->write(sprintf( |
|
112
|
|
|
"\n > Created <info>%s</info>", |
|
113
|
|
|
str_replace('\\', '/', Hyde::pathToRelative($path)) |
|
|
|
|
|
|
114
|
|
|
)); |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** Write a fluent message to the output with the execution time of the task. */ |
|
120
|
|
|
public function withExecutionTime(): static |
|
121
|
|
|
{ |
|
122
|
|
|
$this->write(" in {$this->getExecutionTimeString()}"); |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|