codigo-generator
codigo-generator, what is it ?
codigo-generator is designed to automate code generation so you can spend more time on the coffee machine.
How ?
- Well, it takes a database and generates several files according to your wishes.
Files ? But what kind of files ?
- mostly models, repositories, services, etc.
What languages are supported ?
- Well, so far typescript, php, java, python, c# and i hope many more to come...
Any frameworks ?
Demo
Demo with php template and symfony processors
![how to use](https://twini.fra1.cdn.digitaloceanspaces.com/vscode-ext/img/codigo-generator-php-symfony-demo.gif)
README translations
Find your README here
Requirements
You will need at least :
- A
.env
file in your project root folder as codigo-generator will load environment variables through dotenv and this file. We expect to have secret data in this file. For example :
MYSQL_DB1_ROOT_PASSWORD="OnceUponAMidnightDreary"
MYSQL_DB2_ROOT_PASSWORD="WhileIPondered"
- A json config file. You will say in that file what key you want to use for the password.
Example :
{
"global_config": {
"db_type": "mysql",
"database": {
"host": "localhost",
"user": "quizuser",
"password": "${MYSQL_DB1_ROOT_PASSWORD}",
"database": "quiz",
"port": 3306
},
"template": "php",
"packageBaseName": "MyApp",
"out_folder": "./out"
},
"processors": [
{ "name": "doctrine_repository" },
{ "name": "doctrine_entity" }
]
}
Note : Using this method is not mandatory, you could use a plain password but we strongly recommend you to use a .env
file as a good practice. So, you will be able to commit the config file if needed.
Config file
Global Configuration
The global_config key contains global configuration.
db_type
db_type define the database type, it can have these values :
database
database contains database information to connect to database. You must provide these information :
- host
- user
- password
- port
- schema [optional] define the schema name to use. public by default. Not used in mysql/mariaDB Databases.
- many_to_many_sep [optional] define the string separator between both tables in a ManyToMany pivot table. "_has_" as default.
template
template define the language mostly (well, so far), it can have these values :
- typescript
- php
- java
- python
- csharp
packageBaseName
packageBaseName is optional but necessary for some templates, as this is your base package/namespace name.
If you use the model processor with php template and "packageBaseName": "MyApp"
, your model files will contain namespace MyApp\Models;
.
If you use the model processor with java template and "packageBaseName": "com.myapp"
, your model files will contain package com.myapp.models;
.
If you use the model processor with csharp template and "packageBaseName": "AngularApp1.Server"
, your model files will contain namespace AngularApp1.Server.Models;
.
django processors need packageBaseName
, this is the name of the app.
out_folder
out_folder is where the files must be created. Codigo-generator generate files based on your config file, so yes, there are gonna be files generated, and there are gonne be there. This path value can be :
- a relative path to this workspace, your relative path must start with a dot, e.g. =>
"out_folder": "./out"
- an absolute path =>
"out_folder": "C:\\Users\\marco\\OneDrive\\tmp\\out"
skip_these_tables
skip_these_tables is optional. It is an array of table names to skip.
Example : "skip_these_tables": [ "classrooms" ]
This example will make the classrooms table skipped from the process.
only_these_tables
only_these_tables is optional. It is an array of table names that will be exclusively processed.
Example : "only_these_tables": [ "classrooms", "users" ]
This example will make the classrooms and users table processed but not the others. Note that if classrooms was in skip_these_tables array too, it would be skipped (fair, isn't it ?).
Processors
The processors key contains an array of processors. Every processor has a job to do... for you.
Every processor must have these information :
name : the name of the processor (see below for a list of available processor names)
generate : is optional. It's a boolean value, true means processor will work, false he will be skipped. Default to true.
parameter_prefix : is optional.. This is the text to append to function parameters. By default it is 'r'.
- Example :
function sayHello(rName: string) { ... }
- with this config
"parameter_prefix": "_"
we will have : function sayHello(_Name: string) { ... }
custom_base_template : is optional. It is the absolute path of your custom template file if you want something special. You should not use this if it's not necessary. See below for more information.
extra_config : is optional. It is a json with custom configuration that will work only for some processors. See below for more information.
Processor list
Here is a list of available processors for every templates. Hope it will grow in time.
Some processors can generate extra files, don't remember to copy them too.
typescript template
name |
details |
extra files |
model |
Model class |
|
angular_services |
Angular Service class to call back-end api |
'abstract_api.service.ts' |
row_mappers |
Rowmapper class, like a model class, but only with relations with childs |
|
loopback_model |
loopback framework class model |
|
postman |
Postman collection (will be on root) |
|
php template
name |
details |
extra files |
model |
Model class |
|
pdo_dao |
Dao (repository) class with PDO |
|
doctrine_entity |
Doctrine Entity for Symfony users (php 8) |
|
doctrine_repository |
Doctrine Repository for Symfony users (php 8) |
|
artisan_make_model |
Generate a single bash file with artisan make commands |
|
eloquent_model |
Eloquent model file for Laravel users |
|
symfony_service |
Symfony Service |
|
symfony_api_controller |
Symfony api Controller |
|
postman |
Postman collection (will be on root) |
|
java template
name |
details |
extra files |
model |
Model class |
|
jpa_entity |
Spring JPA Entity |
|
jpa_repository |
Spring JPA Repository |
|
spring_service |
Spring service using repository |
|
spring_controller |
Spring controller using service |
|
spring_security_jwt |
Spring security with JWT and single role |
|
postman |
Postman collection (will be on root) |
|
python template
name |
details |
extra files |
django_model |
Django model class |
|
django_import_models |
One models.py file to import your models, will be on root |
|
django_controller |
Django controller class |
|
django_controller_path |
One urls_controllers.py file to use your controllers, will be on root |
|
postman |
Postman collection (will be on root) |
|
csharp template
name |
details |
extra files |
model |
model class |
|
efcore_model |
EF Core model class (prefer using efcore to generate models) |
|
efcore_repository |
EF Core repository |
IAbstractRepository/AbstractRepository |
net_service |
.Net service |
|
net_controller |
.Net controller class |
|
net_program |
.Net Program.cs file with import for services/repositories |
|
postman |
Postman collection (will be on root) |
|
custom_base_template
custom_base_template is the absolute path of your custom template directory if you want something special.
You should not use this unless it is necessary.
Processors use template files to populate data. Every template receives incoming data delimited by {{ }}. If you use your own, you must know what it will receive.
You can check the templates in the templates
folder on the GitLab extension repository.
Example:
Let's say you want your own template file for the java
template and model
processor. First, create a local folder to store your template file. For example: "c:/my_templates". Then go to the GitLab project repository templates
folder, then in the java
folder.
There you will find two files related to the model
processor (there could be one or more):
- tpl_model_enum.java
- tpl_model.java
Download these two files to your "c:/my_templates" folder but don't rename them!
Change their contents to suit your needs. Be careful with {{ }} data.
Then you're good to go. You can run your processor, and it will use your files.
extra_config as said before can be use ony with some processors.
Example with an extra config :
"processors": [
{ "name": "model", "generate": false },
{ "name": "jpa_entity", "extra_config": { "lombok": false } },
{ "name": "jpa_repository", "generate": false }
]
This is the list of processors and their available extra_config
template |
name |
extra_config property |
default value |
Details |
Example |
java |
model |
lombok |
true |
Add lombok annotations |
"extra_config": { "lombok": false } |
java |
jpa_entity |
lombok |
true |
Add lombok annotations |
"extra_config": { "lombok": false } |
java |
jpa_entity |
use_enum |
true |
Use enum class when enum |
"extra_config": { "use_enum": false } |
java |
spring_service |
logger |
true |
Add logger |
"extra_config": { "logger": false } |
java |
spring_controller |
logger |
true |
Add logger |
"extra_config": { "logger": false } |
php |
artisan_make_model |
option |
empty string |
Artisan model command option |
"extra_config": { "option": "--all" } |
php |
eloquent_model |
do_getters_setters |
false |
Add Getters/Setters |
"extra_config": { "do_getters_setters": true } |
php |
symfony_api_controller |
base_api |
"/api" |
Base for api url |
"extra_config": { "base_api": "/myapi" } |
php |
symfony_api_controller |
folder_api |
"Api" |
Folder name where to put the files |
"extra_config": { "folder_api": "MyApi" } |
php |
postman |
with_base_api |
true |
Add {{base_api}} variable on urls |
"extra_config": { "with_base_api": false } |
java |
postman |
with_base_api |
true |
Add {{base_api}} variable on urls |
"extra_config": { "with_base_api": false } |
python |
postman |
with_base_api |
true |
Add {{base_api}} variable on urls |
"extra_config": { "with_base_api": false } |
typescript |
postman |
with_base_api |
true |
Add {{base_api}} variable on urls |
"extra_config": { "with_base_api": false } |
csharp |
postman |
with_base_api |
true |
Add {{base_api}} variable on urls |
"extra_config": { "with_base_api": false } |
php |
postman |
name |
empty string |
Name of postman json file |
"extra_config": { "name": "myproject-postman" } |
java |
postman |
name |
empty string |
Name of postman json file |
"extra_config": { "name": "myproject-postman" } |
python |
postman |
name |
empty string |
Name of postman json file |
"extra_config": { "name": "myproject-postman" } |
typescript |
postman |
name |
empty string |
Name of postman json file |
"extra_config": { "name": "myproject-postman" } |
csharp |
postman |
name |
empty string |
Name of postman json file |
"extra_config": { "name": "myproject-postman" } |
java |
spring_security_jwt |
user_field/pass_field/role_field |
|
You must name fields used to identify user (email, username, ..), password (pass, password, ...) and role (rolename, role, ...) |
"extra_config": { "user_field": "users.email", "pass_field": "users.password", "role_field": "roles.role" } |
Configuration examples
Extension Settings
No extension settings by now.
Author
Marc Flausino
Contributors