Code Generation involves a wide range of fields, but many of the technologies used by the most common layer generators are common. The following describes several technologies commonly used by layer generators and the key points to consider when choosing a development language for your layer generators.
Frequently used techniques for writing code generators:
N Text Template,
Code Generation usually means creating files with complex structured text types. To maintain the simplicity and integrity of the generator, you can use the Text Template tool so that you can separate the Logic definition from the logic code format, which is the ideal situation.
N Regular Expression:
It is used to search and scan source files to extract the information you need. The disadvantage of regular expressions is that it is not easy to read.
N XML:
Read the XML file for parsing, verification, or use the XML file as the output of the code generator.
N file directory operations:
Almost every builder uses the technology to read the code format from the file and write the generated code to the output file.
N command line:
Command lines have their own advantages. Because code generation has many automatic processes, in some cases, command lines are more efficient and simple.
Based on the above points, let's take a look at how to select a language before writing a Code Generator:
Whether n has powerful read/write capabilities and the ability to parse text files is critical.
N text templates are the basis of generators, so it is very important to have powerful and easy-to-use Text Template tools.
N can process XML quickly and conveniently.
N fast, powerful file operation capabilities, maintenance, search, and so on.
N language efficiency is not given a high priority here. This is because the generator is not used in the product and will not run frequently for a long time.
Considering the above situations, the ideal language for writing code generators is Python, Ruby, and other scripting languages. On the one hand, these languages have powerful text processing capabilities, regular expressions, and XML processing capabilities, facilitating file and directory operations. On the other hand, because of its simplicity, low code size, and high development efficiency, although the execution efficiency of the program is slightly inferior, this is not the main consideration of the code generator. We can use these scripting languages to generate code for our enterprise application projects to avoid repeated and boring work.
BTW: This article is mainly based on the book "Code Generation in Action". If you are interested, you can check it out.