ignore hidden dir support

This commit is contained in:
asoseil 2017-10-22 23:22:45 +02:00
parent d02dc91443
commit bfff06d38a
5 changed files with 137 additions and 156 deletions

100
cmd.go
View File

@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"time"
"reflect"
)
// Tool options customizable, should be moved in Cmd
@ -50,15 +51,6 @@ func (r *realize) clean() {
}
}
// Check whether there is a project
func (r *realize) check() error {
if len(r.Schema) > 0 {
r.clean()
return nil
}
return errors.New("there are no projects")
}
// Add a new project
func (r *realize) add(p *cli.Context) error {
project := Project{
@ -88,7 +80,7 @@ func (r *realize) add(p *cli.Context) error {
Args: params(p),
Watcher: Watch{
Paths: []string{"/"},
Ignore: []string{"vendor"},
Ignore: []string{".git",".realize","vendor"},
Exts: []string{"go"},
},
}
@ -102,8 +94,17 @@ func (r *realize) add(p *cli.Context) error {
// Run launches the toolchain for each project
func (r *realize) run(p *cli.Context) error {
var match bool
err := r.check()
if err == nil {
// check projects and remove duplicates
if len(r.Schema) > 0 {
r.clean()
}else{
return errors.New("there are no projects")
}
// set gobin
err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin"))
if err != nil {
return err
}
// loop projects
if p.String("name") != "" {
wg.Add(1)
@ -115,27 +116,45 @@ func (r *realize) run(p *cli.Context) error {
if p.String("name") != "" && r.Schema[k].Name != p.String("name") {
continue
}
//fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds))
//// Loop struct Cmds fields
//for i := 0; i < fields.NumField(); i++ {
// field := fields.Type().Field(i).Name
// if fields.FieldByName(field).Type().Name() == "Cmd" {
// v := fields.FieldByName(field)
// // Loop struct Cmd
// for i := 0; i < v.NumField(); i++ {
// f := v.Field(i)
// if f.IsValid() {
// validate project path, if invalid get wdir or clean current
if !filepath.IsAbs(elm.path){
r.Schema[k].path = wdir()
}else{
r.Schema[k].path = filepath.Clean(elm.path)
}
// env variables
for key, item := range r.Schema[k].Environment {
if err := os.Setenv(key, item); err != nil {
r.Schema[k].Buffer.StdErr = append(r.Schema[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
}
}
// get basepath name
r.Schema[k].name = filepath.Base(r.Schema[k].path)
fields := reflect.Indirect(reflect.ValueOf(&r.Schema[k].Cmds))
// Loop struct Cmds fields
for i := 0; i < fields.NumField(); i++ {
field := fields.Type().Field(i).Name
if fields.FieldByName(field).Type().Name() == "Cmd" {
v := fields.FieldByName(field)
// Loop struct Cmd
for i := 0; i < v.NumField(); i++ {
//f := v.Type().Field(i).Name
//fmt.Println(f)
//if f.IsValid() {
// if f.CanSet() {
// switch f.Kind() {
// case reflect.Bool:
// case reflect.String:
// case reflect.Slice:
// }
// }
// }
// }
// fmt.Println(f.)
// //switch f.Kind() {
// //case reflect.Bool:
// //case reflect.String:
// //case reflect.Slice:
// //}
// }
//}
}
}
}
if elm.Cmds.Fmt.Status {
if len(elm.Cmds.Fmt.Args) == 0 {
elm.Cmds.Fmt.Args = []string{"-s", "-w", "-e", "./"}
@ -195,26 +214,6 @@ func (r *realize) run(p *cli.Context) error {
r.Schema[k].parent = r
r.Schema[k].path = r.Schema[k].Path
// env variables
for key, item := range r.Schema[k].Environment {
if err := os.Setenv(key, item); err != nil {
r.Schema[k].Buffer.StdErr = append(r.Schema[k].Buffer.StdErr, BufferOut{Time: time.Now(), Text: err.Error(), Type: "Env error", Stream: ""})
}
}
// base path of the project
wd, err := os.Getwd()
if err != nil {
return err
}
if elm.path == "." || elm.path == "/" {
r.Schema[k].base = wd
r.Schema[k].path = elm.wdir()
} else if filepath.IsAbs(elm.path) {
r.Schema[k].base = elm.path
} else {
r.Schema[k].base = filepath.Join(wd, elm.path)
}
match = true
go r.Schema[k].watch()
}
@ -222,7 +221,6 @@ func (r *realize) run(p *cli.Context) error {
return errors.New("there is no project with the given name")
}
wg.Wait()
}
return err
}

View File

@ -70,6 +70,7 @@ func (p *Project) goRun(stop <-chan bool, runner chan bool) {
args = append(args, a...)
}
gobin := os.Getenv("GOBIN")
path := filepath.Join(gobin, p.name)
if _, err := os.Stat(path); err == nil {

View File

@ -344,7 +344,7 @@ func main() {
Subs: []*interact.Question{
{
Before: func(d interact.Context) error {
d.SetDef(r.Settings.wdir(), green.regular("("+r.Settings.wdir()+")"))
d.SetDef(wdir(), green.regular("("+wdir()+")"))
return nil
},
Quest: interact.Quest{
@ -1144,15 +1144,6 @@ func before(*cli.Context) error {
return nil
}
// Wdir return current working directory
func wdir() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(prefix(err.Error()))
}
return dir
}
// Rewrite the layout of the log timestamp
func (w logWriter) Write(bytes []byte) (int, error) {
return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]"), string(bytes))

View File

@ -4,20 +4,10 @@ import (
"errors"
"gopkg.in/urfave/cli.v2"
"os"
"path/filepath"
"strings"
"log"
)
// getEnvPath returns the first path found in env or empty string
func getEnvPath(env string) string {
path := filepath.SplitList(os.Getenv(env))
if len(path) == 0 {
return ""
}
return path[0]
}
// Array check if a string is in given array
func array(str string, list []string) bool {
for _, v := range list {

View File

@ -55,7 +55,7 @@ type Project struct {
watcher FileWatcher
init bool
files, folders int64
base, path, lastFile string
name, path, lastFile string
tools []tool
paths []string
lastTime time.Time
@ -98,7 +98,7 @@ func (p *Project) watch() {
p.cmd(stop, "before", true)
// indexing files and dirs
for _, dir := range p.Watcher.Paths {
base := filepath.Join(p.base, dir)
base := filepath.Join(p.path, dir)
if _, err := os.Stat(base); err == nil {
if err := filepath.Walk(base, p.walk); err == nil {
p.tool(stop, base)
@ -312,7 +312,8 @@ func (p *Project) changed(event fsnotify.Event, stop chan bool) {
// Watch the files tree of a project
func (p *Project) walk(path string, info os.FileInfo, err error) error {
for _, v := range p.Watcher.Ignore {
if strings.Contains(path, filepath.Join(p.base, v)) {
s := append([]string{p.path},strings.Split(v,string(os.PathSeparator))...)
if strings.Contains(path, filepath.Join(s...)) {
return nil
}
}
@ -338,7 +339,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
case "out":
p.Buffer.StdOut = append(p.Buffer.StdOut, o)
if p.Files.Outputs.Status {
f := p.create(p.base, p.Files.Outputs.Name)
f := p.create(p.path, p.Files.Outputs.Name)
t := time.Now()
s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"}
if _, err := f.WriteString(strings.Join(s, " ")); err != nil {
@ -348,7 +349,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
case "log":
p.Buffer.StdLog = append(p.Buffer.StdLog, o)
if p.Files.Logs.Status {
f := p.create(p.base, p.Files.Logs.Name)
f := p.create(p.path, p.Files.Logs.Name)
t := time.Now()
s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Text, "\r\n"}
if stream != "" {
@ -361,7 +362,7 @@ func (p *Project) stamp(t string, o BufferOut, msg string, stream string) {
case "error":
p.Buffer.StdErr = append(p.Buffer.StdErr, o)
if p.Files.Errors.Status {
f := p.create(p.base, p.Files.Errors.Name)
f := p.create(p.path, p.Files.Errors.Name)
t := time.Now()
s := []string{t.Format("2006-01-02 15:04:05"), strings.ToUpper(p.Name), ":", o.Type, o.Text, o.Path, "\r\n"}
if stream != "" {