mirror of https://github.com/Hilbis/Hilbish
feat(docgen): support outputting emmylua sources (closes #91)
parent
165cedf328
commit
e75c60ff49
|
@ -11,10 +11,24 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// feel free to clean this up
|
// feel free to clean this up
|
||||||
// it works, dont really care about the code
|
// it works, dont really care about the code
|
||||||
func main() {
|
func main() {
|
||||||
fset := token.NewFileSet()
|
fset := token.NewFileSet()
|
||||||
|
os.Mkdir("docs", 0777)
|
||||||
|
os.Mkdir("emmyLuaDocs", 0777)
|
||||||
|
|
||||||
|
|
||||||
dirs := []string{"./"}
|
dirs := []string{"./"}
|
||||||
filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error {
|
filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error {
|
||||||
|
@ -44,7 +58,8 @@ func main() {
|
||||||
"bait": "b",
|
"bait": "b",
|
||||||
"terminal": "term",
|
"terminal": "term",
|
||||||
}
|
}
|
||||||
docs := make(map[string][]string)
|
docs := make(map[string][]DocPiece)
|
||||||
|
emmyDocs := make(map[string][]EmmyPiece)
|
||||||
|
|
||||||
for l, f := range pkgs {
|
for l, f := range pkgs {
|
||||||
p := doc.New(f, "./", doc.AllDecls)
|
p := doc.New(f, "./", doc.AllDecls)
|
||||||
|
@ -55,8 +70,30 @@ func main() {
|
||||||
parts := strings.Split(strings.TrimSpace(t.Doc), "\n")
|
parts := strings.Split(strings.TrimSpace(t.Doc), "\n")
|
||||||
funcsig := parts[0]
|
funcsig := parts[0]
|
||||||
doc := parts[1:]
|
doc := parts[1:]
|
||||||
|
funcdoc := []string{}
|
||||||
docs[mod] = append(docs[mod], funcsig + " > " + strings.Join(doc, "\n"))
|
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)
|
||||||
}
|
}
|
||||||
for _, t := range p.Types {
|
for _, t := range p.Types {
|
||||||
for _, m := range t.Methods {
|
for _, m := range t.Methods {
|
||||||
|
@ -64,15 +101,64 @@ func main() {
|
||||||
parts := strings.Split(strings.TrimSpace(m.Doc), "\n")
|
parts := strings.Split(strings.TrimSpace(m.Doc), "\n")
|
||||||
funcsig := parts[0]
|
funcsig := parts[0]
|
||||||
doc := parts[1:]
|
doc := parts[1:]
|
||||||
|
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]),
|
||||||
|
}
|
||||||
|
|
||||||
docs[l] = append(docs[l], funcsig + " > " + strings.Join(doc, "\n"))
|
docs[l] = append(docs[l], dps)
|
||||||
|
emmyDocs[l] = append(emmyDocs[l], em)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for mod, v := range docs {
|
for mod, v := range docs {
|
||||||
os.Mkdir("docs", 0777)
|
if mod == "main" { continue }
|
||||||
f, _ := os.Create("docs/" + mod + ".txt")
|
f, _ := os.Create("docs/" + mod + ".txt")
|
||||||
f.WriteString(strings.Join(v, "\n\n") + "\n")
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue