-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
224 lines (198 loc) · 3.9 KB
/
context.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/cheggaaa/pb/v3"
"github.com/huandu/xstrings"
)
var (
enable = "aligner enable"
disable = "aligner disable"
todo = "todo"
)
type Context struct {
path string // file path
contexts []string
isEnable bool // default true
}
func New(path string) *Context {
return &Context{
isEnable: true,
path: path,
}
}
func (c *Context) FormatFile() {
f, err := os.Open(c.path)
errorCheck(err)
defer f.Close()
reader := bufio.NewReader(f)
var section []string
maxDistance := 0
transfer := func() {
for _, val := range section {
lastIndex := strings.LastIndex(val, comment)
diff := maxDistance - lastIndex
if isCheck && diff > 0 {
info := "file : " + c.path + ", context: "
fmt.Println(info, section)
os.Exit(1)
}
blank := addBlankString(diff)
newLine := xstrings.Insert(val, blank, lastIndex)
c.contexts = append(c.contexts, newLine)
}
section = section[:0]
maxDistance = 0
}
for {
line, _, err := reader.ReadLine()
if err != nil && err != io.EOF {
panic(err)
}
l := string(line)
if err == io.EOF {
transfer()
break
}
c.switched(l)
if c.isSkip(l) {
transfer()
c.contexts = append(c.contexts, l)
continue
}
if !strings.Contains(l, comment) {
transfer()
c.contexts = append(c.contexts, l)
} else {
lastIndex := strings.LastIndex(l, comment)
if lastIndex > maxDistance {
maxDistance = lastIndex
}
section = append(section, l)
}
}
c.writer()
}
func (c *Context) writer() {
if replace {
// TODO
out, err := os.Create(c.path)
defer out.Close()
errorCheck(err)
err = writer(out, c.contexts)
errorCheck(err)
} else {
fmt.Println("-----------" + c.path + "-----------")
err := writer(os.Stdout, c.contexts)
errorCheck(err)
fmt.Println("-----------" + c.path + "-----------")
}
}
func (c *Context) switched(line string) {
if strings.Contains(line, enable) {
c.isEnable = true
}
if strings.Contains(line, disable) {
c.isEnable = false
}
}
func (c *Context) isSkip(line string) bool {
if !c.isEnable {
return true
}
if strings.Contains(line, todo) || strings.Contains(line, strings.ToUpper(todo)) {
return true
}
l := strings.TrimSpace(line)
return strings.Index(l, comment) <= 1
}
func IsIgnore(path string) bool {
if ignore == "" {
return false
}
pi, err := filepath.Abs(ignore)
errorCheck(err)
p, err := filepath.Rel(PWD(), pi)
errorCheck(err)
return strings.Contains(path, p)
}
func IsDir(p string) bool {
s, err := os.Stat(p)
errorCheck(err)
return s.IsDir()
}
func IsDotFile(p string) bool {
return strings.Index(filepath.Base(p), ".") == 0
}
func IsFormatFile(p string) bool {
e := filepath.Ext(p)
if e != "" && e == ext {
if comment == "" {
c, is := langExt[e]
if !is {
return is
}
comment = c // TODO
}
return true
}
if ext == "" && comment != "" && e != "" {
return true
}
return false
}
func load(rootPath string) {
var count int64 = 0
// create and start new bar
bar := pb.StartNew(int(count))
// finish bar
defer bar.Finish()
err := filepath.Walk(
rootPath,
func(path string, info os.FileInfo, err error) error {
if IsIgnore(path) {
return nil
}
if info.IsDir() {
return nil
}
if IsDotFile(path) {
return nil
}
if IsFormatFile(path) {
New(path).FormatFile()
count++
bar.SetCurrent(count - 2)
bar.SetTotal(count)
bar.Increment()
}
return err
},
)
errorCheck(err)
}
func addBlankString(num int) string {
blankStr := ""
for i := 0; i < num; i++ {
blankStr += " "
}
return blankStr
}
func TmpDir() string {
return filepath.Join(PWD(), "tmp")
}
func writer(w io.Writer, contexts []string) error {
bw := bufio.NewWriter(w)
for _, val := range contexts {
// lineStr := fmt.Sprintf("%s", val)
_, err := fmt.Fprintln(bw, val)
if err != nil {
return err
}
}
return bw.Flush()
}