From 390763e6c431d66eae80c1036ea049d3ab5333ca Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 26 May 2017 02:25:03 -0600 Subject: [PATCH] Add option to generate libraries for generate:theme command. (#3309) --- src/Command/Generate/ThemeCommand.php | 23 +++++++++++++++ src/Command/Shared/ThemeRegionTrait.php | 39 +++++++++++++++++++++++++ src/Generator/ThemeGenerator.php | 10 +++++++ templates/theme/info.yml.twig | 6 +++- templates/theme/libraries.yml.twig | 16 ++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 templates/theme/libraries.yml.twig diff --git a/src/Command/Generate/ThemeCommand.php b/src/Command/Generate/ThemeCommand.php index d9d2d210b..fc014a7c2 100644 --- a/src/Command/Generate/ThemeCommand.php +++ b/src/Command/Generate/ThemeCommand.php @@ -146,6 +146,12 @@ protected function configure() null, InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.theme.options.global-library') + ) + ->addOption( + 'libraries', + null, + InputOption::VALUE_OPTIONAL, + $this->trans('commands.generate.theme.options.libraries') ) ->addOption( 'base-theme', @@ -189,6 +195,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $package = $input->getOption('package'); $base_theme = $input->getOption('base-theme'); $global_library = $input->getOption('global-library'); + $libraries = $input->getOption('libraries'); $regions = $input->getOption('regions'); $breakpoints = $input->getOption('breakpoints'); @@ -201,6 +208,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $package, $base_theme, $global_library, + $libraries, $regions, $breakpoints ); @@ -328,6 +336,21 @@ function ($theme_path) use ($drupalRoot, $machine_name) { $input->setOption('global-library', $global_library); } + + // --libraries option. + $libraries = $input->getOption('libraries'); + if (!$libraries) { + if ($io->confirm( + $this->trans('commands.generate.theme.questions.library-add'), + true + ) + ) { + // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::libraryQuestion + $libraries = $this->libraryQuestion($io); + $input->setOption('libraries', $libraries); + } + } + // --regions option. $regions = $input->getOption('regions'); if (!$regions) { diff --git a/src/Command/Shared/ThemeRegionTrait.php b/src/Command/Shared/ThemeRegionTrait.php index cf0e2480e..c4bb8ac97 100644 --- a/src/Command/Shared/ThemeRegionTrait.php +++ b/src/Command/Shared/ThemeRegionTrait.php @@ -54,4 +54,43 @@ function ($regionMachineName) use ($validators) { return $regions; } + + /** + * @param DrupalStyle $io + * + * @return mixed + */ + public function libraryQuestion(DrupalStyle $io) + { + $validators = $this->validator; + $libraries = []; + while (true) { + $libraryName = $io->ask( + $this->trans('commands.generate.theme.questions.library-name') + ); + + $libraryVersion = $io->ask( + $this->trans('commands.generate.theme.questions.library-version'), + '1.0' + ); + + array_push( + $libraries, + [ + 'library_name' => $libraryName, + 'library_version'=> $libraryVersion, + ] + ); + + if (!$io->confirm( + $this->trans('commands.generate.theme.questions.library-add'), + true + ) + ) { + break; + } + } + + return $libraries; + } } diff --git a/src/Generator/ThemeGenerator.php b/src/Generator/ThemeGenerator.php index e9f15173c..78342bf98 100644 --- a/src/Generator/ThemeGenerator.php +++ b/src/Generator/ThemeGenerator.php @@ -40,6 +40,7 @@ public function generate( $package, $base_theme, $global_library, + $libraries, $regions, $breakpoints ) { @@ -81,6 +82,7 @@ public function generate( 'package' => $package, 'base_theme' => $base_theme, 'global_library' => $global_library, + 'libraries' => $libraries, 'regions' => $regions, 'breakpoints' => $breakpoints, ]; @@ -97,6 +99,14 @@ public function generate( $parameters ); + if ($libraries) { + $this->renderFile( + 'theme/libraries.yml.twig', + $dir . '/' . $machine_name . '.libraries.yml', + $parameters + ); + } + if ($breakpoints) { $this->renderFile( 'theme/breakpoints.yml.twig', diff --git a/templates/theme/info.yml.twig b/templates/theme/info.yml.twig index e711e06cf..f68e9fb7e 100644 --- a/templates/theme/info.yml.twig +++ b/templates/theme/info.yml.twig @@ -5,7 +5,11 @@ package: {{ package }} core: {{ core }} libraries: - {{ machine_name }}/{{ global_library }} - +{% if libraries %} +{% for library in libraries %} + - {{ machine_name }}/{{ library.library_name }} +{% endfor %} +{% endif %} base theme: {{ base_theme }} {% if base_theme == 'classy' %} #Using Classy as a base theme https://www.drupal.org/theme-guide/8/classy diff --git a/templates/theme/libraries.yml.twig b/templates/theme/libraries.yml.twig new file mode 100644 index 000000000..8d2379ea6 --- /dev/null +++ b/templates/theme/libraries.yml.twig @@ -0,0 +1,16 @@ +{{ global_library }}: + version: 1.0 + css: + theme: + #css/your_style_sheet.css : {} + js: + #js/your_js.js : {} +{% for library in libraries %} +{{ library.library_name }}: + version: {{ library.library_version }} + css: + theme: + #js/your_js.js : {} + js: + #js/your_js.js : {} +{% endfor %} \ No newline at end of file