diff --git a/bin/templates/configs_/settings.txt b/bin/templates/configs_/settings.txt index d8fdd15..9106aeb 100644 --- a/bin/templates/configs_/settings.txt +++ b/bin/templates/configs_/settings.txt @@ -136,6 +136,9 @@ NEED_CREATE_NRPC_SERVER_TEST=true #NEED_CREATE_NRPC_CLIENT_TEST - fill "true" if you want create NRPC client _test.go files NEED_CREATE_NRPC_CLIENT_TEST=true +#NEED_CREATE_MANUAL_FILES - fill "true" if you want create "_manual.go" files, intended for user manual changes +NEED_CREATE_MANUAL_FILES=true + #PREFIX_SERVER_GRPC - filename prefix for grpc server files PREFIX_SERVER_GRPC="server_grpc_" diff --git a/bin/templates/pkg/crud_starter/starter_tables/starter_tables_manual.go_ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables_manual.go_ new file mode 100644 index 0000000..d4e5f9b --- /dev/null +++ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables_manual.go_ @@ -0,0 +1,12 @@ +package crud_starter_lawsuit_status_types + +import ( + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" +) + +// SetCrudManualInterface - заполняет интерфейс crud: DB, GRPC, NRPC +func SetCrudManualInterface(crud lawsuit_status_types.ICrud_manual_LawsuitStatusType) { + lawsuit_status_types.Crud_manual_LawsuitStatusType = crud + + return +} diff --git a/bin/templates/pkg/crud_starter/starter_tables/starter_tables_manual_test.go_ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables_manual_test.go_ new file mode 100644 index 0000000..b2eda4a --- /dev/null +++ b/bin/templates/pkg/crud_starter/starter_tables/starter_tables_manual_test.go_ @@ -0,0 +1,19 @@ +package crud_starter_lawsuit_status_types + +import ( + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/crud/crud_starter_lawsuit_status_types" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "testing" +) + +func TestSetCrudManualInterface(t *testing.T) { + + crud := crud_lawsuit_status_types.Crud_DB{} + + SetCrudManualInterface(crud) + + // Test that the crud variable is set correctly + if employees.Crud_manual_LawsuitStatusType != crud { + t.Errorf("Expected lawsuit_status_types.Crud_manual_LawsuitStatusType to be set to crud, but got %+v", lawsuit_status_types.Crud_manual_LawsuitStatusType) + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 1812cba..9e68171 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -47,6 +47,7 @@ type SettingsINI struct { NEED_CREATE_NRPC bool NEED_CREATE_NRPC_SERVER_TEST bool NEED_CREATE_NRPC_CLIENT_TEST bool + NEED_CREATE_MANUAL_FILES bool SERVICE_NAME string SERVICE_REPOSITORY_URL string TEXT_TEMPLATE_MODEL string @@ -167,6 +168,7 @@ func FillSettings() { Settings.TEMPLATE_FOLDER_CRUD_FUNCTIONS = os.Getenv("TEMPLATE_FOLDER_CRUD_FUNCTIONS") Settings.TEMPLATE_EXTERNAL_PROTO_FILENAME = os.Getenv("TEMPLATE_EXTERNAL_PROTO_FILENAME") Settings.TEMPLATE_FOLDERNAME_GRPC_NRPC = os.Getenv("TEMPLATE_FOLDERNAME_GRPC_NRPC") + Settings.NEED_CREATE_MANUAL_FILES = BoolFromString(os.Getenv("NEED_CREATE_MANUAL_FILES")) } diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 9f35b15..ea6268f 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -39,6 +39,10 @@ const MAKEFILE_FILENAME = "Makefile" const ENV_FILENAME = ".env" const STARTER_TABLES_FILENAME = "starter_tables.go_" -const STARTER_TABLES_FILENAME_TEST = "starter_tables_test.go_" +const STARTER_TABLES_TEST_FILENAME = "starter_tables_test.go_" +const STARTER_TABLES_MANUAL_FILENAME = "starter_tables_manual.go_" +const STARTER_TABLES_TEST_MANUAL_FILENAME = "starter_tables_manual_test.go_" const STARTER_TABLES_PREFIX = "crud_starter_" const CRUD_TABLES_FREFIX = "crud_" + +const MODEL_TABLE_MANUAL_FILENAME = "model_table_manual.go_" diff --git a/internal/create_files/crud_starter_tables/crud_starter_tables.go b/internal/create_files/crud_starter_tables/crud_starter_tables.go index 9717bbc..63ca3db 100644 --- a/internal/create_files/crud_starter_tables/crud_starter_tables.go +++ b/internal/create_files/crud_starter_tables/crud_starter_tables.go @@ -32,6 +32,7 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { return err } } + //тестовые файлы starter if config.Settings.NEED_CREATE_DB_TEST == true { err = CreateTestFiles(Table1) @@ -40,6 +41,20 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { return err } } + + // создание файла manual + if config.Settings.NEED_CREATE_MANUAL_FILES == true { + err = CreateFiles_manual(Table1) + if err != nil { + log.Error("CreateFilesModel_manual() table: ", Table1.Name, " error: ", err) + return err + } + err = CreateFiles_manual_test(Table1) + if err != nil { + log.Error("CreateFiles_manual_test() table: ", Table1.Name, " error: ", err) + return err + } + } } return err @@ -119,7 +134,7 @@ func CreateTestFiles(Table1 *types.Table) error { DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + "starter_tables" + micro.SeparatorFile() DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() - FilenameTemplateDB := DirTemplatesDB + constants.STARTER_TABLES_FILENAME_TEST + FilenameTemplateDB := DirTemplatesDB + constants.STARTER_TABLES_TEST_FILENAME DirReadyTable := DirReadyDB + constants.STARTER_TABLES_PREFIX + TableName FilenameReadyDB := DirReadyTable + micro.SeparatorFile() + constants.STARTER_TABLES_PREFIX + TableName + "_test.go" @@ -161,3 +176,130 @@ func CreateTestFiles(Table1 *types.Table) error { return err } + +// CreateFiles_manual - создаёт 1 файл в папке crud_starter +func CreateFiles_manual(Table1 *types.Table) error { + var err error + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile() + DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + "starter_tables" + micro.SeparatorFile() + DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + + FilenameTemplateDB := DirTemplatesDB + constants.STARTER_TABLES_MANUAL_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyDB + constants.STARTER_TABLES_PREFIX + TableName + FilenameReadyManual := DirReadyTable + micro.SeparatorFile() + constants.STARTER_TABLES_PREFIX + TableName + "_manual.go" + + //создадим каталог + ok, err := micro.FileExists(DirReadyTable) + if ok == false { + err = os.MkdirAll(DirReadyTable, 0777) + if err != nil { + log.Panic("Mkdir() ", DirReadyTable, " error: ", err) + } + } + + bytes, err := os.ReadFile(FilenameTemplateDB) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err) + } + TextManual := string(bytes) + + //заменим имя пакета на новое + TextManual = create_files.ReplacePackageName(TextManual, DirReadyTable) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextManual = create_files.DeleteTemplateRepositoryImports(TextManual) + + ModelTableURL := create_files.FindModelTableURL(TableName) + TextManual = create_files.AddImport(TextManual, ModelTableURL) + + } + + //создание текста + ModelName := Table1.NameGo + TextManual = strings.ReplaceAll(TextManual, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextManual = strings.ReplaceAll(TextManual, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextManual = TextManual + + //замена импортов на новые URL + TextManual = create_files.ReplaceServiceURLImports(TextManual) + + //удаление пустого импорта + TextManual = create_files.DeleteEmptyImport(TextManual) + + //запись файла + err = os.WriteFile(FilenameReadyManual, []byte(TextManual), constants.FILE_PERMISSIONS) + + return err +} + +// CreateFiles_manual_test - создаёт 1 файл в папке crud_starter +func CreateFiles_manual_test(Table1 *types.Table) error { + var err error + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile() + DirTemplatesDB := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + "starter_tables" + micro.SeparatorFile() + DirReadyDB := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD_STARTER + micro.SeparatorFile() + + FilenameTemplateDB := DirTemplatesDB + constants.STARTER_TABLES_TEST_MANUAL_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyDB + constants.STARTER_TABLES_PREFIX + TableName + FilenameReadyManual := DirReadyTable + micro.SeparatorFile() + constants.STARTER_TABLES_PREFIX + TableName + "_manual_test.go" + + //создадим каталог + ok, err := micro.FileExists(DirReadyTable) + if ok == false { + err = os.MkdirAll(DirReadyTable, 0777) + if err != nil { + log.Panic("Mkdir() ", DirReadyTable, " error: ", err) + } + } + + bytes, err := os.ReadFile(FilenameTemplateDB) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateDB, " error: ", err) + } + TextManual := string(bytes) + + //заменим имя пакета на новое + TextManual = create_files.ReplacePackageName(TextManual, DirReadyTable) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextManual = create_files.DeleteTemplateRepositoryImports(TextManual) + + // + ModelTableURL := create_files.FindModelTableURL(TableName) + TextManual = create_files.AddImport(TextManual, ModelTableURL) + + // + CrudStarterTableURL := create_files.FindCrudStarterTableURL(TableName) + TextManual = create_files.AddImport(TextManual, CrudStarterTableURL) + + } + + //создание текста + ModelName := Table1.NameGo + TextManual = strings.ReplaceAll(TextManual, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextManual = strings.ReplaceAll(TextManual, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextManual = TextManual + + //замена импортов на новые URL + TextManual = create_files.ReplaceServiceURLImports(TextManual) + + //удаление пустого импорта + TextManual = create_files.DeleteEmptyImport(TextManual) + + //запись файла + err = os.WriteFile(FilenameReadyManual, []byte(TextManual), constants.FILE_PERMISSIONS) + + return err +} diff --git a/internal/create_files/model/model_test.go b/internal/create_files/model/model_test.go deleted file mode 100644 index 8b53790..0000000 --- a/internal/create_files/model/model_test.go +++ /dev/null @@ -1 +0,0 @@ -package model diff --git a/internal/create_files/model/model.go b/internal/create_files/model_tables/model_tables.go similarity index 88% rename from internal/create_files/model/model.go rename to internal/create_files/model_tables/model_tables.go index 2f67120..ce7f013 100644 --- a/internal/create_files/model/model.go +++ b/internal/create_files/model_tables/model_tables.go @@ -1,4 +1,4 @@ -package model +package model_tables import ( "errors" @@ -18,6 +18,7 @@ import ( func CreateAllFiles(MapAll map[string]*types.Table) error { var err error + //для каждой таблицы for _, table1 := range MapAll { err = CreateFiles(table1) if err != nil { @@ -58,10 +59,20 @@ func CreateFiles(Table1 *types.Table) error { if config.Settings.NEED_CREATE_MODEL_CRUD == true { err = CreateFilesModel_crud(Table1, DirTemplatesModel, DirReadyModel) if err != nil { - log.Error("CreateFilesModel_struct() table: ", Table1.Name, " error: ", err) + log.Error("CreateFilesModel_crud() table: ", Table1.Name, " error: ", err) return err } } + + // создание файла manual + if config.Settings.NEED_CREATE_MANUAL_FILES == true { + err = CreateFilesModel_manual(Table1, DirTemplatesModel, DirReadyModel) + if err != nil { + log.Error("CreateFilesModel_manual() table: ", Table1.Name, " error: ", err) + return err + } + } + return err } @@ -453,3 +464,46 @@ func DeleteFromInterfaceFind_ByExtID(TextModel string, Table1 *types.Table) stri return Otvet } + +// CreateFilesModel_manual - создаёт 1 файл с _manual.go +func CreateFilesModel_manual(Table1 *types.Table, DirTemplatesModel, DirReadyModel string) error { + var err error + + // + ModelName := Table1.NameGo + + TableName := strings.ToLower(Table1.Name) + FilenameTemplateModel := DirTemplatesModel + constants.MODEL_TABLE_MANUAL_FILENAME + FilenameReadyModel := DirReadyModel + TableName + "_manual.go" + + //чтение файла шаблона + bytes, err := os.ReadFile(FilenameTemplateModel) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateModel, " error: ", err) + } + TextModel := string(bytes) + + //заменим имя пакета на новое + TextModel = create_files.ReplacePackageName(TextModel, DirReadyModel) + + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + //TextModel = create_files.DeleteTemplateRepositoryImports(TextModel) + } + + //создание текста + TextModel = strings.ReplaceAll(TextModel, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextModel = strings.ReplaceAll(TextModel, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + //TextModel = config.Settings.TEXT_MODULE_GENERATED + TextModel + + //замена импортов на новые URL + TextModel = create_files.ReplaceServiceURLImports(TextModel) + + //удаление пустого импорта + TextModel = create_files.DeleteEmptyImport(TextModel) + + //запись файла + err = os.WriteFile(FilenameReadyModel, []byte(TextModel), constants.FILE_PERMISSIONS) + + return err +} diff --git a/internal/create_files/model_tables/model_tables_test.go b/internal/create_files/model_tables/model_tables_test.go new file mode 100644 index 0000000..d315b8e --- /dev/null +++ b/internal/create_files/model_tables/model_tables_test.go @@ -0,0 +1 @@ +package model_tables diff --git a/internal/logic/logic.go b/internal/logic/logic.go index 35ab744..55391dc 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -13,7 +13,7 @@ import ( "github.com/ManyakRus/crud_generator/internal/create_files/grpc_server_tables" "github.com/ManyakRus/crud_generator/internal/create_files/main_file" "github.com/ManyakRus/crud_generator/internal/create_files/makefile" - "github.com/ManyakRus/crud_generator/internal/create_files/model" + "github.com/ManyakRus/crud_generator/internal/create_files/model_tables" "github.com/ManyakRus/crud_generator/internal/create_files/nrpc_client" "github.com/ManyakRus/crud_generator/internal/create_files/protobuf" "github.com/ManyakRus/crud_generator/internal/create_files/server_grpc_func" @@ -51,7 +51,7 @@ func StartFillAll() error { } //модель - err = model.CreateAllFiles(MapAll) + err = model_tables.CreateAllFiles(MapAll) if err != nil { //log.Error("model.CreateAllFiles() error: ", err) return err