-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbuild.go
204 lines (171 loc) · 5.56 KB
/
build.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
Tool for compiling small snippets in `nsroot` directory into a single snippet file
at `snippets/snippets` and generating `COMMANDS.md`.
To compile to binary (you need golang installed):
$ go build build.go
To run without build (you need golang installed):
$ go run build.go
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const Version = "1.1.0" // 2021-05-02
const root = "nsroot"
const tabSpace = " "
type Snippet struct {
Prefix interface{} `json:"prefix"`
Body interface{} `json:"body"`
Description string `json:"description"`
}
func main() {
if len(os.Args) > 1 { // No argument accepted
fmt.Printf(`Shellman build tool v%v
This tool doesn't accept any argument. Run it from project root directory.
It concatenates 'nsroot' snippets to 'snippets/snippets.json' and generates 'COMMANDS.md'.
`, Version)
os.Exit(1)
}
cwd, errGetCurrentDirectory := os.Getwd()
if errGetCurrentDirectory != nil {
fmt.Printf("Cannot get current directory due to error: %v\n", errGetCurrentDirectory)
panic(errGetCurrentDirectory)
}
snippetFilePath := filepath.Join(cwd, "snippets", "snippets.json")
snippetFilePath = filepath.FromSlash(snippetFilePath) // OS agnostic path
docFilePath := filepath.Join(cwd, "COMMANDS.md")
docFilePath = filepath.FromSlash(docFilePath) // OS agnostic path
docTitleBuilder := strings.Builder{}
docBodyBuilder := strings.Builder{}
docTitleBuilder.WriteString("# Commands\n\n")
changeToRootErr := os.Chdir(root)
if changeToRootErr != nil {
fmt.Println("Run this program from Shellman root directory")
panic(changeToRootErr)
}
folders := getFolders(".")
snippets := map[string]Snippet{}
for _, folder := range folders {
docTitleBuilder.WriteString("### " + folder + "\n\n")
files := getFiles(folder)
errChangeToChildDir := os.Chdir(folder)
if errChangeToChildDir != nil {
fmt.Printf("Cannot change path to child directory: %s due to error: %v\n",
folder, errChangeToChildDir)
panic(errChangeToChildDir)
}
for _, file := range files {
snippetName := folder + "." + strings.TrimSuffix(file, ".json")
jsonFileBytes, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("Cannot read file: %s due to error: %v\n", file, err)
panic(err)
}
var snippet Snippet
json.Unmarshal(jsonFileBytes, &snippet)
namedSnippet := map[string]Snippet{
snippetName: snippet,
}
for k, v := range namedSnippet {
snippets[k] = v
prefixes := []string{}
docTitleBuilder.WriteString(" - [")
docBodyBuilder.WriteString("## ")
switch v.Prefix.(type) {
case []interface{}: // snippet with multiple prefixes
for _, prefix := range v.Prefix.([]interface{}) {
p := prefix.(string)
prefixes = append(prefixes, p)
}
link := strings.Join(prefixes[:], " , ")
docTitleBuilder.WriteString(link)
docTitleBuilder.WriteString("](#")
docBodyBuilder.WriteString(link + "\n\n")
docBodyBuilder.WriteString(v.Description + "[↑](#" + folder + ")\n\n")
link = strings.ReplaceAll(link, " ", "-")
docTitleBuilder.WriteString(link)
case interface{}: // snippet with single prefix
p := v.Prefix.(string)
docTitleBuilder.WriteString(p)
docTitleBuilder.WriteString("](#")
docBodyBuilder.WriteString(p + "\n\n")
docBodyBuilder.WriteString(v.Description + "[↑](#" + folder + ")\n\n")
link := strings.ReplaceAll(p, " ", "-")
docTitleBuilder.WriteString(link)
default:
panic("Unknown prefix type")
}
docTitleBuilder.WriteString(")\n\n")
}
}
errChangeToParentDir := os.Chdir("..")
if errChangeToParentDir != nil {
fmt.Printf("Cannot change path to parent directory: %s due to error: %v\n",
folder, errChangeToParentDir)
panic(errChangeToParentDir)
}
}
// write snippet file
var buf bytes.Buffer
jsonEncoder := json.NewEncoder(&buf)
jsonEncoder.SetEscapeHTML(false)
jsonEncoder.SetIndent("", tabSpace)
jsonEncoder.Encode(snippets)
snippetFileErr := ioutil.WriteFile(snippetFilePath, buf.Bytes(), 0755)
if snippetFileErr != nil {
fmt.Printf("Cannot write snippet file: %v due to error: %v\n",
snippetFilePath, snippetFileErr)
panic(snippetFileErr)
}
// Write documentation file
docFile, docFileErr := os.Create(docFilePath)
if docFileErr != nil {
fmt.Printf("Cannot create documentation file: %v due to error: %v\n",
docFilePath, docFileErr)
panic(docFileErr)
}
defer docFile.Close()
documentation := docTitleBuilder.String() + docBodyBuilder.String()
_, docFileWriteErr := docFile.WriteString(documentation)
if docFileWriteErr != nil {
fmt.Printf("Cannot write documentation file: %v due to error: %v\n",
docFilePath, docFileWriteErr)
panic(docFileWriteErr)
}
}
func getFolders(root string) []string {
var folders []string
files, err := ioutil.ReadDir(root)
if err != nil {
fmt.Printf("Cannot read "+root+" directories: %v due to error: %v\n", root, err)
panic(err)
}
for _, fileInfo := range files {
if fileInfo.IsDir() {
folders = append(folders, fileInfo.Name())
}
}
return folders
}
func getFiles(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Printf("Cannot read directory: %v due to error: %v\n", dir, err)
panic(err)
}
var result []string
for _, fileInfo := range files {
if fileInfo.IsDir() {
fmt.Printf("Namespace directories should not contain nested directories: '%v' contains '%v'\n", dir, fileInfo.Name())
panic("Found nested directories inside namespace directory")
}
result = append(result, fileInfo.Name())
}
return result
}