realize/realize/project.go

126 lines
2.5 KiB
Go
Raw Normal View History

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-17 14:56:06 +00:00
"strings"
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-17 14:56:06 +00:00
name := strings.Split(p.Path, "/")
2016-08-17 23:35:37 +00:00
stop := make(chan bool, 1)
2016-08-17 19:34:06 +00:00
var run string
if len(name) == 1 {
name := strings.Split(p.base, "/")
run = name[len(name)-1]
2016-08-21 13:38:00 +00:00
} else {
2016-08-17 19:34:06 +00:00
run = name[len(name)-1]
}
2016-08-21 10:35:56 +00:00
build := exec.Command(slash(os.Getenv("GOBIN")) + slash(run))
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
base, _ := os.Getwd()
2016-08-04 19:57:19 +00:00
path := base + p.Path
2016-08-21 13:37:30 +00:00
2016-08-21 13:38:00 +00:00
err := os.Setenv("GOBIN", slash(os.Getenv("GOPATH"))+slash("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")
build.Dir = path
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
func (p *Project) GoFmt() (error, io.Writer) {
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 {
return err, build.Stderr
}
return nil, nil
}