1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cecil. |
5
|
|
|
* |
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cecil\Command; |
15
|
|
|
|
16
|
|
|
use Cecil\Util; |
17
|
|
|
use Joli\JoliNotif\DefaultNotifier; |
18
|
|
|
use Symfony\Component\Console\Helper\Table; |
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Build command. |
26
|
|
|
* |
27
|
|
|
* This command generates the website in the output directory. |
28
|
|
|
* It can include drafts, optimize generated files, and perform a dry run. |
29
|
|
|
* It also allows building a specific page or a subset of pages, clearing the cache, and showing build metrics. |
30
|
|
|
*/ |
31
|
|
|
class Build extends AbstractCommand |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setName('build') |
40
|
|
|
->setDescription('Builds the website') |
41
|
|
|
->setDefinition([ |
42
|
|
|
new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'), |
43
|
|
|
new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'), |
44
|
|
|
new InputOption('baseurl', 'u', InputOption::VALUE_REQUIRED, 'Set the base URL'), |
45
|
|
|
new InputOption('output', 'o', InputOption::VALUE_REQUIRED, 'Set the output directory'), |
46
|
|
|
new InputOption('optimize', null, InputOption::VALUE_NEGATABLE, 'Enable (or disable --no-optimize) optimization of generated files'), |
47
|
|
|
new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'), |
48
|
|
|
new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Set the path to extra config files (comma-separated)'), |
49
|
|
|
new InputOption('clear-cache', null, InputOption::VALUE_OPTIONAL, 'Clear cache before build (optional cache key as regular expression)', false), |
50
|
|
|
new InputOption('page', 'p', InputOption::VALUE_REQUIRED, 'Build a specific page'), |
51
|
|
|
new InputOption('render-subset', null, InputOption::VALUE_REQUIRED, 'Render a subset of pages'), |
52
|
|
|
new InputOption('show-pages', null, InputOption::VALUE_NONE, 'Show list of built pages in a table'), |
53
|
|
|
new InputOption('metrics', 'm', InputOption::VALUE_NONE, 'Show build metrics (duration and memory) of each step'), |
54
|
|
|
]) |
55
|
|
|
->setHelp( |
56
|
|
|
<<<'EOF' |
57
|
|
|
The <info>%command.name%</> command generates the website in the <comment>output</comment> directory. |
58
|
|
|
|
59
|
|
|
<info>%command.full_name%</> |
60
|
|
|
<info>%command.full_name% path/to/the/working/directory</> |
61
|
|
|
<info>%command.full_name% --baseurl=https://example.com/</> |
62
|
|
|
<info>%command.full_name% --output=_site</> |
63
|
|
|
|
64
|
|
|
To build the website with <comment>optimization</comment> of generated files, you can use the <info>--optimize</info> option. |
65
|
|
|
This is useful to reduce the size of the generated files and <comment>improve performance</comment>: |
66
|
|
|
|
67
|
|
|
<info>%command.full_name% --optimize</> |
68
|
|
|
<info>%command.full_name% --no-optimize</> |
69
|
|
|
|
70
|
|
|
To build the website <comment>without overwriting files in the output</comment> directory, you can use the <info>--dry-run</info> option. |
71
|
|
|
This is useful to check what would be built without actually writing files: |
72
|
|
|
|
73
|
|
|
<info>%command.full_name% --dry-run</> |
74
|
|
|
|
75
|
|
|
To build the website with a specific subset of rendered pages, you can use the <info>--render-subset</info> option. |
76
|
|
|
This is useful to <comment>build only a part of the website</comment>, for example, only "hot" pages or a specific section: |
77
|
|
|
|
78
|
|
|
<info>%command.full_name% --render-subset=subset</> |
79
|
|
|
|
80
|
|
|
To show build steps <comment>metrics</comment>, run: |
81
|
|
|
|
82
|
|
|
<info>%command.full_name% --metrics</> |
83
|
|
|
EOF |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
91
|
|
|
{ |
92
|
|
|
$config = []; |
93
|
|
|
$options = []; |
94
|
|
|
$messageOpt = ''; |
95
|
|
|
|
96
|
|
|
if ($input->getOption('baseurl')) { |
97
|
|
|
$config['baseurl'] = $input->getOption('baseurl'); |
98
|
|
|
} |
99
|
|
|
if ($input->getOption('output')) { |
100
|
|
|
$config['output']['dir'] = $input->getOption('output'); |
101
|
|
|
if ($input->getOption('output') != self::SERVE_OUTPUT) { |
102
|
|
|
Util\File::getFS()->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'), (string) $input->getOption('output')); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
if ($input->getOption('optimize') === true) { |
106
|
|
|
$config['optimize']['enabled'] = true; |
107
|
|
|
} |
108
|
|
|
if ($input->getOption('optimize') === false) { |
109
|
|
|
$config['optimize']['enabled'] = false; |
110
|
|
|
} |
111
|
|
|
if ($input->getOption('clear-cache') === null) { |
112
|
|
|
$config['cache']['enabled'] = false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$builder = $this->getBuilder($config); |
116
|
|
|
|
117
|
|
|
if ($input->getOption('drafts')) { |
118
|
|
|
$options['drafts'] = true; |
119
|
|
|
$messageOpt .= ' with drafts'; |
120
|
|
|
} |
121
|
|
|
if ($input->getOption('dry-run')) { |
122
|
|
|
$options['dry-run'] = true; |
123
|
|
|
$messageOpt .= ' (dry-run)'; |
124
|
|
|
} |
125
|
|
|
if ($input->getOption('page')) { |
126
|
|
|
$options['page'] = $input->getOption('page'); |
127
|
|
|
} |
128
|
|
|
if ($input->getOption('render-subset')) { |
129
|
|
|
$options['render-subset'] = (string) $input->getOption('render-subset'); |
130
|
|
|
} |
131
|
|
|
if ($input->getOption('clear-cache')) { |
132
|
|
|
if (0 < $removedFiles = (new \Cecil\Cache($this->getBuilder()))->clearByPattern((string) $input->getOption('clear-cache'))) { |
133
|
|
|
$output->writeln(\sprintf('<info>%s cache files removed by regular expression "%s"</info>', $removedFiles, $input->getOption('clear-cache'))); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$output->writeln(\sprintf('Building website%s...', $messageOpt)); |
138
|
|
|
$output->writeln(\sprintf('<comment>Path: %s</comment>', $this->getPath()), OutputInterface::VERBOSITY_VERY_VERBOSE); |
139
|
|
|
if (!empty($this->getConfigFiles())) { |
140
|
|
|
$output->writeln(\sprintf('<comment>Config: %s</comment>', implode(', ', $this->getConfigFiles())), OutputInterface::VERBOSITY_VERY_VERBOSE); |
141
|
|
|
} |
142
|
|
|
$output->writeln(\sprintf('<comment>Output: %s</comment>', $this->getBuilder()->getConfig()->getOutputPath()), OutputInterface::VERBOSITY_VERY_VERBOSE); |
143
|
|
|
if ($builder->getConfig()->isEnabled('cache') !== false) { |
144
|
|
|
$output->writeln(\sprintf('<comment>Cache: %s</comment>', $builder->getConfig()->getCachePath()), OutputInterface::VERBOSITY_VERY_VERBOSE); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// build |
148
|
|
|
$builder->build($options); |
149
|
|
|
$output->writeln('Done 🎉'); |
150
|
|
|
// notification |
151
|
|
|
if (self::DESKTOP_NOTIFICATION) { |
152
|
|
|
$notifier = new DefaultNotifier(); |
153
|
|
|
$this->notification->setBody('Build done 🎉'); |
154
|
|
|
$notifier->send($this->notification); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// show build steps metrics |
158
|
|
|
if ($input->getOption('metrics')) { |
159
|
|
|
$table = new Table($output); |
160
|
|
|
$table |
161
|
|
|
->setHeaderTitle('Build steps metrics') |
162
|
|
|
->setHeaders(['Step', 'Duration', 'Memory']) |
163
|
|
|
->setRows($builder->getMetrics()['steps']) |
164
|
|
|
; |
165
|
|
|
$table->setStyle('box')->render(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// show built pages as table |
169
|
|
|
if ($input->getOption('show-pages')) { |
170
|
|
|
$pagesAsArray = []; |
171
|
|
|
foreach ( |
172
|
|
|
$this->getBuilder()->getPages()->filter(function (\Cecil\Collection\Page\Page $page) { |
173
|
|
|
return $page->getVariable('published'); |
174
|
|
|
})->usort(function (\Cecil\Collection\Page\Page $pageA, \Cecil\Collection\Page\Page $pageB) { |
175
|
|
|
return strnatcmp((string) $pageA['language'], (string) $pageB['language']); |
176
|
|
|
}) as $page |
177
|
|
|
) { |
178
|
|
|
/** @var \Cecil\Collection\Page\Page $page */ |
179
|
|
|
$pagesAsArray[] = [ |
180
|
|
|
$page->getId(), |
181
|
|
|
$page->getVariable('language'), |
182
|
|
|
\sprintf("%s %s", $page->getType(), $page->getType() !== \Cecil\Collection\Page\Type::PAGE->value ? "(" . \count($page->getPages() ?: []) . ")" : ''), |
183
|
|
|
$page->getSection(), |
184
|
|
|
$page->isVirtual() ? 'true' : 'false', |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
$table = new Table($output); |
188
|
|
|
$table |
189
|
|
|
->setHeaderTitle(\sprintf("Built pages (%s)", \count($pagesAsArray))) |
190
|
|
|
->setHeaders(['ID', 'Lang', 'Type', 'Section', 'Virtual']) |
191
|
|
|
->setRows($pagesAsArray) |
192
|
|
|
; |
193
|
|
|
$table->setStyle('box')->render(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return 0; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|