[Open source] Duplicate and boring code? Generate it! Code generator

Hi everyone, and welcome to the Stop Refactoring channel.

In this issue, we release a general-purpose code generator written in Python3 , named Christmas  , which is currently version 2.0.

The beginning of constructing Christmas stems from an idea, if the boring and repetitive code cannot be written only once, then generate it.

Thanks to Christmas, our front-end framework Trick allows page modules to run and develop independently, and the back-end Once framework can complete business code development through Json configuration.

The software is open source, and interested friends can download it from GitHub , and our official website has complete instructions for use .

In addition, Christmas is a command-line tool . If you use VS Code , you can download the Christmas auxiliary plug-in in the plug-in store of VS Code to improve its ease of use. 

Next, introduce the five major functions of Christmas:

1. Generate code GenCode from template 

2. Generate code from existing code MergeCode 

3. Clean up files/directories CleanCode

4. Download code DownloadCode 

5. Arranging multiple shell commands ExcuteShell 

Generate code from template GenCode

After sorting out the code template and configuring the Json generation rules , Christmas can generate new code from the code of the code template according to the rules according to the nesting relationship of Json.

For example, run the sample in this mode to generate new code according to the rules.

 

This method is suitable for new scenarios such as initialization projects and new modules , and is also suitable for scenarios with clear rules such as business code generation.

Generate code from existing code MergeCode

This mode allows reintegrating , nesting and generating new code from existing runnable code , as well as changing the directory structure.

This mode is more similar to compilation . Our front-end Trick framework uses this mode to compile production environment code, and our back-end Once framework uses this mode to connect completely independent module codes.

For example, running sample#complex in this mode can sort out new code from existing code.

Clean files/directories CleanCode 

This mode is mainly for cleaning up backups or unnecessary files/directories in batches. In this mode, multiple locations can be cleaned up at one time. It supports traversal of files in a directory, and supports keyword matching and filtering.

 

Download CodeDownloadCode 

Download code is convenient for teams to download code files from their own code base .

In order to adapt to more scenarios, this mode actually downloads the Load file on the server, and then downloads specific files according to the Load file.

So don't worry that the file structure is not the same and you can't use this function.

Our front-end Trick framework and back-end Once framework use this function to download the module code, and the file structure of the module code may be different.

Arranging multiple Shell commands ExcuteShell 

Although the above functions seem relatively complete, sometimes it is necessary to integrate several functions.

If you need to connect the module code after downloading the module code, you need to run MergeCode after DownloadCode.

Although the Shell command line itself also supports one line to represent multiple commands, and && can be used to implement the success of the previous command and then execute the next command. But if the program requires the user to enter a string during operation, it is not easy to handle.

Therefore, Christmas has added the ExcuteShell function, allowing multiple Shell commands to be set , whether to continue when the current command fails , or to automatically input strings according to the program output log .

Summarize

Generally speaking, Christmas is very useful for projects with clear rules and specifications. I hope it can help you write boring and repetitive code only once.

In the previous content, we proposed the concept of top-level architecture. Some friends said that it is difficult to implement some rules, so you can try Christmas.

Guess you like

Origin blog.csdn.net/Daniel_Leung/article/details/131892837