/Mage2Gen

Python library for generating Magento 2 module

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

image

Mage2Gen

Mage2Gen is a python library for generating Magento 2 modules. It is build to be extendable with snippets for creating more complex Magento 2 modules based on simple input.

Installation

To install the Python library and the command line utility, run:

sudo pip3 install mage2gen

Interactive command line

With the mage2gen command line tool you can interactively create and generate Magento 2 modules. If you have installed mage2gen for your whole system you can start it by running mage2gen. You will be asked to give the module a package name, name and description:

Help

You can use help or ? to show all available commands and help <command> to show command specific help descriptions:

List snippets

With the list command you get a list of all the available snippets you can add to your module:

Add snippet

To add a snippet you can use the add <snippet name> command, you can auto-complete a snippet name with TAB:

Show added snippets

When you have added multiple snippets and you want to see which snippets are added to the module you can use the info command to show an overview:

Remove snippet

When you want to remove an added snippet you can use the remove <snippet name> <index> command, to remove the snippet from the module:

Generate module

When you are ready with your module and added the snippets you want to use, you can generate the module with the generate command. If you are inside a Magento 2 project directory, it will select the default path for the module:

Example usage library

Snippets

Mage2Gen has core classes for creating and merging PHP classes, XML files and static files. For generating a module you don't want to define your PHP class or XML file for basic module concepts like observers, plugins or controllers. This is where snippets come in, which add these concepts based on simple input. The currently supported snippets are listed below. If you would like to add a snippet to Mage2Gen, simply fork this project. Add your snippet or other improvements and create a pull request afterwards.

Controller

Creates a controller with block, layout.xml and template. Can create a controller for frontend and adminhtml.

Params:

  • (str) frontname: frontame route for module
  • (str) section: subfolder in module/Controller
  • (str) action: action class
  • (bool) adminhtml [False]: if controller is used for adminhtml

Example:

Plugin

Creates a plugin for a public method, link to Magento 2 docs

Params:

  • (str) classname: full class namespace of class with method
  • (str) methodname: method name of class
  • (str) plugintype: type for plugin (before, after or around)
  • (bool) sortorder [10]: the order the plugin is executed in respect to other plugins.
  • (bool) disabled [False]: disable a plugin

Example:

Observer

Create an observer for an event

Params:

  • (str) event: event name
  • (int) scope [ObserverSnippet.SCOPE_ALL]: handle observer for all (SCOPE_ALL), frontend (SCOPE_FRONTEND) or backend (SCOPE_ADMINHTML)

Example:

Create a Snippet

You can create your own snippets. If you would like to add a snippet to Mage2Gen, simply fork this project. Add you snippet or other improvements and create a pull request afterwards. You can read this blog post for an how to guide on creating a snippet.

Base snippet

Adding a PHP class

TODO

Adding XML file

TODO

Adding Static file

TODO

TODO

  • Increase test coverage.
  • Adding more snippets:
    • Model attributes
    • Custom models with adminhtml grid
    • Adding fields to checkout process

Example implementation:

  • Interactive command line
  • Mage2gen Online Magento 2 Module Creator mage2gen