The attached patch demonstrates a modern cli command that will work with future versions of Drush and Drupal Console. Works today with patched versions of both Drupal Console and Drush. Note that this command does not need to directly reference any Drush or Drupal Console APIs.
- Drupal Console PR: "Register annotation commands"
- Drush PR: "Use Symfony Console application and annotation commands in Drush"
The roadmap / plan is described in the Drush PR. The general idea is to offer a very simple way to provide a cli interface for module commandline tools that supports loosely-coupled code (does not require a lot of cli-framework-specific code) that works in both Drush and Drupal Console.
The interfaces and search paths are not final yet, and are likely to change. It would be best to keep this PR open until such a time as the Drush and Drupal Console PRs are merged. I will update the patch here as necessary to keep up with changes in the tools.
Comment | File | Size | Author |
---|---|---|---|
#11 | default_content-2703131-11.patch | 12.34 KB | greg.1.anderson |
Comments
Comment #2
jibranThank you @greg.1.anderson nice work with Drush, DrupalConsole, Robo and AnnotationCommand. I'm happy to keep this open and once we are done we can remove the existing drush code form the repo. We can also write some unit test for this class as well. ;-)
Comment #3
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedComment #4
andypostthis argument optional, we used to allow export all entities when no second argument
Comment #5
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedJust a note that the Drupal Console team did not like the new proposal. I am working on improvements that will hopefully be more palatable to all. I'll post an update here when ready.
Comment #6
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedWe have made some progress, and I have three more patches to share.
The first is an example of annotated command handlers. We still have multiple annotated commands in a single class file; the big difference is that we are now using tagged services to declare where our command handler classes are. The advantage of using tagged services is that now we can use dependency injection to get references to our services, so that we can avoid using the DI container from within our module implementation. We could also inject \Drupal::entityQuery() here -- I just didn't take the time to do that. This should be done before finishing up here.
Drupal Console did not accept the PR for this, so this patch is just an example of what is possible with annotations. This does work with Drush, using as-of-yet unmerged PRs for both the 8.x and master (9.x) branches.
Comment #7
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedThe next version extends AnnotatedCommand rather than using a command handler class. We are still using tagged service discovery. Note that these *are* Symfony Console commands; we are just using annotations to configure them.
Comment #8
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedThe final patch shows a straight Symfony Console command. This is also using tagged services for command discovery. This implementation should work equally well in both Drush and Drupal Console once the respective PRs land in each project.
Comment #9
dawehnerNice work!
Comment #10
andypostLooks cool!
Just needs a bit of code styling and drush support merged
Comment #11
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedHere's an update with a couple of minor issues fixed, and a demonstration of using a logger in the default-content-export-references command.
Comment #12
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedAs a follow-up / update, the Drush and Drupal Console maintainers agreed that something akin to the straight Symfony Console command shown in #8 should be supported in Drush and Drupal Console. Both #8 and #11 are already supported in the latest dev branches for Drush 8 and Drush 9, although we still need to do a tagged release. I also need to review with the latest dev of Drupal Console and see what still needs to be supported there, and submit a PR or two there if needed.
I find the use of a PSR-3 logger to be a very useful to provide a uniform interface to bridge code used by both cli and web clients, but there is still some controversy here, e.g. in the example in #11, while you may want to see progress each time through the loop in the cli tool, you probably don't want that much information in, say, your watchdog log, so the semantic meaning of the PSR-3 interface is not uniform between these two clients. While this is resolvable by selectively providing a PSR-3 adapter object as needed, this particular use is not as clean or unambiguous as one might like, so I don't know that we will be recommending #11 universally.
Comment #13
larowlanNote #2758847: Drupal Console commands - can we collaborate towards the best solution?
Comment #14
avibrazil@gmail.com CreditAttribution: avibrazil@gmail.com commentedI can see there are 2 work streams to integrate this with Drupal Console. Which one is the best and most current and official? Which version of code of each component should I use? The -dev? The Git? The attached patches? Against which version of what ?
I just want to export and import content between sites:
site1> drupal export:.... > artifacts.yml
site2> drupal import:.... < artifacts.yml
Thank you
Comment #15
greg.1.anderson CreditAttribution: greg.1.anderson at Pantheon commentedThe patch here works with Drush 8 and Drush master. The patch at #2758847 works with Drupal Console. We are still working out a strategy to unify these approaches.
Comment #16
andypostBtw drush 8.1.3 released and new supports annotated commands
So probably current patch should be updated?!
I see no reason to register commands in container if it possible to discover them with annotations
PS: Related to console integration #2786795: DrupalConsole integration breaks Drush looks console has the same issues
Any reason to keep this in container all the time?
not needed
there's no annotation?
Comment #17
badjava CreditAttribution: badjava at Metasun for Pfizer, Inc. commentedSorry, didn't see this issue. I posted a new patch on #2912723: Add Drush 9 support.
Comment #18
andypostThe leftover is console command issue #2758847: Drupal Console commands
Drush 9 landed in Alpha 7