-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathInitCommand.php
206 lines (182 loc) · 8.08 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace CRM\CivixBundle\Command;
use CRM\CivixBundle\Builder\CopyFile;
use CRM\CivixBundle\Builder\Template;
use CRM\CivixBundle\Services;
use CRM\CivixBundle\Utils\Naming;
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 CRM\CivixBundle\Builder\Collection;
use CRM\CivixBundle\Builder\Dirs;
use CRM\CivixBundle\Builder\Info;
use CRM\CivixBundle\Builder\License;
use CRM\CivixBundle\Builder\Module;
use CRM\CivixBundle\Utils\Path;
class InitCommand extends AbstractCommand {
protected function configure() {
Services::templating();
$this
->setName('generate:module')
->setDescription('Create a new CiviCRM Module-Extension (Regenerate module.civix.php if ext.name not specified)')
->addArgument('key', InputArgument::OPTIONAL, "Extension identifier (Ex: \"foo-bar\" or \"org.example.foo-bar\")")
->addOption('enable', NULL, InputOption::VALUE_REQUIRED, 'Whether to auto-enable the new module (yes/no/ask)', 'ask')
->addOption('license', NULL, InputOption::VALUE_OPTIONAL, 'License for the extension (' . implode(', ', $this->getLicenses()) . ')', $this->getDefaultLicense())
->addOption('author', NULL, InputOption::VALUE_REQUIRED, 'Name of the author', $this->getDefaultAuthor())
->addOption('email', NULL, InputOption::VALUE_OPTIONAL, 'Email of the author', $this->getDefaultEmail())
->setHelp(
"Create a new CiviCRM Module-Extension (Regenerate module.civix.php if ext.name not specified)\n" .
"\n" .
"<comment>Identification:</comment>\n" .
" Keys must be lowercase alphanumeric (with dashes and underscores allowed).\n" .
"\n" .
" Optionally, you may use a Java-style prefix (reverse domain name).\n" .
"\n" .
" However, the prefix is mostly cosmetic. The base part of the key should be globally unique.\n" .
"\n" .
"<comment>Examples:</comment>\n" .
" civix generate:module foo-bar\n" .
" civix generate:module foo-bar --license=AGPL-3.0 --author=\"Alice\" --email=\"alice@example.org\"\n" .
" civix generate:module org.example.foo-bar \n" .
"\n"
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output) {
$ctx = [];
$ctx['type'] = 'module';
if (!$input->getArgument('key')) {
// Refresh existing module
$ctx['basedir'] = \CRM\CivixBundle\Application::findExtDir();
$basedir = new Path($ctx['basedir']);
$info = new Info($basedir->string('info.xml'));
$info->load($ctx);
$attrs = $info->get()->attributes();
if ($attrs['type'] != 'module') {
$output->writeln('<error>Wrong extension type: ' . $attrs['type'] . '</error>');
return;
}
$module = new Module(Services::templating());
$module->loadInit($ctx);
$module->save($ctx, $output);
return;
}
$licenses = new \LicenseData\Repository();
$name = $input->getArgument('key');
// Name should start with an alpha and only contain alphanumeric, - and .
if (!Naming::isValidFullName($name)) {
$output->writeln('<error>Malformed package name</error>');
return;
}
$ctx['basedir'] = $name;
$ctx['fullName'] = $name;
$ctx['mainFile'] = Naming::createShortName($name);
$ctx['namespace'] = 'CRM/' . Naming::createCamelName($name);
$ctx['angularModuleName'] = 'crm' . Naming::createCamelName($name);
if ($input->getOption('author') && $input->getOption('email')) {
$ctx['author'] = $input->getOption('author');
$ctx['email'] = $input->getOption('email');
}
else {
$output->writeln("<error>Missing author name or email address</error>");
$output->writeln("<error>Please pass --author and --email, or set defaults in ~/.gitconfig</error>");
return;
}
$ctx['license'] = $input->getOption('license');
if ($licenses->get($ctx['license'])) {
$output->writeln(sprintf('<comment>License set to %s (authored by %s \<%s>)</comment>', $ctx['license'], $ctx['author'], $ctx['email']));
$output->writeln('<comment>If this is in error, please correct info.xml and LICENSE.txt</comment>');
}
else {
$output->writeln('<error>Unrecognized license (' . $ctx['license'] . ')</error>');
return;
}
$ext = new Collection();
$output->writeln("<info>Initalize module " . $ctx['fullName'] . "</info>");
$basedir = new Path($ctx['basedir']);
$ext->builders['dirs'] = new Dirs([
$basedir->string('build'),
$basedir->string('templates'),
$basedir->string('xml'),
$basedir->string('images'),
$basedir->string($ctx['namespace']),
]);
$ext->builders['info'] = new Info($basedir->string('info.xml'));
$ext->builders['module'] = new Module(Services::templating());
$ext->builders['license'] = new License($licenses->get($ctx['license']), $basedir->string('LICENSE.txt'), FALSE);
$ext->builders['readme'] = new Template('readme.md.php', $basedir->string('README.md'), FALSE, Services::templating());
$ext->builders['screenshot'] = new CopyFile(dirname(dirname(dirname(dirname(__DIR__)))) . '/images/placeholder.png', $basedir->string('images/screenshot.png'), FALSE);
$ext->loadInit($ctx);
$ext->save($ctx, $output);
$this->tryEnable($input, $output, $ctx['fullName']);
}
/**
* Attempt to enable the extension on the linked CiviCRM site
*
* @return bool TRUE on success; FALSE if there's no site or if there's an error
*/
protected function tryEnable(InputInterface $input, OutputInterface $output, $key) {
Services::boot(['output' => $output]);
$civicrm_api3 = Services::api3();
if ($civicrm_api3 && $civicrm_api3->local && version_compare(\CRM_Utils_System::version(), '4.3.dev', '>=')) {
$siteName = \CRM_Utils_System::baseURL(); /* \CRM_Core_Config::singleton()->userSystem->cmsRootPath(); */
$output->writeln("<info>Refresh extension list for \"$siteName\"</info>");
if (!$civicrm_api3->Extension->refresh(['local' => TRUE, 'remote' => FALSE])) {
$output->writeln("<error>Refresh error: " . $civicrm_api3->errorMsg() . "</error>");
return FALSE;
}
if ($input->getOption('enable') === 'no') {
return FALSE;
}
if ($input->getOption('enable') === 'yes' || $this->confirm($input, $output, "Enable extension ($key) in \"$siteName\"? [Y/n] ")) {
$output->writeln("<info>Enable extension ($key) in \"$siteName\"</info>");
if (!$civicrm_api3->Extension->install(['key' => $key])) {
$output->writeln("<error>Install error: " . $civicrm_api3->errorMsg() . "</error>");
}
}
return TRUE;
}
// fallback
$output->writeln("NOTE: This might be a good time to refresh the extension list and install \"$key\".");
return FALSE;
}
protected function getDefaultLicense() {
$config = Services::config();
$license = NULL;
if (!empty($config['parameters']['license'])) {
$license = $config['parameters']['license'];
}
return empty($license) ? 'AGPL-3.0' : $license;
}
protected function getDefaultEmail() {
$config = Services::config();
$value = NULL;
if (!empty($config['parameters']['email'])) {
$value = $config['parameters']['email'];
}
return empty($value) ? $this->getGitConfig('user.email', 'FIXME') : $value;
}
protected function getDefaultAuthor() {
$config = Services::config();
$value = NULL;
if (!empty($config['parameters']['author'])) {
$value = $config['parameters']['author'];
}
return empty($value) ? $this->getGitConfig('user.name', 'FIXME') : $value;
}
protected function getLicenses() {
$licenses = new \LicenseData\Repository();
return array_keys($licenses->getAll());
}
protected function getGitConfig($key, $default) {
$result = NULL;
if (\CRM\CivixBundle\Utils\Commands::findExecutable('git')) {
$result = trim(`git config --get $key`);
}
if (empty($result)) {
$result = $default;
}
return $result;
}
}