Build::execute()   F
last analyzed

Complexity

Conditions 22
Paths > 20000

Size

Total Lines 106
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 68
nc 73728
nop 2
dl 0
loc 106
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Symfony\Component\Console\Helper\Table;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Build command.
25
 *
26
 * This command generates the website in the output directory.
27
 * It can include drafts, optimize generated files, and perform a dry run.
28
 * It also allows building a specific page or a subset of pages, clearing the cache, and showing build metrics.
29
 */
30
class Build extends AbstractCommand
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('build')
39
            ->setDescription('Builds the website')
40
            ->setDefinition([
41
                new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
42
                new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
43
                new InputOption('baseurl', 'u', InputOption::VALUE_REQUIRED, 'Set the base URL'),
44
                new InputOption('output', 'o', InputOption::VALUE_REQUIRED, 'Set the output directory'),
45
                new InputOption('optimize', null, InputOption::VALUE_NEGATABLE, 'Enable (or disable --no-optimize) optimization of generated files'),
46
                new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
47
                new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Set the path to extra config files (comma-separated)'),
48
                new InputOption('clear-cache', null, InputOption::VALUE_OPTIONAL, 'Clear cache before build (optional cache key as regular expression)', false),
49
                new InputOption('page', 'p', InputOption::VALUE_REQUIRED, 'Build a specific page'),
50
                new InputOption('render-subset', null, InputOption::VALUE_REQUIRED, 'Render a subset of pages'),
51
                new InputOption('show-pages', null, InputOption::VALUE_NONE, 'Show list of built pages in a table'),
52
                new InputOption('metrics', 'm', InputOption::VALUE_NONE, 'Show build metrics (duration and memory) of each step'),
53
                new InputOption('notif', null, InputOption::VALUE_NONE, 'Send desktop notification on build completion'),
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
84
Send a desktop <comment>notification</comment> on build completion, run:
85
86
  <info>%command.full_name% --notif</>
87
EOF
88
            );
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function execute(InputInterface $input, OutputInterface $output): int
95
    {
96
        $config = [];
97
        $options = [];
98
        $messageOpt = '';
99
100
        if ($input->getOption('baseurl')) {
101
            $config['baseurl'] = $input->getOption('baseurl');
102
        }
103
        if ($input->getOption('output')) {
104
            $config['output']['dir'] = $input->getOption('output');
105
            if ($input->getOption('output') != self::SERVE_OUTPUT) {
106
                Util\File::getFS()->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'), (string) $input->getOption('output'));
107
            }
108
        }
109
        if ($input->getOption('optimize') === true) {
110
            $config['optimize']['enabled'] = true;
111
        }
112
        if ($input->getOption('optimize') === false) {
113
            $config['optimize']['enabled'] = false;
114
        }
115
        if ($input->getOption('clear-cache') === null) {
116
            $config['cache']['enabled'] = false;
117
        }
118
119
        $builder = $this->getBuilder($config);
120
121
        if ($input->getOption('drafts')) {
122
            $options['drafts'] = true;
123
            $messageOpt .= ' with drafts';
124
        }
125
        if ($input->getOption('dry-run')) {
126
            $options['dry-run'] = true;
127
            $messageOpt .= ' (dry-run)';
128
        }
129
        if ($input->getOption('page')) {
130
            $options['page'] = $input->getOption('page');
131
        }
132
        if ($input->getOption('render-subset')) {
133
            $options['render-subset'] = (string) $input->getOption('render-subset');
134
        }
135
        if ($input->getOption('clear-cache')) {
136
            if (0 < $removedFiles = (new \Cecil\Cache($this->getBuilder()))->clearByPattern((string) $input->getOption('clear-cache'))) {
137
                $output->writeln(\sprintf('<info>%s cache files removed by regular expression "%s"</info>', $removedFiles, $input->getOption('clear-cache')));
138
            }
139
        }
140
141
        $output->writeln(\sprintf('Building website%s...', $messageOpt));
142
        $output->writeln(\sprintf('<comment>Path:   %s</comment>', $this->getPath()), OutputInterface::VERBOSITY_VERY_VERBOSE);
143
        if (!empty($this->getConfigFiles())) {
144
            $output->writeln(\sprintf('<comment>Config: %s</comment>', implode(', ', $this->getConfigFiles())), OutputInterface::VERBOSITY_VERY_VERBOSE);
145
        }
146
        $output->writeln(\sprintf('<comment>Output: %s</comment>', $this->getBuilder()->getConfig()->getOutputPath()), OutputInterface::VERBOSITY_VERY_VERBOSE);
147
        if ($builder->getConfig()->isEnabled('cache') !== false) {
148
            $output->writeln(\sprintf('<comment>Cache:  %s</comment>', $builder->getConfig()->getCachePath()), OutputInterface::VERBOSITY_VERY_VERBOSE);
149
        }
150
151
        // build
152
        $builder->build($options);
153
        $output->writeln('Build done');
154
155
        // notification
156
        if ($input->getOption('notif')) {
157
            $this->notification('Build done 🎉');
158
        }
159
160
        // show build steps metrics
161
        if ($input->getOption('metrics')) {
162
            $table = new Table($output);
163
            $table
164
                ->setHeaderTitle('Build steps metrics')
165
                ->setHeaders(['Step', 'Duration', 'Memory'])
166
                ->setRows($builder->getMetrics()['steps'])
167
            ;
168
            $table->setStyle('box')->render();
169
        }
170
171
        // show built pages as table
172
        if ($input->getOption('show-pages')) {
173
            $pagesAsArray = [];
174
            foreach (
175
                $this->getBuilder()->getPages()->filter(function (\Cecil\Collection\Page\Page $page) {
176
                    return $page->getVariable('published');
177
                })->usort(function (\Cecil\Collection\Page\Page $pageA, \Cecil\Collection\Page\Page $pageB) {
178
                    return strnatcmp((string) $pageA['language'], (string) $pageB['language']);
179
                }) as $page
180
            ) {
181
                /** @var \Cecil\Collection\Page\Page $page */
182
                $pagesAsArray[] = [
183
                    $page->getId(),
184
                    $page->getVariable('language'),
185
                    \sprintf("%s %s", $page->getType(), $page->getType() !== \Cecil\Collection\Page\Type::PAGE->value ? "(" . \count($page->getPages() ?: []) . ")" : ''),
186
                    $page->getSection(),
187
                    $page->isVirtual() ? 'true' : 'false',
188
                ];
189
            }
190
            $table = new Table($output);
191
            $table
192
                ->setHeaderTitle(\sprintf("Built pages (%s)", \count($pagesAsArray)))
193
                ->setHeaders(['ID', 'Lang', 'Type', 'Section', 'Virtual'])
194
                ->setRows($pagesAsArray)
195
            ;
196
            $table->setStyle('box')->render();
197
        }
198
199
        return 0;
200
    }
201
}
202