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

258
cmd.go
View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"reflect"
) )
// Tool options customizable, should be moved in Cmd // 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 // Add a new project
func (r *realize) add(p *cli.Context) error { func (r *realize) add(p *cli.Context) error {
project := Project{ project := Project{
@ -88,7 +80,7 @@ func (r *realize) add(p *cli.Context) error {
Args: params(p), Args: params(p),
Watcher: Watch{ Watcher: Watch{
Paths: []string{"/"}, Paths: []string{"/"},
Ignore: []string{"vendor"}, Ignore: []string{".git",".realize","vendor"},
Exts: []string{"go"}, Exts: []string{"go"},
}, },
} }
@ -102,127 +94,133 @@ func (r *realize) add(p *cli.Context) error {
// Run launches the toolchain for each project // Run launches the toolchain for each project
func (r *realize) run(p *cli.Context) error { func (r *realize) run(p *cli.Context) error {
var match bool var match bool
err := r.check() // check projects and remove duplicates
if err == nil { if len(r.Schema) > 0 {
// loop projects r.clean()
if p.String("name") != "" { }else{
wg.Add(1) return errors.New("there are no projects")
} else {
wg.Add(len(r.Schema))
}
for k, elm := range r.Schema {
// command start using name flag
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() {
// if f.CanSet() {
// 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", "./"}
}
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Fmt.Status,
cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method),
options: split([]string{}, elm.Cmds.Fmt.Args),
name: "Fmt",
})
}
if elm.Cmds.Generate.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Generate.Status,
cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method),
options: split([]string{}, elm.Cmds.Generate.Args),
name: "Generate",
dir: true,
})
}
if elm.Cmds.Test.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Test.Status,
cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method),
options: split([]string{}, elm.Cmds.Test.Args),
name: "Test",
dir: true,
})
}
if elm.Cmds.Vet.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Vet.Status,
cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method),
options: split([]string{}, elm.Cmds.Vet.Args),
name: "Vet",
dir: true,
})
}
// default settings
r.Schema[k].Cmds.Install = Cmd{
Status: elm.Cmds.Install.Status,
Args: append([]string{}, elm.Cmds.Install.Args...),
method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method),
name: "Install",
startTxt: "Installing...",
endTxt: "Installed",
}
r.Schema[k].Cmds.Build = Cmd{
Status: elm.Cmds.Build.Status,
Args: append([]string{}, elm.Cmds.Build.Args...),
method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method),
name: "Build",
startTxt: "Building...",
endTxt: "Built",
}
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()
}
if !match {
return errors.New("there is no project with the given name")
}
wg.Wait()
} }
// 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)
} else {
wg.Add(len(r.Schema))
}
for k, elm := range r.Schema {
// command start using name flag
if p.String("name") != "" && r.Schema[k].Name != p.String("name") {
continue
}
// 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() {
// 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", "./"}
}
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Fmt.Status,
cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method),
options: split([]string{}, elm.Cmds.Fmt.Args),
name: "Fmt",
})
}
if elm.Cmds.Generate.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Generate.Status,
cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method),
options: split([]string{}, elm.Cmds.Generate.Args),
name: "Generate",
dir: true,
})
}
if elm.Cmds.Test.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Test.Status,
cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method),
options: split([]string{}, elm.Cmds.Test.Args),
name: "Test",
dir: true,
})
}
if elm.Cmds.Vet.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Vet.Status,
cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method),
options: split([]string{}, elm.Cmds.Vet.Args),
name: "Vet",
dir: true,
})
}
// default settings
r.Schema[k].Cmds.Install = Cmd{
Status: elm.Cmds.Install.Status,
Args: append([]string{}, elm.Cmds.Install.Args...),
method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method),
name: "Install",
startTxt: "Installing...",
endTxt: "Installed",
}
r.Schema[k].Cmds.Build = Cmd{
Status: elm.Cmds.Build.Status,
Args: append([]string{}, elm.Cmds.Build.Args...),
method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method),
name: "Build",
startTxt: "Building...",
endTxt: "Built",
}
r.Schema[k].parent = r
r.Schema[k].path = r.Schema[k].Path
match = true
go r.Schema[k].watch()
}
if !match {
return errors.New("there is no project with the given name")
}
wg.Wait()
return err return err
} }

View File

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

View File

@ -344,7 +344,7 @@ func main() {
Subs: []*interact.Question{ Subs: []*interact.Question{
{ {
Before: func(d interact.Context) error { Before: func(d interact.Context) error {
d.SetDef(r.Settings.wdir(), green.regular("("+r.Settings.wdir()+")")) d.SetDef(wdir(), green.regular("("+wdir()+")"))
return nil return nil
}, },
Quest: interact.Quest{ Quest: interact.Quest{
@ -1144,15 +1144,6 @@ func before(*cli.Context) error {
return nil 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 // Rewrite the layout of the log timestamp
func (w logWriter) Write(bytes []byte) (int, error) { func (w logWriter) Write(bytes []byte) (int, error) {
return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]"), string(bytes)) return fmt.Fprint(output, yellow.regular("["), time.Now().Format("15:04:05"), yellow.regular("]"), string(bytes))

View File

@ -4,20 +4,10 @@ import (
"errors" "errors"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"os" "os"
"path/filepath"
"strings" "strings"
"log" "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 // Array check if a string is in given array
func array(str string, list []string) bool { func array(str string, list []string) bool {
for _, v := range list { for _, v := range list {

View File

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