230 likes | 489 Views
San Francisco Drupal Users Group. DRUSH A Presentation on Recent Changes by Greg Anderson. 10 February 2014. Drush Installation. Recommended method is now Composer. Benefits of Composer. Manages and installs dependencies Drupal 8 is using Composer Comes with PSR-0 / PSR-4 autoloader.
E N D
San Francisco Drupal Users Group DRUSH A Presentation on Recent Changes by Greg Anderson San Francisco Drupal Users Group – Drush 10 February 2014
San Francisco Drupal Users Group – Drush Drush Installation • Recommended method is now Composer
San Francisco Drupal Users Group – Drush Benefits of Composer Manages and installs dependencies Drupal 8 is using Composer Comes with PSR-0 / PSR-4 autoloader
San Francisco Drupal Users Group – Drush PSR-What? PSR-4 PSR-0 Source: http://www.sitepoint.com/battle-autoloaders-psr-0-vs-psr-4/ Note: Drupal currently uses 'lib', but may switch to 'src'
San Francisco Drupal Users Group – Drush Autoloading in Drush Bold text removed with Autoloader function drush_get_class($class_name, $args, $class_dir = NULL) { if (!isset($class_dir)) { $class_dir = DRUSH_BASE_PATH . '/classes'; } $version = drush_drupal_major_version(); drush_include($class_dir, $class_name, $version, 'php'); $versioned_name = $class_name . $version; if (class_exists($versioned_name)) { $reflectionClass = new ReflectionClass($versioned_name); return $reflectionClass->newInstanceArgs($args); } } NOT COMMITTED YET. Issue: https://github.com/drush-ops/drush/pull/88
San Francisco Drupal Users Group – Drush Autoloading with Drush Commands $items['role-list'] = array( 'description' => 'Display a list of all roles.', 'examples' => array( "drush role-list 'anonymous user'" => 'Display all of the permissions assigned to the anonymous user role.' ), 'command-class' => 'Drush\core\RoleCommand', 'aliases' => array('rls'), ); NOT IMPELEMENTED YET. Issue: https://github.com/drush-ops/drush/pull/88 Advantage: Drush can create a RoleCommand7 for Drupal 7, and a RoleCommand8 for Drupal8. The base class can handle logic and argument parsing, and derived classes provide active code.
San Francisco Drupal Users Group – Drush DEMO $ drush @site php $ drush @remote php $ drush ev “return menu_get_object();” Q: Why “return” instead of “print”?
Basics of Drush Output Formats • New Way in Drush 6: function drush_hello_world() { return "Hello World." } • Drush 5 Way: function drush_hello_world() { print "Hello World." } Drush will now print our results for you if you return them as the result of your main command function. A secondary advantage of this is that Drush will return a proper object when called via a remote alias. San Francisco Drupal Users Group – Drush
DEMO $ drush hello The drush command ‘hello’ could not be found. $ drush dl drushify $ drush drushify hello $ drush hello Language Message English Hello world! We will use the drushify code generator to make a quick “Hello World” Drush command San Francisco Drupal Users Group – Drush
Selecting Output Format Most Drush commands now support formatters Commands return data (usually in arrays) Formatters render the data in different ways $ drush status --format=json San Francisco Drupal Users Group – Drush
Declaring Default Formats $items['hello'] = array( … 'outputformat' => array( 'default' => 'table', 'pipe-format' => 'var_export', ), ); The minimum requirements for a command to support the --format option is to declare its default types San Francisco Drupal Users Group – Drush
Declaring Supported Data Types $items['hello'] = array( … 'outputformat' => array( 'default' => 'table', 'pipe-format' => 'var_export', 'output-data-type' => 'format-table', … Commands may also declare a data type; this will exclude incompatible formatters from drush help, etc. San Francisco Drupal Users Group – Drush
San Francisco Drupal Users Group – Drush Available Output Data Types
San Francisco Drupal Users Group – Drush Declare Field Labels $items['hello'] = array( … 'outputformat' => array( 'field-labels' => array( 'mgs' => 'Message', 'lang' => 'Language', ), 'fields-default' => array( 'msg', 'lang'), …
San Francisco Drupal Users Group – Drush DEMO $ drush hello --fields=msg $ drush hello --fields=lang $ drush hello --fields=Message,Language
Create a Scratch Drupal Site … and, while we're waiting for that to run... $ drush qd d7 og cck devel --site-name=SFDUG $ drush @remote uli admin admin/index San Francisco Drupal Users Group – Drush
Drush Command Hooks $ drush status --show-invoke 2>&1 | grep hello drush_hello_core_status_pre_validate drush_hello_core_status_validate drush_hello_pre_core_status drush_hello_core_status drush_hello_post_core_status Note: The output from --show-invoke is sent to STDERR, just like the Drush log and error output; we redirect STDERR to STDOUT to use grep. San Francisco Drupal Users Group – Drush
San Francisco Drupal Users Group – Drush Example Post-Sync Hook function drush_sync_enable_post_sql_sync($src, $dst) { $modules = drush_get_option_list('enable'); if (!empty($modules)) { drush_log(dt("Enable !modules post-sql-sync", array('!modules' => implode(',', $modules))), 'ok'); drush_invoke_process($dst, 'en', $modules, array('yes' => TRUE)); } // … processing continues below
Using Sync-Enable Hook Configure your site alias: $aliases["dev"] = array ( 'root' => '/path/to/drupalroot', 'uri' => 'http://dev.site.org', 'target-command-specific' => array ( 'sql-sync' => array ( 'enable' => array ('devel', 'stage_file_proxy' ), 'permission' => array ( 'authenticated user' => array ( 'add' => array ('access devel information',), ), Copy the example commandfile: $ cp examples/sync_enable.drush.inc ~/.drush San Francisco Drupal Users Group – Drush
Use settings.php to modify variables $conf['error_level'] = 2; error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); $conf['preprocess_css'] = 0; $conf['cache'] = 0; It is frequently suggested that the sync_enable hook should support variable_set() and variable_delete(); however, this is not necessary, as $conf statements in settings.php can be used to the same effect. San Francisco Drupal Users Group – Drush
San Francisco Drupal Users Group – Drush Sanitize Hook to Clear CCK Fields function MYMODULE_drush_sql_sync_sanitize($site) { $fields_to_sanitize = array( 'field_baby_pictures_url' => 'http://bit.ly/1daFGUm', 'field_secret_missile_launch_codes' => '1234', ); foreach ($fields_to_sanitize as $field_name => $field_value) { $sql = "UPDATE field_data_{$field_name} SET {$field_name}_value = '$field_value' where {$field_name}_value != ''; "; $sql .= "UPDATE field_revision_{$field_name} SET {$field_name}_value = '$field_value' where {$field_name}_value != '';"; drush_sql_register_post_sync_op('sanitize ' . $field_name, dt("Set all data in !field to '!value'", array('!field' => $field_name, '!value' => $field_value)), $sql); } }
San Francisco Drupal Users Group – Drush Spur of the Moment Demos $ drush topic # bastion examples/example.bashrc # cd @site:%files $ drush @remote status-report $ drush pm-updatestatus $ drush pm-update --lock=module-to-skip
San Francisco Drupal Users Group – Drush Drush Resources Project Page: • https://github.com/drush-ops/drush Support: • http://drupal.stackexchange.com/questions/tagged/drush Drush Site: • http://drush.org Drush Chapter of DGD7 (Free Download): • http://dgd7.org/book/26-drush