-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathInitCommand.php
More file actions
168 lines (146 loc) · 6.15 KB
/
Copy pathInitCommand.php
File metadata and controls
168 lines (146 loc) · 6.15 KB
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
<?php
namespace Openl10n\Cli\Command;
use GuzzleHttp\Exception\ClientException;
use Openl10n\Sdk\Model\Project;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
class InitCommand extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('init')
->setDefinition(array(
new InputOption('project', null, InputOption::VALUE_REQUIRED, 'Slug of the project'),
new InputOption('hostname', null, InputOption::VALUE_REQUIRED, 'Set server hostname'),
new InputOption('port', null, InputOption::VALUE_REQUIRED, 'Specific port for the server'),
new InputOption('ssl', null, InputOption::VALUE_NONE, 'Use SSL'),
new InputOption('username', null, InputOption::VALUE_REQUIRED, 'Set server user'),
new InputOption('password', null, InputOption::VALUE_REQUIRED, 'Set server password'),
new InputOption('files', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Pattern of translation ressources, e.g. "locales/<locale>.yml"')
))
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$options = $input->getOptions();
if (null === $options['project']) {
$project = basename(realpath('.'));
$options['project'] = $dialog->ask($output, "<info>Project's slug</info> [<comment>$project</comment>]: ", $project);
}
if (null === $options['hostname']) {
$options['hostname'] = $dialog->ask($output, '<info>Hostname</info> [<comment>openl10n.dev</comment>]: ', 'openl10n.dev');
}
if (false === $options['ssl']) {
$options['ssl'] = $dialog->askConfirmation($output, '<info>Enable ssl</info> [<comment>no</comment>]? ', false);
}
if (null === $options['port']) {
$port = $options['ssl'] ? 443 : 80;
$options['port'] = $dialog->askAndValidate(
$output,
"<info>Port</info> [<comment>$port</comment>]: ",
function ($answer) {
if (!is_int($answer) && !ctype_digit($answer)) {
throw new \RuntimeException('The port must be an integer.');
}
return (int) $answer;
},
false,
$port
);
} else {
$options['port'] = (int) $options['port'];
}
if (null === $options['username']) {
$user = get_current_user();
$options['username'] = $dialog->ask($output, "<info>Username</info> [<comment>$user</comment>]: ", $user);
}
if (null === $options['password']) {
$options['password'] = $dialog->askHiddenResponseAndValidate(
$output,
'<info>Password</info> []: ',
function ($answer) {
if ('' === trim($answer)) {
throw new \RuntimeException('The password can not be empty.');
}
return $answer;
},
false,
false
);
}
if (empty($options['files'])) {
$output->writeln('');
while (null !== $file = $dialog->ask($output, '<info>Pattern file</info> []: ')) {
if (false !== $file) {
$options['files'][] = $file;
}
}
}
$config = array(
'server' => array(
'hostname' => $options['hostname'],
'port' => (int) $options['port'],
'use_ssl' => (bool) $options['ssl'],
'username' => $options['username'],
'password' => $options['password'],
),
'project' => $options['project'],
);
if (null !== $options['files']) {
foreach ($options['files'] as $file) {
$config['files'][] = ['pattern' => $file];
}
}
if ((80 === $options['port'] && !$options['ssl']) ||
(443 === $options['port'] && $options['ssl'])
) {
unset($config['server']['port']);
}
if (!$options['ssl']) {
unset($config['server']['use_ssl']);
}
$content = Yaml::dump($config, 3);
$output->writeln(['', $content]);
if (!$dialog->askConfirmation($output, '<info>Do you confirm generation</info> [<comment>yes</comment>]? ')) {
return 1;
}
file_put_contents('./openl10n.yml', $content);
$projectApi = $this->get('openl10n.api')->getEntryPoint('project');
try {
$project = $projectApi->get($config['project']);
return;
} catch (ClientException $e) {
if ('404' !== $e->getResponse()->getStatusCode()) {
throw $e;
}
}
$output->writeln('');
if ($dialog->askConfirmation($output, '<info>Would you like to create the project</info> [<comment>yes</comment>]? ')) {
$project = new Project($config['project']);
$defaultName = ucfirst($project->getSlug());
$name = $dialog->ask($output, "<info>Project's name</info> [<comment>$defaultName</comment>]: ", $defaultName);
$project->setName($name);
$defaultLocale = $dialog->ask($output, "<info>Default locale</info> [<comment>en</comment>]: ", 'en');
$project->setDefaultLocale($defaultLocale);
$description = $dialog->ask($output, "<info>Description</info> []: ");
$project->setDescription($description);
try {
$projectApi->create($project);
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return 1;
}
}
}
}