5 * Contains \Drupal\Console\Command\Generate\ProfileCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Drupal\Console\Command\Shared\ConfirmationTrait;
11 use Drupal\Console\Core\Command\Command;
12 use Drupal\Console\Generator\ProfileGenerator;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Drupal\Console\Extension\Manager;
17 use Drupal\Console\Core\Utils\StringConverter;
18 use Drupal\Console\Utils\Validator;
19 use Webmozart\PathUtil\Path;
22 * Class ProfileCommand
24 * @package Drupal\Console\Command\Generate
27 class ProfileCommand extends Command
29 use ConfirmationTrait;
34 protected $extensionManager;
37 * @var ProfileGenerator
42 * @var StringConverter
44 protected $stringConverter;
52 * ProfileCommand constructor.
54 * @param Manager $extensionManager
55 * @param ProfileGenerator $generator
56 * @param StringConverter $stringConverter
57 * @param Validator $validator
60 public function __construct(
61 Manager $extensionManager,
62 ProfileGenerator $generator,
63 StringConverter $stringConverter,
67 $this->extensionManager = $extensionManager;
68 $this->generator = $generator;
69 $this->stringConverter = $stringConverter;
70 $this->validator = $validator;
71 $this->appRoot = $appRoot;
72 parent::__construct();
78 protected function configure()
81 ->setName('generate:profile')
82 ->setDescription($this->trans('commands.generate.profile.description'))
83 ->setHelp($this->trans('commands.generate.profile.help'))
87 InputOption::VALUE_REQUIRED,
88 $this->trans('commands.generate.profile.options.profile')
93 InputOption::VALUE_REQUIRED,
94 $this->trans('commands.generate.profile.options.machine-name')
99 InputOption::VALUE_REQUIRED,
100 $this->trans('commands.generate.profile.options.profile-path')
105 InputOption::VALUE_OPTIONAL,
106 $this->trans('commands.generate.profile.options.description')
111 InputOption::VALUE_OPTIONAL,
112 $this->trans('commands.generate.profile.options.core')
117 InputOption::VALUE_OPTIONAL,
118 $this->trans('commands.generate.profile.options.dependencies'),
124 InputOption::VALUE_OPTIONAL,
125 $this->trans('commands.generate.profile.options.themes'),
131 InputOption::VALUE_OPTIONAL,
132 $this->trans('commands.generate.profile.options.distribution')
133 )->setAliases(['gpr']);
139 protected function execute(InputInterface $input, OutputInterface $output)
141 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
142 if (!$this->confirmOperation()) {
146 // Get the profile path and define a profile path if it is null
147 // Check that it is an absolute path or otherwise create an absolute path using appRoot
148 $profile_path = $input->getOption('profile-path');
149 $profile_path = $profile_path == null ? 'profiles' : $profile_path;
150 $profile_path = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
151 $profile_path = $this->validator->validateModulePath($profile_path, true);
153 $profile = $this->validator->validateModuleName($input->getOption('profile'));
154 $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
155 $description = $input->getOption('description');
156 $core = $input->getOption('core');
157 $dependencies = $this->validator->validateExtensions($input->getOption('dependencies'), 'module', $this->getIo());
158 $themes = $this->validator->validateExtensions($input->getOption('themes'), 'theme', $this->getIo());
159 $distribution = $input->getOption('distribution');
161 $this->generator->generate([
162 'profile' => $profile,
163 'machine_name' => $machine_name,
166 'description' => $description,
167 'dependencies' => $dependencies,
169 'distribution' => $distribution,
170 'dir' => $profile_path,
177 protected function interact(InputInterface $input, OutputInterface $output)
179 //$stringUtils = $this->getStringHelper();
180 $validators = $this->validator;
183 // A profile is technically also a module, so we can use the same
184 // validator to check the name.
185 $profile = $input->getOption('profile') ? $validators->validateModuleName($input->getOption('profile')) : null;
186 } catch (\Exception $error) {
187 $this->getIo()->error($error->getMessage());
193 $profile = $this->getIo()->ask(
194 $this->trans('commands.generate.profile.questions.profile'),
196 function ($profile) use ($validators) {
197 return $validators->validateModuleName($profile);
200 $input->setOption('profile', $profile);
204 $machine_name = $input->getOption('machine-name') ? $validators->validateModuleName($input->getOption('machine-name')) : null;
205 } catch (\Exception $error) {
206 $this->getIo()->error($error->getMessage());
211 if (!$machine_name) {
212 $machine_name = $this->getIo()->ask(
213 $this->trans('commands.generate.profile.questions.machine-name'),
214 $this->stringConverter->createMachineName($profile),
215 function ($machine_name) use ($validators) {
216 return $validators->validateMachineName($machine_name);
219 $input->setOption('machine-name', $machine_name);
222 $profile_path = $input->getOption('profile-path');
223 if (!$profile_path) {
224 $profile_path = $this->getIo()->ask(
225 $this->trans('commands.generate.profile.questions.profile-path'),
227 function ($profile_path) use ($machine_name) {
228 $fullPath = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
229 $fullPath = $fullPath.'/'.$machine_name;
230 if (file_exists($fullPath)) {
231 throw new \InvalidArgumentException(
233 $this->trans('commands.generate.profile.errors.directory-exists'),
239 return $profile_path;
243 $input->setOption('profile-path', $profile_path);
245 $description = $input->getOption('description');
247 $description = $this->getIo()->ask(
248 $this->trans('commands.generate.profile.questions.description'),
249 $this->trans('commands.generate.profile.suggestions.my-useful-profile')
251 $input->setOption('description', $description);
254 $core = $input->getOption('core');
256 $core = $this->getIo()->ask(
257 $this->trans('commands.generate.profile.questions.core'),
260 $input->setOption('core', $core);
263 $dependencies = $input->getOption('dependencies');
264 if (!$dependencies) {
265 if ($this->getIo()->confirm(
266 $this->trans('commands.generate.profile.questions.dependencies'),
270 $dependencies = $this->getIo()->ask(
271 $this->trans('commands.generate.profile.options.dependencies'),
275 $input->setOption('dependencies', $dependencies);
278 $distribution = $input->getOption('distribution');
279 if (!$distribution) {
280 if ($this->getIo()->confirm(
281 $this->trans('commands.generate.profile.questions.distribution'),
285 $distribution = $this->getIo()->ask(
286 $this->trans('commands.generate.profile.options.distribution'),
287 $this->trans('commands.generate.profile.suggestions.my-kick-ass-distribution')
289 $input->setOption('distribution', $distribution);