#87 partial fixed
This commit is contained in:
parent
9df2440e7a
commit
164987d2aa
164
watcher/exec.go
164
watcher/exec.go
|
@ -15,6 +15,88 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GoBuild is an implementation of the "go build"
|
||||||
|
func (p *Project) goBuild() (string, error) {
|
||||||
|
var out bytes.Buffer
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
args := []string{"build"}
|
||||||
|
args = arguments(args, p.Cmds.Build.Args)
|
||||||
|
build := exec.Command("go", args...)
|
||||||
|
build.Dir = p.base
|
||||||
|
build.Stdout = &out
|
||||||
|
build.Stderr = &stderr
|
||||||
|
if err := build.Run(); err != nil {
|
||||||
|
return stderr.String(), err
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GoInstall is an implementation of the "go install"
|
||||||
|
func (p *Project) goInstall() (string, error) {
|
||||||
|
var out bytes.Buffer
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
err := os.Setenv("GOBIN", filepath.Join(getEnvPath("GOPATH"), "bin"))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
args := []string{"install"}
|
||||||
|
for _, arg := range p.Cmds.Install.Args {
|
||||||
|
arr := strings.Fields(arg)
|
||||||
|
args = append(args, arr...)
|
||||||
|
}
|
||||||
|
build := exec.Command("go", args...)
|
||||||
|
build.Dir = p.base
|
||||||
|
build.Stdout = &out
|
||||||
|
build.Stderr = &stderr
|
||||||
|
if err := build.Run(); err != nil {
|
||||||
|
return stderr.String(), err
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exec an additional command from a defined path if specified
|
||||||
|
func (p *Project) command(cmd Command) (errors string, logs string) {
|
||||||
|
var stdout bytes.Buffer
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
command := strings.Replace(strings.Replace(cmd.Command, "'", "", -1), "\"", "", -1)
|
||||||
|
c := strings.Split(command, " ")
|
||||||
|
build := exec.Command(c[0], c[1:]...)
|
||||||
|
build.Dir = p.base
|
||||||
|
if cmd.Path != "" {
|
||||||
|
if strings.Contains(cmd.Path, p.base) {
|
||||||
|
build.Dir = cmd.Path
|
||||||
|
} else {
|
||||||
|
build.Dir = filepath.Join(p.base, cmd.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
build.Stdout = &stdout
|
||||||
|
build.Stderr = &stderr
|
||||||
|
err := build.Run()
|
||||||
|
// check if log
|
||||||
|
logs = stdout.String()
|
||||||
|
if err != nil {
|
||||||
|
errors = stderr.String()
|
||||||
|
return errors, logs
|
||||||
|
}
|
||||||
|
return "", logs
|
||||||
|
}
|
||||||
|
|
||||||
|
// GoTool is used for run go methods such as fmt, test, generate...
|
||||||
|
func (p *Project) goTool(dir string, name string, cmd ...string) (string, error) {
|
||||||
|
if s := filepath.Ext(dir); s != "" && s != ".go" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
var out, stderr bytes.Buffer
|
||||||
|
build := exec.Command(name, cmd...)
|
||||||
|
build.Dir = dir
|
||||||
|
build.Stdout = &out
|
||||||
|
build.Stderr = &stderr
|
||||||
|
if err := build.Run(); err != nil {
|
||||||
|
return stderr.String() + out.String(), err
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
// GoRun is an implementation of the bin execution
|
// GoRun is an implementation of the bin execution
|
||||||
func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error {
|
func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error {
|
||||||
var build *exec.Cmd
|
var build *exec.Cmd
|
||||||
|
@ -105,85 +187,3 @@ func (p *Project) goRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoBuild is an implementation of the "go build"
|
|
||||||
func (p *Project) goBuild() (string, error) {
|
|
||||||
var out bytes.Buffer
|
|
||||||
var stderr bytes.Buffer
|
|
||||||
args := []string{"build"}
|
|
||||||
args = arguments(args, p.Cmds.Build.Args)
|
|
||||||
build := exec.Command("go", args...)
|
|
||||||
build.Dir = p.base
|
|
||||||
build.Stdout = &out
|
|
||||||
build.Stderr = &stderr
|
|
||||||
if err := build.Run(); err != nil {
|
|
||||||
return stderr.String(), err
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GoInstall is an implementation of the "go install"
|
|
||||||
func (p *Project) goInstall() (string, error) {
|
|
||||||
var out bytes.Buffer
|
|
||||||
var stderr bytes.Buffer
|
|
||||||
err := os.Setenv("GOBIN", filepath.Join(getEnvPath("GOPATH"), "bin"))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
args := []string{"install"}
|
|
||||||
for _, arg := range p.Cmds.Install.Args {
|
|
||||||
arr := strings.Fields(arg)
|
|
||||||
args = append(args, arr...)
|
|
||||||
}
|
|
||||||
build := exec.Command("go", args...)
|
|
||||||
build.Dir = p.base
|
|
||||||
build.Stdout = &out
|
|
||||||
build.Stderr = &stderr
|
|
||||||
if err := build.Run(); err != nil {
|
|
||||||
return stderr.String(), err
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GoTool is used for run go methods such as fmt, test, generate...
|
|
||||||
func (p *Project) goTool(dir string, name string, cmd ...string) (string, error) {
|
|
||||||
if s := filepath.Ext(dir); s != "" && s != ".go" {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
var out, stderr bytes.Buffer
|
|
||||||
build := exec.Command(name, cmd...)
|
|
||||||
build.Dir = dir
|
|
||||||
build.Stdout = &out
|
|
||||||
build.Stderr = &stderr
|
|
||||||
if err := build.Run(); err != nil {
|
|
||||||
return stderr.String() + out.String(), err
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exec an additional command from a defined path if specified
|
|
||||||
func (p *Project) command(cmd Command) (errors string, logs string) {
|
|
||||||
var stdout bytes.Buffer
|
|
||||||
var stderr bytes.Buffer
|
|
||||||
command := strings.Replace(strings.Replace(cmd.Command, "'", "", -1), "\"", "", -1)
|
|
||||||
c := strings.Split(command, " ")
|
|
||||||
build := exec.Command(c[0], c[1:]...)
|
|
||||||
build.Dir = p.base
|
|
||||||
if cmd.Path != "" {
|
|
||||||
if strings.Contains(cmd.Path, p.base) {
|
|
||||||
build.Dir = cmd.Path
|
|
||||||
} else {
|
|
||||||
build.Dir = filepath.Join(p.base, cmd.Path)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
build.Stdout = &stdout
|
|
||||||
build.Stderr = &stderr
|
|
||||||
err := build.Run()
|
|
||||||
// check if log
|
|
||||||
logs = stdout.String()
|
|
||||||
if err != nil {
|
|
||||||
errors = stderr.String()
|
|
||||||
return errors, logs
|
|
||||||
}
|
|
||||||
return "", logs
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,13 +4,13 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
|
"github.com/tockins/realize/settings"
|
||||||
"github.com/tockins/realize/style"
|
"github.com/tockins/realize/style"
|
||||||
"log"
|
"log"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -21,26 +21,91 @@ import (
|
||||||
var msg string
|
var msg string
|
||||||
var out BufferOut
|
var out BufferOut
|
||||||
|
|
||||||
// Add a path to paths list
|
// Project defines the informations of a single project
|
||||||
func (w *pollWatcher) Add(path string) error {
|
type Project struct {
|
||||||
if w.paths == nil {
|
settings.Settings
|
||||||
w.paths = map[string]bool{}
|
parent *Blueprint
|
||||||
}
|
path string
|
||||||
w.paths[path] = true
|
tools tools
|
||||||
return nil
|
wr sync.WaitGroup
|
||||||
|
watcher *fsnotify.Watcher
|
||||||
|
channel chan bool
|
||||||
|
exit chan os.Signal
|
||||||
|
base string
|
||||||
|
LastChangedOn time.Time `yaml:"-" json:"-"`
|
||||||
|
Name string `yaml:"name" json:"name"`
|
||||||
|
Path string `yaml:"path" json:"path"`
|
||||||
|
Environment map[string]string `yaml:"environment,omitempty" json:"environment,omitempty"`
|
||||||
|
Cmds Cmds `yaml:"commands" json:"commands"`
|
||||||
|
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
|
||||||
|
Watcher Watcher `yaml:"watcher" json:"watcher"`
|
||||||
|
Buffer Buffer `yaml:"-" json:"buffer"`
|
||||||
|
ErrorOutputPattern string `yaml:"errorOutputPattern,omitempty" json:"errorOutputPattern,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if is watching
|
// Watch the project by fsnotify
|
||||||
func (w *pollWatcher) isWatching(path string) bool {
|
func (p *Project) watchByNotify() {
|
||||||
a, b := w.paths[path]
|
wr := sync.WaitGroup{}
|
||||||
return a && b
|
channel := make(chan bool, 1)
|
||||||
|
watcher := &fsnotify.Watcher{}
|
||||||
|
exit := make(chan os.Signal, 2)
|
||||||
|
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
|
||||||
|
watcher, err := fsnotify.NewWatcher()
|
||||||
|
p.Fatal(err)
|
||||||
|
defer func() {
|
||||||
|
p.cmd("after", true)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
p.cmd("before", true)
|
||||||
|
go p.routines(&wr, channel, watcher, "")
|
||||||
|
p.LastChangedOn = time.Now().Truncate(time.Second)
|
||||||
|
L:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event := <-watcher.Events:
|
||||||
|
if time.Now().Truncate(time.Second).After(p.LastChangedOn) {
|
||||||
|
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if index := strings.Index(filepath.Ext(event.Name), "__"); index != -1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ext := filepath.Ext(event.Name)
|
||||||
|
if inArray(filepath.Ext(event.Name), p.Watcher.Exts) {
|
||||||
|
if p.Cmds.Run {
|
||||||
|
close(channel)
|
||||||
|
channel = make(chan bool)
|
||||||
|
}
|
||||||
|
// repeat the initial cycle
|
||||||
|
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(event.Name))
|
||||||
|
out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + event.Name}
|
||||||
|
p.stamp("log", out, msg, "")
|
||||||
|
// check if is deleted
|
||||||
|
if event.Op&fsnotify.Remove == fsnotify.Remove {
|
||||||
|
go p.routines(&wr, channel, watcher, "")
|
||||||
|
} else {
|
||||||
|
go p.routines(&wr, channel, watcher, event.Name)
|
||||||
|
}
|
||||||
|
p.LastChangedOn = time.Now().Truncate(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err := <-watcher.Errors:
|
||||||
|
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular(err.Error()))
|
||||||
|
out = BufferOut{Time: time.Now(), Text: err.Error()}
|
||||||
|
p.stamp("error", out, msg, "")
|
||||||
|
case <-exit:
|
||||||
|
break L
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch the project by polling
|
// Watch the project by polling
|
||||||
func (p *Project) watchByPolling() {
|
func (p *Project) watchByPolling() {
|
||||||
var wr sync.WaitGroup
|
wr := sync.WaitGroup{}
|
||||||
var watcher = new(pollWatcher)
|
watcher := new(pollWatcher)
|
||||||
channel, exit := make(chan bool, 1), make(chan os.Signal, 2)
|
channel := make(chan bool, 1)
|
||||||
|
exit := make(chan os.Signal, 2)
|
||||||
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
|
||||||
defer func() {
|
defer func() {
|
||||||
p.cmd("after", true)
|
p.cmd("after", true)
|
||||||
|
@ -50,7 +115,6 @@ func (p *Project) watchByPolling() {
|
||||||
go p.routines(&wr, channel, watcher, "")
|
go p.routines(&wr, channel, watcher, "")
|
||||||
p.LastChangedOn = time.Now().Truncate(time.Second)
|
p.LastChangedOn = time.Now().Truncate(time.Second)
|
||||||
walk := func(changed string, info os.FileInfo, err error) error {
|
walk := func(changed string, info os.FileInfo, err error) error {
|
||||||
var ext string
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !watcher.isWatching(changed) {
|
} else if !watcher.isWatching(changed) {
|
||||||
|
@ -58,24 +122,21 @@ func (p *Project) watchByPolling() {
|
||||||
} else if !info.ModTime().Truncate(time.Second).After(p.LastChangedOn) {
|
} else if !info.ModTime().Truncate(time.Second).After(p.LastChangedOn) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if index := strings.Index(filepath.Ext(changed), "_"); index == -1 {
|
if index := strings.Index(filepath.Ext(changed), "__"); index != -1 {
|
||||||
ext = filepath.Ext(changed)
|
return nil
|
||||||
} else {
|
|
||||||
ext = filepath.Ext(changed)[0:index]
|
|
||||||
}
|
}
|
||||||
i := strings.Index(changed, filepath.Ext(changed))
|
ext := filepath.Ext(changed)
|
||||||
file := changed[:i] + ext
|
if inArray(ext, p.Watcher.Exts) {
|
||||||
if changed[:i] != "" && inArray(ext, p.Watcher.Exts) {
|
|
||||||
if p.Cmds.Run {
|
if p.Cmds.Run {
|
||||||
close(channel)
|
close(channel)
|
||||||
channel = make(chan bool)
|
channel = make(chan bool)
|
||||||
}
|
}
|
||||||
p.LastChangedOn = time.Now().Truncate(time.Second)
|
p.LastChangedOn = time.Now().Truncate(time.Second)
|
||||||
// repeat the initial cycle
|
// repeat the initial cycle
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file))
|
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(changed))
|
||||||
out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file}
|
out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + changed}
|
||||||
p.stamp("log", out, msg, "")
|
p.stamp("log", out, msg, "")
|
||||||
go p.routines(&wr, channel, watcher, file)
|
go p.routines(&wr, channel, watcher, changed)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -102,67 +163,58 @@ func (p *Project) watchByPolling() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch the project by fsnotify
|
// Build calls an implementation of the "go build"
|
||||||
func (p *Project) watchByNotify() {
|
func (p *Project) build() error {
|
||||||
var wr sync.WaitGroup
|
if p.Cmds.Build.Status {
|
||||||
var watcher *fsnotify.Watcher
|
start := time.Now()
|
||||||
channel, exit := make(chan bool, 1), make(chan os.Signal, 2)
|
log.Println(p.pname(p.Name, 1), ":", "Building..")
|
||||||
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
|
stream, err := p.goBuild()
|
||||||
watcher, err := fsnotify.NewWatcher()
|
if err != nil {
|
||||||
p.Fatal(err)
|
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Build"), style.Red.Regular(err.Error()))
|
||||||
defer func() {
|
out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream}
|
||||||
p.cmd("after", true)
|
p.stamp("error", out, msg, stream)
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
p.cmd("before", true)
|
|
||||||
go p.routines(&wr, channel, watcher, "")
|
|
||||||
p.LastChangedOn = time.Now().Truncate(time.Second)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case event := <-watcher.Events:
|
|
||||||
if time.Now().Truncate(time.Second).After(p.LastChangedOn) {
|
|
||||||
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var ext string
|
|
||||||
if index := strings.Index(filepath.Ext(event.Name), "_"); index == -1 {
|
|
||||||
ext = filepath.Ext(event.Name)
|
|
||||||
} else {
|
} else {
|
||||||
ext = filepath.Ext(event.Name)[0:index]
|
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Built")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
||||||
|
out = BufferOut{Time: time.Now(), Text: "Built after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
|
||||||
|
p.stamp("log", out, msg, stream)
|
||||||
}
|
}
|
||||||
i := strings.Index(event.Name, filepath.Ext(event.Name))
|
return err
|
||||||
file := event.Name[:i] + ext
|
}
|
||||||
if event.Name[:i] != "" && inArray(ext, p.Watcher.Exts) {
|
return nil
|
||||||
if p.Cmds.Run {
|
|
||||||
close(channel)
|
|
||||||
channel = make(chan bool)
|
|
||||||
}
|
}
|
||||||
// repeat the initial cycle
|
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 4), ":", style.Magenta.Bold(strings.ToUpper(ext[1:])+" changed"), style.Magenta.Bold(file))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: strings.ToUpper(ext[1:]) + " changed " + file}
|
|
||||||
p.stamp("log", out, msg, "")
|
|
||||||
|
|
||||||
// check if is deleted
|
// Install calls an implementation of "go install"
|
||||||
if event.Op&fsnotify.Remove == fsnotify.Remove {
|
func (p *Project) install() error {
|
||||||
go p.routines(&wr, channel, watcher, "")
|
if p.Cmds.Install.Status {
|
||||||
|
start := time.Now()
|
||||||
|
log.Println(p.pname(p.Name, 1), ":", "Installing..")
|
||||||
|
stream, err := p.goInstall()
|
||||||
|
if err != nil {
|
||||||
|
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Install"), style.Red.Regular(err.Error()))
|
||||||
|
out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream}
|
||||||
|
p.stamp("error", out, msg, stream)
|
||||||
} else {
|
} else {
|
||||||
go p.routines(&wr, channel, watcher, file)
|
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Installed")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
||||||
|
out = BufferOut{Time: time.Now(), Text: "Installed after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
|
||||||
|
p.stamp("log", out, msg, stream)
|
||||||
}
|
}
|
||||||
p.LastChangedOn = time.Now().Truncate(time.Second)
|
return err
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
case err := <-watcher.Errors:
|
}
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Regular(err.Error()))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: err.Error()}
|
// Ignore and validate a path
|
||||||
p.stamp("error", out, msg, "")
|
func (p *Project) ignore(str string) bool {
|
||||||
case <-exit:
|
for _, v := range p.Watcher.Ignore {
|
||||||
return
|
if strings.Contains(str, filepath.Join(p.base, v)) {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch the files tree of a project
|
// Watch the files tree of a project
|
||||||
func (p *Project) watch(watcher watcher) error {
|
func (p *Project) walk(watcher watcher) error {
|
||||||
var files, folders int64
|
var files, folders int64
|
||||||
walk := func(path string, info os.FileInfo, err error) error {
|
walk := func(path string, info os.FileInfo, err error) error {
|
||||||
if !p.ignore(path) {
|
if !p.ignore(path) {
|
||||||
|
@ -203,85 +255,6 @@ func (p *Project) watch(watcher watcher) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Install calls an implementation of "go install"
|
|
||||||
func (p *Project) install() error {
|
|
||||||
if p.Cmds.Install.Status {
|
|
||||||
start := time.Now()
|
|
||||||
log.Println(p.pname(p.Name, 1), ":", "Installing..")
|
|
||||||
stream, err := p.goInstall()
|
|
||||||
if err != nil {
|
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Install"), style.Red.Regular(err.Error()))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Install", Stream: stream}
|
|
||||||
p.stamp("error", out, msg, stream)
|
|
||||||
} else {
|
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Installed")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: "Installed after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
|
|
||||||
p.stamp("log", out, msg, stream)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Install calls an implementation of "go run"
|
|
||||||
func (p *Project) run(channel chan bool, wr *sync.WaitGroup) {
|
|
||||||
if p.Cmds.Run {
|
|
||||||
start := time.Now()
|
|
||||||
runner := make(chan bool, 1)
|
|
||||||
log.Println(p.pname(p.Name, 1), ":", "Running..")
|
|
||||||
go p.goRun(channel, runner, wr)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-runner:
|
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Started")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: "Started after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
|
|
||||||
p.stamp("log", out, msg, "")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build calls an implementation of the "go build"
|
|
||||||
func (p *Project) build() error {
|
|
||||||
if p.Cmds.Build.Status {
|
|
||||||
start := time.Now()
|
|
||||||
log.Println(p.pname(p.Name, 1), ":", "Building..")
|
|
||||||
stream, err := p.goBuild()
|
|
||||||
if err != nil {
|
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold("Go Build"), style.Red.Regular(err.Error()))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: err.Error(), Type: "Go Build", Stream: stream}
|
|
||||||
p.stamp("error", out, msg, stream)
|
|
||||||
} else {
|
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Built")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: "Built after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
|
|
||||||
p.stamp("log", out, msg, stream)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tool logs the result of a go command
|
|
||||||
func (p *Project) tool(path string, tool tool) error {
|
|
||||||
if tool.status != nil {
|
|
||||||
v := reflect.ValueOf(tool.status).Elem()
|
|
||||||
if v.Interface().(bool) && (strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "")) {
|
|
||||||
if strings.HasSuffix(path, ".go") {
|
|
||||||
tool.options = append(tool.options, path)
|
|
||||||
path = p.base
|
|
||||||
}
|
|
||||||
if stream, err := p.goTool(path, tool.cmd, tool.options...); err != nil {
|
|
||||||
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold(tool.name), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path))
|
|
||||||
out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: tool.name, Stream: stream}
|
|
||||||
p.stamp("error", out, msg, stream)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cmd calls an wrapper for execute the commands after/before
|
// Cmd calls an wrapper for execute the commands after/before
|
||||||
func (p *Project) cmd(flag string, global bool) {
|
func (p *Project) cmd(flag string, global bool) {
|
||||||
for _, cmd := range p.Watcher.Scripts {
|
for _, cmd := range p.Watcher.Scripts {
|
||||||
|
@ -308,38 +281,23 @@ func (p *Project) cmd(flag string, global bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore and validate a path
|
// Tool logs the result of a go command
|
||||||
func (p *Project) ignore(str string) bool {
|
func (p *Project) tool(path string, tool tool) error {
|
||||||
for _, v := range p.Watcher.Ignore {
|
if tool.status {
|
||||||
if strings.Contains(str, filepath.Join(p.base, v)) {
|
if strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "") {
|
||||||
return true
|
if strings.HasSuffix(path, ".go") {
|
||||||
|
tool.options = append(tool.options, path)
|
||||||
|
path = p.base
|
||||||
|
}
|
||||||
|
if stream, err := p.goTool(path, tool.cmd, tool.options...); err != nil {
|
||||||
|
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", style.Red.Bold(tool.name), style.Red.Regular("there are some errors in"), ":", style.Magenta.Bold(path))
|
||||||
|
out = BufferOut{Time: time.Now(), Text: "there are some errors in", Path: path, Type: tool.name, Stream: stream}
|
||||||
|
p.stamp("error", out, msg, stream)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Routines launches the toolchain run, build, install
|
|
||||||
func (p *Project) routines(wr *sync.WaitGroup, channel chan bool, watcher watcher, file string) {
|
|
||||||
p.cmd("before", false)
|
|
||||||
if len(file) > 0 {
|
|
||||||
path := filepath.Dir(file)
|
|
||||||
p.tool(file, p.tools.Fmt)
|
|
||||||
p.tool(path, p.tools.Vet)
|
|
||||||
p.tool(path, p.tools.Test)
|
|
||||||
p.tool(path, p.tools.Generate)
|
|
||||||
} else {
|
|
||||||
p.Fatal(p.watch(watcher))
|
|
||||||
}
|
|
||||||
install := p.install()
|
|
||||||
build := p.build()
|
|
||||||
wr.Add(1)
|
|
||||||
if install == nil && build == nil {
|
|
||||||
go p.run(channel, wr)
|
|
||||||
}
|
|
||||||
wr.Wait()
|
|
||||||
if len(file) > 0 {
|
|
||||||
p.cmd("after", false)
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Defines the colors scheme for the project name
|
// Defines the colors scheme for the project name
|
||||||
|
@ -364,6 +322,25 @@ func (p *Project) pname(name string, color int) string {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Install calls an implementation of "go run"
|
||||||
|
func (p *Project) run(channel chan bool, wr *sync.WaitGroup) {
|
||||||
|
if p.Cmds.Run {
|
||||||
|
start := time.Now()
|
||||||
|
runner := make(chan bool, 1)
|
||||||
|
log.Println(p.pname(p.Name, 1), ":", "Running..")
|
||||||
|
go p.goRun(channel, runner, wr)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-runner:
|
||||||
|
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", style.Green.Regular("Started")+" after", style.Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
|
||||||
|
out = BufferOut{Time: time.Now(), Text: "Started after " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
|
||||||
|
p.stamp("log", out, msg, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Print on files, cli, ws
|
// Print on files, cli, ws
|
||||||
func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
|
func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
|
||||||
switch t {
|
switch t {
|
||||||
|
@ -414,3 +391,42 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
|
||||||
p.parent.Sync <- "sync"
|
p.parent.Sync <- "sync"
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Routines launches the toolchain run, build, install
|
||||||
|
func (p *Project) routines(wr *sync.WaitGroup, channel chan bool, watcher watcher, file string) {
|
||||||
|
p.cmd("before", false)
|
||||||
|
if len(file) > 0 {
|
||||||
|
path := filepath.Dir(file)
|
||||||
|
p.tool(file, p.tools.Fmt)
|
||||||
|
p.tool(path, p.tools.Vet)
|
||||||
|
p.tool(path, p.tools.Test)
|
||||||
|
p.tool(path, p.tools.Generate)
|
||||||
|
} else {
|
||||||
|
p.Fatal(p.walk(watcher))
|
||||||
|
}
|
||||||
|
install := p.install()
|
||||||
|
build := p.build()
|
||||||
|
wr.Add(1)
|
||||||
|
if install == nil && build == nil {
|
||||||
|
go p.run(channel, wr)
|
||||||
|
}
|
||||||
|
wr.Wait()
|
||||||
|
if len(file) > 0 {
|
||||||
|
p.cmd("after", false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a path to paths list
|
||||||
|
func (w *pollWatcher) Add(path string) error {
|
||||||
|
if w.paths == nil {
|
||||||
|
w.paths = map[string]bool{}
|
||||||
|
}
|
||||||
|
w.paths[path] = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if is watching
|
||||||
|
func (w *pollWatcher) isWatching(path string) bool {
|
||||||
|
a, b := w.paths[path]
|
||||||
|
return a && b
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue