This commit is contained in:
alessio 2016-08-21 15:38:00 +02:00
parent a5eb8b0251
commit 09873ae083
6 changed files with 43 additions and 43 deletions

View File

@ -3,9 +3,9 @@ package realize
import ( import (
"fmt" "fmt"
"github.com/fatih/color" "github.com/fatih/color"
"log"
"sync" "sync"
"time" "time"
"log"
) )
const ( const (
@ -32,14 +32,14 @@ var watcherIgnores = []string{"vendor", "bin"}
var watcherExts = []string{".go"} var watcherExts = []string{".go"}
var watcherPaths = []string{"/"} var watcherPaths = []string{"/"}
type logWriter struct {} type logWriter struct{}
// App struct contains the informations about realize // App struct contains the informations about realize
type App struct { type App struct {
Name, Version, Description, Author, Email string Name, Version, Description, Author, Email string
} }
func init(){ func init() {
log.SetFlags(0) log.SetFlags(0)
log.SetOutput(new(logWriter)) log.SetOutput(new(logWriter))
} }
@ -62,5 +62,5 @@ func (app *App) Information() {
} }
func (writer logWriter) Write(bytes []byte) (int, error) { func (writer logWriter) Write(bytes []byte) (int, error) {
return fmt.Print(YellowS("[")+time.Now().UTC().Format("15:04:05") +YellowS("]")+string(bytes)) return fmt.Print(YellowS("[") + time.Now().UTC().Format("15:04:05") + YellowS("]") + string(bytes))
} }

View File

@ -1,14 +1,14 @@
package realize package realize
import ( import (
"testing"
"reflect" "reflect"
"testing"
) )
func TestInit(t *testing.T) { func TestInit(t *testing.T) {
actual := Init() actual := Init()
expected := &App{Name:AppName,Version:AppVersion,Description:AppDescription,Author:AppAuthor,Email:AppEmail} expected := &App{Name: AppName, Version: AppVersion, Description: AppDescription, Author: AppAuthor, Email: AppEmail}
if !reflect.DeepEqual(actual,expected) { if !reflect.DeepEqual(actual, expected) {
t.Errorf("Test failed, expected: '%s', got: '%s'", expected, actual) t.Errorf("Test failed, expected: '%s', got: '%s'", expected, actual)
} }
} }

View File

@ -18,18 +18,18 @@ type Config struct {
} }
// nameParam check the project name presence. If empty takes the working directory name // nameParam check the project name presence. If empty takes the working directory name
func nameParam(params *cli.Context) string{ func nameParam(params *cli.Context) string {
var name string var name string
if params.String("name") == "" { if params.String("name") == "" {
name = params.String("base") name = params.String("base")
}else{ } else {
name = params.String("name") name = params.String("name")
} }
return name return name
} }
func boolParam(b bool) bool{ func boolParam(b bool) bool {
if b{ if b {
return false return false
} }
return true return true
@ -61,7 +61,7 @@ func New(params *cli.Context) *Config {
func Duplicates(value Project, arr []Project) error { func Duplicates(value Project, arr []Project) error {
for _, val := range arr { for _, val := range arr {
if value.Path == val.Path || value.Name == val.Name { if value.Path == val.Path || value.Name == val.Name {
return errors.New("There is a duplicate of '"+val.Name+"'. Check your config file!") return errors.New("There is a duplicate of '" + val.Name + "'. Check your config file!")
} }
} }
return nil return nil
@ -134,7 +134,7 @@ func (h *Config) Add(params *cli.Context) error {
return err return err
} }
err = h.Create() err = h.Create()
if err == nil{ if err == nil {
fmt.Println(Green("The config file was successfully created")) fmt.Println(Green("The config file was successfully created"))
} }
return err return err
@ -167,9 +167,9 @@ func (h *Config) List() error {
fmt.Println(Blue("|"), Blue(strings.ToUpper(val.Name))) fmt.Println(Blue("|"), Blue(strings.ToUpper(val.Name)))
fmt.Println(MagentaS("|"), "\t", Yellow("Base Path"), ":", MagentaS(val.Path)) fmt.Println(MagentaS("|"), "\t", Yellow("Base Path"), ":", MagentaS(val.Path))
fmt.Println(MagentaS("|"), "\t", Yellow("Run"), ":", MagentaS(val.Run)) fmt.Println(MagentaS("|"), "\t", Yellow("Run"), ":", MagentaS(val.Run))
fmt.Println(MagentaS("|"), "\t", Yellow("Build"),":", MagentaS(val.Build)) fmt.Println(MagentaS("|"), "\t", Yellow("Build"), ":", MagentaS(val.Build))
fmt.Println(MagentaS("|"), "\t", Yellow("Install"), ":", MagentaS(val.Bin)) fmt.Println(MagentaS("|"), "\t", Yellow("Install"), ":", MagentaS(val.Bin))
fmt.Println(MagentaS("|"), "\t", Yellow("Watcher"),":") fmt.Println(MagentaS("|"), "\t", Yellow("Watcher"), ":")
fmt.Println(MagentaS("|"), "\t\t", Yellow("After"), ":", MagentaS(val.Watcher.After)) fmt.Println(MagentaS("|"), "\t\t", Yellow("After"), ":", MagentaS(val.Watcher.After))
fmt.Println(MagentaS("|"), "\t\t", Yellow("Before"), ":", MagentaS(val.Watcher.Before)) fmt.Println(MagentaS("|"), "\t\t", Yellow("Before"), ":", MagentaS(val.Watcher.Before))
fmt.Println(MagentaS("|"), "\t\t", Yellow("Extensions"), ":", MagentaS(val.Watcher.Exts)) fmt.Println(MagentaS("|"), "\t\t", Yellow("Extensions"), ":", MagentaS(val.Watcher.Exts))

View File

@ -9,8 +9,8 @@ var context *cli.Context
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
actual := New(context) actual := New(context)
expected := &Config{file:AppFile,Version: AppVersion} expected := &Config{file: AppFile, Version: AppVersion}
if actual == expected { if actual == expected {
t.Errorf("Test failed, expected: '%s', got: '%s'",expected, actual) t.Errorf("Test failed, expected: '%s', got: '%s'", expected, actual)
} }
} }

View File

@ -32,7 +32,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
if len(name) == 1 { if len(name) == 1 {
name := strings.Split(p.base, "/") name := strings.Split(p.base, "/")
run = name[len(name)-1] run = name[len(name)-1]
}else { } else {
run = name[len(name)-1] run = name[len(name)-1]
} }
build := exec.Command(slash(os.Getenv("GOBIN")) + slash(run)) build := exec.Command(slash(os.Getenv("GOBIN")) + slash(run))
@ -41,7 +41,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
if err := build.Process.Kill(); err != nil { if err := build.Process.Kill(); err != nil {
log.Fatal("failed to stop: ", err) log.Fatal("failed to stop: ", err)
} }
log.Println(pname(p.Name,2),":", RedS("Stopped")) log.Println(pname(p.Name, 2), ":", RedS("Stopped"))
wr.Done() wr.Done()
}() }()
@ -61,7 +61,7 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
for in.Scan() { for in.Scan() {
select { select {
default: default:
log.Println(pname(p.Name,3),":",BlueS(in.Text())) log.Println(pname(p.Name, 3), ":", BlueS(in.Text()))
} }
} }
close(stop) close(stop)
@ -96,7 +96,7 @@ func (p *Project) GoInstall() error {
base, _ := os.Getwd() base, _ := os.Getwd()
path := base + p.Path path := base + p.Path
err := os.Setenv("GOBIN",slash(os.Getenv("GOPATH")) + slash("bin")) err := os.Setenv("GOBIN", slash(os.Getenv("GOPATH"))+slash("bin"))
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,13 +3,13 @@ package realize
import ( import (
"fmt" "fmt"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"log"
"math/big"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"time" "time"
"log"
"math/big"
) )
// The Watcher struct defines the livereload's logic // The Watcher struct defines the livereload's logic
@ -46,12 +46,12 @@ func (p *Project) Watching() {
var watcher *fsnotify.Watcher var watcher *fsnotify.Watcher
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
log.Println(strings.ToUpper(pname(p.Name, 1)),": \t", Red(err.Error())) log.Println(strings.ToUpper(pname(p.Name, 1)), ": \t", Red(err.Error()))
} }
channel := make(chan bool, 1) channel := make(chan bool, 1)
base, err := os.Getwd() base, err := os.Getwd()
if err != nil { if err != nil {
log.Println(pname(p.Name, 1),": \t", Red(err.Error())) log.Println(pname(p.Name, 1), ": \t", Red(err.Error()))
} }
walk := func(path string, info os.FileInfo, err error) error { walk := func(path string, info os.FileInfo, err error) error {
@ -71,7 +71,7 @@ func (p *Project) Watching() {
channel = make(chan bool) channel = make(chan bool)
wr.Add(1) wr.Add(1)
go p.build() go p.build()
go p.install(channel,&wr) go p.install(channel, &wr)
} }
end := func() { end := func() {
watcher.Close() watcher.Close()
@ -91,7 +91,7 @@ func (p *Project) Watching() {
log.Println(Red(err.Error())) log.Println(Red(err.Error()))
} }
} else { } else {
fmt.Println(pname(p.Name, 1), ":\t", Red(base + " path doesn't exist")) fmt.Println(pname(p.Name, 1), ":\t", Red(base+" path doesn't exist"))
} }
} }
routines() routines()
@ -107,7 +107,7 @@ func (p *Project) Watching() {
if _, err := os.Stat(event.Name); err == nil { if _, err := os.Stat(event.Name); err == nil {
i := strings.Index(event.Name, filepath.Ext(event.Name)) i := strings.Index(event.Name, filepath.Ext(event.Name))
if event.Name[:i] != "" { if event.Name[:i] != "" {
log.Println(pname(p.Name, 4),":", Magenta(event.Name[:i])) log.Println(pname(p.Name, 4), ":", Magenta(event.Name[:i]))
// stop and run again // stop and run again
close(channel) close(channel)
@ -125,24 +125,24 @@ func (p *Project) Watching() {
} }
// Install call an implementation of the "go install" // Install call an implementation of the "go install"
func (p *Project) install(channel chan bool,wr *sync.WaitGroup) { func (p *Project) install(channel chan bool, wr *sync.WaitGroup) {
if p.Bin { if p.Bin {
log.Println(pname(p.Name, 1), ":","Installing..") log.Println(pname(p.Name, 1), ":", "Installing..")
start := time.Now() start := time.Now()
if err := p.GoInstall(); err != nil { if err := p.GoInstall(); err != nil {
log.Println(pname(p.Name, 1),":",Red(err.Error())) log.Println(pname(p.Name, 1), ":", Red(err.Error()))
wr.Done() wr.Done()
} else { } else {
log.Println(pname(p.Name, 5),":", Green("Installed")+ " after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s")) log.Println(pname(p.Name, 5), ":", Green("Installed")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
if p.Run { if p.Run {
runner := make(chan bool, 1) runner := make(chan bool, 1)
log.Println(pname(p.Name, 1), ":","Running..") log.Println(pname(p.Name, 1), ":", "Running..")
start = time.Now() start = time.Now()
go p.GoRun(channel, runner, wr) go p.GoRun(channel, runner, wr)
for { for {
select { select {
case <-runner: case <-runner:
log.Println(pname(p.Name, 5), ":", Green("Has been run") + " after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s")) log.Println(pname(p.Name, 5), ":", Green("Has been run")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
return return
} }
} }
@ -160,7 +160,7 @@ func (p *Project) build() {
if err := p.GoBuild(); err != nil { if err := p.GoBuild(); err != nil {
log.Println(pname(p.Name, 1), ":", Red(err.Error())) log.Println(pname(p.Name, 1), ":", Red(err.Error()))
} else { } else {
log.Println(pname(p.Name, 5),":", Green("Builded")+ " after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s")) log.Println(pname(p.Name, 5), ":", Green("Builded")+" after", MagentaS(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), "s"))
} }
return return
} }
@ -206,22 +206,22 @@ func slash(str string) string {
} }
// defines the colors scheme for the project name // defines the colors scheme for the project name
func pname(name string, color int) string{ func pname(name string, color int) string {
switch color { switch color {
case 1: case 1:
name = Yellow("[")+strings.ToUpper(name)+Yellow("]") name = Yellow("[") + strings.ToUpper(name) + Yellow("]")
break break
case 2: case 2:
name = Yellow("[")+Red(strings.ToUpper(name))+Yellow("]") name = Yellow("[") + Red(strings.ToUpper(name)) + Yellow("]")
break break
case 3: case 3:
name = Yellow("[")+Blue(strings.ToUpper(name))+Yellow("]") name = Yellow("[") + Blue(strings.ToUpper(name)) + Yellow("]")
break break
case 4: case 4:
name = Yellow("[")+Magenta(strings.ToUpper(name))+Yellow("]") name = Yellow("[") + Magenta(strings.ToUpper(name)) + Yellow("]")
break break
case 5: case 5:
name = Yellow("[")+Green(strings.ToUpper(name))+Yellow("]") name = Yellow("[") + Green(strings.ToUpper(name)) + Yellow("]")
break break
} }
return name return name