2021-10-16 03:58:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-16 14:21:05 +00:00
|
|
|
"path/filepath"
|
|
|
|
"go/ast"
|
2021-10-16 03:58:56 +00:00
|
|
|
"go/doc"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2021-10-16 14:21:05 +00:00
|
|
|
"strings"
|
|
|
|
"os"
|
2021-10-16 03:58:56 +00:00
|
|
|
)
|
|
|
|
|
2022-02-25 22:00:18 +00:00
|
|
|
type EmmyPiece struct {
|
|
|
|
FuncName string
|
|
|
|
Docs []string
|
|
|
|
Params []string // we only need to know param name to put in function
|
|
|
|
}
|
|
|
|
type DocPiece struct {
|
|
|
|
Doc []string
|
|
|
|
FuncSig string
|
|
|
|
FuncName string
|
|
|
|
}
|
|
|
|
|
2021-10-16 16:38:46 +00:00
|
|
|
// feel free to clean this up
|
|
|
|
// it works, dont really care about the code
|
2021-10-16 03:58:56 +00:00
|
|
|
func main() {
|
|
|
|
fset := token.NewFileSet()
|
2022-02-25 22:00:18 +00:00
|
|
|
os.Mkdir("docs", 0777)
|
|
|
|
os.Mkdir("emmyLuaDocs", 0777)
|
|
|
|
|
2021-10-16 03:58:56 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
dirs := []string{"./"}
|
|
|
|
filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error {
|
|
|
|
if !info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dirs = append(dirs, "./" + path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
pkgs := make(map[string]*ast.Package)
|
|
|
|
for _, path := range dirs {
|
|
|
|
d, err := parser.ParseDir(fset, path, nil, parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range d {
|
|
|
|
pkgs[k] = v
|
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
prefix := map[string]string{
|
2021-10-16 16:38:46 +00:00
|
|
|
"hilbish": "hl",
|
2021-10-16 14:21:05 +00:00
|
|
|
"fs": "f",
|
|
|
|
"commander": "c",
|
|
|
|
"bait": "b",
|
2021-10-17 20:10:57 +00:00
|
|
|
"terminal": "term",
|
2021-10-16 14:21:05 +00:00
|
|
|
}
|
2022-02-25 22:00:18 +00:00
|
|
|
docs := make(map[string][]DocPiece)
|
|
|
|
emmyDocs := make(map[string][]EmmyPiece)
|
2021-10-16 03:58:56 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
for l, f := range pkgs {
|
|
|
|
p := doc.New(f, "./", doc.AllDecls)
|
2021-10-16 03:58:56 +00:00
|
|
|
for _, t := range p.Funcs {
|
2021-10-16 16:38:46 +00:00
|
|
|
mod := l
|
|
|
|
if strings.HasPrefix(t.Name, "hl") { mod = "hilbish" }
|
|
|
|
if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" { continue }
|
2022-02-25 00:42:54 +00:00
|
|
|
parts := strings.Split(strings.TrimSpace(t.Doc), "\n")
|
2021-10-16 14:21:05 +00:00
|
|
|
funcsig := parts[0]
|
2021-10-16 16:38:46 +00:00
|
|
|
doc := parts[1:]
|
2022-02-25 22:00:18 +00:00
|
|
|
funcdoc := []string{}
|
|
|
|
em := EmmyPiece{FuncName: strings.TrimPrefix(t.Name, prefix[mod])}
|
|
|
|
for _, d := range doc {
|
|
|
|
if strings.HasPrefix(d, "---") {
|
|
|
|
emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---"))
|
|
|
|
emmyLinePieces := strings.Split(emmyLine, " ")
|
|
|
|
emmyType := emmyLinePieces[0]
|
|
|
|
if emmyType == "@param" {
|
|
|
|
em.Params = append(em.Params, emmyLinePieces[1])
|
|
|
|
}
|
|
|
|
em.Docs = append(em.Docs, d)
|
|
|
|
} else {
|
|
|
|
funcdoc = append(funcdoc, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dps := DocPiece{
|
|
|
|
Doc: funcdoc,
|
|
|
|
FuncSig: funcsig,
|
|
|
|
FuncName: strings.TrimPrefix(t.Name, prefix[mod]),
|
|
|
|
}
|
|
|
|
|
|
|
|
docs[mod] = append(docs[mod], dps)
|
|
|
|
emmyDocs[mod] = append(emmyDocs[mod], em)
|
2021-10-16 14:21:05 +00:00
|
|
|
}
|
|
|
|
for _, t := range p.Types {
|
|
|
|
for _, m := range t.Methods {
|
|
|
|
if !strings.HasPrefix(m.Name, prefix[l]) || m.Name == "Loader" { continue }
|
2022-02-25 00:42:54 +00:00
|
|
|
parts := strings.Split(strings.TrimSpace(m.Doc), "\n")
|
2021-10-16 14:21:05 +00:00
|
|
|
funcsig := parts[0]
|
2021-10-16 16:38:46 +00:00
|
|
|
doc := parts[1:]
|
2022-02-25 22:00:18 +00:00
|
|
|
funcdoc := []string{}
|
|
|
|
em := EmmyPiece{FuncName: strings.TrimPrefix(m.Name, prefix[l])}
|
|
|
|
for _, d := range doc {
|
|
|
|
if strings.HasPrefix(d, "---") {
|
|
|
|
emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---"))
|
|
|
|
emmyLinePieces := strings.Split(emmyLine, " ")
|
|
|
|
emmyType := emmyLinePieces[0]
|
|
|
|
if emmyType == "@param" {
|
|
|
|
em.Params = append(em.Params, emmyLinePieces[1])
|
|
|
|
}
|
|
|
|
em.Docs = append(em.Docs, d)
|
|
|
|
} else {
|
|
|
|
funcdoc = append(funcdoc, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dps := DocPiece{
|
|
|
|
Doc: funcdoc,
|
|
|
|
FuncSig: funcsig,
|
|
|
|
FuncName: strings.TrimPrefix(m.Name, prefix[l]),
|
|
|
|
}
|
2021-10-16 14:21:05 +00:00
|
|
|
|
2022-02-25 22:00:18 +00:00
|
|
|
docs[l] = append(docs[l], dps)
|
|
|
|
emmyDocs[l] = append(emmyDocs[l], em)
|
2021-10-16 14:21:05 +00:00
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-16 16:38:46 +00:00
|
|
|
|
|
|
|
for mod, v := range docs {
|
2022-02-25 22:00:18 +00:00
|
|
|
if mod == "main" { continue }
|
2021-10-16 16:38:46 +00:00
|
|
|
f, _ := os.Create("docs/" + mod + ".txt")
|
2022-02-25 22:00:18 +00:00
|
|
|
for _, dps := range v {
|
|
|
|
f.WriteString(dps.FuncSig + " > ")
|
|
|
|
for _, doc := range dps.Doc {
|
|
|
|
if !strings.HasPrefix(doc, "---") {
|
|
|
|
f.WriteString(doc + "\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for mod, v := range emmyDocs {
|
|
|
|
if mod == "main" { continue }
|
|
|
|
f, _ := os.Create("emmyLuaDocs/" + mod + ".lua")
|
|
|
|
f.WriteString("--- @meta\n\nlocal " + mod + " = {}\n\n")
|
|
|
|
for _, em := range v {
|
|
|
|
var funcdocs []string
|
|
|
|
for _, dps := range docs[mod] {
|
|
|
|
if dps.FuncName == em.FuncName {
|
|
|
|
funcdocs = dps.Doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n")
|
|
|
|
if len(em.Docs) != 0 {
|
|
|
|
f.WriteString(strings.Join(em.Docs, "\n") + "\n")
|
|
|
|
}
|
|
|
|
f.WriteString("function " + mod + "." + em.FuncName + "(" + strings.Join(em.Params, ", ") + ") end\n\n")
|
|
|
|
}
|
|
|
|
f.WriteString("return " + mod + "\n")
|
2021-10-16 16:38:46 +00:00
|
|
|
}
|
2021-10-16 03:58:56 +00:00
|
|
|
}
|