Passed
Push — serve ( 6ba102 )
by Arnaud
05:33
created

Build   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 87
rs 10
c 2
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
C execute() 0 54 9
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Command;
12
13
use Cecil\Util;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputDefinition;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Builds the website.
22
 */
23
class Build extends AbstractCommand
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('build')
32
            ->setDescription('Builds the website')
33
            ->setDefinition(
34
                new InputDefinition([
35
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
36
                    new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
37
                    new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
38
                    new InputOption('baseurl', null, InputOption::VALUE_REQUIRED, 'Set the base URL'),
39
                    new InputOption('destination', null, InputOption::VALUE_REQUIRED, 'Set the output directory'),
40
                    new InputOption(
41
                        'postprocess',
42
                        null,
43
                        InputOption::VALUE_OPTIONAL,
44
                        'Post-process output (disable with "no")',
45
                        false
46
                    ),
47
                    new InputOption('no-cache', null, InputOption::VALUE_NONE, 'Clear cache after build'),
48
                ])
49
            )
50
            ->setHelp('Builds the website in the output directory');
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $config = [];
59
        $options = [];
60
        $messageOpt = '';
61
62
        if ($input->getOption('drafts')) {
63
            $options['drafts'] = true;
64
            $messageOpt .= ' with drafts';
65
        }
66
        if ($input->getOption('dry-run')) {
67
            $options['dry-run'] = true;
68
            $messageOpt .= ' (dry-run)';
69
        }
70
        if ($input->getOption('baseurl')) {
71
            $config['baseurl'] = $input->getOption('baseurl');
72
        }
73
        if ($input->getOption('destination')) {
74
            $config['output']['dir'] = $input->getOption('destination');
75
            $this->fs->dumpFile(
76
                Util::joinFile($this->getPath(), self::TMP_DIR, 'output'),
77
                (string) $input->getOption('destination')
78
            );
79
        }
80
        if ($input->getOption('postprocess') === null) {
81
            $config['postprocess']['enabled'] = true;
82
        }
83
        if ($input->getOption('postprocess') == 'no') {
84
            $config['postprocess']['enabled'] = false;
85
        }
86
        if ($input->getOption('no-cache')) {
87
            $config['cache']['enabled'] = false;
88
        }
89
90
        $output->writeln(sprintf('Building website%s...', $messageOpt));
91
        $output->writeln(
92
            sprintf('<comment>Path: %s</comment>', $this->getPath()),
93
            OutputInterface::VERBOSITY_VERBOSE
94
        );
95
96
        $builder = $this->getBuilder($config);
97
98
        if ((bool) $this->builder->getConfig()->get('cache.enabled')) {
99
            $output->writeln(
100
                sprintf('<comment>Cache: %s</comment>', $this->builder->getConfig()->getCachePath()),
101
                OutputInterface::VERBOSITY_VERBOSE
102
            );
103
        }
104
105
        $builder->build($options);
106
        $this->fs->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'changes.flag'), time());
107
        $output->writeln('Done! 🎉');
108
109
        return 0;
110
    }
111
}
112