Skip to content

julioacontreras/model2code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Model2Code

Tool to convert model to code.

  • Design more and program less: Most projects have a lot of similar code. This tool allows you to create templates and models to generate code. Increasing productivity and reducing human error.

  • Easy to migrate from another technology: Once created templates and model, is possible change the technology by changing the interpreter.

  • Flexible: If you can choose which parts want generic or not.

Dependencies

  • Python 2.7.15

Generate code

interpreter/$python main.py myproject.json ../input/example interpreterVue:vue.json,interpreterPG:pg.json

python main.py [projectJSON] [modelPath] [interpreters]

projectJSON: Project filename in format JSON.

modelPath: Project path than containg the model files.

interpreters: Interpreters you want to use.

How works

This is the architecture divided into inputs, interpreters and outputs.

  • Inputs: All the information that the interpreter needs to generate code containing: template, template and config.

  • Interpreters: Are set of actions generate code.

  • Outputs: And all the code generated by the interpreters.

+------------+   +--------------+   +--------------+
| INPUTS     +-->+ INTERPRETERS +-->+ OUTPUTS      |
|            |   |              |   |              |
| - Modelo   |   | - Tech Foo   |   | - MyProject  |
| - Template |   | - Tech Bar   |   |              |
| - Config   |   |              |   |              |
+------------+   +--------------+   +--------------+

Get started to create an interpreter

This JSON file is a model project. Here will put all information about project, like: Components, Models and Flow.

Model specification

{
    "project": "My Application",
    "description": "My super amazing application",
    "components": [
        {
            "name": "MyForm",
            "parameters":{
                "area": "components",
                "filename": "myForm.vue",
                "tag": "my-form"
            }
        }
    ],
    "models": [
        {
            "name": "user",
            "fields": [
                {"field": "id", "type": "number"},
                {"field": "name", "type": "string"},
                {"field": "age", "type": "integer"},
                {"field": "password", "type": "string"}
            ]
        }
    ]
}

This is a config file interpreter. Is a simple example when you connect the actions like myFunction with my-tag to generate code:

{
    "root": "/example",
    "inPath": "../../input/example",
    "outPath": "c:/myabsolutepath/model2code/output",
    "templatePath": "../input/example/templates/vue",
    "symbol": "@@",
    "actions" : [
        { "action": "createDirectory", "pathOut":""},
        { "action": "createDirectory", "pathOut":"/front"},
        { "action": "createDirectory", "pathOut":"/front/src"},
        { "action": "copy", "filename": "App.vue", "pathIn":"/src", "pathOut":"/front/src",
            "actions": [
                  {"action": "myFunction", "tag": "my-tag"}
            ]
        }
    ]
}

This is a simple example code template than interpreter will replace de tag @@my-tag@@:

<template>
    <div/>
</template>

<script>
export default {
      components:{
          @@my-tag@@
      }
}
</script>

This is a basic interpreter when is execute de action myFunction replacing de tag @@my-tag@@ to code HTML:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from interpreter import Interpreter

class InterpreterVue:

    def __init__(self, configJSON):
        self.interpreter = Interpreter(configJSON)
        self.config = self.interpreter.getConfig()
        pass

    def myFunction(self, data):
        code = ""
        for c in data['components']:
            code += "<{}/>\n".format(c['parameters']['tag'])
        return code

    def generateCode(self, item, model, content):
        if 'actions' in item:
            for el in item['actions']:
                if el['action'] == "myFunction":
                    print('  adding myFunction works!...')
                    content = self.interpreter.replace(content, el['tag'], self.myFunction(model) )
        return content

    def generate(self, filenameJSON):
        templateDirectory = self.interpreter.getPathTemplate()
        outputDirectory = self.interpreter.getPathOut()
        model = self.interpreter.loadModel(filenameJSON)
        for el in self.config['actions']:

            if el['action'] == "createDirectory":
                print("- Create directory {} ...".format(el['pathOut']))
                self.interpreter.createDirectory(outputDirectory + el['pathOut'])

            if el['action'] == "copy":
                filenameIn = templateDirectory+"{}/{}".format(el["pathIn"], el['filename'])
                print("- Coping file {} ...".format(filenameIn))
                content = self.interpreter.loadFile(filenameIn)
                content = self.generateCode(el, model, content)
                filenameOut = outputDirectory+"{}/{}".format(el["pathOut"], el['filename'])
                self.interpreter.saveFile(filenameOut, content)
                self.interpreter.verifyFile(filenameOut)

About

Tool to convert model to code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published