-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoc-watch.go
133 lines (106 loc) · 2.53 KB
/
protoc-watch.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
var (
watcher *fsnotify.Watcher
basePath string
)
func init() {
w, err := fsnotify.NewWatcher()
if err != nil {
fmt.Fprintf(os.Stderr, "could not create the watcher: %s\n", err)
os.Exit(1)
}
watcher = w
if args := strings.Join(os.Args, " "); strings.Contains(args, "-h") || strings.Contains(args, "--help") {
usage()
os.Exit(0)
}
if len(os.Args) < 2 {
fmt.Fprint(os.Stderr, "missing path as argument\n")
os.Exit(1)
}
basePath = filepath.Join(filepath.Dir(os.Args[0]), os.Args[len(os.Args)-1])
if err := lookForProtoc(); err != nil {
fmt.Fprintf(os.Stderr, "could not find the protoc library, please ensure you have it installed: %s\n", err)
os.Exit(1)
}
}
func main() {
registerListeners(basePath)
watch()
}
func lookForProtoc() error {
cmd := exec.Command("protoc", "--version")
return cmd.Start()
}
func usage() {
out, err := exec.Command("protoc", "--help").Output()
if err != nil {
fmt.Fprint(os.Stderr, "could not read: protoc --help\n")
os.Exit(1)
}
fmt.Print(strings.Replace(string(out), "protoc ", "protoc-watch ", 1))
}
func registerListeners(path string) {
watcher.Add(path)
descriptors, err := ioutil.ReadDir(path)
if err != nil {
fmt.Fprintf(os.Stderr, "could not read path: %s\n", err)
os.Exit(1)
}
for _, descriptor := range descriptors {
next := filepath.Join(path, descriptor.Name())
if descriptor.IsDir() && string(descriptor.Name()[0]) != "." {
registerListeners(next)
continue
}
compile(next)
watcher.Add(next)
}
}
func watch() {
for {
select {
case event := <-watcher.Events:
handle(event)
case err := <-watcher.Errors:
fmt.Fprintln(os.Stderr, err)
}
}
}
func handle(event fsnotify.Event) {
if event.Op&fsnotify.Rename == fsnotify.Rename || event.Op&fsnotify.Chmod == fsnotify.Chmod {
return
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
watcher.Remove(event.Name)
return
}
descriptor, err := os.Stat(event.Name)
if err != nil {
fmt.Fprintf(os.Stderr, "could not read path: %s\n", err)
return
}
if descriptor.IsDir() {
registerListeners(event.Name)
return
}
if !strings.Contains(event.String(), ".proto") {
return
}
compile(event.Name)
}
func compile(path string) {
args := strings.Replace(strings.Join(os.Args[1:], " "), basePath, path, 2)
args = strings.Replace(args, ".proto/", ".proto", 1)
out, _ := exec.Command("protoc", strings.Split(args, " ")...).Output()
fmt.Println(path, string(out))
}