Tutorial on Using Bash Script for Magento 2 Themes Management

bash script for magento 2 theme

Each Magento developer certainly came up against the situation when he/she wanted to install or change something on Magento and it took a lot of time and efforts. In some cases, he/ she was forced to reproduce the whole process from very beginning. And our developers are not exception. When one of them was creating a custom theme for Magento 2 website, he wanted to simplify somehow this process. He decided to develop a Bash script that will streamline the process. We want to share the information about his work with you and hope that our tutorial on script implementation will help you to cope with all possible challenges: 

Script implementation process

Firstly, you need to download the zip or copy it from Github repository into your Magento 2 project root:

git clone git@github.com:dvrgoc/magento2-theme-manager.git

After that, you have to open terminal from the root of your Magento 2 project and run this command in order to start the script:

bash magento2-theme-manager.sh

So, when the script is running, you can enter one of these numbers:

Magento 2 bash script

Create a new theme

When a user has already selected one time, he/she will be suggested to type in the next order:

  • Vendor name;
  • Theme name;
  • Theme title (you will see it in Admin);
  • Local setting;
  • User’s password - to set the owner and group of the created files.

bash script for magento 2 theme

The script will generate a small LESS file (sam-ecommerce.less) that will be put together into CSS file during static files will be being deployed. Below, you can see its content:

@sam-ecommerce-green: #79AD36;

body {

   background-color: @sam-ecommerce-green !important;

}

Now, it is time to open Grunts theme.js file and insert the code presented below:

bash script for Magento 2 theme management

Please, do not forget to open Admin Dashboard and check if the theme is visible under Content -> Themes.

magento 2 admin pannel

And when you know that everything is ok, it is time to run the next commands. They will allow you to make sure that your theme is compilable by Grunt and deployable by Magento:

grunt clean grunt exec:ParentTheme php bin/magento cache:clean php bin/magento setup:static-content:deploy

Below, there is an output of: 

‘grunt exec’ command

bash script implementation Magento 2

 static content deployment

Magento 2 bash script implementation

Then, you can find the compiled sam-ecommerce.css file in app/design/frontend/YourVendor/YourTheme/YourLocale/css folder:

body{background-color:#79ad36 !important}

Create a child theme

In order to do it, user has to enter valid MySql credentials. 

magento 2 bash script option 2

A user will be suggested to enter a number of the theme that will become parent theme to his/her theme. Our developer entered number 24 and the process of creating a theme was the same as creating a new theme.

 

bash script magento 2 admin

Delete a theme

As with child theme creation, user needs to enter valid MySql credentials, then the script will connect to database. And after, all of the registered themes will be listed. You have to delete all child themes! And if you do not do that, you will see an error.

magento 2 theme bash script

To sum up, I would like just say that we hope our work experience will help you to prepare your theme for deployment. If you have some additional questions, our team of Magento developers is always ready to help you! Contact us right away, and we will provide you with all information you need! Read more about Magento 2 here:

Comments (0):
Leave a comment:
*Your comment will be published after approval by site administrator.