Skip to content

Instantly share code, notes, and snippets.

@weitzman
Last active September 29, 2017 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weitzman/75a94e4f39c52937ed8790204af0fc6a to your computer and use it in GitHub Desktop.
Save weitzman/75a94e4f39c52937ed8790204af0fc6a to your computer and use it in GitHub Desktop.
Porting Commands to Drush9

Drush 9 features a deep rewrite of our app, both user facing and internals. We created and open sourced AnnotatedCommand, OutputFormatters, and Config. We leveraged Symfony Console for our CLI fundmentals. For details on Drush9, see the video or slides from our Drupalcon Vienna presentation.

** ADD AN ANNOTATED COMMAND EXAMPLE **

Unfortunately, old commandfiles such as example.drush.inc no longer load in Drush 9. We've made it relatively painless to port this code to Drush 9:

** ADD A GIF OF GENERATOR **

  1. Using Drush 9 on a working site, run drush generate drush-command-file. generate is a wrapper for the Drupal Code Generator library.
  2. You will be prompted for 2 pieces of information:
    1. Module name: example
    2. Absolute path to legacy Drush command file: /path/to/example.drush.inc
  3. Drush writes 2 files to the Example module:
    1. drush.services.yml is ready to go. No edits are needed unless you want to inject Drupal dependencies into your class (e.g. yml, class.
    2. ExampleCommands.php: Each item in example_drush_command() in your old commandfile has been transformed into an Annotated method in a new ExampleCommands file. Copy the body of your command callback functions into the corresponding method in ExampleCommands. Then modernize the code as below. Compare the Drush9 commands versus the Drush8 commands for guidance.
      1. Replace drush_log() with $this->logger()->info() or $this->logger()->warning() or similar.
      2. Replace drush_set_error() with throw new \Exception()
      3. Replace drush_print() with $this->output()->writeln()
      4. Replace drush_sitealias_get_record() as with $this->siteAliasManager()->getSelf(). From there you can call getRoot(), getUri(), legacyRecord(), and other handy methods. In order for this to work, edit your class to implement SiteAliasManagerAwareInterface (LoginCommands is an example).
      5. Optional - move user interaction to a @hook interact. Also, note the new $this-io()->confirm() and $this->io()->ask() methods.
  4. Run drush cr to add your commandfile to the Drupal container.
  5. Congrats - your commands are now runnable in Drush9!
    1. Please post a patch if Example is a Contrib module.
    2. Please leave example.drush.inc in your module so that Drush 8 users may still use it.

I'm in #drush on IRC and Slack in case anyone wants some help with porting.

Note that drush generate knows how to generate controllers, plugins, config, services, libraries, etc. Here is its help:

** ADD IMAGE OF GENERATE HELP **

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment