A method, apparatus, electronic device, and storage medium for generating a code template
By creating template generation projects in the code generation tool, configuring template meta information and file generation rules, defining attributes and functional parameters, and inheriting abstract classes for the template engine to generate code files, the problem of insufficient flexibility of existing tool templates is solved, and highly customized and automated code generation is achieved, which improves development efficiency and code consistency.
Patent Information
- Application Number
- CN202410849793.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2025-08-05
- Estimated Expiration
- 2044-06-27
AI Technical Summary
The existing code generation tool templates are not flexible enough to adapt to the personalized needs of different projects, and lack of dynamic parameter support, resulting in the generated code requiring a lot of manual adjustment.
By creating a template generation project, configuring template meta information, file generation rules and attribute parameters, defining functional parameters and default values, inheriting the abstract class of the template engine to generate code files, and rewriting the generation method, and packaging the code template compressed file.
Realize highly customizable and automated code generation, improve development efficiency, ensure consistency of code style and framework, and have high flexibility and adaptability.
Smart Images

Figure CN118819490B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of code development technology, and in particular to a method, device, electronic device, and storage medium for generating a code template. Background Art
[0002] Code generation is a common tool used in software development to improve development efficiency and reduce human error. Using predefined templates and configurations, code generation can automatically generate the various code files required for a project, streamlining the development process. Code generation is particularly important in large-scale projects.
[0003] Currently, there are a variety of code generation technologies and tools on the market, but some shortcomings remain: Insufficient template flexibility: Many existing code generation tools have relatively fixed template designs, making it difficult to adapt to the individual needs of different projects. Developers need to manually modify the generated code based on specific project requirements, increasing workload and the risk of errors. Lack of support for dynamic parameters: Many tools lack support for dynamic parameters, making the generated code templates unable to adapt to the specific configurations of different projects, resulting in the need for extensive manual adjustments to the generated code. Summary of the Invention
[0004] The embodiments of the present disclosure provide at least one method, device, electronic device, and storage medium for generating a code template, which can achieve highly customizable and automated code generation, improve development efficiency, and ensure consistency between code style and framework, while having high flexibility and adaptability to different projects.
[0005] The present disclosure provides a method for generating a code template, including:
[0006] Creating a template generation project, creating a template description file in the template generation project, and configuring template meta information in the template description file;
[0007] Creating a template structure file in the template generation project, declaring a file generation rule corresponding to a code file generated by the code template, and defining attribute parameters corresponding to the code template;
[0008] Creating a template parameter file in the template generation project, and defining function parameters corresponding to the code template and default values corresponding to the function parameters in the template parameter file;
[0009] Create a generator class for generating code files, the generator class inherits the abstract class for generating code files using a template engine, and rewrites a code file generation method in the generator class;
[0010] A path interface file including a package path corresponding to the generator class is created in the template generation project, and the template generation project is packaged to generate a code template compressed file.
[0011] In an optional implementation, the template meta-information includes at least: template name, template type, template version number, template description information, type of generated project, coding expression of generated project, and directory where the template is located.
[0012] In an optional implementation, the method further includes:
[0013] A template storage directory is created, wherein the attribute value corresponding to the template storage directory and the directory where the template is located in the template description file is the same.
[0014] In an optional implementation, after packaging the template generation project to generate a code template compressed file, the method further includes:
[0015] In response to a template selection operation, determining a target code template corresponding to the template selection operation, and loading the target code template according to the template storage directory;
[0016] Parsing a target template parameter file corresponding to the target code template to determine target function parameters corresponding to the target template parameter file;
[0017] In response to the first parameter configuration operation, determining a target parameter value corresponding to the target function parameter;
[0018] In response to the second parameter configuration operation, determining a target attribute value corresponding to the attribute parameter;
[0019] The default value corresponding to the target function parameter is replaced by the target parameter value.
[0020] In an optional implementation, after replacing the default value corresponding to the target function parameter with the target parameter value, the method further includes:
[0021] Creating a template engine instance, and loading the target code template into the template engine instance;
[0022] Parsing a target template description file and a target template structure file corresponding to the target code template, and determining target template meta information included in the target template description file and a target file generation rule corresponding to the target template structure file;
[0023] Parsing the target file generation rule to determine the target file name and target file location corresponding to the code file generated by the target code template;
[0024] The target generator class corresponding to the target template description file is called, the target code template is rendered through the template engine instance, a target code file is generated, and the target code file is written to the target file path.
[0025] In an optional implementation, a template structure file is created in the template generation project, the file generation rules corresponding to the code file generated by the code template are declared, and the attribute parameters corresponding to the code template are defined, specifically including:
[0026] Obtaining the preset Meta object attribute corresponding to the code template as the attribute parameter;
[0027] An engineering coding expression is constructed according to the preset Meta object attributes to declare that the code template corresponds to the file generation rule, wherein the file generation rule includes a file format and a file location corresponding to the code file generated by the code template.
[0028] In an optional implementation, rendering the target code template by the template engine instance to generate the target code file specifically includes:
[0029] constructing the target parameter value, the target attribute value, and the template meta information into a configuration data group;
[0030] The template engine instance is combined with the configuration data group so that the template engine instance generates the target code file according to the configuration data group by calling the target generator class and adopting the code file generation method in the target generator class.
[0031] The present disclosure also provides a device for generating a code template, including:
[0032] A template meta-information configuration module is used to create a template generation project, create a template description file in the template generation project, and configure template meta-information in the template description file;
[0033] An attribute parameter declaration module is used to create a template structure file in the template generation project, declare the file generation rules corresponding to the code file generated by the code template, and define the attribute parameters corresponding to the code template;
[0034] A function parameter definition module is used to create a template parameter file in the template generation project, and define function parameters corresponding to the code template and default values corresponding to the function parameters in the template parameter file;
[0035] A code generation method embedding module is used to create a generator class for generating code files, wherein the generator class inherits an abstract class for generating code files using a template engine, and the code file generation method is rewritten in the generator class;
[0036] The project compression module is used to create a path interface file including the package path corresponding to the generator class in the template generation project, and package the template generation project to generate a code template compressed file.
[0037] An embodiment of the present disclosure also provides an electronic device, including: a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the above-mentioned code template generation method or the steps in any possible implementation of the above-mentioned code template generation method are executed.
[0038] An embodiment of the present disclosure further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the computer program executes the above-mentioned method for generating a code template, or the steps of any possible implementation of the above-mentioned method for generating a code template.
[0039] The embodiments of the present disclosure further provide a computer program product, including a computer program / instruction, which, when executed by a processor, implements the above-mentioned method for generating a code template, or the steps in any possible implementation of the above-mentioned method for generating a code template.
[0040] The embodiment of the present disclosure provides a method, device, electronic device and storage medium for generating a code template, which is implemented by creating a template generation project, creating a template description file in the template generation project, and configuring template meta-information in the template description file; creating a template structure file in the template generation project, declaring the file generation rules corresponding to the code file generated by the code template, and defining the attribute parameters corresponding to the code template; creating a template parameter file in the template generation project, and defining the function parameters corresponding to the code template and the default values corresponding to the function parameters in the template parameter file; creating a generator class for generating code files, the generator class inherits the abstract class for generating code files using a template engine, and rewrites the code file generation method in the generator class; creating a path interface file including the package path corresponding to the generator class in the template generation project, and packaging the template generation project to generate a code template compressed file. The method can achieve highly customizable and automated code generation, improve development efficiency and ensure consistency of code style and framework, while having high flexibility and adaptability to different projects.
[0041] In order to make the above-mentioned objectives, features and advantages of the present disclosure more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the following briefly introduces the drawings required for use in the embodiments. The drawings herein are incorporated into and constitute a part of the specification. These drawings illustrate embodiments consistent with the present disclosure and, together with the specification, are used to illustrate the technical solutions of the present disclosure. It should be understood that the following drawings only illustrate certain embodiments of the present disclosure and should not be regarded as limiting the scope. For those of ordinary skill in the art, other relevant drawings can be obtained based on these drawings without inventive effort.
[0043] Figure 1 A flowchart of a method for generating a code template provided by an embodiment of the present disclosure is shown;
[0044] Figure 2 A flowchart showing another method for generating a code template provided by an embodiment of the present disclosure is shown;
[0045] Figure 3 A schematic diagram of a device for generating a code template provided by an embodiment of the present disclosure is shown;
[0046] Figure 4 A schematic diagram of an electronic device provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0047] In order to make the purpose, technical solutions and advantages of the embodiments of the present disclosure clearer, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, not all of the embodiments. The components of the embodiments of the present disclosure generally described and shown in the drawings herein can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present disclosure provided in the drawings is not intended to limit the scope of the disclosure for which protection is sought, but merely represents selected embodiments of the present disclosure. Based on the embodiments of the present disclosure, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present disclosure.
[0048] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, it does not need to be further defined or explained in subsequent drawings.
[0049] The term "and / or" herein simply describes an association relationship, indicating that three relationships can exist. For example, A and / or B can represent the existence of A alone, the simultaneous existence of A and B, and the existence of B alone. In addition, the term "at least one" herein refers to any combination of at least two of any one or more of a plurality of items. For example, "at least one of A, B, and C" can represent any one or more elements selected from the set consisting of A, B, and C.
[0050] Research has revealed that while there are a variety of code generation technologies and tools on the market, some shortcomings remain: Insufficient template flexibility: Many existing code generation tools have relatively fixed template designs, making them difficult to adapt to the individual needs of different projects. Developers must manually modify the generated code based on specific project requirements, increasing workload and the risk of errors. Lack of support for dynamic parameters: Many tools lack support for dynamic parameters, making the generated code templates incapable of adapting to the specific configurations of different projects, often requiring extensive manual adjustments to the generated code.
[0051] Based on the above research, the present disclosure provides a method, device, electronic device and storage medium for generating a code template, which creates a template generation project, creates a template description file in the template generation project, and configures template meta-information in the template description file; creates a template structure file in the template generation project, declares the file generation rules corresponding to the code file generated by the code template, and defines the attribute parameters corresponding to the code template; creates a template parameter file in the template generation project, and defines the function parameters corresponding to the code template and the default values corresponding to the function parameters in the template parameter file; creates a generator class for generating code files, the generator class inherits the abstract class for generating code files using a template engine, and rewrites the code file generation method in the generator class; creates a path interface file including the package path corresponding to the generator class in the template generation project, and packages the template generation project to generate a code template compressed file. It can achieve highly customizable and automated code generation, improve development efficiency and ensure consistency of code style and framework, while having high flexibility and adaptability to different projects.
[0052] To facilitate understanding of this embodiment, a method for generating a code template disclosed in an embodiment of the present disclosure is first introduced in detail. The execution subject of the method for generating a code template provided in the embodiment of the present disclosure is generally a computer device with certain computing capabilities, such as a terminal device or a server or other processing device. The terminal device can be a user equipment (UE), a mobile device, a user terminal, a terminal, a cellular phone, a cordless phone, a personal digital assistant (PDA), a handheld device, a computing device, a vehicle-mounted device, a wearable device, etc. In some possible implementations, the method for generating a code template can be implemented by a processor calling computer-readable instructions stored in a memory.
[0053] See also Figure 1 FIG. 1 is a flowchart of a method for generating a code template provided by an embodiment of the present disclosure, the method comprising steps S101 to S105, wherein:
[0054] S101: Create a template generation project, create a template description file in the template generation project, and configure template meta information in the template description file.
[0055] In the specific implementation, a new Maven type project or module is created to store the template file, and a template description file is created in the `resources` directory. The template description file is used to define the template meta information of the code template.
[0056] Here, the template meta information of the code template may include at least: template name, template type, template version number, template description information, type of generated project, coding expression of generated project and directory where the template is located.
[0057] Specifically, define the `name`, `type`, `version`, `description`, `projectType`, `projectCodeEl`, and `templateFileDir` fields in the template description file, where the `name` field represents the template name, the `type` field represents the template type, the `version` field represents the template version number, the `description` field represents the template description information, the `projectType` field represents the type of the generated project, the `projectCodeEl` field represents the coding expression of the generated project, and the `templateFileDir` field represents the directory where the template is located.
[0058] It should be noted that when a template is upgraded, the template name and template type remain unchanged, and only the version number is changed.
[0059] For example, the template description file includes the following template metadata information: `name`: `ExampleTemplate` represents the name of the template is Example Template, which is used to identify the purpose or function of the template; `type`: `velocity` is the type of the template, and `velocity` is used here to indicate that the template uses the Velocity template engine; `version`: `1.0.0` is the version number of the template; `description`: `A template for generatingexample classes` represents a brief description of the template, explaining the function and purpose of the template; `projectType`: `maven` indicates that the generated project type is a back-end `maven` project, and if it is a front-end project, `npm` is used; `templateFileDir`: `template / velocity` represents the directory where the template file is located, and the template file will be searched from this directory when generating code.
[0060] Here, the code expression for generating the project may be a SpEL (Spring Expression Language) expression, which is used to dynamically generate the code for the project or engineering. This code is usually a string composed of specific rules and input parameters, and is used to uniquely identify the project or engineering.
[0061] For example, the sample SpEL expression "projectCodeEl": "#baseProject.code+'-'+#params['projectType']+'-'+#params['shortName']+'-project'". Among them, `#baseProject.code` is the first part, which is used to get the value of the `code` attribute of the `baseProject` object. This value represents the base code of the project; '-' is a connector used to connect different parts into a complete string; `#params['projectType']` is the second part, using the `projectType` parameter value in the `params` object. This value indicates the type of project, such as `maven` or `npm`; '-' uses a connector again; `#params['shortName']` is the third part, using the `shortName` parameter value in the `params` object. This value is usually the short name of the project or module; '-project'` is the last part, a fixed string `'-project'`, used to identify that this is a project.
[0062] As a possible implementation, variables and parameters are often set during the template generation process, and these variables and parameters can be used in SpEL expressions. For example, the `baseProject` object contains basic project information, such as `code`; the `params` object contains user-defined parameters, such as `projectType` and `shortName`; in the template description file, define the `projectCodeEl` field and use SpEL expressions to combine these variables and parameters to generate the project or project code. During code generation, the system parses the SpEL expressions in the template description file, calculates the expression value, and uses it as the project or project code.
[0063] In this way, SpEL expressions provide a flexible way to dynamically generate project code based on input parameters and project properties. This approach not only simplifies the code generation process, but also ensures the consistency and uniqueness of the code.
[0064] As a possible implementation, a template storage directory may be created, where the attribute value corresponding to the template storage directory is the same as the attribute value corresponding to the directory where the template in the template description file is located.
[0065] Here, a path is created under the `resources` directory to store the template file. This path corresponds to the attribute value corresponding to the template directory attribute in the template description file.
[0066] For example, if the `templateFileDir` value in the template description file is `template / velocity`, the template file should be stored in the `resources / template / velocity` directory. If the `templateFileDir` value is `aaa / bbb`, the template file should be stored in the `resources / aaa / bbb` directory.
[0067] In this way, a template description file is created in the `resources` directory, and the template meta information is correctly configured. When generating code, the template can be correctly identified and applied according to the configuration in the template description file.
[0068] S102: creating a template structure file in the template generation project, declaring a file generation rule corresponding to a code file generated by the code template, and defining attribute parameters corresponding to the code template.
[0069] In the specific implementation, a template structure file is created in the `resources` directory of a Maven-type project or module. The template structure file is mainly used to declare the file name and file location of the target file to be generated by the template source file. The file dynamically determines the location and name of the target file through the SpEL expression.
[0070] Specifically, the preset Meta object attributes corresponding to the code template are obtained as attribute parameters; an engineering coding expression declaring the file generation rules corresponding to the code template is constructed according to the preset Meta object attributes, wherein the file generation rules include the file name and file location corresponding to the code file generated by the code template.
[0071] Here, when using these properties in the template file, you can access them through `${meta.configs.get('keyName')}`.
[0072] Exemplarily, the template structure file may include: `source` represents the name of the template source file, for example, "boots.vm" means using a template file named `boots.vm`. `targetDir` represents the SpEL expression of the target file directory, for example, `'src / main / java / '+#meta.ns.replace('.', ' / ')+' / '+#meta.shortName`, where 'src / main / java / ' represents the fixed part of the target directory; `#meta.ns.replace('.', ' / ')` is used to replace the dots in the namespace with slashes to conform to the directory structure. For example, if the value of `meta.ns` is `aaa.bbb.ccc`, it will be converted to `aaa / bbb / ccc` after conversion; `#meta.shortName` represents the short name of the namespace of the project currently being created. For example, if the value of `meta.shortName` is `xxx`, the final target directory is `src / main / java / aaa / bbb / ccc / xxx`. `targetName` represents the SpEL expression of the target file name, for example, `#meta.baseProjectName+'-code-'+#meta.shortName+'-api'`, where `#meta.baseProjectName` represents the base name of the project, '-code-' is a connection part, a string used to connect different parts, `#meta.shortName` represents the short name of the project currently being created, and '-api' is a fixed part of the file name. For example, if the value of `meta.baseProjectName` is `BP1234` and the value of `meta.shortName` is `core`, the final target file name is `BP1234-code-core-api`.
[0073] Here, multiple properties of the `meta` object are used in the template structure file. These properties will be dynamically set when the template generates code. For example, `meta.templateType` represents the template type; `meta.templateVersion` represents the template version; `meta.ns` represents the project namespace; `meta.shortName` represents the short name of the namespace of the currently created project; `meta.Version` represents the version of the current project; `meta.baseProjectName` represents the encoding of the project where the project is located; `meta.name` represents the encoding of the created project.
[0074] Optionally, the template structure file also contains a `map` attribute, which stores more key-value pairs about projects and project information, such as `projectName` (project name); `projectDescription` (project description); `baseProjectDescription` (project description); `baseProjec tName` (project name); `baseProjectManager` (project manager's name); `baseProjec tManagerEmail` (project manager's email address).
[0075] Exemplarily, the SpEL expression can be in the following form: for the target file name, "targetNa me": "#meta.baseProjectName+'-code-'+#meta.shortName+'-api'", the parsing result can be: `BP1234-code-core-api`; for the target directory, "targetDir": "'src / mai n / java / '+#meta.ns.replace('.',' / ')+' / '+#meta.shortName", the parsing result can be: `src / main / java / aaa / bbb / ccc / xxx` (assuming `meta.ns` is set to `aaa.bbb.ccc` and `meta.shortName` is set to `xxx`).
[0076] By declaring how template files are converted into target files and their location, the template structure file makes the template generation process more dynamic and flexible. Using SpEL expressions, you can dynamically generate file names and locations that meet your needs based on the specific project and engineering requirements. This not only increases the reusability and flexibility of templates, but also simplifies project management and maintenance.
[0077] S103: creating a template parameter file in the template generation project, and defining functional parameters corresponding to the code template and default values corresponding to the functional parameters in the template parameter file.
[0078] In practice, a template parameter file is created in the `resources` directory of a Maven project or module. This file defines the function parameters used in the template file and sets default values for these parameters. When generating code from the template, users can choose to use these default values or modify them as needed.
[0079] For example, the structure of a template parameter file can be: "springBootVersion": "2.7.6", "javaVersion": "11", "groupId": "com.example", "artifactId": "my-project", "version": "0.0.1-SNAPSHOT", "packageName": "com.example.myproject". "springBootVersion" indicates the Spring Boot version, with a default value of 2.7.6; "javaVersion" indicates the Java version, with a default value of 11; "groupId" indicates the Maven project's groupId, with a default value of com.example; "artifactId" indicates the Maven project's artifactId, with a default value of my-project; "version" indicates the Maven project's version, with a default value of 0.0.1-SNAPSHOT; and "packageName" indicates the package name of the generated code, with a default value of com.example.myproject.
[0080] Here, in the template file, you can use `${params. <parametername>}` form to reference these parameters. When using the template to generate code, users can modify the default values of the parameters defined in the template parameter file through the interface or configuration file.
[0081] For example, if you change the value of the `springBootVersion` parameter to `2.5.4`, any use of `${params.springBootVersion}` will be replaced with `2.5.4`.
[0082] In this way, the template parameter file provides a flexible way to define and manage the parameters used in the template file. By setting default values, users can quickly generate code that conforms to the default configuration. At the same time, users can also modify these parameters according to specific needs to generate customized code. This approach not only improves the reusability and flexibility of the template, but also simplifies the code generation process.
[0083] S104: Create a generator class for generating a code file. The generator class inherits the abstract class for generating a code file using a template engine, and rewrites a code file generation method in the generator class.
[0084] In the specific implementation, a generator class is created to inherit the abstract class that uses the template engine to generate code files, and the method of creating template files is rewritten in the generator class. The method of creating template files can receive the name of the template file as a parameter and generate the corresponding code file.
[0085] For example, inherit the `AbstractVelocityGenerator` abstract class and implement the `Generator` interface, override the code generation method, and call the `createCodeFiles()` or `createModules()` method to generate code files.
[0086] S105 , creating a path interface file including a package path corresponding to the generator class in the template generation project, and packaging the template generation project to generate a code template compressed file.
[0087] To ensure that the custom generator class is correctly instantiated when loading the compressed code template file, you need to configure a service provider interface file (also known as a path interface file) in the `resources` directory of the Maven project or module. The path interface file's name is the path to the generator class, and its content contains the fully qualified name of the generator class, i.e., the class's full package path. The created Maven project or module is then packaged into a Jar file (also known as the compressed code template file).
[0088] Here, ensure that the template description file, template structure file, template parameter file and other files have been correctly configured and placed in the template storage directory. Run the packaging command in the root directory of the project and package the project into a JAR file.
[0089] For example, create a `META-INF / services` directory under the `resources` directory, ensure that there is a directory with the path `META-INF / services` in the `resources` directory, create a file named `com.leadingtek.dmp.code.Generator` in the `META-INF / services` directory, and fill in the fully qualified name of the generator class in the `com.leadingtek.dmp.code.Generator` file, for example, `com.leadingtek.dmp.code.CustomVelocityGenerator`.
[0090] Thus, by creating the `com.leadingtek.dmp.code.Generator` file in the `resources / META-INF / services` directory and filling in the fully qualified name of the generator class in the file, you can ensure that the customized generator class `CustomVel ocityGenerator` is automatically instantiated when the code template compressed file package is loaded. This configuration method utilizes the Java service provider interface mechanism, allowing the code template compressed file package to automatically discover and load the specified implementation class.
[0091] As a possible implementation method, after the code template compressed file package is packaged, it can also be uploaded to the preset code development platform. The user can access the upload interface of the preset code development platform, choose to upload the generated code template compressed file and fill in information such as the template name, version, description, etc. This information can be obtained from the template description file. The preset code development platform will automatically parse the uploaded code template compressed file package, read the template description file, template structure file, template parameter file and other files therein, understand the configuration and structure of the template, and decompress the code template compressed file package, and upload the code template to the designated directory of the server for subsequent use.
[0092] Here, after uploading the code template, the preset code development platform can also create a record in the database, which contains basic information of the template (such as name, version, description, etc.) for users to select and use when creating a project.
[0093] As another possible implementation, after uploading the code templates to the database on the preset code development platform, users can also select and use these code templates to create new projects on the preset code development platform. Figure 2 FIG. 2 is a flowchart of another method for generating a code template provided by an embodiment of the present disclosure, wherein the method includes steps S201 to S205, wherein:
[0094] S201. In response to a template selection operation, determine a target code template corresponding to the template selection operation, and load the target code template according to the template storage directory; parse a target template parameter file corresponding to the target code template, and determine a target function parameter corresponding to the target template parameter file.
[0095] S202. In response to the first parameter configuration operation, determine the target parameter value corresponding to the target function parameter; in response to the second parameter configuration operation, determine the target attribute value corresponding to the attribute parameter; and replace the default value corresponding to the target function parameter with the target parameter value.
[0096] S203. Create a template engine instance and load the target code template into the template engine instance; parse the target template description file and the target template structure file corresponding to the target code template, determine the target template meta-information included in the target template description file, and the target file generation rules corresponding to the target template structure file.
[0097] S204: Parse the target file generation rule to determine the target file name and target file location corresponding to the code file generated by the target code template.
[0098] S205. Call the target generator class corresponding to the target template description file, render the target code template through the template engine instance, generate a target code file, and write the target code file to the target file path.
[0099] In practice, users can access the Preset Code Development Platform, find and click "Create Project" or a similar option. Within the project creation options, select "Create from Template" or a similar option. This option allows users to quickly generate new projects based on predefined templates. The Preset Code Development Platform will display a list of available templates. This list includes all uploaded and parsed templates, displaying information such as the template name, version, and description. Users can select the appropriate template from the list based on their project requirements. For example, if you want to create a backend project, you can select a template of the "Maven" type.
[0100] Here, users can click a template name or the "View Details" button to view detailed information about the template, including its description, applicable scope, and version information. After selecting a template, the platform will display the template's parameter configuration interface. These parameters are defined in the template parameter file, including parameter names and default values. Users can modify parameter defaults based on their needs, for example, changing the default value of the `springBootVersion` parameter from `2.7.6` to another version. Depending on the template requirements, users will need to fill in or confirm certain required information, such as the project name and description.
[0101] Specifically, when a user selects a template, the platform records the template ID selected by the user and searches the database for the corresponding template information based on the template ID. This information includes the template name, version, description, and the path stored on the server. Based on the queried path, the platform accesses the directory storing the template source file on the server and finds the required `.vm` template file.
[0102] After confirming all parameters and configuration information are correct, the user can click the "Generate Project" or "Create" button. The platform will use the Velocity template engine library to create an engine instance and configure the Velocity engine's properties, including the template file loading path and character encoding. The obtained `.vm` template file will then be loaded into the Velocity engine.
[0103] Optionally, the target parameter values, target attribute values and template meta-information are constructed into a configuration data group; the template engine instance is combined with the configuration data group so that the template engine instance generates a target code file according to the configuration data group by calling the target generator class and adopting the code file generation method in the target generator class.
[0104] Here, a configuration data group is created based on the user-configured parameters and the system-predefined parameters. The configuration data group includes the parameter values entered by the user, the system-predefined parameter values (such as project name, version, etc.), and the information parsed from the template description file and template structure file. The path and name of the target code file are generated by parsing the SpEL expression. The Velocity engine is used to combine the data model and template file to generate the target code file content, and the rendered content is written to the predetermined target file path and file. When the methods corresponding to all template files are executed, the code file generation is completed. The development platform will organize these files into a complete code project.
[0105] Optionally, the platform can upload the generated code project to the corresponding code repository (such as a Git repository). After the upload is completed, the platform will notify the relevant developers that the code has been uploaded to the code repository, and the developers can start pulling down the code for subsequent development work.
[0106] The embodiment of the present disclosure provides a method for generating a code template, which is as follows: creating a template generation project, creating a template description file in the template generation project, and configuring template meta-information in the template description file; creating a template structure file in the template generation project, declaring the file generation rules corresponding to the code file generated by the code template, and defining the attribute parameters corresponding to the code template; creating a template parameter file in the template generation project, and defining the function parameters corresponding to the code template and the default values corresponding to the function parameters in the template parameter file; creating a generator class for generating code files, the generator class inherits the abstract class for generating code files using a template engine, and rewrites the code file generation method in the generator class; creating a path interface file including the package path corresponding to the generator class in the template generation project, and packaging the template generation project to generate a code template compressed file. This method can achieve highly customizable and automated code generation, improve development efficiency, and ensure consistency between code style and framework, while having high flexibility and adaptability to different projects.
[0107] Those skilled in the art will understand that in the above-mentioned method of the specific implementation method, the writing order of each step does not mean a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of each step should be determined by its function and possible internal logic.
[0108] Based on the same inventive concept, the embodiment of the present disclosure also provides a device for generating a code template corresponding to the method for generating a code template. Since the principle of solving the problem by the device in the embodiment of the present disclosure is similar to the method for generating a code template in the above-mentioned embodiment of the present disclosure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0109] See also Figure 3 , Figure 3 Schematic diagram of a device for generating a code template provided by an embodiment of the present disclosure. Figure 3 As shown in , the code template generation device 300 provided by the embodiment of the present disclosure includes:
[0110] The template meta-information configuration module 310 is used to create a template generation project, create a template description file in the template generation project, and configure template meta-information in the template description file.
[0111] The attribute parameter declaration module 320 is used to create a template structure file in the template generation project, declare the file generation rules corresponding to the code file generated by the code template, and define the attribute parameters corresponding to the code template.
[0112] The function parameter definition module 330 is used to create a template parameter file in the template generation project, and define the function parameters corresponding to the code template and the default values corresponding to the function parameters in the template parameter file.
[0113] The code generation method embedding module 340 is used to create a generator class for generating code files. The generator class inherits the abstract class for generating code files using a template engine and rewrites the code file generation method in the generator class.
[0114] The project compression module 350 is used to create a path interface file including the package path corresponding to the generator class in the template generation project, and package the template generation project to generate a code template compressed file.
[0115] For descriptions of the processing flow of each module in the device and the interaction flow between each module, reference can be made to the relevant descriptions in the above method embodiment, which will not be described in detail here.
[0116] The embodiment of the present disclosure provides a device for generating a code template, which is configured by creating a template generation project, creating a template description file in the template generation project, and configuring template meta-information in the template description file; creating a template structure file in the template generation project, declaring the file generation rules corresponding to the code file generated by the code template, and defining the attribute parameters corresponding to the code template; creating a template parameter file in the template generation project, and defining the function parameters corresponding to the code template and the default values corresponding to the function parameters in the template parameter file; creating a generator class for generating code files, the generator class inherits the abstract class for generating code files using a template engine, and rewrites the code file generation method in the generator class; creating a path interface file including the package path corresponding to the generator class in the template generation project, and packaging the template generation project to generate a code template compressed file. The device can achieve highly customizable and automated code generation, improve development efficiency and ensure consistency of code style and framework, while having high flexibility and adaptability to different projects.
[0117] Corresponding to Figure 1 The method for generating a code template in the present disclosure also provides an electronic device 400, such as Figure 4 FIG. 4 is a schematic diagram of the structure of an electronic device 400 provided in an embodiment of the present disclosure, including:
[0118] Processor 41, memory 42, and bus 43; memory 42 is used to store execution instructions, including memory 421 and external memory 422; the memory 421 here is also called internal memory, which is used to temporarily store the operation data in the processor 41 and the data exchanged with the external memory 422 such as the hard disk. The processor 41 exchanges data with the external memory 422 through the memory 421. When the electronic device 400 is running, the processor 41 and the memory 42 communicate through the bus 43, so that the processor 41 executes Figure 1 and Figure 2 The steps in the code template generation method.
[0119] The present disclosure also provides a computer-readable storage medium having a computer program stored thereon. When executed by a processor, the computer program executes the steps of the method for generating a code template described in the above method embodiment. The storage medium may be a volatile or non-volatile computer-readable storage medium.
[0120] The embodiments of the present disclosure also provide a computer program product, which includes computer instructions. When the computer instructions are executed by a processor, the steps of the code template generation method described in the above method embodiment can be executed. For details, please refer to the above method embodiment, which will not be repeated here.
[0121] The computer program product may be implemented in hardware, software, or a combination thereof. In one embodiment, the computer program product is implemented as a computer storage medium. In another embodiment, the computer program product is implemented as a software product, such as a software development kit (SDK).
[0122] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the several embodiments provided in the present disclosure, it should be understood that the disclosed device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of the device or unit can be electrical, mechanical or other forms.
[0123] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0124] In addition, each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0125] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present disclosure, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0126] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present disclosure, which are used to illustrate the technical solutions of the present disclosure, rather than to limit them. The scope of protection of the present disclosure is not limited thereto. Although the present disclosure has been described in detail with reference to the above-described embodiments, those skilled in the art should understand that any person skilled in the art can modify or easily conceive of changes to the technical solutions described in the above-described embodiments within the technical scope disclosed in the present disclosure, or replace some of the technical features therein with equivalents. Such modifications, changes, or replacements do not deviate from the spirit and scope of the technical solutions of the embodiments of the present disclosure, and should be included in the scope of protection of the present disclosure. Therefore, the scope of protection of the present disclosure shall be subject to the scope of protection of the claims.< / parametername>
Claims
1. A method for generating a code template, characterized in that: include: Creating a template generation project, creating a template description file in the template generation project, and configuring template meta information in the template description file; In the template generation project, a template structure file is created, a file generation rule corresponding to the code file generated by the code template is declared, and attribute parameters corresponding to the code template are defined; specifically, preset Meta object attributes corresponding to the code template are obtained as attribute parameters; Constructing an engineering coding expression that declares a file generation rule corresponding to the code template based on the preset Meta object attributes. The file generation rule includes the file name and file location corresponding to the code file generated by the code template. Creating a template parameter file in the template generation project, and defining function parameters corresponding to the code template and default values corresponding to the function parameters in the template parameter file; Create a generator class for generating code files, the generator class inherits the abstract class for generating code files using a template engine, and rewrites a code file generation method in the generator class; Creating a path interface file including a package path corresponding to the generator class in the template generation project, and packaging the template generation project to generate a code template compressed file; The template meta-information includes at least: template name, template type, template version number, template description information, type of generated project, and directory where the template is located. The method further includes: Creating a template storage directory, wherein the template storage directory has the same attribute value as the directory where the template is located in the template description file; After the template generation project is packaged to generate a code template compressed file, in response to a template selection operation, the target code template corresponding to the template selection operation is determined, and the target code template is loaded according to the template storage directory; the target template parameter file corresponding to the target code template is parsed to determine the target function parameter corresponding to the target template parameter file; in response to a first parameter configuration operation, the target parameter value corresponding to the target function parameter is determined; in response to a second parameter configuration operation, the target attribute value corresponding to the attribute parameter is determined; and the default value corresponding to the target function parameter is replaced with the target parameter value.
2. The method according to claim 1, characterized in that After replacing the default value corresponding to the target function parameter with the target parameter value, the method further includes: Creating a template engine instance, and loading the target code template into the template engine instance; Parsing a target template description file and a target template structure file corresponding to the target code template, and determining target template meta information included in the target template description file and a target file generation rule corresponding to the target template structure file; Parsing the target file generation rule to determine the target file name and target file location corresponding to the code file generated by the target code template; The target generator class corresponding to the target template description file is called, the target code template is rendered through the template engine instance, a target code file is generated, and the target code file is written to the target file path.
3. The method according to claim 2, characterized in that Rendering the target code template by the template engine instance to generate a target code file specifically includes: constructing the target parameter value, the target attribute value, and the template meta information into a configuration data group; The template engine instance is combined with the configuration data group so that the template engine instance generates the target code file according to the configuration data group by calling the target generator class and adopting the code file generation method in the target generator class.
4. A device for generating a code template, characterized in that: include: A template meta-information configuration module is used to create a template generation project, create a template description file in the template generation project, and configure template meta-information in the template description file; An attribute parameter declaration module is used to create a template structure file in the template generation project, declare the file generation rules corresponding to the code file generated by the code template, and define the attribute parameters corresponding to the code template; specifically, obtain the preset Meta object attributes corresponding to the code template as attribute parameters; Constructing an engineering coding expression that declares a file generation rule corresponding to the code template based on the preset Meta object attributes. The file generation rule includes a file name and a file location corresponding to the code file generated by the code template. A function parameter definition module is used to create a template parameter file in the template generation project, and define function parameters corresponding to the code template and default values corresponding to the function parameters in the template parameter file; A code generation method embedding module is used to create a generator class for generating code files, wherein the generator class inherits an abstract class for generating code files using a template engine, and the code file generation method is rewritten in the generator class; A project compression module is used to create a path interface file including a package path corresponding to the generator class in the template generation project, and package the template generation project to generate a code template compressed file; The template meta-information includes at least: template name, template type, template version number, template description information, type of generated project and directory where the template is located. The device is further configured to: Creating a template storage directory, wherein the template storage directory has the same attribute value as the directory where the template is located in the template description file; After the template generation project is packaged to generate a code template compressed file, in response to a template selection operation, the target code template corresponding to the template selection operation is determined, and the target code template is loaded according to the template storage directory; the target template parameter file corresponding to the target code template is parsed to determine the target function parameter corresponding to the target template parameter file; in response to a first parameter configuration operation, the target parameter value corresponding to the target function parameter is determined; in response to a second parameter configuration operation, the target attribute value corresponding to the attribute parameter is determined; and the default value corresponding to the target function parameter is replaced with the target parameter value.
5. An electronic device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the method for generating a code template as described in any one of claims 1 to 3 are performed.
6. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, which, when executed by a processor, executes the steps of the method for generating a code template according to any one of claims 1 to 3.
Citation Information
Patent Citations
Code file generation method, electronic equipment, storage medium, and program product
CN113760352A