1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Hyde\Framework\Actions\PostBuildTasks\GenerateRssFeed; |
7
|
|
|
use Hyde\Framework\Actions\PostBuildTasks\GenerateSearch; |
8
|
|
|
use Hyde\Framework\Actions\PostBuildTasks\GenerateSitemap; |
9
|
|
|
use Hyde\Framework\Helpers\Features; |
10
|
|
|
use Hyde\Framework\Hyde; |
11
|
|
|
use Hyde\Framework\Services\BuildService; |
12
|
|
|
use Hyde\Framework\Services\BuildTaskService; |
13
|
|
|
use Hyde\Framework\Services\DiscoveryService; |
14
|
|
|
use Illuminate\Support\Facades\Config; |
15
|
|
|
use LaravelZero\Framework\Commands\Command; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Hyde Command to run the Build Process. |
19
|
|
|
* |
20
|
|
|
* @see \Hyde\Framework\Testing\Feature\StaticSiteServiceTest |
21
|
|
|
*/ |
22
|
|
|
class HydeBuildSiteCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The signature of the command. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'build |
30
|
|
|
{--run-dev : Run the NPM dev script after build} |
31
|
|
|
{--run-prod : Run the NPM prod script after build} |
32
|
|
|
{--run-prettier : Format the output using NPM Prettier} |
33
|
|
|
{--pretty-urls : Should links in output use pretty URLs?} |
34
|
|
|
{--no-api : Disable API calls, for example, Torchlight}'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The description of the command. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $description = 'Build the static site'; |
42
|
|
|
|
43
|
|
|
protected BuildService $service; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the console command. |
47
|
|
|
* |
48
|
|
|
* @return int |
49
|
|
|
* |
50
|
|
|
* @throws Exception |
51
|
|
|
*/ |
52
|
|
|
public function handle(): int |
53
|
|
|
{ |
54
|
|
|
$time_start = microtime(true); |
55
|
|
|
|
56
|
|
|
$this->title('Building your static site!'); |
57
|
|
|
|
58
|
|
|
$this->service = new BuildService($this->output); |
59
|
|
|
|
60
|
|
|
$this->runPreBuildActions(); |
61
|
|
|
|
62
|
|
|
$this->service->cleanOutputDirectory(); |
63
|
|
|
|
64
|
|
|
$this->service->transferMediaAssets(); |
65
|
|
|
|
66
|
|
|
$this->service->compileStaticPages(); |
67
|
|
|
|
68
|
|
|
$this->runPostBuildActions(); |
69
|
|
|
|
70
|
|
|
$this->printFinishMessage($time_start); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @internal */ |
76
|
|
|
protected function runPreBuildActions(): void |
77
|
|
|
{ |
78
|
|
|
if ($this->option('no-api')) { |
79
|
|
|
$this->info('Disabling external API calls'); |
80
|
|
|
$this->newLine(); |
81
|
|
|
$config = config('hyde.features'); |
82
|
|
|
unset($config[array_search('torchlight', $config)]); |
83
|
|
|
Config::set(['hyde.features' => $config]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->option('pretty-urls')) { |
87
|
|
|
$this->info('Generating site with pretty URLs'); |
88
|
|
|
$this->newLine(); |
89
|
|
|
Config::set(['site.pretty_urls' => true]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Run any post-build actions. |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function runPostBuildActions(): void |
99
|
|
|
{ |
100
|
|
|
$service = new BuildTaskService($this->output); |
101
|
|
|
|
102
|
|
|
if ($this->option('run-prettier')) { |
103
|
|
|
$this->runNodeCommand( |
104
|
|
|
'npx prettier '.Hyde::pathToRelative(Hyde::getSiteOutputPath()).'/**/*.html --write --bracket-same-line', |
105
|
|
|
'Prettifying code!', |
106
|
|
|
'prettify code' |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->option('run-dev')) { |
111
|
|
|
$this->runNodeCommand('npm run dev', 'Building frontend assets for development!'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->option('run-prod')) { |
115
|
|
|
$this->runNodeCommand('npm run prod', 'Building frontend assets for production!'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$service->runIf(GenerateSitemap::class, $this->canGenerateSitemap()); |
119
|
|
|
$service->runIf(GenerateRssFeed::class, $this->canGenerateFeed()); |
120
|
|
|
$service->runIf(GenerateSearch::class, $this->canGenerateSearch()); |
121
|
|
|
|
122
|
|
|
$service->runPostBuildTasks(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** @internal */ |
126
|
|
|
protected function printFinishMessage(float $time_start): void |
127
|
|
|
{ |
128
|
|
|
$execution_time = (microtime(true) - $time_start); |
129
|
|
|
$this->info(sprintf( |
130
|
|
|
'All done! Finished in %s seconds. (%sms)', |
131
|
|
|
number_format($execution_time, 2), |
132
|
|
|
number_format($execution_time * 1000, 2) |
133
|
|
|
)); |
134
|
|
|
|
135
|
|
|
$this->info('Congratulations! 🎉 Your static site has been built!'); |
136
|
|
|
$this->line( |
137
|
|
|
'Your new homepage is stored here -> '. |
138
|
|
|
DiscoveryService::createClickableFilepath(Hyde::getSiteOutputPath('index.html')) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/* @internal */ |
143
|
|
|
protected function runNodeCommand(string $command, string $message, ?string $actionMessage = null): void |
144
|
|
|
{ |
145
|
|
|
$this->info($message.' This may take a second.'); |
146
|
|
|
|
147
|
|
|
$output = shell_exec(sprintf( |
148
|
|
|
'%s%s', |
149
|
|
|
app()->environment() === 'testing' ? 'echo ' : '', |
|
|
|
|
150
|
|
|
$command |
151
|
|
|
)); |
152
|
|
|
|
153
|
|
|
$this->line($output ?? sprintf( |
154
|
|
|
'<fg=red>Could not %s! Is NPM installed?</>', |
155
|
|
|
$actionMessage ?? 'run script' |
156
|
|
|
)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function canGenerateSitemap(): bool |
160
|
|
|
{ |
161
|
|
|
return Features::sitemap(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function canGenerateFeed(): bool |
165
|
|
|
{ |
166
|
|
|
return Features::rss(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function canGenerateSearch(): bool |
170
|
|
|
{ |
171
|
|
|
return Features::hasDocumentationSearch(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|