2016-07-29 23:02:11 +00:00
|
|
|
package realize
|
|
|
|
|
|
|
|
import (
|
2016-07-31 21:39:56 +00:00
|
|
|
"fmt"
|
2016-08-17 23:35:37 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2016-08-21 21:31:02 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
2016-08-21 13:38:00 +00:00
|
|
|
"log"
|
|
|
|
"math/big"
|
2016-07-29 23:02:11 +00:00
|
|
|
"os"
|
2016-08-17 23:35:37 +00:00
|
|
|
"path/filepath"
|
2016-07-31 21:39:56 +00:00
|
|
|
"strings"
|
2016-08-16 10:20:55 +00:00
|
|
|
"sync"
|
2016-08-17 23:35:37 +00:00
|
|
|
"time"
|
2016-07-29 23:02:11 +00:00
|
|
|
)
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// The Watcher struct defines the livereload's logic
|
2016-08-04 22:59:41 +00:00
|
|
|
type Watcher struct {
|
2016-08-03 08:30:45 +00:00
|
|
|
// different before and after on re-run?
|
2016-08-04 22:59:41 +00:00
|
|
|
Before []string `yaml:"before,omitempty"`
|
|
|
|
After []string `yaml:"after,omitempty"`
|
|
|
|
Paths []string `yaml:"paths,omitempty"`
|
|
|
|
Ignore []string `yaml:"ignore_paths,omitempty"`
|
|
|
|
Exts []string `yaml:"exts,omitempty"`
|
2016-08-17 23:35:37 +00:00
|
|
|
Preview bool `yaml:"preview,omitempty"`
|
2016-07-31 21:39:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Watch method adds the given paths on the Watcher
|
2016-08-04 22:59:41 +00:00
|
|
|
func (h *Config) Watch() error {
|
2016-08-18 07:35:06 +00:00
|
|
|
err := h.Read()
|
2016-08-18 07:27:08 +00:00
|
|
|
if err == nil {
|
2016-08-04 19:56:42 +00:00
|
|
|
// loop projects
|
|
|
|
wg.Add(len(h.Projects))
|
|
|
|
for k := range h.Projects {
|
2016-08-23 00:07:07 +00:00
|
|
|
go h.Projects[k].watching()
|
2016-08-03 16:49:37 +00:00
|
|
|
}
|
2016-08-04 19:56:42 +00:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
2016-08-03 16:49:37 +00:00
|
|
|
}
|
2016-08-18 07:27:08 +00:00
|
|
|
return err
|
2016-08-03 16:49:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-21 18:14:18 +00:00
|
|
|
// Fast method run a project from his working directory without makes a config file
|
2016-08-21 21:31:02 +00:00
|
|
|
func (h *Config) Fast(params *cli.Context) error {
|
2016-08-21 18:14:18 +00:00
|
|
|
fast := h.Projects[0]
|
2016-08-23 00:07:07 +00:00
|
|
|
// Takes the values from config if wd path match with someone else
|
2016-08-21 21:31:02 +00:00
|
|
|
if params.Bool("config") {
|
|
|
|
if err := h.Read(); err == nil {
|
|
|
|
for _, val := range h.Projects {
|
|
|
|
if fast.Path == val.Path {
|
|
|
|
fast = val
|
|
|
|
}
|
2016-08-21 18:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
2016-08-23 00:07:07 +00:00
|
|
|
go fast.watching()
|
2016-08-21 18:14:18 +00:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Watching method is the main core. It manages the livereload and the watching
|
2016-08-23 00:07:07 +00:00
|
|
|
func (p *Project) watching() {
|
2016-07-31 23:52:11 +00:00
|
|
|
|
2016-08-16 10:20:55 +00:00
|
|
|
var wr sync.WaitGroup
|
2016-07-31 23:52:11 +00:00
|
|
|
var watcher *fsnotify.Watcher
|
2016-08-24 12:03:12 +00:00
|
|
|
|
2016-08-17 14:56:06 +00:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
2016-08-17 23:35:37 +00:00
|
|
|
if err != nil {
|
2016-08-23 00:07:07 +00:00
|
|
|
log.Println(strings.ToUpper(pname(p.Name, 1)), ":", Red(err.Error()))
|
2016-08-17 14:56:06 +00:00
|
|
|
}
|
2016-08-17 23:35:37 +00:00
|
|
|
channel := make(chan bool, 1)
|
|
|
|
if err != nil {
|
2016-08-23 00:07:07 +00:00
|
|
|
log.Println(pname(p.Name, 1), ":", Red(err.Error()))
|
2016-08-17 14:56:06 +00:00
|
|
|
}
|
2016-08-17 23:35:37 +00:00
|
|
|
end := func() {
|
2016-08-17 14:56:06 +00:00
|
|
|
watcher.Close()
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
defer end()
|
|
|
|
|
2016-08-24 12:03:12 +00:00
|
|
|
p.walks(watcher)
|
2016-08-23 00:07:07 +00:00
|
|
|
go routines(p, channel, &wr)
|
2016-08-04 19:56:42 +00:00
|
|
|
p.reload = time.Now().Truncate(time.Second)
|
2016-08-24 12:03:12 +00:00
|
|
|
|
|
|
|
// waiting for an event
|
2016-08-01 17:08:37 +00:00
|
|
|
for {
|
|
|
|
select {
|
2016-08-04 22:59:41 +00:00
|
|
|
case event := <-watcher.Events:
|
|
|
|
if time.Now().Truncate(time.Second).After(p.reload) {
|
2016-08-17 23:35:37 +00:00
|
|
|
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
|
2016-08-04 22:59:41 +00:00
|
|
|
continue
|
2016-08-01 17:08:37 +00:00
|
|
|
}
|
2016-08-04 22:59:41 +00:00
|
|
|
if _, err := os.Stat(event.Name); err == nil {
|
2016-08-23 12:07:01 +00:00
|
|
|
var ext string
|
|
|
|
if index := strings.Index(filepath.Ext(event.Name), "_"); index == -1 {
|
|
|
|
ext = filepath.Ext(event.Name)
|
|
|
|
} else {
|
|
|
|
ext = filepath.Ext(event.Name)
|
|
|
|
ext = ext[0:index]
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:59:41 +00:00
|
|
|
i := strings.Index(event.Name, filepath.Ext(event.Name))
|
2016-08-24 13:06:23 +00:00
|
|
|
if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) {
|
2016-08-23 12:07:01 +00:00
|
|
|
log.Println(pname(p.Name, 4), ":", Magenta(event.Name[:i]+ext))
|
2016-08-20 10:47:32 +00:00
|
|
|
// stop and run again
|
2016-08-23 08:52:00 +00:00
|
|
|
if p.Run {
|
|
|
|
close(channel)
|
|
|
|
wr.Wait()
|
2016-08-23 14:32:25 +00:00
|
|
|
channel = make(chan bool)
|
2016-08-23 08:52:00 +00:00
|
|
|
}
|
2016-08-24 12:03:12 +00:00
|
|
|
|
2016-08-23 20:21:15 +00:00
|
|
|
err := p.fmt(event.Name[:i] + ext)
|
2016-08-24 12:03:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(Red(err))
|
2016-08-23 14:10:17 +00:00
|
|
|
} else {
|
2016-08-24 12:17:42 +00:00
|
|
|
go routines(p, channel, &wr)
|
|
|
|
p.reload = time.Now().Truncate(time.Second)
|
2016-08-24 12:03:12 +00:00
|
|
|
}
|
2016-08-20 10:47:32 +00:00
|
|
|
}
|
2016-08-04 22:59:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case err := <-watcher.Errors:
|
2016-08-20 22:29:32 +00:00
|
|
|
log.Println(Red(err.Error()))
|
2016-08-04 19:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 16:49:37 +00:00
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Install call an implementation of the "go install"
|
2016-08-21 13:38:00 +00:00
|
|
|
func (p *Project) install(channel chan bool, wr *sync.WaitGroup) {
|
2016-08-04 19:56:42 +00:00
|
|
|
if p.Bin {
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 1), ":", "Installing..")
|
2016-08-20 14:14:11 +00:00
|
|
|
start := time.Now()
|
2016-08-04 22:59:41 +00:00
|
|
|
if err := p.GoInstall(); err != nil {
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 1), ":", Red(err.Error()))
|
2016-08-19 20:52:53 +00:00
|
|
|
wr.Done()
|
2016-08-04 22:59:41 +00:00
|
|
|
} else {
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 5), ":", Green("Installed")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
|
2016-08-19 20:52:53 +00:00
|
|
|
if p.Run {
|
|
|
|
runner := make(chan bool, 1)
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 1), ":", "Running..")
|
2016-08-20 22:29:32 +00:00
|
|
|
start = time.Now()
|
2016-08-19 20:52:53 +00:00
|
|
|
go p.GoRun(channel, runner, wr)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-runner:
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 5), ":", Green("Has been run")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
|
2016-08-19 20:52:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-31 21:39:56 +00:00
|
|
|
}
|
2016-07-31 23:52:11 +00:00
|
|
|
}
|
2016-08-04 19:56:42 +00:00
|
|
|
return
|
2016-08-01 17:08:37 +00:00
|
|
|
}
|
2016-07-31 21:39:56 +00:00
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Build call an implementation of the "go build"
|
2016-08-04 22:59:41 +00:00
|
|
|
func (p *Project) build() {
|
2016-08-04 19:56:42 +00:00
|
|
|
if p.Build {
|
2016-08-21 10:03:31 +00:00
|
|
|
log.Println(pname(p.Name, 1), ":", "Building..")
|
2016-08-20 14:14:11 +00:00
|
|
|
start := time.Now()
|
2016-08-04 22:59:41 +00:00
|
|
|
if err := p.GoBuild(); err != nil {
|
2016-08-21 10:03:31 +00:00
|
|
|
log.Println(pname(p.Name, 1), ":", Red(err.Error()))
|
2016-08-04 22:59:41 +00:00
|
|
|
} else {
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 5), ":", Green("Builded")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
|
2016-07-29 23:02:11 +00:00
|
|
|
}
|
2016-08-18 07:27:08 +00:00
|
|
|
return
|
2016-07-29 23:02:11 +00:00
|
|
|
}
|
2016-08-04 19:56:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-21 15:12:11 +00:00
|
|
|
// Build call an implementation of the "gofmt"
|
2016-08-23 14:10:17 +00:00
|
|
|
func (p *Project) fmt(path string) error {
|
2016-08-21 15:12:11 +00:00
|
|
|
if p.Fmt {
|
2016-08-23 14:10:17 +00:00
|
|
|
if msg, err := p.GoFmt(path); err != nil {
|
|
|
|
log.Println(pname(p.Name, 1), Red("There are some errors in "), Red(path), Red(":"))
|
2016-08-21 15:12:11 +00:00
|
|
|
fmt.Println(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-24 12:03:12 +00:00
|
|
|
// Walks the file tree of a project
|
|
|
|
func (p *Project) walks(watcher *fsnotify.Watcher) {
|
|
|
|
var files, folders int64
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
|
|
|
|
walk := func(path string, info os.FileInfo, err error) error {
|
2016-08-24 12:17:42 +00:00
|
|
|
if !p.ignore(path) {
|
|
|
|
if (info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.HasPrefix(path, ".")) && !strings.Contains(path, "/.") || (inArray(filepath.Ext(path), p.Watcher.Exts)) {
|
|
|
|
if p.Watcher.Preview {
|
|
|
|
fmt.Println(pname(p.Name, 1), ":", path)
|
|
|
|
}
|
|
|
|
if err = watcher.Add(path); err != nil {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
if inArray(filepath.Ext(path), p.Watcher.Exts) {
|
|
|
|
files++
|
2016-08-24 13:30:42 +00:00
|
|
|
go func() {
|
|
|
|
if err := p.fmt(path); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-08-24 12:17:42 +00:00
|
|
|
} else {
|
|
|
|
folders++
|
|
|
|
}
|
2016-08-24 12:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Path == "." || p.Path == "/" {
|
|
|
|
p.base = wd
|
|
|
|
p.Path = WorkingDir()
|
|
|
|
} else {
|
|
|
|
p.base = filepath.Join(wd, p.Path)
|
|
|
|
}
|
|
|
|
for _, dir := range p.Watcher.Paths {
|
|
|
|
base := filepath.Join(p.base, dir)
|
|
|
|
if _, err := os.Stat(base); err == nil {
|
2016-08-24 12:21:46 +00:00
|
|
|
if err := filepath.Walk(base, walk); err != nil {
|
|
|
|
log.Println(Red(err.Error()))
|
2016-08-24 12:03:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println(pname(p.Name, 1), ":\t", Red(base+" path doesn't exist"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(Red("Watching: "), pname(p.Name, 1), Magenta(files), "files", Magenta(folders), "folders")
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// Ignore validates a path
|
2016-08-17 14:56:06 +00:00
|
|
|
func (p *Project) ignore(str string) bool {
|
|
|
|
for _, v := range p.Watcher.Ignore {
|
2016-08-23 00:07:07 +00:00
|
|
|
if strings.Contains(str, filepath.Join(p.base, v)) {
|
2016-08-03 08:30:45 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-08-23 00:23:17 +00:00
|
|
|
// Routines launches the following methods: run, build, fmt, install
|
|
|
|
func routines(p *Project, channel chan bool, wr *sync.WaitGroup) {
|
2016-08-23 20:21:15 +00:00
|
|
|
wr.Add(1)
|
|
|
|
go p.build()
|
|
|
|
go p.install(channel, wr)
|
|
|
|
wr.Wait()
|
2016-08-23 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 07:27:08 +00:00
|
|
|
// check if a string is inArray
|
2016-08-17 14:56:06 +00:00
|
|
|
func inArray(str string, list []string) bool {
|
2016-08-03 08:30:45 +00:00
|
|
|
for _, v := range list {
|
2016-08-17 14:56:06 +00:00
|
|
|
if v == str {
|
2016-08-03 08:30:45 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2016-08-14 15:22:20 +00:00
|
|
|
}
|
|
|
|
|
2016-08-21 10:03:31 +00:00
|
|
|
// defines the colors scheme for the project name
|
2016-08-21 13:38:00 +00:00
|
|
|
func pname(name string, color int) string {
|
2016-08-21 10:03:31 +00:00
|
|
|
switch color {
|
|
|
|
case 1:
|
2016-08-21 13:38:00 +00:00
|
|
|
name = Yellow("[") + strings.ToUpper(name) + Yellow("]")
|
2016-08-21 10:03:31 +00:00
|
|
|
break
|
|
|
|
case 2:
|
2016-08-21 13:38:00 +00:00
|
|
|
name = Yellow("[") + Red(strings.ToUpper(name)) + Yellow("]")
|
2016-08-21 10:03:31 +00:00
|
|
|
break
|
|
|
|
case 3:
|
2016-08-21 13:38:00 +00:00
|
|
|
name = Yellow("[") + Blue(strings.ToUpper(name)) + Yellow("]")
|
2016-08-21 10:03:31 +00:00
|
|
|
break
|
|
|
|
case 4:
|
2016-08-21 13:38:00 +00:00
|
|
|
name = Yellow("[") + Magenta(strings.ToUpper(name)) + Yellow("]")
|
2016-08-21 10:03:31 +00:00
|
|
|
break
|
|
|
|
case 5:
|
2016-08-21 13:38:00 +00:00
|
|
|
name = Yellow("[") + Green(strings.ToUpper(name)) + Yellow("]")
|
2016-08-21 10:03:31 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|