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

View File

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

View File

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

View File

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

View File

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