2016-08-03 08:28:58 +00:00
|
|
|
package realize
|
|
|
|
|
|
|
|
import (
|
2016-08-04 19:57:19 +00:00
|
|
|
"bufio"
|
2016-08-17 23:35:37 +00:00
|
|
|
"bytes"
|
2016-08-21 15:12:11 +00:00
|
|
|
"io"
|
2016-08-04 19:57:19 +00:00
|
|
|
"log"
|
2016-08-17 23:35:37 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-08-23 00:07:07 +00:00
|
|
|
"path/filepath"
|
2016-08-17 23:35:37 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-08-03 08:28:58 +00:00
|
|
|
)
|
|
|
|
|
2016-08-18 07:29:36 +00:00
|
|
|
// The Project struct defines the informations about a project
|
2016-08-03 08:28:58 +00:00
|
|
|
type Project struct {
|
2016-08-04 22:59:41 +00:00
|
|
|
reload time.Time
|
2016-08-17 23:35:37 +00:00
|
|
|
base string
|
2016-08-18 07:57:02 +00:00
|
|
|
Name string `yaml:"app_name,omitempty"`
|
2016-08-17 23:35:37 +00:00
|
|
|
Path string `yaml:"app_path,omitempty"`
|
|
|
|
Run bool `yaml:"app_run,omitempty"`
|
|
|
|
Bin bool `yaml:"app_bin,omitempty"`
|
|
|
|
Build bool `yaml:"app_build,omitempty"`
|
2016-08-21 15:12:11 +00:00
|
|
|
Fmt bool `yaml:"app_fmt,omitempty"`
|
2016-08-03 08:28:58 +00:00
|
|
|
Watcher Watcher `yaml:"app_watcher,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-08-18 07:29:36 +00:00
|
|
|
// GoRun is an implementation of the bin execution
|
2016-08-17 22:24:06 +00:00
|
|
|
func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error {
|
2016-08-22 11:29:21 +00:00
|
|
|
|
2016-08-23 00:07:07 +00:00
|
|
|
stop := make(chan bool, 1)
|
|
|
|
build := exec.Command(filepath.Join(os.Getenv("GOBIN"), filepath.Base(p.Path)))
|
2016-08-17 19:34:06 +00:00
|
|
|
build.Dir = p.base
|
2016-08-16 10:20:55 +00:00
|
|
|
defer func() {
|
2016-08-16 16:36:54 +00:00
|
|
|
if err := build.Process.Kill(); err != nil {
|
2016-08-21 15:12:11 +00:00
|
|
|
log.Fatal(Red("failed to stop: "), Red(err))
|
2016-08-16 16:36:54 +00:00
|
|
|
}
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 2), ":", RedS("Stopped"))
|
2016-08-16 10:20:55 +00:00
|
|
|
wr.Done()
|
|
|
|
}()
|
2016-08-04 19:57:19 +00:00
|
|
|
|
|
|
|
stdout, err := build.StdoutPipe()
|
|
|
|
if err != nil {
|
2016-08-20 22:29:32 +00:00
|
|
|
log.Println(Red(err.Error()))
|
2016-08-20 10:47:32 +00:00
|
|
|
return err
|
2016-08-04 19:57:19 +00:00
|
|
|
}
|
|
|
|
if err := build.Start(); err != nil {
|
2016-08-20 22:29:32 +00:00
|
|
|
log.Println(Red(err.Error()))
|
2016-08-20 10:47:32 +00:00
|
|
|
return err
|
2016-08-04 19:57:19 +00:00
|
|
|
}
|
2016-08-17 22:24:06 +00:00
|
|
|
close(runner)
|
2016-08-04 19:57:19 +00:00
|
|
|
|
|
|
|
in := bufio.NewScanner(stdout)
|
2016-08-17 14:56:06 +00:00
|
|
|
go func() {
|
|
|
|
for in.Scan() {
|
|
|
|
select {
|
|
|
|
default:
|
2016-08-21 13:38:00 +00:00
|
|
|
log.Println(pname(p.Name, 3), ":", BlueS(in.Text()))
|
2016-08-17 14:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 19:34:06 +00:00
|
|
|
close(stop)
|
2016-08-17 14:56:06 +00:00
|
|
|
}()
|
|
|
|
|
2016-08-17 23:35:37 +00:00
|
|
|
for {
|
2016-08-04 19:57:19 +00:00
|
|
|
select {
|
2016-08-17 23:35:37 +00:00
|
|
|
case <-channel:
|
2016-08-17 19:34:06 +00:00
|
|
|
return nil
|
|
|
|
case <-stop:
|
2016-08-04 22:59:41 +00:00
|
|
|
return nil
|
2016-08-04 19:57:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-03 08:28:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-21 15:12:11 +00:00
|
|
|
// GoBuild is an implementation of the "go build"
|
2016-08-04 22:59:41 +00:00
|
|
|
func (p *Project) GoBuild() error {
|
2016-08-03 08:28:58 +00:00
|
|
|
var out bytes.Buffer
|
2016-08-20 22:29:32 +00:00
|
|
|
|
2016-08-20 10:47:32 +00:00
|
|
|
build := exec.Command("go", "build")
|
|
|
|
build.Dir = p.base
|
2016-08-03 08:28:58 +00:00
|
|
|
build.Stdout = &out
|
|
|
|
if err := build.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-21 15:12:11 +00:00
|
|
|
// GoInstall is an implementation of the "go install"
|
2016-08-04 22:59:41 +00:00
|
|
|
func (p *Project) GoInstall() error {
|
2016-08-03 16:49:17 +00:00
|
|
|
var out bytes.Buffer
|
2016-08-23 00:07:07 +00:00
|
|
|
err := os.Setenv("GOBIN", filepath.Join(os.Getenv("GOPATH"), "bin"))
|
2016-08-20 10:47:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-03 16:49:17 +00:00
|
|
|
build := exec.Command("go", "install")
|
2016-08-23 00:07:07 +00:00
|
|
|
build.Dir = p.base
|
2016-08-03 16:49:17 +00:00
|
|
|
build.Stdout = &out
|
|
|
|
if err := build.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-03 08:28:58 +00:00
|
|
|
return nil
|
2016-08-21 13:38:00 +00:00
|
|
|
}
|
2016-08-21 15:12:11 +00:00
|
|
|
|
2016-08-21 18:30:11 +00:00
|
|
|
// GoFmt is an implementation of the gofmt
|
2016-08-21 18:29:19 +00:00
|
|
|
func (p *Project) GoFmt() (io.Writer, error) {
|
2016-08-21 15:12:11 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
build := exec.Command("gofmt", "-s", "-w", "-e", ".")
|
|
|
|
build.Dir = p.base
|
|
|
|
build.Stdout = &out
|
|
|
|
build.Stderr = &out
|
|
|
|
if err := build.Run(); err != nil {
|
2016-08-21 18:29:19 +00:00
|
|
|
return build.Stderr, err
|
2016-08-21 15:12:11 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|