Skip to content

Register annotated commands in Drupal Console. - #2081

Closed
greg-1-anderson wants to merge 8 commits into
hechoendrupal:masterfrom
greg-1-anderson:annotation-command
Closed

greg-1-anderson wants to merge 8 commits into
hechoendrupal:masterfrom
greg-1-anderson:annotation-command

Conversation

@greg-1-anderson

Copy link
Copy Markdown
Contributor

This PR adds annotation command support to DrupalConsole.

Any command file FooCommand.php may now contain either a DrupalConsole Symfony Console Command implementation, in which case it works as it always has, or, alternately, if the class FooCommand does not extend Drupal\Console\Command\Command, then it is presumed that the instance is an annotation command, and all of its public methods are registered as DrupalConsole commands.

The purpose of this is to allow Drupal modules to implement simple, loosely-coupled commandline tool interfaces. These commands might be implemented by extending Robo, although this is not required.

I was planning on converting the DrupalConsole InitCommand into an annotation class, simply to demonstrate how that is done; however, since this command is already tightly coupled with Drupal\Console\Command\Command helpers, getModule(), etc., doing this would be counterproductive; in general, it is a non-goal to convert core DrupalConsole commands into annotation commands. For a typical simple Drupal module, though, whose main purpose is to provide a CLI interface that simply calls a method or two in the module, the annotation class will be a win.

Follow development in Drush at drush-ops/drush#2103.

DrupalConsole checklist (to consider / discuss):

… instantiate them via an AnnotationCommandFactory.
@greg-1-anderson

Copy link
Copy Markdown
Contributor Author

Replaced old technique with something better. No longer overloads the "*Command.php" namespace, and instead looks for command files following their own rules.

Untested, but on the right track.

@greg-1-anderson

Copy link
Copy Markdown
Contributor Author

The latest commit (12daaa6) works great with the sample cli command I wrote (ported) for the default_content module. Search paths and naming conventions still need to be discussed and finalized. I am not set on (or even very happy with) the placeholder "CliTools" folder name that I am using now; just picked something quickly to get started.

Does not work with Drush yet, but it will only be a small effort to fix up the PR there as well; I'm not going to do that tonight, but will have it working in short order.

@greg-1-anderson

Copy link
Copy Markdown
Contributor Author

Now also works with Drush.

@greg-1-anderson

Copy link
Copy Markdown
Contributor Author

Discussion on search paths and naming conventions for command files at consolidation/annotated-command#12

@greg-1-anderson

Copy link
Copy Markdown
Contributor Author

Just for fun, I added a generate:meme example command. We can take this out prior to merge if desired; in the interim, it is useful to have a test command bundled with the PR.

Uses Impact font on a Mac. If running on some other platform, download Anton.ttf from https://googlefontdirectory.googlecode.com/hg/ofl/anton/ and place Anton.ttf in ~/.fonts.

@greg-1-anderson greg-1-anderson changed the title Register annotation commands. Register annotation commands in Drupal Console. Apr 11, 2016
@greg-1-anderson greg-1-anderson changed the title Register annotation commands in Drupal Console. Register annotated commands in Drupal Console. Apr 12, 2016
@jmolivas

Copy link
Copy Markdown
Member

@greg-1-anderson Nice usage of annotations, we have been discussing using them before as you can see on issue Use annotations in Commands and the related PR Annotations in Drupal Commands.

Our proposal for using annotation was for other purpose. We wanted annotations to define any behavior not related to Symfony/Console Command and make our commands as much as close to the official standard. Creating a basic Command - Symfony documentation

As stated on issue definition

The Drupal commands interact with more stuffs that the commands in Symfony, in Drupal, one command can be have many dependencies with other modules or themes or any other scope in your installation.

This approach is to use Annotations to provide metadata on each command to specify dependencies or check another variables in Drupal or any other stuff that interacts with the console.

For example:

/**
 * @DrupalCommand (
 *      dependencies = {
 *          "simpletest"
 *      },
 * )
 */
class TestDebugCommand extends ContainerAwareCommand { ... }

With that said, after discussing with the other project maintainers we don’t agree with creating a new Drupalisim or a new Drupal way to write Symfony/Console based Commands when there is a well accepted and defined standard provided and supported by the community.

We really like the idea about decouple code create components to reuse between different Drupal CLI tools.

@greg-1-anderson

Copy link
Copy Markdown
Contributor Author

I have an idea on how we could configure Symfony/Console commands without diverging from the Symfony API. Re-opening so I can push another commit here to demonstrate.

* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class MemeCommand extends Command

@greg-1-anderson greg-1-anderson Apr 16, 2016 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commands extend Drupal\Console\Command\Command, just as always. These are Symfony commands.

@greg-1-anderson

Copy link
Copy Markdown
Contributor Author

As you can see from the diffs in this PR, the code now conforms to the Symfony/Console command API exactly, with annotations being used only as an optional configuration method. Having this in place also gets us one step closer to being able to use annotations to specify dependencies, as referenced in the comments above.

The importance of this PR extends beyond the simple application of annotations for configuration. Using a common base class from Consolidation-org will allow us to do things such as share the output formatters library between Drush and Drupal Console. Drupal Console has a similar feature, but the consolidation output formatters are more complete, and using a common implementation will allow us to present a uniform interface for Drupal modules to use.

I hope that you will find this new implementation more palatable than the previous one. Using annotations in Drupal Console core is not required; accepting this PR would allow module developers to use or not use them as they saw fit.

@jmolivas

Copy link
Copy Markdown
Member

Looking at this, thanks for the update @greg-1-anderson

@jmolivas

Copy link
Copy Markdown
Member

Support for annotations merged on PR #2642

@jmolivas jmolivas closed this Aug 30, 2016
@jmolivas jmolivas modified the milestone: nice-to-have Dec 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants