-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (89 loc) · 2.8 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
c "github.com/fatih/color"
"github.com/fsnotify/fsnotify"
)
var p *c.Color = c.New(c.Bold, c.FgGreen)
func cls() {
print("\033[H\033[2J")
}
func banner() {
c.New(c.FgCyan).Println(
`
░██████╗░░█████╗░░░░░░░░██╗░░░░░░░██╗░█████╗░████████╗░█████╗░██╗░░██╗
██╔════╝░██╔══██╗░░░░░░░██║░░██╗░░██║██╔══██╗╚══██╔══╝██╔══██╗██║░░██║
██║░░██╗░██║░░██║█████╗░╚██╗████╗██╔╝███████║░░░██║░░░██║░░╚═╝███████║
██║░░╚██╗██║░░██║╚════╝░░████╔═████║░██╔══██║░░░██║░░░██║░░██╗██╔══██║
╚██████╔╝╚█████╔╝░░░░░░░░╚██╔╝░╚██╔╝░██║░░██║░░░██║░░░╚█████╔╝██║░░██║
░╚═════╝░░╚════╝░░░░░░░░░░╚═╝░░░╚═╝░░╚═╝░░╚═╝░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝
`)
}
func printWaiting() {
p.Println("Watching for changes...")
}
func printSetup() {
c.New(c.FgHiMagenta, c.Bold).Println("Preparing and starting...")
}
func main() {
cls()
banner()
printSetup()
path, err := os.Getwd()
if err != nil {
print(path)
panic(err.Error())
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
Run(path)
printWaiting()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
separedByDot := strings.Split(event.Name, ".")
if separedByDot[len(separedByDot)-1] == "go" {
// Is a go file
cls()
banner()
printSetup()
Run(path)
printWaiting()
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
err = watcher.Add(path)
if err != nil {
log.Fatal(err)
}
<-done
}
func Run(path string) {
command := fmt.Sprintf(`go build -o bin.exe %v; %v\bin.exe`, path, path)
cmd := exec.Command("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}