forked from CouscousPHP/Couscous
/
DeployCommand.php
122 lines (105 loc) · 3.2 KB
/
DeployCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Couscous\Application\Cli;
use Couscous\CommandRunner\Git;
use Couscous\Deployer;
use Couscous\Generator;
use Couscous\Model\Project;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* Generate and deploy the website.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class DeployCommand extends Command
{
/**
* @var Generator
*/
private $generator;
/**
* @var Deployer
*/
private $deployer;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var Git
*/
private $git;
public function __construct(Generator $generator, Deployer $deployer, Filesystem $filesystem, Git $git)
{
$this->generator = $generator;
$this->deployer = $deployer;
$this->filesystem = $filesystem;
$this->git = $git;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
try {
$remoteUrl = $this->git->getRemoteUrl();
} catch (\Exception $e) {
$remoteUrl = null;
}
$this
->setName('deploy')
->setDescription('Generate and deploy the website')
->addArgument(
'source',
InputArgument::OPTIONAL,
'Repository you want to generate.',
getcwd()
)
->addOption(
'repository',
null,
InputOption::VALUE_REQUIRED,
'Target repository in which to deploy the website.',
$remoteUrl
)
->addOption(
'branch',
null,
InputOption::VALUE_REQUIRED,
'Target branch in which to deploy the website.'
)
->addOption(
'config',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'If specified will override entries in couscous.yml (key=value)',
[]
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$sourceDirectory = $input->getArgument('source');
$repositoryUrl = $input->getOption('repository');
$targetBranch = $input->getOption('branch');
$cliConfig = $input->getOption('config');
$project = new Project($sourceDirectory, getcwd().'/.couscous/generated');
$project->metadata['cliConfig'] = $cliConfig;
// Generate the website
$this->generator->generate($project, $output);
// If no branch was provided, use the configured one or the default
if (!$targetBranch) {
$targetBranch = isset($project->metadata['branch']) ? $project->metadata['branch'] : 'gh-pages';
}
$output->writeln('');
// Deploy it
$this->deployer->deploy($project, $output, $repositoryUrl, $targetBranch);
}
}