This commit is contained in:
asoseil 2017-11-27 00:01:08 +01:00
parent 19139c287b
commit 3f131e3817
6 changed files with 63 additions and 47 deletions

View File

@ -2,8 +2,8 @@ package realize
import ( import (
"fmt" "fmt"
"go/build"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"go/build"
"log" "log"
"os" "os"
"os/signal" "os/signal"
@ -14,17 +14,25 @@ import (
) )
const ( const (
// RPrefix tool name
RPrefix = "realize" RPrefix = "realize"
// RVersion current version
RVersion = "2.0" RVersion = "2.0"
// RExt file extension
RExt = ".yaml" RExt = ".yaml"
// RFile config file name
RFile = RPrefix + RExt RFile = RPrefix + RExt
// RDir config dir
RDir = "." + RPrefix RDir = "." + RPrefix
//RExtWin windows extension
RExtWin = ".exe" RExtWin = ".exe"
) )
type ( type (
// LogWriter used for all log
LogWriter struct{} LogWriter struct{}
// Realize main struct
Realize struct { Realize struct {
Settings Settings `yaml:"settings" json:"settings"` Settings Settings `yaml:"settings" json:"settings"`
Server Server `yaml:"server" json:"server"` Server Server `yaml:"server" json:"server"`
@ -38,6 +46,7 @@ type (
Reload Func `yaml:"-"` Reload Func `yaml:"-"`
} }
// Context is used as argument for func
Context struct { Context struct {
Path string Path string
Project *Project Project *Project
@ -46,6 +55,7 @@ type (
Event fsnotify.Event Event fsnotify.Event
} }
// Func is used instead realize func
Func func(Context) Func func(Context)
) )
@ -67,7 +77,7 @@ func (r *Realize) Stop() {
close(r.exit) close(r.exit)
} }
// Run realize workflow // Start realize workflow
func (r *Realize) Start() { func (r *Realize) Start() {
r.exit = make(chan os.Signal, 2) r.exit = make(chan os.Signal, 2)
signal.Notify(r.exit, os.Interrupt, syscall.SIGTERM) signal.Notify(r.exit, os.Interrupt, syscall.SIGTERM)

View File

@ -66,7 +66,7 @@ func PollingWatcher(interval time.Duration) FileWatcher {
} }
} }
// NewWatcher tries to use an fs-event watcher, and falls back to the poller if there is an error // NewFileWatcher tries to use an fs-event watcher, and falls back to the poller if there is an error
func NewFileWatcher(force bool, interval time.Duration) (FileWatcher, error) { func NewFileWatcher(force bool, interval time.Duration) (FileWatcher, error) {
if !force { if !force {
if w, err := EventWatcher(); err == nil { if w, err := EventWatcher(); err == nil {

View File

@ -87,18 +87,18 @@ type BufferOut struct {
} }
// After stop watcher // After stop watcher
func (p *Project) After(){ func (p *Project) After() {
if p.parent.After != nil{ if p.parent.After != nil {
p.parent.After(Context{Project:p}) p.parent.After(Context{Project: p})
return return
} }
p.cmd(nil, "after", true) p.cmd(nil, "after", true)
} }
// Before start watcher // Before start watcher
func (p *Project) Before(){ func (p *Project) Before() {
if p.parent.Before != nil{ if p.parent.Before != nil {
p.parent.Before(Context{Project:p}) p.parent.Before(Context{Project: p})
return return
} }
// setup go tools // setup go tools
@ -127,10 +127,10 @@ func (p *Project) Before(){
p.stamp("log", out, msg, "") p.stamp("log", out, msg, "")
} }
// Error occurred // Err occurred
func (p *Project) Err(err error) { func (p *Project) Err(err error) {
if p.parent.Err != nil{ if p.parent.Err != nil {
p.parent.Err(Context{Project:p}) p.parent.Err(Context{Project: p})
return return
} }
if err != nil { if err != nil {
@ -142,8 +142,8 @@ func (p *Project) Err(err error) {
// Change event message // Change event message
func (p *Project) Change(event fsnotify.Event) { func (p *Project) Change(event fsnotify.Event) {
if p.parent.Change != nil{ if p.parent.Change != nil {
p.parent.Change(Context{Project:p,Event:event}) p.parent.Change(Context{Project: p, Event: event})
return return
} }
// file extension // file extension
@ -159,8 +159,8 @@ func (p *Project) Change(event fsnotify.Event) {
// Reload launches the toolchain run, build, install // Reload launches the toolchain run, build, install
func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) { func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) {
if p.parent.Reload != nil{ if p.parent.Reload != nil {
p.parent.Reload(Context{Project:p,Watcher:watcher,Path:path,Stop:stop}) p.parent.Reload(Context{Project: p, Watcher: watcher, Path: path, Stop: stop})
return return
} }
var done bool var done bool
@ -183,9 +183,9 @@ func (p *Project) Reload(watcher FileWatcher, path string, stop <-chan bool) {
return return
} }
// Go supported tools // Go supported tools
if len(path) > 0{ if len(path) > 0 {
fi, err := os.Stat(path) fi, err := os.Stat(path)
if err != nil{ if err != nil {
p.Err(err) p.Err(err)
} }
p.tools(stop, path, fi) p.tools(stop, path, fi)
@ -340,7 +340,7 @@ L:
// Validate a file path // Validate a file path
func (p *Project) Validate(path string, fiche bool) bool { func (p *Project) Validate(path string, fiche bool) bool {
if len(path) <= 0{ if len(path) <= 0 {
return false return false
} }
// check if skip hidden // check if skip hidden
@ -414,7 +414,7 @@ func (p *Project) tools(stop <-chan bool, path string, fi os.FileInfo) {
if tool.dir { if tool.dir {
result <- tool.Exec(path, stop) result <- tool.Exec(path, stop)
} }
} else if !tool.dir{ } else if !tool.dir {
result <- tool.Exec(path, stop) result <- tool.Exec(path, stop)
} }
} }
@ -483,7 +483,7 @@ func (p *Project) walk(path string, info os.FileInfo, err error) error {
if p.Validate(path, true) { if p.Validate(path, true) {
result := p.watcher.Walk(path, p.init) result := p.watcher.Walk(path, p.init)
if result != "" { if result != "" {
p.tools(p.stop, path,info) p.tools(p.stop, path, info)
if info.IsDir() { if info.IsDir() {
// tools dir // tools dir
p.folders++ p.folders++

View File

@ -1,12 +1,12 @@
package realize package realize
import ( import (
"testing"
"log"
"bytes" "bytes"
"strings"
"os"
"errors" "errors"
"log"
"os"
"strings"
"testing"
) )
func TestProject_After(t *testing.T) { func TestProject_After(t *testing.T) {
@ -14,10 +14,10 @@ func TestProject_After(t *testing.T) {
log.SetOutput(&buf) log.SetOutput(&buf)
r := Realize{} r := Realize{}
input := "text" input := "text"
r.After = func(context Context){ r.After = func(context Context) {
log.Println(input) log.Println(input)
} }
r.Projects = append(r.Projects,Project{ r.Projects = append(r.Projects, Project{
parent: &r, parent: &r,
}) })
r.Projects[0].After() r.Projects[0].After()
@ -30,11 +30,11 @@ func TestProject_Before(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
log.SetOutput(&buf) log.SetOutput(&buf)
r := Realize{} r := Realize{}
r.Projects = append(r.Projects,Project{ r.Projects = append(r.Projects, Project{
parent: &r, parent: &r,
}) })
input := "text" input := "text"
r.Before = func(context Context){ r.Before = func(context Context) {
log.Println(input) log.Println(input)
} }
r.Projects[0].Before() r.Projects[0].Before()
@ -43,15 +43,15 @@ func TestProject_Before(t *testing.T) {
} }
r = Realize{} r = Realize{}
r.Projects = append(r.Projects,Project{ r.Projects = append(r.Projects, Project{
parent: &r, parent: &r,
Environment: map[string]string{ Environment: map[string]string{
input: input, input: input,
}, },
}) })
r.Projects[0].Before() r.Projects[0].Before()
if os.Getenv(input) != input{ if os.Getenv(input) != input {
t.Error("Unexpected error expected", input, "instead",os.Getenv(input)) t.Error("Unexpected error expected", input, "instead", os.Getenv(input))
} }
} }
@ -59,11 +59,11 @@ func TestProject_Err(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
log.SetOutput(&buf) log.SetOutput(&buf)
r := Realize{} r := Realize{}
r.Projects = append(r.Projects,Project{ r.Projects = append(r.Projects, Project{
parent: &r, parent: &r,
}) })
input := "text" input := "text"
r.Err = func(context Context){ r.Err = func(context Context) {
log.Println(input) log.Println(input)
} }
r.Projects[0].Err(errors.New(input)) r.Projects[0].Err(errors.New(input))

View File

@ -5,11 +5,17 @@ import (
) )
var ( var (
//Output writer
Output = color.Output Output = color.Output
// Red color
Red = colorBase(color.FgHiRed) Red = colorBase(color.FgHiRed)
// Blue color
Blue = colorBase(color.FgHiBlue) Blue = colorBase(color.FgHiBlue)
// Green color
Green = colorBase(color.FgHiGreen) Green = colorBase(color.FgHiGreen)
// Yellow color
Yellow = colorBase(color.FgHiYellow) Yellow = colorBase(color.FgHiYellow)
// Magenta color
Magenta = colorBase(color.FgHiMagenta) Magenta = colorBase(color.FgHiMagenta)
) )

View File

@ -2,11 +2,11 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"github.com/tockins/realize/realize" "github.com/tockins/realize/realize"
"log" "log"
"strings" "strings"
"testing" "testing"
"errors"
) )
var mockResponse interface{} var mockResponse interface{}