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"
"log"
"os"
"strings"
)
func main() {
@ -30,15 +29,6 @@ func main() {
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{
Name: app.Name,
Version: app.Version,
@ -74,7 +64,6 @@ func main() {
},
Action: func(p *cli.Context) error {
y := r.New(p)
y.Projects[0].Path = "/"
return handle(y.Fast(p))
},
Before: func(c *cli.Context) error {
@ -88,7 +77,7 @@ func main() {
Aliases: []string{"a"},
Usage: "Add another project",
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.BoolFlag{Name: "build", Value: false, Usage: "Enable go build"},
&cli.BoolFlag{Name: "norun", Usage: "Disables the run"},

View File

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

View File

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

View File

@ -31,8 +31,8 @@ func (h *Config) Watch() error {
// loop projects
wg.Add(len(h.Projects))
for k := range h.Projects {
h.Projects[k].Path = slash(h.Projects[k].Path)
go h.Projects[k].Watching()
h.Projects[k].Path = h.Projects[k].Path
go h.Projects[k].watching()
}
wg.Wait()
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
func (h *Config) Fast(params *cli.Context) error {
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 err := h.Read(); err == nil {
for _, val := range h.Projects {
@ -54,26 +54,35 @@ func (h *Config) Fast(params *cli.Context) error {
}
}
wg.Add(1)
fast.Path = slash(fast.Path)
go fast.Watching()
go fast.watching()
wg.Wait()
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
func (p *Project) Watching() {
func (p *Project) watching() {
var wr sync.WaitGroup
var watcher *fsnotify.Watcher
watcher, err := fsnotify.NewWatcher()
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)
base, err := os.Getwd()
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 {
if !p.ignore(path) {
@ -88,25 +97,20 @@ func (p *Project) Watching() {
}
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() {
watcher.Close()
wg.Done()
}
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 {
dir = slash(dir)
base = p.base + dir
base := filepath.Join(p.base, dir)
if _, err := os.Stat(base); err == nil {
if err := filepath.Walk(base, walk); err != nil {
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(Red("Watching: " + pname(p.Name, 1) + "\n"))
routines()
go routines(p, channel, &wr)
p.reload = time.Now().Truncate(time.Second)
for {
select {
@ -133,8 +138,7 @@ func (p *Project) Watching() {
// stop and run again
close(channel)
wr.Wait()
routines()
go routines(p, channel, &wr)
p.reload = time.Now().Truncate(time.Second)
}
}
@ -203,8 +207,7 @@ func (p *Project) fmt() error {
// Ignore validates a path
func (p *Project) ignore(str string) bool {
for _, v := range p.Watcher.Ignore {
v = slash(v)
if strings.Contains(str, p.base+v) {
if strings.Contains(str, filepath.Join(p.base, v)) {
return true
}
}
@ -221,21 +224,6 @@ func inArray(str string, list []string) bool {
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
func pname(name string, color int) string {
switch color {