-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.go
120 lines (108 loc) · 2.14 KB
/
util.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
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"unicode"
)
// Converts the given identifier from camelCase to snake_case.
func toSnakeCase(identifier string) string {
var buff bytes.Buffer
for i, char := range identifier {
if i > 0 && unicode.IsUpper(char) {
buff.WriteRune('_')
}
buff.WriteRune(unicode.ToLower(char))
}
return buff.String()
}
// Formats the given comment to have a line length of max. 80 characters.
func formatComment(comment string, indent string) string {
if strings.TrimSpace(comment) == "" {
return ""
}
// split words by whitespaces
var words []string
var word bytes.Buffer
for _, char := range comment {
if unicode.IsSpace(char) {
if word.Len() > 0 {
words = append(words, word.String())
}
word.Reset()
continue
}
word.WriteRune(char)
}
if word.Len() > 0 {
words = append(words, word.String())
}
if len(words) == 0 {
return ""
}
// format the comment
text := ""
line := indent + "//"
for _, word := range words {
nextLine := line + " " + word
if len(nextLine) < 80 {
line = nextLine
} else {
text += line + "\n"
line = indent + "// " + word
}
}
if line != indent+"// " {
text += line + "\n"
}
return text
}
func writeFile(file, content string) {
err := os.WriteFile(file, []byte(content), os.ModePerm)
check(err, "failed to write file: "+file)
}
func isDir(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
func mkdir(path string) {
stat, err := os.Stat(path)
if err == nil {
if stat.IsDir() {
return
}
fmt.Println("ERROR:", path, "exists but it is not a folder")
os.Exit(1)
}
if os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
}
check(err, "could not create folder: "+path)
}
func cleanDir(path ...string) string {
dir := filepath.Join(path...)
_, err := os.Stat(dir)
if err == nil {
os.RemoveAll(dir)
}
mkdir(dir)
return dir
}
func startsWithLower(s string) bool {
if s == "" {
return false
}
for _, char := range s {
if unicode.IsLower(char) {
return true
} else {
return false
}
}
return false
}
func strip(s string) string {
return strings.TrimSpace(s)
}