aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 0888e934925a1bb84b18b15afc159549f309f174 (plain)
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
// wbr will run a build command and a run command on file system changes.  This is so you can
// build&run on save.
// You specify a build command and run command as flags and optionally you can filter by extension.

// TODO:
// fix bug when only build command is provided that the output is like this:
// [./build.sh [./build.sh] ./main.c ]

package main

import (
	"flag"
	"fmt"
	"os/exec"
	"strings"
	"time"

	"github.com/fsnotify/fsnotify"
)

// Colors in escape sequences
var (
	Reset    = "\033[0m"
	BoldCyan = "\033[36;1m"
	Cyan     = "\033[36m"
	// Red     = "\033[31m"
	// Green   = "\033[32m"
	// Yellow  = "\033[33m"
	// Blue = "\033[34m"
	// Magenta = "\033[35m"
	// Cyan = "\033[36m"
	// Gray    = "\033[37m"
	// White   = "\033[97m"
)

var (
	// Path in which to look for commands
	Path string
	// Command run for building
	BuildCommand []string
	// Command run after build
	RunCommand []string
	// Command will only run on files that have the Extensions extension
	Extensions []string
)

const (
	DateLayout string = "15:04:05"
)

func CompileRun(fileName string) {
	found := false
	for _, Ext := range Extensions {
		if strings.HasSuffix(fileName, Ext) {
			found = true
			break
		}
	}
	if !found {
		return
	}

	// Build the program, exit if return if there was an error
	if len(BuildCommand) > 0 && BuildCommand[0] != "" {
		var buildCmd *exec.Cmd
		if len(BuildCommand) > 1 {
			buildCmd = exec.Command(BuildCommand[0], append(BuildCommand[1:], fileName)...)
		} else {
			buildCmd = exec.Command(BuildCommand[0], fileName)
		}
		// run the build command
		out, err := buildCmd.CombinedOutput()

		date := time.Now().Format(DateLayout)
		if len(BuildCommand) > 1 {
			fmt.Printf("[%s %s%s %s%s]\n", date, Cyan, BuildCommand[0], strings.Join(append(BuildCommand[1:], fileName), " "), Reset)
		} else {
			fmt.Printf("[%s %s%s %s %s%s]\n", date, Cyan, BuildCommand[0], BuildCommand, fileName, Reset)
		}

		if len(out) > 0 {
			fmt.Printf("%s", out)
		}

		if err != nil {
			if _, ok := err.(*exec.ExitError); ok {
				return
			} else if !ok {
				panic(err)
			}
		}
	}

	if len(RunCommand) > 0 && RunCommand[0] != "" {
		// There was no error in the build so we can execute the run command
		var runCmd *exec.Cmd
		if len(RunCommand) > 1 {
			runCmd = exec.Command(RunCommand[0], RunCommand[1:]...)
		} else {
			runCmd = exec.Command(RunCommand[0])
		}

		date := time.Now().Format(DateLayout)
		out, err := runCmd.CombinedOutput()
		if len(RunCommand) > 1 {
			fmt.Printf("[%s %s%s %s%s]\n", date, Cyan, RunCommand[0], strings.Join(RunCommand[1:], " "), Reset)
		} else {
			fmt.Printf("[%s %s%s%s]\n", date, Cyan, RunCommand[0], Reset)

		}
		if len(out) > 0 {
			fmt.Printf("%s", out)
		}

		if err != nil {
			if _, ok := err.(*exec.ExitError); ok {
				return
			} else if !ok {
				panic(err)
			}
		}
	}

}

func main() {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		panic(err)
	}
	defer watcher.Close()

	bc := flag.String("b", "", "Command to run for building")
	rc := flag.String("r", "", "Command for running after build")
	e := flag.String("e", "", "Filter watched files by suffix")
	p := flag.String("p", ".", "Path to watch for files")
	flag.Parse()

	BuildCommand = strings.Split(*bc, " ")
	RunCommand = strings.Split(*rc, " ")
	Extensions = strings.Split(*e, ",")
	Path = *p

	fmt.Printf("[%s watching '%s'%s]\n", Cyan, Path, Reset)

	go func() {
		for {
			select {
			case event, ok := <-watcher.Events:
				if !ok {
					return
				}
				if event.Has(fsnotify.Write) {
					CompileRun(event.Name)
				}
			case err, ok := <-watcher.Errors:
				if !ok {
					return
				}
				fmt.Println("error:", err)
			}
		}
	}()

	err = watcher.Add(Path)
	if err != nil {
		panic(err)
	}

	<-make(chan struct{})
}