1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-12-16 21:52:42 +02:00
crud_generator/internal/create_files/makefile/makefile.go
2024-09-20 14:58:46 +03:00

97 lines
2.8 KiB
Go

package makefile
import (
"github.com/ManyakRus/crud_generator/internal/config"
"github.com/ManyakRus/crud_generator/internal/folders"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/micro"
"io/fs"
"os"
"strings"
)
// CreateAllFiles - создаёт файл makefile в корне проекта
func CreateAllFiles() error {
var err error
err = CreateMakefile()
if err != nil {
log.Error("CreateMakefile() error: ", err)
return err
}
return err
}
// CreateMakefile - создаёт 1 файл в папке grpc
func CreateMakefile() error {
var err error
if config.Settings.USE_DEFAULT_TEMPLATE == false {
return err
}
//чтение файлов
DirBin := micro.ProgramDir_bin()
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
DirTemplatesMakefile := DirTemplates
DirReadyMakefile := DirReady
FilenameReadyMakefile := DirReadyMakefile + config.Settings.MAKEFILE_FILENAME
FilenameTemplateMakefile := DirTemplatesMakefile + config.Settings.MAKEFILE_FILENAME + "_"
//создадим папку готовых файлов
folders.CreateFolder(DirReadyMakefile)
bytes, err := os.ReadFile(FilenameTemplateMakefile)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateMakefile, " error: ", err)
}
TextMakefile := string(bytes)
//ReplaceAll
TextMakefile = strings.ReplaceAll(TextMakefile, config.Settings.TEMPLATE_SERVICE_NAME, strings.ToLower(config.Settings.SERVICE_NAME))
//заполним SERVICEURL
VariableName := "SERVICEURL"
Value := config.Settings.SERVICE_REPOSITORY_URL
TextMakefile = ReplaceVariable(TextMakefile, VariableName, Value)
////заполним GENERATION_PROTO
//VariableName = "CD_GENERATION_PROTO"
//Value = "cd ./" + config.Settings.TEMPLATE_FOLDERNAME_GRPC_PROTO + " && \\"
//TextMakefile = ReplaceVariable(TextMakefile, VariableName, Value)
//заполним GENERATION_PROTO
VariableName = "GENERATION_PROTO"
Value = "cd ./" + config.Settings.TEMPLATE_FOLDERNAME_GRPC_PROTO + " && " + "./" + config.Settings.GENERATION_PROTO_FILENAME
TextMakefile = ReplaceVariable(TextMakefile, VariableName, Value)
//запись файла
err = os.WriteFile(FilenameReadyMakefile, []byte(TextMakefile), fs.FileMode(config.Settings.FILE_PERMISSIONS))
return err
}
// ReplaceVariable - заменяет переменную в тексте
func ReplaceVariable(Text, VariableName, Value string) string {
Otvet := Text
pos1 := strings.Index(Otvet, "\n"+VariableName)
if pos1 < 0 {
//нет такого
Otvet = Otvet + "\n" + VariableName + " = " + Value
return Otvet
}
s2 := Text[pos1+1:]
posEnd := strings.Index(s2, "\n")
if posEnd < 0 {
return Otvet
}
Otvet = Otvet[:pos1+1] + VariableName + "=" + Value + Otvet[pos1+1+posEnd:]
return Otvet
}