paths logic rewritten

This commit is contained in:
alessio 2016-08-23 02:07:07 +02:00
parent 91e27188b3
commit 8eddfb3189
4 changed files with 50 additions and 80 deletions

13
main.go
View File

@ -6,7 +6,6 @@ import (
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"log" "log"
"os" "os"
"strings"
) )
func main() { func main() {
@ -30,15 +29,6 @@ func main() {
return nil return nil
} }
wd := func() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(r.Red(err))
}
wd := strings.Split(dir, "/")
return wd[len(wd)-1]
}
cli := &cli.App{ cli := &cli.App{
Name: app.Name, Name: app.Name,
Version: app.Version, Version: app.Version,
@ -74,7 +64,6 @@ func main() {
}, },
Action: func(p *cli.Context) error { Action: func(p *cli.Context) error {
y := r.New(p) y := r.New(p)
y.Projects[0].Path = "/"
return handle(y.Fast(p)) return handle(y.Fast(p))
}, },
Before: func(c *cli.Context) error { Before: func(c *cli.Context) error {
@ -88,7 +77,7 @@ func main() {
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "Add another project", Usage: "Add another project",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: wd(), Usage: "Project name"}, &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: r.WorkingDir(), Usage: "Project name"},
&cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"}, &cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"},
&cli.BoolFlag{Name: "build", Value: false, Usage: "Enable go build"}, &cli.BoolFlag{Name: "build", Value: false, Usage: "Enable go build"},
&cli.BoolFlag{Name: "norun", Usage: "Disables the run"}, &cli.BoolFlag{Name: "norun", Usage: "Disables the run"},

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path/filepath"
"strings" "strings"
) )
@ -22,15 +23,9 @@ type Config struct {
func nameParam(params *cli.Context) string { func nameParam(params *cli.Context) string {
var name string var name string
if params.String("name") == "" && params.String("path") == "" { if params.String("name") == "" && params.String("path") == "" {
dir, err := os.Getwd() return WorkingDir()
if err != nil {
log.Fatal(Red(err))
}
wd := strings.Split(dir, "/")
return wd[len(wd)-1]
} else if params.String("path") != "/" { } else if params.String("path") != "/" {
name = slash(params.String("path")) name = filepath.Base(params.String("path"))
name = name[1:]
} else { } else {
name = params.String("name") name = params.String("name")
} }
@ -45,6 +40,15 @@ func boolParam(b bool) bool {
return true return true
} }
// workingDir returns the last element of the working dir path
func WorkingDir() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(Red(err))
}
return filepath.Base(dir)
}
// New method puts the cli params in the struct // New method puts the cli params in the struct
func New(params *cli.Context) *Config { func New(params *cli.Context) *Config {
return &Config{ return &Config{
@ -53,7 +57,7 @@ func New(params *cli.Context) *Config {
Projects: []Project{ Projects: []Project{
{ {
Name: nameParam(params), Name: nameParam(params),
Path: slash(params.String("path")), Path: filepath.Clean(params.String("path")),
Build: params.Bool("build"), Build: params.Bool("build"),
Bin: boolParam(params.Bool("nobin")), Bin: boolParam(params.Bool("nobin")),
Run: boolParam(params.Bool("norun")), Run: boolParam(params.Bool("norun")),
@ -124,7 +128,7 @@ func (h *Config) Add(params *cli.Context) error {
if err == nil { if err == nil {
new := Project{ new := Project{
Name: nameParam(params), Name: nameParam(params),
Path: slash(params.String("path")), Path: filepath.Clean(params.String("path")),
Build: params.Bool("build"), Build: params.Bool("build"),
Bin: boolParam(params.Bool("nobin")), Bin: boolParam(params.Bool("nobin")),
Run: boolParam(params.Bool("norun")), Run: boolParam(params.Bool("norun")),

View File

@ -3,11 +3,12 @@ package realize
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"io" "io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"strings" "path/filepath"
"sync" "sync"
"time" "time"
) )
@ -27,18 +28,9 @@ type Project struct {
// 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 {
name := strings.Split(p.Path, "/")
stop := make(chan bool, 1) stop := make(chan bool, 1)
var run string build := exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path)))
if name[len(name)-1] == "" {
name := strings.Split(slash(p.base), "/")
run = name[len(name)-1]
} else {
run = name[len(name)-1]
}
build := exec.Command(slash(os.Getenv("GOBIN")) + slash(run))
build.Dir = p.base build.Dir = p.base
defer func() { defer func() {
if err := build.Process.Kill(); err != nil { if err := build.Process.Kill(); err != nil {
@ -96,16 +88,13 @@ func (p *Project) GoBuild() error {
// GoInstall is an implementation of the "go install" // GoInstall is an implementation of the "go install"
func (p *Project) GoInstall() error { func (p *Project) GoInstall() error {
var out bytes.Buffer var out bytes.Buffer
base, _ := os.Getwd() err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin"))
path := base + p.Path
err := os.Setenv("GOBIN", slash(os.Getenv("GOPATH"))+slash("bin"))
if err != nil { if err != nil {
return err return err
} }
build := exec.Command("go", "install") build := exec.Command("go", "install")
build.Dir = path build.Dir = p.base
build.Stdout = &out build.Stdout = &out
if err := build.Run(); err != nil { if err := build.Run(); err != nil {
return err return err

View File

@ -31,8 +31,8 @@ func (h *Config) Watch() error {
// loop projects // loop projects
wg.Add(len(h.Projects)) wg.Add(len(h.Projects))
for k := range h.Projects { for k := range h.Projects {
h.Projects[k].Path = slash(h.Projects[k].Path) h.Projects[k].Path = h.Projects[k].Path
go h.Projects[k].Watching() go h.Projects[k].watching()
} }
wg.Wait() wg.Wait()
return nil return nil
@ -43,7 +43,7 @@ func (h *Config) Watch() error {
// Fast method run a project from his working directory without makes a config file // Fast method run a project from his working directory without makes a config file
func (h *Config) Fast(params *cli.Context) error { func (h *Config) Fast(params *cli.Context) error {
fast := h.Projects[0] fast := h.Projects[0]
// Takes the values from config if wd path match someone else // Takes the values from config if wd path match with someone else
if params.Bool("config") { if params.Bool("config") {
if err := h.Read(); err == nil { if err := h.Read(); err == nil {
for _, val := range h.Projects { for _, val := range h.Projects {
@ -54,26 +54,35 @@ func (h *Config) Fast(params *cli.Context) error {
} }
} }
wg.Add(1) wg.Add(1)
fast.Path = slash(fast.Path) go fast.watching()
go fast.Watching()
wg.Wait() wg.Wait()
return nil return nil
} }
func routines(p *Project, channel chan bool, wr *sync.WaitGroup) {
channel = make(chan bool)
err := p.fmt()
if err == nil {
wr.Add(1)
go p.build()
go p.install(channel, wr)
}
}
// Watching method is the main core. It manages the livereload and the watching // Watching method is the main core. It manages the livereload and the watching
func (p *Project) Watching() { func (p *Project) watching() {
var wr sync.WaitGroup var wr sync.WaitGroup
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)), ":", Red(err.Error()))
} }
channel := make(chan bool, 1) channel := make(chan bool, 1)
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), ":", Red(err.Error()))
} }
wd, _ := os.Getwd()
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) {
@ -88,25 +97,20 @@ func (p *Project) Watching() {
} }
return nil return nil
} }
routines := func() {
channel = make(chan bool)
err = p.fmt()
if err == nil {
wr.Add(1)
go p.build()
go p.install(channel, &wr)
}
}
end := func() { end := func() {
watcher.Close() watcher.Close()
wg.Done() wg.Done()
} }
defer end() defer end()
p.base = base + p.Path if p.Path == "." {
p.base = wd
p.Path = WorkingDir()
} else {
p.base = filepath.Join(wd, p.Path)
}
for _, dir := range p.Watcher.Paths { for _, dir := range p.Watcher.Paths {
dir = slash(dir) base := filepath.Join(p.base, dir)
base = p.base + dir
if _, err := os.Stat(base); err == nil { if _, err := os.Stat(base); err == nil {
if err := filepath.Walk(base, walk); err != nil { if err := filepath.Walk(base, walk); err != nil {
log.Println(Red(err.Error())) log.Println(Red(err.Error()))
@ -115,8 +119,9 @@ func (p *Project) Watching() {
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"))
} }
} }
fmt.Println(Red("Watching: " + pname(p.Name, 1) + "\n")) fmt.Println(Red("Watching: " + pname(p.Name, 1) + "\n"))
routines() go routines(p, channel, &wr)
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
for { for {
select { select {
@ -133,8 +138,7 @@ func (p *Project) Watching() {
// stop and run again // stop and run again
close(channel) close(channel)
wr.Wait() wr.Wait()
routines() go routines(p, channel, &wr)
p.reload = time.Now().Truncate(time.Second) p.reload = time.Now().Truncate(time.Second)
} }
} }
@ -203,8 +207,7 @@ func (p *Project) fmt() error {
// Ignore validates a path // Ignore validates a path
func (p *Project) ignore(str string) bool { func (p *Project) ignore(str string) bool {
for _, v := range p.Watcher.Ignore { for _, v := range p.Watcher.Ignore {
v = slash(v) if strings.Contains(str, filepath.Join(p.base, v)) {
if strings.Contains(str, p.base+v) {
return true return true
} }
} }
@ -221,21 +224,6 @@ func inArray(str string, list []string) bool {
return false return false
} }
// add a slash at the beginning if not exist
func slash(str string) string {
if len(str) == 0 || string(str[0]) != "/" {
str = "/" + str
}
if string(str[len(str)-1]) == "/" {
if string(str) == "/" {
return str
} else {
str = str[0 : len(str)-1]
}
}
return str
}
// 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 {