2023-11-07 16:35:47 +03:00
|
|
|
|
package lib
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-08 12:50:39 +03:00
|
|
|
|
"fmt"
|
2023-11-07 22:50:48 +03:00
|
|
|
|
"log"
|
2023-11-10 10:08:27 +03:00
|
|
|
|
"strings"
|
2023-11-07 19:59:05 +03:00
|
|
|
|
"text/template"
|
|
|
|
|
|
2023-11-07 16:35:47 +03:00
|
|
|
|
"git.ymnuktech.ru/ymnuk/yt-gen-app/lib/templ"
|
|
|
|
|
"git.ymnuktech.ru/ymnuk/yt-gen-app/structs"
|
2023-11-07 22:50:48 +03:00
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2023-11-07 16:35:47 +03:00
|
|
|
|
"github.com/yuzutech/kroki-go"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Documentation() {
|
|
|
|
|
if !AppConfig.IsHtml {
|
|
|
|
|
AppConfig.IsMarkdown = true
|
|
|
|
|
}
|
|
|
|
|
// Генерация общей схемы БД
|
2023-11-12 14:34:18 +03:00
|
|
|
|
fullDocDBGen()
|
2023-11-07 22:50:48 +03:00
|
|
|
|
// Генерация схем для каждой таблицы
|
|
|
|
|
for _, table := range Project.DB.Tables {
|
|
|
|
|
docDBGen(&table)
|
2023-11-12 14:34:18 +03:00
|
|
|
|
}
|
2023-11-12 14:33:05 +03:00
|
|
|
|
// Генерация информации о ролях
|
|
|
|
|
docRoles()
|
2023-11-07 19:59:05 +03:00
|
|
|
|
var err error
|
|
|
|
|
var tmpl *template.Template
|
|
|
|
|
var out []byte
|
|
|
|
|
if AppConfig.IsSingleDoc {
|
2023-11-12 14:33:05 +03:00
|
|
|
|
docSinglePage()
|
2023-11-09 16:29:29 +03:00
|
|
|
|
} else {
|
|
|
|
|
if tmpl, err = templ.ReadTmplFile("tmpl/docs/index.tmpl"); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if out, err = templ.ExecuteTmplFile(tmpl, Project); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if AppConfig.IsMarkdown {
|
|
|
|
|
if err = templ.WriteFile(AppConfig.OutdirDoc+"/index.md", out); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if AppConfig.IsHtml {
|
|
|
|
|
var buff []byte
|
|
|
|
|
if buff, err = templ.Content.ReadFile("tmpl/docs/styles.tmpl"); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
//{{ includeTemplPart "tmpl/docs/styles.tmpl" nil }}
|
|
|
|
|
out = append(buff, out...)
|
2023-11-10 10:08:27 +03:00
|
|
|
|
out = []byte(strings.ReplaceAll(string(out), ".md", ".html"))
|
|
|
|
|
out = mdToHTML(out)
|
|
|
|
|
if err = templ.WriteFile(AppConfig.OutdirDoc+"/index.html", out); err != nil {
|
2023-11-09 16:29:29 +03:00
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Генерация каждого отдельного файла для каждой таблицы
|
|
|
|
|
if tmpl, err = templ.ReadTmplFile("tmpl/docs/db/entity-table-href.tmpl"); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
for _, table := range Project.DB.Tables {
|
|
|
|
|
// TODO
|
|
|
|
|
if out, err = templ.ExecuteTmplFile(tmpl, table); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if AppConfig.IsMarkdown {
|
|
|
|
|
if err = templ.WriteFile(AppConfig.OutdirDoc+"/"+templ.FieldNameLowerPrepare(table.Name)+".md", out); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if AppConfig.IsHtml {
|
|
|
|
|
var buff1 []byte
|
|
|
|
|
if buff1, err = templ.Content.ReadFile("tmpl/docs/styles.tmpl"); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
//{{ includeTemplPart "tmpl/docs/styles.tmpl" nil }}
|
|
|
|
|
out = append(buff1, out...)
|
2023-11-10 10:08:27 +03:00
|
|
|
|
out = []byte(strings.ReplaceAll(string(out), ".md", ".html"))
|
|
|
|
|
out = mdToHTML(out)
|
|
|
|
|
if err = templ.WriteFile(AppConfig.OutdirDoc+"/"+templ.FieldNameLowerPrepare(table.Name)+".html", out); err != nil {
|
2023-11-09 16:29:29 +03:00
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-07 19:59:05 +03:00
|
|
|
|
}
|
2023-11-07 16:35:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Генерация общей схемы БД
|
|
|
|
|
func fullDocDBGen() {
|
|
|
|
|
// TODO
|
|
|
|
|
if tmpl, err := templ.ReadTmplFile("tmpl/docs/db/full-schema.tmpl"); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
|
|
|
|
if buff, err := templ.ExecuteTmplFile(tmpl, Project.DB); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Println(string(buff))
|
2023-11-07 19:59:05 +03:00
|
|
|
|
//templ.WriteFile(AppConfig.OutdirDoc+"/test.txt", buff)
|
2023-11-07 16:35:47 +03:00
|
|
|
|
client := NewKrokiClient()
|
|
|
|
|
if result, err := client.FromString(string(buff), "dbml", kroki.SVG); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Println(result)
|
2023-11-07 19:59:05 +03:00
|
|
|
|
templ.WriteFile(AppConfig.OutdirDoc+"/full-schemadb.svg", []byte(result))
|
2023-11-07 16:35:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Генерация отдельной таблицы с прилегающими ближайшими таблицами
|
2023-11-07 22:50:48 +03:00
|
|
|
|
func docDBGen(table *structs.Table) {
|
|
|
|
|
restStruct := structs.RestStruct{
|
|
|
|
|
Table: table,
|
|
|
|
|
Children: GetChildrenTables(table),
|
|
|
|
|
Parents: GetParentsTables(table),
|
|
|
|
|
}
|
|
|
|
|
if tmpl, err := templ.ReadTmplFile("tmpl/docs/db/adjacents-tables.tmpl"); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
|
|
|
|
if buff, err := templ.ExecuteTmplFile(tmpl, restStruct); err != nil {
|
2023-11-08 12:50:39 +03:00
|
|
|
|
fmt.Println(string(buff))
|
2023-11-07 22:50:48 +03:00
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Println(string(buff))
|
|
|
|
|
//templ.WriteFile(AppConfig.OutdirDoc+"/test.txt", buff)
|
|
|
|
|
client := NewKrokiClient()
|
|
|
|
|
if result, err := client.FromString(string(buff), "dbml", kroki.SVG); err != nil {
|
2023-11-08 12:50:39 +03:00
|
|
|
|
fmt.Println(string(buff))
|
2023-11-07 22:50:48 +03:00
|
|
|
|
panic(err)
|
|
|
|
|
} else {
|
|
|
|
|
//fmt.Println(result)
|
|
|
|
|
templ.WriteFile(AppConfig.OutdirDoc+"/"+templ.FieldNameLowerPrepare(restStruct.Table.Name)+".svg", []byte(result))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-07 16:35:47 +03:00
|
|
|
|
// TODO
|
|
|
|
|
}
|
2023-11-07 22:50:48 +03:00
|
|
|
|
|
|
|
|
|
func GetParentsTables(table *structs.Table) (tables []structs.Table) {
|
|
|
|
|
for _, fk := range table.FKs {
|
|
|
|
|
for _, tbl := range Project.DB.Tables {
|
|
|
|
|
if fk.TableID == uuid.Nil {
|
|
|
|
|
// Это одна из трех таблиц: user, role, user_role
|
|
|
|
|
switch templ.FieldNameLowerPrepare(fk.TableName) {
|
|
|
|
|
case "user":
|
|
|
|
|
tables = append(tables, structs.Table{
|
|
|
|
|
ID: uuid.Nil,
|
|
|
|
|
Name: "user",
|
|
|
|
|
Pk: "uuid",
|
|
|
|
|
})
|
|
|
|
|
case "role":
|
|
|
|
|
tables = append(tables, structs.Table{
|
|
|
|
|
ID: uuid.Nil,
|
|
|
|
|
Name: "role",
|
|
|
|
|
Pk: "uuid",
|
|
|
|
|
})
|
|
|
|
|
default:
|
|
|
|
|
log.Fatalf("Not found standart table %s", templ.FieldNameLowerPrepare(fk.TableName))
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if fk.TableID == tbl.ID {
|
|
|
|
|
tables = append(tables, tbl)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetChildrenTables(table *structs.Table) (tables []structs.Table) {
|
|
|
|
|
for _, tbl := range Project.DB.Tables {
|
|
|
|
|
for _, fk := range tbl.FKs {
|
|
|
|
|
if fk.TableID == table.ID {
|
|
|
|
|
tables = append(tables, tbl)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|