hechoendrupal / drupal-console

The Drupal CLI. A tool to generate boilerplate code, interact with and debug Drupal.
http://drupalconsole.com
GNU General Public License v2.0
939 stars 559 forks source link

Use chains to replace site:mode command #3541

Open marcelovani opened 7 years ago

marcelovani commented 7 years ago

Issue title

[site:mode] Use chains to replace site:mode command

Problem/Motivation

Since chains are getting more powerful, I figured we could replace the code found in https://github.com/hechoendrupal/drupal-console/blob/master/src/Command/Site/ModeCommand.php with a chain

Details to include: We can simply have less code to maintain and add more flexibility to users

Solution

This is how the site:mode:dev chain would look

command:
  name: site:mode:dev
  description: 'Switch system performance configuration'
commands:
  # system.performance
  - command: config:override
    arguments:
      name: system.performance
        - cache.page.use_internal: false
        - css.preprocess: false
        - css.gzip: false
        - js.preprocess: false
        - js.gzip: false
        - response.gzip: false
  # views.settings
  - command: config:override
    arguments:
      name: views.settings
        - ui.show.sql_query.enabled: true
        - ui.show.performance_statistics: true
  # system.logging
  - command: config:override
    arguments:
      name: system.logging
        - error_level: all

The prod chain would be the inverse of the chain above

This chain should appear only when there is a site installed, that would automatically happen when we implement https://github.com/hechoendrupal/drupal-console-core/issues/240

marcelovani commented 6 years ago

Another proof of concept: Use twig + a new command to override services.

File: chain-site-mode.yml

# How to use
# site:mode --mode="dev"
command:
  name: site:mode
  description: 'Changes the site mode'
vars:
  mode:
    - dev
    - prod
commands:
  # System Performance
  {% set keys = ['cache.page.use_internal', 'css.preprocess', 'css.gzip', 'js.preprocess', 'js.gzip', 'response.gzip'] %}
  {% for key in keys %}
  - command: config:override
    arguments:
      name: system.performance
      key: {{ key }}
      value: {% if mode == "dev" %} false {% else %} true {% endif %}
  {% endfor %}

  # Views settings
  {% set keys = ['ui.show.sql_query.enabled', 'ui.show.performance_statistics'] %}
  {% for key in keys %}
  - command: config:override
    arguments:
      name: views.settings
      key: {{ key }}
      value: {% if mode == "dev" %} false {% else %} true {% endif %}
  {% endfor %}

  # System Logging
  - command: config:override
    arguments:
      name: system.logging
      key: error_level
      value: {% if mode == "dev" %} all {% else %} none {% endif %}

  # Services http.response.debug_cacheability_headers
  - command: service:override
    arguments:
      name: http.response.debug_cacheability_headers
      key: error_level
      value: {% if mode == "dev" %} true {% else %} false {% endif %}

  # Services Twig config
  {% set keys = ['debug', 'auto_reload', 'cache'] %}
  {% for key in keys %}
  - command: service:override
    arguments:
      name: twig.config
      key: {{ key }}
      value: {% if mode == "dev" %} true {% else %} false {% endif %}
  {% endfor %}

The if/else is not working, I think I am using an invalid syntax

jmolivas commented 6 years ago

@marcelovani could you try using as :

{{ mode === 'dev' }}

or

{{ mode === 'dev' ?? false }}
marcelovani commented 6 years ago

Ok, try this https://github.com/hechoendrupal/drupal-console-core/pull/306/files with https://github.com/hechoendrupal/drupal-console/pull/3771/files

It works fine but the chain looks cluttered with all the twig stuff, I think its neater if we do the chain as per #3540

Testing: Install a drupal site and run drupal site:mode --mode="dev" drupal site:mode --mode="prod"

Todo: