5 * Contains \Drupal\Console\Command\Generate\ThemeCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Drupal\Console\Command\Shared\ArrayInputTrait;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\ThemeRegionTrait;
15 use Drupal\Console\Command\Shared\ThemeBreakpointTrait;
16 use Drupal\Console\Generator\ThemeGenerator;
17 use Drupal\Console\Command\Shared\ConfirmationTrait;
18 use Drupal\Console\Core\Command\Command;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Utils\Site;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Utils\Validator;
23 use Drupal\Core\Extension\ThemeHandler;
24 use Webmozart\PathUtil\Path;
29 * @package Drupal\Console\Command\Generate
31 class ThemeCommand extends Command
33 use ConfirmationTrait;
35 use ThemeBreakpointTrait;
41 protected $extensionManager;
61 protected $themeHandler;
69 * @var StringConverter
71 protected $stringConverter;
74 * ThemeCommand constructor.
76 * @param Manager $extensionManager
77 * @param ThemeGenerator $generator
78 * @param Validator $validator
80 * @param ThemeHandler $themeHandler
82 * @param StringConverter $stringConverter
84 public function __construct(
85 Manager $extensionManager,
86 ThemeGenerator $generator,
89 ThemeHandler $themeHandler,
91 StringConverter $stringConverter
93 $this->extensionManager = $extensionManager;
94 $this->generator = $generator;
95 $this->validator = $validator;
96 $this->appRoot = $appRoot;
97 $this->themeHandler = $themeHandler;
99 $this->stringConverter = $stringConverter;
100 parent::__construct();
107 protected function configure()
110 ->setName('generate:theme')
111 ->setDescription($this->trans('commands.generate.theme.description'))
112 ->setHelp($this->trans('commands.generate.theme.help'))
116 InputOption::VALUE_REQUIRED,
117 $this->trans('commands.generate.theme.options.theme')
122 InputOption::VALUE_REQUIRED,
123 $this->trans('commands.generate.theme.options.machine-name')
128 InputOption::VALUE_REQUIRED,
129 $this->trans('commands.generate.theme.options.theme-path')
134 InputOption::VALUE_OPTIONAL,
135 $this->trans('commands.generate.theme.options.description')
137 ->addOption('core', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.theme.options.core'))
141 InputOption::VALUE_OPTIONAL,
142 $this->trans('commands.generate.theme.options.package')
147 InputOption::VALUE_OPTIONAL,
148 $this->trans('commands.generate.theme.options.global-library')
153 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
154 $this->trans('commands.generate.theme.options.libraries')
159 InputOption::VALUE_OPTIONAL,
160 $this->trans('commands.generate.theme.options.base-theme')
165 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
166 $this->trans('commands.generate.theme.options.regions')
171 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
172 $this->trans('commands.generate.theme.options.breakpoints')
174 ->setAliases(['gt']);
180 protected function execute(InputInterface $input, OutputInterface $output)
182 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
183 if (!$this->confirmOperation()) {
187 $theme = $this->validator->validateModuleName($input->getOption('theme'));
188 // Get the profile path and define a profile path if it is null
189 // Check that it is an absolute path or otherwise create an absolute path using appRoot
190 $theme_path = $input->getOption('theme-path');
191 $theme_path = $theme_path == null ? 'themes/custom' : $theme_path;
192 $theme_path = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
193 $theme_path = $this->validator->validateModulePath($theme_path, true);
195 $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
196 $description = $input->getOption('description');
197 $core = $input->getOption('core');
198 $package = $input->getOption('package');
199 $base_theme = $input->getOption('base-theme');
200 $global_library = $input->getOption('global-library');
201 $libraries = $input->getOption('libraries');
202 $regions = $input->getOption('regions');
203 $breakpoints = $input->getOption('breakpoints');
204 $noInteraction = $input->getOption('no-interaction');
206 // Parse nested data.
207 if ($noInteraction) {
208 $libraries = $this->explodeInlineArray($libraries);
209 $regions = $this->explodeInlineArray($regions);
210 $breakpoints = $this->explodeInlineArray($breakpoints);
213 $this->generator->generate([
215 'machine_name' => $machine_name,
216 'dir' => $theme_path,
218 'description' => $description,
219 'package' => $package,
220 'base_theme' => $base_theme,
221 'global_library' => $global_library,
222 'libraries' => $libraries,
223 'regions' => $regions,
224 'breakpoints' => $breakpoints,
234 protected function interact(InputInterface $input, OutputInterface $output)
237 $theme = $input->getOption('theme') ? $this->validator->validateModuleName($input->getOption('theme')) : null;
238 } catch (\Exception $error) {
239 $this->getIo()->error($error->getMessage());
245 $validators = $this->validator;
246 $theme = $this->getIo()->ask(
247 $this->trans('commands.generate.theme.questions.theme'),
249 function ($theme) use ($validators) {
250 return $validators->validateModuleName($theme);
253 $input->setOption('theme', $theme);
257 $machine_name = $input->getOption('machine-name') ? $this->validator->validateModuleName($input->getOption('machine-name')) : null;
258 } catch (\Exception $error) {
259 $this->getIo()->error($error->getMessage());
264 if (!$machine_name) {
265 $machine_name = $this->getIo()->ask(
266 $this->trans('commands.generate.theme.questions.machine-name'),
267 $this->stringConverter->createMachineName($theme),
268 function ($machine_name) use ($validators) {
269 return $validators->validateMachineName($machine_name);
272 $input->setOption('machine-name', $machine_name);
275 $theme_path = $input->getOption('theme-path');
277 $theme_path = $this->getIo()->ask(
278 $this->trans('commands.generate.theme.questions.theme-path'),
280 function ($theme_path) use ($machine_name) {
281 $fullPath = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
282 $fullPath = $fullPath.'/'.$machine_name;
283 if (file_exists($fullPath)) {
284 throw new \InvalidArgumentException(
286 $this->trans('commands.generate.theme.errors.directory-exists'),
295 $input->setOption('theme-path', $theme_path);
298 $description = $input->getOption('description');
300 $description = $this->getIo()->ask(
301 $this->trans('commands.generate.theme.questions.description'),
302 $this->trans('commands.generate.theme.suggestions.my-awesome-theme')
304 $input->setOption('description', $description);
307 $package = $input->getOption('package');
309 $package = $this->getIo()->ask(
310 $this->trans('commands.generate.theme.questions.package'),
311 $this->trans('commands.generate.theme.suggestions.other')
313 $input->setOption('package', $package);
316 $core = $input->getOption('core');
318 $core = $this->getIo()->ask(
319 $this->trans('commands.generate.theme.questions.core'),
322 $input->setOption('core', $core);
325 $base_theme = $input->getOption('base-theme');
327 $themes = $this->themeHandler->rebuildThemeData();
328 $themes['false'] ='';
330 uasort($themes, 'system_sort_modules_by_info_name');
332 $base_theme = $this->getIo()->choiceNoList(
333 $this->trans('commands.generate.theme.options.base-theme'),
336 $input->setOption('base-theme', $base_theme);
339 $global_library = $input->getOption('global-library');
340 if (!$global_library) {
341 $global_library = $this->getIo()->ask(
342 $this->trans('commands.generate.theme.questions.global-library'),
345 $input->setOption('global-library', $global_library);
349 // --libraries option.
350 $libraries = $input->getOption('libraries');
352 if ($this->getIo()->confirm(
353 $this->trans('commands.generate.theme.questions.library-add'),
357 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::libraryQuestion
358 $libraries = $this->libraryQuestion();
361 $libraries = $this->explodeInlineArray($libraries);
363 $input->setOption('libraries', $libraries);
366 $regions = $input->getOption('regions');
368 if ($this->getIo()->confirm(
369 $this->trans('commands.generate.theme.questions.regions'),
373 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
374 $regions = $this->regionQuestion();
377 $regions = $this->explodeInlineArray($regions);
379 $input->setOption('regions', $regions);
381 // --breakpoints option.
382 $breakpoints = $input->getOption('breakpoints');
384 if ($this->getIo()->confirm(
385 $this->trans('commands.generate.theme.questions.breakpoints'),
389 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
390 $breakpoints = $this->breakpointQuestion();
393 $breakpoints = $this->explodeInlineArray($breakpoints);
395 $input->setOption('breakpoints', $breakpoints);