-
Notifications
You must be signed in to change notification settings - Fork 15
/
native.go
125 lines (104 loc) · 2.77 KB
/
native.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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/mgutz/ansi"
)
// ProcessFileNative processes files uing native string search instead of ast parsing
func ProcessFileNative(filePath string, from string, to string) {
//Colors to be used on the console
red := ansi.ColorCode("red+bh")
white := ansi.ColorCode("white+bh")
green := ansi.ColorCode("green+bh")
yellow := ansi.ColorCode("yellow+bh")
blackOnWhite := ansi.ColorCode("black+b:white+h")
//Reset the color
reset := ansi.ColorCode("reset")
fmt.Println(blackOnWhite+"Processing file", filePath, reset)
// Open file to read
fileContent, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println(err)
}
// Scan file line by line
scanner := bufio.NewScanner(bytes.NewReader(fileContent))
// Track line that is being scanned
scanLine := 0
// Track number of changes in file
numChanges := 0
// Control variables
isImportLine := false
// Store final output text
output := ""
// Scan through the lines of go file
for scanner.Scan() {
scanLine++
line := scanner.Text()
bareLine := strings.Replace(line, " ", "", -1)
// If it is a single import statement, replace the path in that line
if strings.Contains(bareLine, "import\"") {
newImport := strings.Replace(line, from, to, -1)
output += newImport + "\n"
if line != newImport {
numChanges++
fmt.Println(red+"Updating "+
reset+white+
strings.TrimSpace(line)+
reset+red+
" to "+
reset+white+
strings.TrimSpace(newImport)+
reset+red+
"on line", scanLine, reset)
}
continue
}
// Change isImportLine accordingly if import statements are detected
if strings.Contains(bareLine, "import(") {
fmt.Println(green+"Found Multiple Imports Starting On Line", scanLine, reset)
isImportLine = true
} else if isImportLine && strings.Contains(bareLine, ")") {
fmt.Println(green+"Imports Finish On Line", scanLine, reset)
isImportLine = false
}
// If it is a import line, replace the import
if isImportLine {
newImport := strings.Replace(line, from, to, -1)
output += newImport + "\n"
if line != newImport {
numChanges++
fmt.Println(red+"Updating text "+
reset+white+
strings.TrimSpace(line)+
reset+red+
" to "+
reset+white+
strings.TrimSpace(newImport)+
reset+red+
" on line", scanLine, reset)
}
continue
}
// Just copy the rest of the lines to the output
output += line + "\n"
}
// Only write if changes were made
if numChanges > 0 {
fmt.Print(yellow+
"File",
filePath,
"saved after",
numChanges,
"changes",
reset, "\n\n\n")
ioutil.WriteFile(filePath, []byte(output), os.ModePerm)
} else {
fmt.Print(yellow+
"No changes to write on this file.",
reset, "\n\n\n")
}
}