#16 supported alternative build tool

This commit is contained in:
asoseil 2017-10-16 15:21:34 +02:00
parent e5e8ff5030
commit 21d4005b6b
4 changed files with 29 additions and 22 deletions

36
cmd.go
View File

@ -12,9 +12,9 @@ import (
type tool struct { type tool struct {
dir bool dir bool
status bool status bool
cmd string
name string name string
err string err string
cmd []string
options []string options []string
} }
@ -24,7 +24,7 @@ type Cmds struct {
Fmt Cmd `yaml:"fmt,omitempty" json:"fmt,omitempty"` Fmt Cmd `yaml:"fmt,omitempty" json:"fmt,omitempty"`
Test Cmd `yaml:"test,omitempty" json:"test,omitempty"` Test Cmd `yaml:"test,omitempty" json:"test,omitempty"`
Generate Cmd `yaml:"generate,omitempty" json:"generate,omitempty"` Generate Cmd `yaml:"generate,omitempty" json:"generate,omitempty"`
Install Cmd `yaml:"install" json:"install"` Install Cmd `yaml:"install,omitempty" json:"install,omitempty"`
Build Cmd `yaml:"build,omitempty" json:"build,omitempty"` Build Cmd `yaml:"build,omitempty" json:"build,omitempty"`
Run bool `yaml:"run,omitempty" json:"run,omitempty"` Run bool `yaml:"run,omitempty" json:"run,omitempty"`
} }
@ -32,8 +32,8 @@ type Cmds struct {
// Cmd // Cmd
type Cmd struct { type Cmd struct {
Status bool `yaml:"status,omitempty" json:"status,omitempty"` Status bool `yaml:"status,omitempty" json:"status,omitempty"`
Method string `yaml:"method,omitempty" json:"method,omitempty"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"` Args []string `yaml:"args,omitempty" json:"args,omitempty"`
Method []string `yaml:"method,omitempty" json:"method,omitempty"`
method []string method []string
name, startTxt, endTxt string name, startTxt, endTxt string
} }
@ -118,35 +118,35 @@ func (r *realize) run(p *cli.Context) error {
} }
r.Schema[k].tools = append(r.Schema[k].tools, tool{ r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Fmt.Status, status: elm.Cmds.Fmt.Status,
cmd: "gofmt", cmd: replace([]string{"gofmt"}, r.Schema[k].Cmds.Fmt.Method),
options: split([]string{}, elm.Cmds.Fmt.Args), options: split([]string{}, elm.Cmds.Fmt.Args),
name: "Go Fmt", name: "Fmt",
}) })
} }
if elm.Cmds.Generate.Status { if elm.Cmds.Generate.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{ r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Generate.Status, status: elm.Cmds.Generate.Status,
cmd: "go", cmd: replace([]string{"go", "generate"}, r.Schema[k].Cmds.Generate.Method),
options: split([]string{"generate"}, elm.Cmds.Generate.Args), options: split([]string{}, elm.Cmds.Generate.Args),
name: "Go Generate", name: "Generate",
dir: true, dir: true,
}) })
} }
if elm.Cmds.Test.Status { if elm.Cmds.Test.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{ r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Test.Status, status: elm.Cmds.Test.Status,
cmd: "go", cmd: replace([]string{"go", "test"}, r.Schema[k].Cmds.Test.Method),
options: split([]string{"test"}, elm.Cmds.Test.Args), options: split([]string{}, elm.Cmds.Test.Args),
name: "Go Test", name: "Test",
dir: true, dir: true,
}) })
} }
if elm.Cmds.Vet.Status { if elm.Cmds.Vet.Status {
r.Schema[k].tools = append(r.Schema[k].tools, tool{ r.Schema[k].tools = append(r.Schema[k].tools, tool{
status: elm.Cmds.Vet.Status, status: elm.Cmds.Vet.Status,
cmd: "go", cmd: replace([]string{"go", "vet"}, r.Schema[k].Cmds.Vet.Method),
options: split([]string{"vet"}, elm.Cmds.Vet.Args), options: split([]string{}, elm.Cmds.Vet.Args),
name: "Go Vet", name: "Vet",
dir: true, dir: true,
}) })
} }
@ -154,16 +154,16 @@ func (r *realize) run(p *cli.Context) error {
r.Schema[k].Cmds.Install = Cmd{ r.Schema[k].Cmds.Install = Cmd{
Status: elm.Cmds.Install.Status, Status: elm.Cmds.Install.Status,
Args: append([]string{}, elm.Cmds.Install.Args...), Args: append([]string{}, elm.Cmds.Install.Args...),
method: []string{"go", "install"}, method: replace([]string{"go", "install"}, r.Schema[k].Cmds.Install.Method),
name: "Go Install", name: "Install",
startTxt: "Installing...", startTxt: "Installing...",
endTxt: "Installed", endTxt: "Installed",
} }
r.Schema[k].Cmds.Build = Cmd{ r.Schema[k].Cmds.Build = Cmd{
Status: elm.Cmds.Build.Status, Status: elm.Cmds.Build.Status,
Args: append([]string{}, elm.Cmds.Build.Args...), Args: append([]string{}, elm.Cmds.Build.Args...),
method: []string{"go", "build"}, method: replace([]string{"go", "build"}, r.Schema[k].Cmds.Build.Method),
name: "Go Build", name: "Build",
startTxt: "Bulding...", startTxt: "Bulding...",
endTxt: "Built", endTxt: "Built",
} }

View File

@ -193,7 +193,8 @@ func (p *Project) goTool(wg *sync.WaitGroup, stop <-chan bool, result chan<- too
if s := ext(path); s == "" || s == "go" { if s := ext(path); s == "" || s == "go" {
var out, stderr bytes.Buffer var out, stderr bytes.Buffer
done := make(chan error) done := make(chan error)
cmd := exec.Command(tool.cmd, tool.options...) tool.cmd = append(tool.cmd, tool.options...)
cmd := exec.Command(tool.cmd[0], tool.cmd[1:]...)
cmd.Dir = path cmd.Dir = path
cmd.Stdout = &out cmd.Stdout = &out
cmd.Stderr = &stderr cmd.Stderr = &stderr

View File

@ -62,6 +62,7 @@ func duplicates(value Project, arr []Project) (Project, error) {
return Project{}, nil return Project{}, nil
} }
// Get file extensions
func ext(path string) string { func ext(path string) string {
var ext string var ext string
for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- { for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
@ -74,3 +75,11 @@ func ext(path string) string {
} }
return "" return ""
} }
// Replace if isn't empty and create a new array
func replace(a []string, b string) []string {
if len(b) > 0 {
return strings.Fields(b)
}
return a
}

View File

@ -214,9 +214,6 @@ func (p *Project) compile(stop <-chan bool, cmd Cmd) error {
channel := make(chan Result) channel := make(chan Result)
go func() { go func() {
log.Println(p.pname(p.Name, 1), ":", cmd.startTxt) log.Println(p.pname(p.Name, 1), ":", cmd.startTxt)
if len(cmd.Method) > 0 {
cmd.method = cmd.Method
}
stream, err := p.goCompile(stop, cmd.method, cmd.Args) stream, err := p.goCompile(stop, cmd.method, cmd.Args)
if stream != "killed" { if stream != "killed" {
channel <- Result{stream, err} channel <- Result{stream, err}