Sunday, February 8, 2015

Generate Magento Module automatically generator

This script will generate Magento module automatically.

#!/bin/sh
magentoRoot="/www/magento1.9.1"
magentoModuleDir="app/code/local"

nameSpace="Mycompany"
nameSpaceLower=`echo ${nameSpace} | tr '[A-Z]' '[a-z]'`

magentoLayoutRoot="${magentoRoot}/app/design/frontend/${nameSpaceLower}/default/layout"

moduleName="Blockgen"
moduleNameLower=`echo ${moduleName} | tr '[A-Z]' '[a-z]'`

moduleDir=${magentoRoot}/${magentoModuleDir}/${nameSpace}/${moduleName}
dirNameArr="Block Helper Model controllers etc sql"

xmlModuleStr="<?xml version=\"1.0\"?>\n<config>\n    <modules>\n        <${nameSpace}_${moduleName}>\n            <active>true</active>\n            <codePool>local</codePool>\n        </${nameSpace}_${moduleName}>\n    </modules>\n</config>\n"

xmlConfigStr="<?xml version=\"1.0\"?>\n<config>\n    <modules>\n        <${nameSpace}_${moduleName}>\n            <version>0.1.0</version>\n        </${nameSpace}_${moduleName}>\n    </modules>\n    <frontend>\n        <routers>\n            <${moduleNameLower}>\n                <use>standard</use>\n                <args>\n                    <module>${nameSpace}_${moduleName}</module>\n                    <frontName>${moduleNameLower}</frontName>\n                </args>\n            </${moduleNameLower}>\n        </routers>\n        <layout>\n            <updates>\n                <${moduleNameLower}>\n                    <file>${nameSpaceLower}/${moduleNameLower}.xml</file>\n                </${moduleNameLower}>\n            </updates>\n        </layout>\n    </frontend>\n    <global>\n        <blocks>\n            <${moduleNameLower}>\n                <class>${nameSpace}_${moduleName}_Block</class>\n            </${moduleNameLower}>\n        </blocks>\n    </global>\n</config>\n"

xmlLayoutStr="<?xml version=\"1.0\"?>\n<layout version=\"0.1.0\">\n    <${moduleNameLower}_index_index>\n        <reference name=\"root\">\n          <action method=\"setTemplate\"><template>page/1column.phtml</template></action>\n        </reference>\n        <reference name=\"content\">\n          <block type=\"blockgen/home\" name=\"home\" template=\"${nameSpaceLower}/${moduleNameLower}/home.phtml\" />\n        </reference>\n    </${moduleNameLower}_index_index>\n\n    <root>\n      <remove name=\"currency\" />\n    </root>\n</layout>\n"

moduleSourceCodeStr="<?php\n/*\n * URL: http://yoursite.com/index.php/frontendName/actionControllerName/actionMethodName\n * Ex: http://yoursite.com/index.php/${moduleNameLower}/index/index\n * Ex: http://yoursite.com/index.php/${moduleNameLower}/index/json\n * Ex: http://yoursite.com/${moduleNameLower}/index/index\n * Ex: http://yoursite.com/${moduleNameLower}/index/json\n */\nclass ${nameSpace}_${moduleName}_IndexController extends Mage_Core_Controller_Front_Action\n{\n  public function indexAction()\n  {\n    \$this->loadLayout();\n    \$this->renderLayout();\n  }\n\n  public function jsonAction()\n  {\n    echo 'Hello World JSON';\n  }\n}\n"

blockSourceCodeStr="<?php\nclass ${nameSpace}_Blockgen_Block_Home extends Mage_Core_Block_Template\n{\n  public function sayHello()\n  {\n    return 'Hello World!';\n  }\n}\n"

for dn in $dirNameArr; do
  if [ ! -d ${moduleDir}/${dn} ]; then
    mkdir -p ${moduleDir}/${dn}
  fi
done

echo -e "${xmlModuleStr}" > ${magentoRoot}/app/etc/modules/${nameSpace}_${moduleName}.xml
echo -e "${xmlConfigStr}" > ${moduleDir}/etc/config.xml

if [ ! -d ${magentoLayoutRoot}/${nameSpaceLower} ]; then
  mkdir -p ${magentoLayoutRoot}/${nameSpaceLower}
fi

echo -e "${xmlLayoutStr}" > ${magentoLayoutRoot}/${nameSpaceLower}/${moduleNameLower}.xml
echo -e "${moduleSourceCodeStr}" > ${moduleDir}/controllers/IndexController.php
echo -e "${blockSourceCodeStr}" > ${moduleDir}/Block/Home.php

if [ ! -d ${magentoRoot}/app/design/frontend/${nameSpaceLower}/default/template/${nameSpaceLower}/blockgen/ ]; then
  mkdir -p ${magentoRoot}/app/design/frontend/${nameSpaceLower}/default/template/${nameSpaceLower}/blockgen/
fi

echo -e "<?php echo \$this->sayHello() ?>" > ${magentoRoot}/app/design/frontend/${nameSpaceLower}/default/template/${nameSpaceLower}/blockgen/home.phtml

No comments: