/
main.go
596 lines (529 loc) · 13.7 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
TODO: make watchers for
- less compilation
- typescript compilation
- compilation in go project,
if a file is changed and is a go file,
- the package of the dir of the file is compiled
- the tests are run
- and the main package is compiled (all dirs up until a main.go is found or GOPATH is reached)
- for css classes and ids: compile them into the typescript and less directory
they should be included in the other files
*/
package main
import (
"gopkg.in/fsnotify.v1"
"fmt"
"gopkg.in/metakeule/goh4.v5"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
// "sync/atomic"
)
var (
//cmdId = atomic.A
)
// sync.WaitGroup
type (
Compiler interface {
Compile(path string) (string, error)
Name() string
Lock()
Unlock()
// directories to watch
Dirs() []string
Affected(path string) bool
}
Notifyer interface {
Notify(msg string)
}
compiler struct {
*sync.Mutex
Bin string
Options []string
Dir string
Ext string
name string
Ignore []string // ignored directories as regular expressions
watchDirs []string
}
less struct {
*compiler
OutPutFile string
MainFile string
}
typescript struct {
*compiler
OutPutFile string
}
gocompiler struct {
*compiler
GoPath string
MainFile string
}
ProjectWatcher struct {
*sync.Mutex
Compilers []Compiler
Watcher *fsnotify.Watcher
Notifyer Notifyer
Pool map[Compiler]string
Frequency time.Duration
Ready chan int
}
)
type execStruct struct {
output string
err error
}
var Debug = false
func Exec(name string, opts ...string) (output string, err error) {
//ch := make(chan execStruct, 1)
//go func() {
stamp := time.Now().UnixNano()
id := fmt.Sprintf("%s %s [%v]", name, strings.Join(opts, " "), stamp)
if Debug {
log.Printf("executing: %s", id)
}
cmd := exec.Command(name, opts...)
var out []byte
out, err = cmd.CombinedOutput()
output = string(out)
if Debug {
log.Printf("output of %s:\n%s", id, output)
}
if err != nil {
err = fmt.Errorf(output)
log.Printf("error of %s:\n%s", id, err.Error())
}
// ch <- execStruct{outp, e}
//}()
//x := <-ch
//output = x.output
//err = x.err
return
}
func Which(cmd string) (path string, err error) {
path, err = Exec("which", cmd)
path = strings.TrimRight(path, "\n")
if path == "" {
err = fmt.Errorf("not found: %s", cmd)
}
return
}
func NewGoCompiler(mainfile string, dir string, ignore []string, options ...string) Compiler {
if mainfile != "" {
if filepath.Ext(mainfile) != ".go" {
log.Fatalf("mainfile %#v is not a go file", mainfile)
}
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
log.Fatal("GOPATH is not set")
}
bin, err := Which("go")
if err != nil {
log.Fatal("you do not have the go compiler installed.")
}
/*
if len(options) == 0 {
// recommended options
//options := []string{"--strict-imports", "--verbose", "--no-color", "--line-numbers=all", "--strict-math=on", "--strict-units=off"}
}
*/
return &gocompiler{NewCompiler(dir, "go build", ".go", bin, ignore, options...), gopath, mainfile}
}
func (ø *gocompiler) Compile(file string) (output string, err error) {
if file != ø.MainFile {
dir := filepath.Dir(file)
packageName := strings.Replace(dir, path.Join(ø.GoPath, "src")+"/", "", 1)
output, err = Exec(ø.compiler.Bin, "install", packageName)
if err != nil {
return
}
}
if ø.MainFile == "" {
return
}
// mainDir := filepath.Dir(ø.MainFile)
//packageNameMain := strings.Replace(mainDir, path.Join(ø.GoPath, "src")+"/", "", 1)
//mainOutput := strings.Replace(ø.MainFile, ".go", "", 1)
//_, err = Exec(ø.compiler.Bin, "build", "-o="+mainOutput, packageNameMain)
//output, err = Exec(ø.compiler.Bin, "build", "-o="+mainOutput, ø.MainFile)
//if err != nil {
//output, err = Exec("/bin/sh", "-c='"+mainOutput+" & '")
//eChan := make (chan error, 1)
//output, err = Exec(ø.compiler.Bin, "run", ø.MainFile)
go Exec(ø.compiler.Bin, "run", ø.MainFile)
//}
return
}
func NewCompiler(dir string, name string, ext string, bin string, ignore []string, options ...string) (ø *compiler) {
ø = &compiler{
Mutex: &sync.Mutex{},
Bin: bin,
Options: options,
Dir: dir,
Ext: ext,
name: name,
Ignore: ignore,
watchDirs: []string{},
}
var err error
ø.watchDirs, err = AllAffectedDirs(ø.Dir, ø.Ignore)
if err != nil {
log.Fatalf("can't set watching dirs: %s", err.Error())
}
ø.watchDirs = append(ø.watchDirs, ø.Dir)
return
}
func IsIgnored(file string, ignores []string) (is bool, err error) {
for _, i := range ignores {
reg, e := regexp.Compile(i)
if e != nil {
err = e
return
}
if reg.MatchString(file) {
is = true
return
}
}
return
}
func AllAffectedDirs(base string, ignores []string) (all []string, err error) {
// TODO for every subdir take the affected dirs and merge them
dirs, err := ioutil.ReadDir(base)
if err != nil {
return
}
all = []string{}
for _, dir := range dirs {
if dir.IsDir() == false {
continue
}
is, e := IsIgnored(dir.Name(), ignores)
if e != nil {
err = e
return
}
if is {
continue
}
dirPath := path.Join(base, dir.Name())
all = append(all, dirPath)
var subs []string
subs, err = AllAffectedDirs(dirPath, ignores)
if err != nil {
return
}
all = append(all, subs...)
}
return
}
func (ø *compiler) Dirs() []string { return ø.watchDirs }
func (ø *compiler) Name() string { return ø.name }
func (ø *compiler) Compile(file string) (output string, err error) {
opts := append([]string{}, ø.Options...)
opts = append(opts, file)
output, err = Exec(ø.Bin, opts...)
return
}
func (ø *compiler) Affected(file string) bool {
if filepath.Ext(file) == ø.Ext && IsInDir(file, ø.Dir) {
return true
}
return false
}
func NewLess(mainFile string, dir string, outputFile string, ignore []string, options ...string) Compiler {
bin, err := Which("lessc")
if err != nil {
log.Fatal("you do not have the less compiler installed. please run 'npm install -g less'")
}
if len(options) == 0 {
// recommended options
options = []string{"--strict-imports", "--verbose", "--no-color", "--line-numbers=all", "--strict-math=on", "--strict-units=off"}
}
return &less{NewCompiler(dir, "less (lessc)", ".less", bin, ignore, options...), outputFile, mainFile}
}
func CompileToFile(file string, output string) error {
return ioutil.WriteFile(file, []byte(output), os.FileMode(0644))
}
func (ø *less) Compile(file string) (output string, err error) {
opts := append([]string{}, ø.compiler.Options...)
opts = append(opts, ø.MainFile)
output, err = Exec(ø.Bin, opts...)
if err != nil {
return
}
err = CompileToFile(ø.OutPutFile, output)
return
}
/*
func (ø *typescript) Compile(file) (output string, err error) {
output, err = ø.compiler.Compile(file)
if err != nil {
return
}
err = CompileToFile(ø.OutPutFile, output)
return
}
*/
func NewTypeScript(dir string, outputDir string, ignore []string, options ...string) Compiler {
bin, err := Which("tsc")
if err != nil {
log.Fatal("you do not have the typescript compiler installed. please run 'npm install -g typescript'")
}
if len(options) == 0 {
// recommended options
options = []string{"--out", outputDir, "-c", "--disallowbool", "--disallowbool", "--sourcemap", "--module", "amd"}
}
return &typescript{NewCompiler(dir, "typescript (tsc)", ".ts", bin, ignore, options...), outputDir}
}
func IsInDir(path string, dir string) bool {
return strings.Contains(path, dir)
}
func New(notifyer Notifyer, compilers ...Compiler) (ø *ProjectWatcher) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("can't create watcher: %s\n", err.Error())
}
return &ProjectWatcher{
Mutex: &sync.Mutex{},
Compilers: compilers,
Watcher: watcher,
Ready: make(chan int, 1),
Notifyer: notifyer,
Frequency: time.Millisecond * 20,
// each compiler should be handled at one time with the last file succeeding
Pool: map[Compiler]string{},
}
}
func (ø *ProjectWatcher) SendMessages() {
for {
ø.Lock()
for comp, file := range ø.Pool {
log.Printf("handle %s with %s\n", file, comp.Name())
out, err := comp.Compile(file)
if err != nil {
ø.Notifyer.Notify(out)
}
delete(ø.Pool, comp)
}
ø.Unlock()
time.Sleep(ø.Frequency)
}
}
func (ø *ProjectWatcher) HandleFile(path string) {
// log.Printf("trying to handle: %s\n", path)
for _, c := range ø.Compilers {
c.Lock()
if c.Affected(path) {
ø.Pool[c] = path
}
c.Unlock()
}
}
func (ø *ProjectWatcher) Run() (err error) {
for _, c := range ø.Compilers {
for _, d := range c.Dirs() {
err = ø.Watcher.Watch(d)
if err != nil {
return
}
}
}
go ø.SendMessages()
go func() {
for {
select {
case ev := <-ø.Watcher.Event:
//log.Println("event:", ev)
what := ""
handleIt := true
switch {
case ev.IsCreate():
what = "created"
handleIt = false
case ev.IsDelete():
handleIt = false
what = "deleted"
case ev.IsModify():
what = "modified"
case ev.IsRename():
handleIt = false
what = "renamed"
}
_ = what
//log.Println("file: ", ev.Name, " ", what)
if handleIt {
ø.Lock()
ø.HandleFile(ev.Name)
ø.Unlock()
}
case err := <-ø.Watcher.Error:
log.Println("watcher error:", err)
}
}
ø.Lock()
ø.Ready <- 1
ø.Unlock()
}()
return
}
type zenityBin string
func (ø zenityBin) Notify(msg string) {
Exec(string(ø), "--info", fmt.Sprintf("--text=%s", msg))
}
func Zenity() zenityBin {
path, err := Which("zenity")
if err != nil {
log.Fatalf("can't find zenity")
}
return zenityBin(path)
}
type stripGoPath struct {
Notifyer
GoPath string
}
func StripGoPath(n Notifyer) Notifyer {
gopath := os.Getenv("GOPATH")
if gopath == "" {
log.Fatal("GOPATH is not set")
}
return &stripGoPath{n, path.Join(gopath, "src") + "/"}
}
func (ø *stripGoPath) Notify(msg string) {
nm := strings.Replace(msg, ø.GoPath, "", -1)
ø.Notifyer.Notify(nm)
}
/*
Flags
FSN_CREATE = 1
FSN_MODIFY = 2
FSN_DELETE = 4
FSN_RENAME = 8
FSN_ALL = FSN_MODIFY | FSN_DELETE | FSN_RENAME | FSN_CREATE
*/
//watcher.WatchFlags(path string, flags uint32) error {
// watcher.RemoveWatch(path string)
//watcher.Close()
type cssVarsCompiler struct {
*sync.Mutex
All []goh4.Class
Package string
Dir string
OutputFile string
Bin string
name string
Var string
}
func (ø *cssVarsCompiler) Name() string { return "cssVars compiler (" + ø.name + ")" }
func (ø *cssVarsCompiler) Dirs() []string { return []string{ø.Dir} }
func (ø *cssVarsCompiler) Affected(path string) bool { return IsInDir(path, ø.Dir) }
func (ø *cssVarsCompiler) Compile(path string) (output string, err error) {
// -in=koelnart/frontend/style/class -target=/home/benny/Entwicklung/gopath/src/koelnart/frontend/less/classes.less
if ø.Var == "" {
output, err = Exec(ø.Bin, "-in="+ø.Package, "-target="+ø.OutputFile)
} else {
output, err = Exec(ø.Bin, "-in="+ø.Package, "-target="+ø.OutputFile, "-var="+ø.Var)
}
return
}
func NewCssVars(name string, dir string, output string, var_ string) (ø *cssVarsCompiler) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
log.Fatal("GOPATH is not set")
}
var err error
ø = &cssVarsCompiler{}
ø.name = name
ø.Bin, err = Which("cssvars")
if err != nil {
log.Fatal("you do not have cssvars installed. run go get github.com/metakeule/goh4/css/cssvars")
}
/*
if !IsInDir(gopath, dir) {
log.Fatalf("%s is not in $GOPATH", dir)
}
*/
d, e := os.Stat(dir)
if e != nil {
log.Fatalf("%s does not exists: %s", dir, e.Error())
}
if !d.IsDir() {
log.Fatalf("%s is no package", dir)
}
ø.Dir = dir
packageName := strings.Replace(dir, path.Join(gopath, "src")+"/", "", 1)
ø.Package = packageName
ø.Var = var_
ø.OutputFile = output
ø.Mutex = &sync.Mutex{}
return
}
/*
Compiler interface {
Compile(path string) (string, error)
Name() string
Lock()
Unlock()
// directories to watch
Dirs() []string
Affected(path string) bool
}
*/
func CompilersForApp(baseDir string, app string) []Compiler {
appDir := path.Join(baseDir, "app", app)
staticDir := path.Join(baseDir, "static")
lessDir := path.Join(appDir, "less")
lessOutput := path.Join(staticDir, "css", app, "all.css")
lessMain := path.Join(lessDir, "main.less")
typeScriptDir := path.Join(appDir, "typescript")
typeScriptOutput := path.Join(staticDir, "js", app)
classDir := path.Join(appDir, "class")
idDir := path.Join(appDir, "id")
classOutput := path.Join(lessDir, "classes.less")
idOutput := path.Join(lessDir, "ids.less")
return []Compiler{
NewCssVars(app+" classes.less", classDir, classOutput, ""),
NewCssVars(app+" ids.less", idDir, idOutput, ""),
NewLess(lessMain, lessDir, lessOutput, []string{}),
NewTypeScript(typeScriptDir, typeScriptOutput, []string{}),
}
}
func CompilersForAllApps(baseDir string) (cs []Compiler) {
dirs, e := ioutil.ReadDir(path.Join(baseDir, "app"))
if e != nil {
log.Fatalf("could not read dir %s: %s", baseDir, e.Error())
}
cs = []Compiler{}
for _, dir := range dirs {
if dir.IsDir() == true {
cs = append(cs, CompilersForApp(baseDir, dir.Name())...)
}
}
return
}
func CompilersForProject(baseDir string) (compilers []Compiler) {
mainFile := path.Join(baseDir, "main.go")
compilers = []Compiler{
NewGoCompiler(mainFile, baseDir, []string{"static", "less", "typescript"}),
}
compilers = append(compilers, CompilersForAllApps(baseDir)...)
return
}
func main() {
compilers := CompilersForProject("/home/benny/Entwicklung/gopath/src/koelnart")
projectWatcher := New(StripGoPath(Zenity()), compilers...)
projectWatcher.Run()
<-projectWatcher.Ready
for {
}
}