Friday, February 14, 2014

Command Line Tools in PhpStorm

Here's a quick tip for creating your own tools in PhpStorm to run command line tools.

To run command line tools in PhpStorm you can either:

Bring up the Terminal with View->Tool Window->Terminal, and run your command line tools from there like normal

Use the Command Line Tools Console with Tools->Run Command

A third option is to make custom commands and run them from the Tools menu.


To illustrate, I'm going to make a tool to create a new Laravel Migration using Artisan

First, open the settings dialog: File->Settings

Search for External Tools

Click the + button to add a new tool.

Give the tool a name, group and description.

For the Program, choose $PhpExecutable$ from the Insert Macro list.

For the Parameters, use the project directory, \artisan, the artisan command, and then I'll use the $Prompt$ macro to get some user input. (In this case, the name of the migration.)

$ProjectFileDir$\artisan migrate:make $Prompt$

Ok and close the settings.


Now, to run the command line tool, go to Tools->[Tool Group]->[Tool Name]

In this case, you'll be prompted to add some text which will be the name of the migration.

The Command Line Tools Console opens and executes the command.


For another example, I use the Laravel Generators by Jeffrey Way to create Laravel components.

Because the commands can get a bit long, I type out the commands in a text file first and then copy and paste into the command window.

This way it's easier to fix if I make mistakes.

Now, I can make a tool to simplify some of the repetitive tasks.

So, if I have a command that looks something like this:

php artisan generate:resource comment --fields="title:string, body:string, user_id:integer, article_id:integer, published_at:timestamp"

First, I'll create another tool that simply runs php artisan and then uses the selected text as a parameter.

Now, I still create my commands in a text file, but instead of copy and pasting, I simply select the command and run the tool.

You can experiment on how to create your own tools. Use a prompt, separate the artisan commands into separate tools, or if you run the same command often, turn the whole command into a tool.


3 comments:

  1. Thanks, this is helpful for beginners!

    ReplyDelete
    Replies
    1. Hope it works. I've just been using PhpStorm for about 2 weeks (still on the trial actually) and I'm just jotting stuff down as I find them.

      Will definitely be purchasing when my trial expires.

      Delete