Using Magerun modules in your projects

The purpose of this article

The purpose of this article is to describe what Magerun scripts are and how to create Magerun scripts that are specific to your project.

About Magerun scripts

Magerun is an extremely useful set of Command Line Interface tools that allows you to quickly perform tasks on your Magento projects – like cache flushing, configuration setting and etc. I have briefly mentioned some of them in the Magerun article. Magerun scripts allow you to automate these commands into script files that are located in script repositories. One very useful implementation of Magerun scripts is to automatically prepare your environment by setting all configuration needed for your environment with a single command.

For example; running this command:

$ n98-magerun2 script:repo:run dalema

… will product this output:

$ n98-magerun2 script:repo:run dalema

 Expand source

(The source code for above output can be found in the “Example Magerun script” section below.)

Script repositories

Magerun scripts must be located in one of these places:

  1. /usr/local/share/n98-magerun2/scripts
  2. ~/.n98-magerun2/scripts
  3. As module in your local project

The first two locations are described on the Use n98-magerun Script Repository page. Both locations are specific to your local machine and are well documented. The third option allows you to ensure that scripts are local to your project, but there is no good documentation available on it. The third option (“As module in your local project”) is the basis for the “How to create Magerun scripts local to your project” section below. You can also find a repository description by showing help for the repository command: n98-magerun2 script:repo:list -h.

How to list which scripts are available to you

$ n98-magerun2 script:repo:list

 Expand source

How to create Magerun scripts local to your project

The scripts must reside in a specific folder structure relative to your project root:

Folder tree

lib

  n98-magerun2

    $moduleName

      n98-magerun2.yaml

      scripts

        $scriptName.magerun

Remember to replace $moduleName with your module’s name and $scriptName with your script’s name. In this article I have been using the name “dalema”. Also notice the .yaml file. It is required and must have the following content:

$ cat lib/n98-magerun2/modules/dalema/n98-magerun2.yaml

1

2

3

script:

  folders:

    – %module%/scripts

Now that you have placed your files in a location local to your project, you can go ahead and commit them to your project’s repository. You can see an example in this repository: project_dalema.

Example Magerun script

$ cat ~/.n98-magerun2/scripts/dalema.magerun

 Expand source

Notice that I automatically set value for a lot of configuration options. This will allow me to pull a Magento database locally and then simply run this script once to completely prepare my local environment. In another article I will describe how to use scripts like this as part of your normal site deployment using Aja hooks. Once you have done that it is exceedingly easy to synchronize databases between environments using Server Portal.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s