/
main.go
168 lines (137 loc) · 3.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"go/format"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"strings"
)
func main() {
var (
// rootPath directory specifies where the project root is. If not specified
// we will find it by looking for the go.mod file from the current working
// directory.
rootPath = flag.String("root", "", "Root Directory")
// docsPath is the relative path from the root to the docs. defaults to docs.
docsPath = flag.String("docs", "docs", "relative path from root to docs")
// pkgName is the package we are going to build. Defaults to GOPACKAGE env var.
pkgName = flag.String("pkg", os.Getenv("GOPACKAGE"), "go package")
)
flag.Parse()
// current working directory is required.
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
rp := cwd
for *rootPath == "" {
if rp == "/" {
log.Fatal("root volume")
}
if _, err := os.Stat(path.Join(rp, "go.mod")); err == nil {
*rootPath = rp
break
} else {
rp = filepath.Dir(rp)
}
}
outputDir := cwd
rel, err := filepath.Rel(*rootPath, cwd)
if err != nil {
log.Fatal(err)
}
docRoot := filepath.Join(*rootPath, *docsPath, rel)
fmt.Printf("[lit-gen] :: source=%s dest=%s\n", docRoot, outputDir)
if err := walk(docRoot, generate(docRoot, outputDir, *pkgName)); err != nil {
log.Fatal(err)
}
}
func walk(root string, genF func(string) error) error {
files, err := os.ReadDir(root)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".go.md") {
if err := genF(filepath.Join(root, file.Name())); err != nil {
return err
}
}
}
return nil
return filepath.WalkDir(root, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, ".go.md") && !info.IsDir() {
if err := genF(path); err != nil {
return err
}
}
return nil
})
}
func generate(root, destination, pkg string) func(string) error {
return func(in string) error {
f, err := os.Open(in)
if err != nil {
return err
}
defer f.Close()
fmt.Printf("[lit-gen] -> %s\n", filepath.Base(in))
dest := filepath.Join(
destination,
strings.TrimSuffix(strings.TrimPrefix(in, root), ".go.md"),
) + ".generated.go"
out, err := os.Create(dest)
if err != nil {
return fmt.Errorf("opening destination file: %w", err)
}
defer out.Close()
var (
scanner = bufio.NewScanner(f)
w bytes.Buffer
)
var (
shouldWrite bool
)
_, _ = w.WriteString(PREFIX + "\n")
_, _ = w.WriteString("package " + pkg + "\n")
for scanner.Scan() {
line := scanner.Text()
switch line {
case "```":
if shouldWrite {
shouldWrite = false
_, _ = w.WriteString("\n")
}
case "```go":
shouldWrite = true
default:
if shouldWrite {
_, _ = w.WriteString(line + "\n")
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("scanning: %w", err)
}
formatted, err := format.Source(w.Bytes())
if err != nil {
return fmt.Errorf("%s formatting error: %w", in, err)
}
_, err = out.Write(formatted)
if err != nil {
return fmt.Errorf("writing file: %w", err)
}
fmt.Printf("[lit-gen] -> DONE\n")
return nil
}
}
const PREFIX = `// Code Generated by github.com/stanistan/veun-http-demo/cmd/lit-gen; DO NOT EDIT.`