Completed
Push — symfony-console ( 505007...6e7652 )
by Arnaud
02:41 queued 10s
created

Build::execute()   B

Complexity

Conditions 6
Paths 80

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.7377
c 0
b 0
f 0
cc 6
nc 80
nop 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Command;
10
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputDefinition;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class Build extends Command
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('build')
26
            ->setDescription('Build the website')
27
            ->setDefinition(
28
                new InputDefinition([
29
                    new InputArgument('path', InputArgument::OPTIONAL, 'If specified, use the given path as working directory'),
30
                    new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
31
                    new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
32
                    new InputOption('baseurl', null, InputOption::VALUE_REQUIRED, 'Set the base URL'),
33
                    new InputOption('destination', null, InputOption::VALUE_REQUIRED, 'Set the output directory'),
34
                ])
35
            )
36
            ->setHelp('Build the website in the output directory.');
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $config = [];
45
        $options = [];
46
        $messageOpt = '';
47
48
        if ($input->getOption('drafts')) {
49
            $options['drafts'] = true;
50
            $messageOpt .= ' with drafts';
51
        }
52
        if ($input->getOption('dry-run')) {
53
            $options['dry-run'] = true;
54
            $messageOpt .= ' dry-run';
55
        }
56
        if ($input->getOption('baseurl')) {
57
            $config['baseurl'] = $input->getOption('baseurl');
58
        }
59
        if ($input->getOption('destination')) {
60
            $config['output']['dir'] = $input->getOption('destination');
61
            $this->fs->dumpFile($this->getPath().'/'.Serve::$tmpDir.'/output', $input->getOption('destination'));
62
        }
63
64
        try {
65
            $output->writeln(sprintf('Building website%s...', $messageOpt));
66
            $output->writeln(sprintf('<comment>Path: %s</comment>', $this->getPath()));
67
            $this->getBuilder($output, $config)->build($options);
68
            //if ($this->getRoute()->getName() == 'serve') {
69
            $this->fs->dumpFile($this->getPath().'/'.Serve::$tmpDir.'/changes.flag', '');
70
            //}
71
        } catch (\Exception $e) {
72
            throw new \Exception(sprintf('%s', $e->getMessage()));
73
        }
74
75
        return 0;
76
    }
77
}
78