-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplacer.go
212 lines (190 loc) · 4.02 KB
/
replacer.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
)
type Replacer struct {
wg sync.WaitGroup
paths []string
extents map[string]struct{}
content string
replace string
recursive bool
ch chan string
}
type option func(*Replacer)
func newReplacer(options ...option) *Replacer {
var wg sync.WaitGroup
defaultPaths := []string{"./"}
defaultExtents := map[string]struct{}{".go": {}, ".js": {}, ".jsx": {}, ".html": {}, ".txt": {}}
ch := make(chan string, 10)
replace := &Replacer{wg: wg, paths: defaultPaths, extents: defaultExtents, ch: ch}
for _, f := range options {
f(replace)
}
return replace
}
func (r *Replacer) init() {
cpuNums := runtime.NumCPU()
for i := 0; i < cpuNums; i++ {
go extracter(&r.wg, r.ch, r.content, r.replace)
}
}
func (r *Replacer) start() {
var once sync.Once
once.Do(r.init)
dumper := func(path string) {
r.wg.Add(1)
r.ch <- path
}
for _, path := range r.paths {
s, err := os.Stat(path)
if os.IsNotExist(err) {
log.Printf("%s not found", path)
continue
}
if s.IsDir() {
if r.recursive {
filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if !info.IsDir() {
if _, ok := r.extents[filepath.Ext(p)]; ok {
absPath, err := filepath.Abs(p)
checkErr(err)
dumper(absPath)
}
}
return nil
})
} else {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Printf("Read dir %s error: %v", path, err)
continue
}
for _, f := range files {
if !f.IsDir() {
if _, ok := r.extents[filepath.Ext(f.Name())]; ok {
absPath, err := filepath.Abs(filepath.Join(path, f.Name()))
checkErr(err)
dumper(absPath)
}
}
}
}
} else {
if _, ok := r.extents[filepath.Ext(path)]; ok {
absPath, err := filepath.Abs(path)
checkErr(err)
dumper(absPath)
}
}
}
//wait utill all the replace complete
r.wg.Wait()
//close the channel
close(r.ch)
}
func withPaths(paths ...string) option {
return func(replacer *Replacer) {
//remove default paths
replacer.paths = []string{}
var exist bool
for i := 0; i < len(paths); i++ {
for j := 0; j < len(replacer.paths); j++ {
if paths[i] == replacer.paths[j] {
exist = true
break
}
}
if !exist {
replacer.paths = append(replacer.paths, paths[i])
}
}
if len(replacer.paths) == 0 {
replacer.paths = []string{"./"}
}
}
}
func withExtents(exts ...string) option {
return func(replacer *Replacer) {
for _, ext := range exts {
if _, ok := replacer.extents[ext]; !ok {
replacer.extents[ext] = struct{}{}
}
}
}
}
func withContent(content string) option {
return func(replace *Replacer) {
replace.content = content
}
}
func withRep(rep string) option {
return func(replace *Replacer) {
replace.replace = rep
}
}
func withRec(rec bool) option {
return func(replace *Replacer) {
replace.recursive = rec
}
}
func extracter(wg *sync.WaitGroup, ch <-chan string, content, rep string) {
for path := range ch {
//control the flow to avoid open too many files
go replace(wg, path, content, rep)
}
}
func replace(wg *sync.WaitGroup, src, content, replace string) {
defer func() {
wg.Done()
<-bucket
}()
//control the flow
bucket <- struct{}{}
var once sync.Once
f, err := os.Open(src)
checkErr(err)
var bs bytes.Buffer
checkErr(err)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, content) {
once.Do(func() {
atomic.AddInt32(&count, 1)
fmt.Printf("Replacing %s...\n", src)
})
line = strings.Replace(line, content, replace, -1)
}
bs.WriteString(line + "\n")
}
if err := scanner.Err(); err != nil {
panic(err)
}
f.Close()
//remove the source file
err = os.Remove(src)
checkErr(err)
//create new file
tf, err := os.Create(src)
checkErr(err)
defer tf.Close()
_, err = io.Copy(tf, &bs)
checkErr(err)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}