-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
InitCommand.php
177 lines (153 loc) · 6.69 KB
/
InitCommand.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Console;
use Deployer\Component\Initializer\Initializer;
use Deployer\Component\Initializer\Template\CakeTemplate;
use Deployer\Component\Initializer\Template\CodeIgniterTemplate;
use Deployer\Component\Initializer\Template\CommonTemplate;
use Deployer\Component\Initializer\Template\DrupalTemplate;
use Deployer\Component\Initializer\Template\LaravelTemplate;
use Deployer\Component\Initializer\Template\SymfonyTemplate;
use Deployer\Component\Initializer\Template\Typo3Template;
use Deployer\Component\Initializer\Template\Yii2AdvancedAppTemplate;
use Deployer\Component\Initializer\Template\Yii2BasicAppTemplate;
use Deployer\Component\Initializer\Template\YiiTemplate;
use Deployer\Component\Initializer\Template\ZendTemplate;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
class InitCommand extends Command
{
protected function configure()
{
$this
->setName('init')
->setDescription('Initialize deployer in your project')
->addOption('template', 't', InputOption::VALUE_OPTIONAL, 'The template of you project')
->addOption('filepath', null, InputOption::VALUE_OPTIONAL, 'The file path (default "deploy.php")', 'deploy.php');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var FormatterHelper $formatter */
$formatter = $this->getHelper('formatter');
$initializer = new Initializer();
$template = $input->getOption('template');
$filepath = $input->getOption('filepath');
if (file_exists($filepath)) {
$output->writeln([
$formatter->formatBlock(
sprintf('The file "%s" already exist.', $filepath),
'bg=red;fg=white', true
),
]);
return 2;
}
$project = 'my_project';
$repository = '';
$hosts = [];
$allow = true;
if ($template === null) {
$io = new SymfonyStyle($input, $output);
// Welcome message
$output->writeln("
_____ _
| __ \ | |
| | | | ___ _ __ | | ___ _ _ ___ _ __
| | | |/ _ \ '_ \| |/ _ \| | | |/ _ \ '__|
| |__| | __/ |_) | | (_) | |_| | __/ |
|_____/ \___| .__/|_|\___/ \__, |\___|_|
| | __/ |
|_| |___/
");
$io->text([
'Welcome to the Deployer config generator.',
'This utility will walk you through creating a deploy.php file.',
'',
'Press ^C at any time to quit.',
]);
// Yes?
$io->confirm('Continue?');
// Template
$recipes = $initializer->getRecipes();
$template = $io->choice('Select project template', $recipes, 'common');
// Repo
$default = false;
try {
$process = Process::fromShellCommandline('git remote get-url origin');
$default = $process->mustRun()->getOutput();
$default = trim($default);
} catch (RuntimeException $e) {
}
$repository = $io->ask('Repository', $default);
// Repo
$default = false;
try {
$process = Process::fromShellCommandline('basename "$PWD"');
$default = $process->mustRun()->getOutput();
$default = trim($default);
} catch (RuntimeException $e) {
}
$project = $io->ask('Project name', $default);
// Hosts
$hosts = explode(',', $io->ask('Hosts (comma separated)', 'deployer.org'));
// Privacy
$io->text([
'Contribute to the Deployer Development',
'',
'In order to help development and improve the features in Deployer,',
'Deployer has a setting for usage data collection. This function',
'collects anonymous usage data and sends it to Deployer. The data is',
'used in Deployer development to get reliable statistics on which',
'features are used (or not used). The information is not traceable',
'to any individual or organization. Participation is voluntary,',
'and you can change your mind at any time.',
'',
'Anonymous usage data contains Deployer version, php version, os type,',
'name of the command being executed and whether it was successful or not,',
'exception class name, count of hosts and anonymized project hash.',
'',
'This function will not affect the performance of Deployer as',
'the data is insignificant and transmitted in a separate process.',
'',
'We appreciate your involvement!',
]);
$allow = $io->confirm('Do you confirm?');
}
file_put_contents($filepath, $initializer->getTemplate($template, $project, $repository, $hosts, $allow));
$output->writeln(sprintf(
'<info>Successfully created</info> <comment>%s</comment>',
$filepath
));
return 0;
}
/**
* Create a initializer system
*
* @return Initializer
*/
private function createInitializer()
{
$initializer = new Initializer();
$initializer->addTemplate('Common', new CommonTemplate());
$initializer->addTemplate('Laravel', new LaravelTemplate());
$initializer->addTemplate('Symfony', new SymfonyTemplate());
$initializer->addTemplate('Yii', new YiiTemplate());
$initializer->addTemplate('Yii2 Basic App', new Yii2BasicAppTemplate());
$initializer->addTemplate('Yii2 Advanced App', new Yii2AdvancedAppTemplate());
$initializer->addTemplate('Zend Framework', new ZendTemplate());
$initializer->addTemplate('CakePHP', new CakeTemplate());
$initializer->addTemplate('CodeIgniter', new CodeIgniterTemplate());
$initializer->addTemplate('Drupal', new DrupalTemplate());
$initializer->addTemplate('TYPO3', new Typo3Template());
return $initializer;
}
}