Skip to content

Commit

Permalink
Add option to generate libraries for generate:theme command. (#3309)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel303 authored and jmolivas committed May 26, 2017
1 parent d67c75c commit 390763e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/Command/Generate/ThemeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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');

Expand All @@ -201,6 +208,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$package,
$base_theme,
$global_library,
$libraries,
$regions,
$breakpoints
);
Expand Down Expand Up @@ -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) {
Expand Down
39 changes: 39 additions & 0 deletions src/Command/Shared/ThemeRegionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 10 additions & 0 deletions src/Generator/ThemeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function generate(
$package,
$base_theme,
$global_library,
$libraries,
$regions,
$breakpoints
) {
Expand Down Expand Up @@ -81,6 +82,7 @@ public function generate(
'package' => $package,
'base_theme' => $base_theme,
'global_library' => $global_library,
'libraries' => $libraries,
'regions' => $regions,
'breakpoints' => $breakpoints,
];
Expand All @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion templates/theme/info.yml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions templates/theme/libraries.yml.twig
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit 390763e

Please sign in to comment.