2016-08-03 08:28:58 +00:00
|
|
|
package realize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"os/exec"
|
|
|
|
"os"
|
|
|
|
"bytes"
|
2016-08-04 19:57:19 +00:00
|
|
|
"bufio"
|
|
|
|
"log"
|
2016-08-16 10:20:55 +00:00
|
|
|
"sync"
|
2016-08-17 14:56:06 +00:00
|
|
|
"strings"
|
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 14:56:06 +00:00
|
|
|
Base string
|
2016-08-04 22:59:41 +00:00
|
|
|
Name string `yaml:"app_name,omitempty"`
|
|
|
|
Path string `yaml:"app_path,omitempty"`
|
|
|
|
Main string `yaml:"app_main,omitempty"`
|
|
|
|
Run bool `yaml:"app_run,omitempty"`
|
|
|
|
Bin bool `yaml:"app_bin,omitempty"`
|
|
|
|
Build bool `yaml:"app_build,omitempty"`
|
2016-08-03 08:28:58 +00:00
|
|
|
Watcher Watcher `yaml:"app_watcher,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-08-16 10:20:55 +00:00
|
|
|
func (p *Project) GoRun(channel chan bool, wr *sync.WaitGroup) error {
|
2016-08-17 14:56:06 +00:00
|
|
|
name := strings.Split(p.Path, "/")
|
|
|
|
build := exec.Command(name[len(name)-1], os.Getenv("PATH"))
|
|
|
|
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 {
|
|
|
|
log.Fatal("failed to stop: ", err)
|
|
|
|
}
|
2016-08-16 10:20:55 +00:00
|
|
|
LogFail(p.Name + ": Stopped")
|
|
|
|
wr.Done()
|
|
|
|
}()
|
2016-08-04 19:57:19 +00:00
|
|
|
|
|
|
|
stdout, err := build.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
Fail(err.Error())
|
|
|
|
}
|
|
|
|
if err := build.Start(); err != nil {
|
|
|
|
Fail(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
in := bufio.NewScanner(stdout)
|
2016-08-17 14:56:06 +00:00
|
|
|
go func() {
|
|
|
|
for in.Scan() {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
log.Println(p.Name + ":", in.Text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for{
|
2016-08-04 19:57:19 +00:00
|
|
|
select {
|
2016-08-17 14:56:06 +00:00
|
|
|
case <-channel:
|
2016-08-04 22:59:41 +00:00
|
|
|
return nil
|
2016-08-04 19:57:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 14:56:06 +00:00
|
|
|
|
2016-08-03 08:28:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-03 14:46:36 +00:00
|
|
|
|
|
|
|
// create bin dir
|
2016-08-17 14:56:06 +00:00
|
|
|
if _, err := os.Stat(p.Base + "/bin"); err != nil {
|
|
|
|
if err = os.Mkdir(p.Base + "/bin", 0777); err != nil {
|
2016-08-03 16:49:17 +00:00
|
|
|
return err
|
2016-08-03 14:46:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 14:56:06 +00:00
|
|
|
build := exec.Command("go", "build", p.Base + p.Main)
|
|
|
|
build.Dir = p.Base + "/bin"
|
2016-08-03 08:28:58 +00:00
|
|
|
build.Stdout = &out
|
|
|
|
if err := build.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
base, _ := os.Getwd()
|
2016-08-04 19:57:19 +00:00
|
|
|
path := base + p.Path
|
2016-08-03 16:49:17 +00:00
|
|
|
|
|
|
|
build := exec.Command("go", "install")
|
|
|
|
build.Dir = path
|
|
|
|
build.Stdout = &out
|
|
|
|
if err := build.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-03 08:28:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|