Author
Limesharp

Share

Author
Limesharp

One of our developers, Mitch, has created a quick and simple way of creating a new module when working with Magento 2 using bash scripts.

If you find you need to create a new module, it can be a nuisance to have to go through the process of creating the same files and directory structure over and over again.

Below is a bash script which will create the directory structure, plus the registration.php and module.xml. It will also enable the module (bin/magento module:enable YourName_NameOfModule).


#!/bin/bash 
MODULENAME=$1
PWD=|pwd
cd $PWD/app/code/
mkdir -p YOUR_NAME/$MODULENAME
cd YOUR_NAME/$MODULENAME 

#Create the registration file
touch registration.php
echo -e "<?php\r
\Magento\Framework\Component\ComponentRegistrar::register(\r
\t\Magento\Framework\Component\ComponentRegistrar::MODULE,\r
\t'Limesharp_"$MODULENAME"',\r
    __DIR__\r
);" >> registration.php
 
#Create the module.xml file
mkdir -p etc/
cd etc/
touch module.xml
echo -e "<?xml version='1.0'?>\r
\r
\t<module name='YourName_"$MODULENAME"' setup_version='1.0.0' />\r
</config>" >> module.xml
 
# Enable the new module
cd ../../../../../bin/
php magento module:enable Limesharp_$MODULENAME


Save the above file as 'create-module.sh' and place it in your Sites/ directory.

In your ~/.bashrc file add:

alias magenewmodule=‘bash /Users/YOURCOMPUTERNAME/Sites/create-module.sh $1’

Now, to create a new module, all you have to do is run the below from your site route:

magenewmodule NameOfModule