realize/realize/project.go

85 lines
1.5 KiB
Go
Raw Normal View History

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-03 08:28:58 +00:00
)
type Project struct {
reload time.Time
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"`
Watcher Watcher `yaml:"app_watcher,omitempty"`
}
2016-08-04 19:57:19 +00:00
func (p *Project) GoRun (channel chan bool) error{
base, _ := os.Getwd()
build := exec.Command("go", "run", p.Main)
path := base + p.Path
build.Dir = path
stdout, err := build.StdoutPipe()
if err != nil {
Fail(err.Error())
}
if err := build.Start(); err != nil {
Fail(err.Error())
}
in := bufio.NewScanner(stdout)
for in.Scan() {
select {
default:
2016-08-04 20:19:56 +00:00
log.Println(p.Name+":",in.Text())
2016-08-04 19:57:19 +00:00
case <- channel:
return nil
}
}
2016-08-03 08:28:58 +00:00
return nil
}
func (p *Project) GoBuild() error{
var out bytes.Buffer
base, _ := os.Getwd()
2016-08-03 14:46:36 +00:00
path := base + p.Path
// create bin dir
if _, err := os.Stat(path + "bin"); err != nil {
if err = os.Mkdir(path + "bin", 0777); err != nil{
2016-08-03 16:49:17 +00:00
return err
2016-08-03 14:46:36 +00:00
}
}
2016-08-03 16:49:17 +00:00
2016-08-03 14:46:36 +00:00
build := exec.Command("go", "build", path + p.Main)
build.Dir = path + "bin"
2016-08-03 08:28:58 +00:00
build.Stdout = &out
if err := build.Run(); err != nil {
return err
}
return nil
}
2016-08-03 16:49:17 +00:00
func (p *Project) GoInstall() error{
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
}