This commit is contained in:
alessio 2016-08-18 01:35:37 +02:00
parent d0a68bb541
commit 2105eff1e0
5 changed files with 96 additions and 98 deletions

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"os"
"gopkg.in/urfave/cli.v2"
r "github.com/tockins/realize/realize" r "github.com/tockins/realize/realize"
"gopkg.in/urfave/cli.v2"
"os"
) )
func main() { func main() {

View File

@ -1,10 +1,10 @@
package realize package realize
import ( import (
"github.com/fatih/color"
"sync"
"fmt" "fmt"
"github.com/fatih/color"
"log" "log"
"sync"
) )
const ( const (
@ -61,4 +61,3 @@ func (app *App) Information() {
fmt.Println(blue(app.Name) + " - " + blue(app.Version)) fmt.Println(blue(app.Name) + " - " + blue(app.Version))
fmt.Println(bluel(app.Description) + "\n") fmt.Println(bluel(app.Description) + "\n")
} }

View File

@ -1,12 +1,12 @@
package realize package realize
import ( import (
"os"
"gopkg.in/yaml.v2"
"errors" "errors"
"gopkg.in/urfave/cli.v2"
"io/ioutil"
"fmt" "fmt"
"gopkg.in/urfave/cli.v2"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
) )
type Config struct { type Config struct {
@ -52,8 +52,8 @@ func Duplicates(value Project, arr []Project) bool {
func (h *Config) Clean() { func (h *Config) Clean() {
arr := h.Projects arr := h.Projects
for key, val := range arr { for key, val := range arr {
if Duplicates(val, arr[key + 1:]) { if Duplicates(val, arr[key+1:]) {
h.Projects = append(arr[:key], arr[key + 1:]...) h.Projects = append(arr[:key], arr[key+1:]...)
break break
} }
} }
@ -132,9 +132,9 @@ func (h *Config) Remove(params *cli.Context) error {
if err := h.Read(); err == nil { if err := h.Read(); err == nil {
for key, val := range h.Projects { for key, val := range h.Projects {
if params.String("name") == val.Name { if params.String("name") == val.Name {
h.Projects = append(h.Projects[:key], h.Projects[key + 1:]...) h.Projects = append(h.Projects[:key], h.Projects[key+1:]...)
err = h.Write() err = h.Write()
if err == nil{ if err == nil {
Success("Your project was successfully removed") Success("Your project was successfully removed")
} }
return err return err
@ -169,5 +169,3 @@ func (h *Config) List() error {
return err return err
} }
} }

View File

@ -1,14 +1,14 @@
package realize package realize
import ( import (
"time"
"os/exec"
"os"
"bytes"
"bufio" "bufio"
"bytes"
"log" "log"
"sync" "os"
"os/exec"
"strings" "strings"
"sync"
"time"
) )
type Project struct { type Project struct {
@ -25,16 +25,16 @@ type Project struct {
func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error { func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup) error {
name := strings.Split(p.Path, "/") name := strings.Split(p.Path, "/")
stop := make(chan bool,1) stop := make(chan bool, 1)
var run string var run string
if len(name) == 1 { if len(name) == 1 {
name := strings.Split(p.base, "/") name := strings.Split(p.base, "/")
run = name[len(name)-1] run = name[len(name)-1]
}else{ } else {
run = name[len(name)-1] run = name[len(name)-1]
} }
build := exec.Command(os.Getenv("GOPATH")+slash("bin")+slash(run)) build := exec.Command(os.Getenv("GOPATH") + slash("bin") + slash(run))
build.Dir = p.base build.Dir = p.base
defer func() { defer func() {
if err := build.Process.Kill(); err != nil { if err := build.Process.Kill(); err != nil {
@ -58,15 +58,15 @@ func (p *Project) GoRun(channel chan bool, runner chan bool, wr *sync.WaitGroup)
for in.Scan() { for in.Scan() {
select { select {
default: default:
log.Println(p.Name + ":", in.Text()) log.Println(p.Name+":", in.Text())
} }
} }
close(stop) close(stop)
}() }()
for{ for {
select { select {
case <- channel: case <-channel:
return nil return nil
case <-stop: case <-stop:
return nil return nil
@ -81,11 +81,11 @@ func (p *Project) GoBuild() error {
// create bin dir // create bin dir
if _, err := os.Stat(p.base + "/bin"); err != nil { if _, err := os.Stat(p.base + "/bin"); err != nil {
if err = os.Mkdir(p.base + "/bin", 0777); err != nil { if err = os.Mkdir(p.base+"/bin", 0777); err != nil {
return err return err
} }
} }
build := exec.Command("go", "build", p.base + p.Main) build := exec.Command("go", "build", p.base+p.Main)
build.Dir = p.base + "/bin" build.Dir = p.base + "/bin"
build.Stdout = &out build.Stdout = &out
if err := build.Run(); err != nil { if err := build.Run(); err != nil {
@ -107,4 +107,3 @@ func (p *Project) GoInstall() error {
} }
return nil return nil
} }

View File

@ -1,14 +1,14 @@
package realize package realize
import ( import (
"github.com/fsnotify/fsnotify"
"fmt" "fmt"
"path/filepath" "github.com/fsnotify/fsnotify"
"os"
"strings"
"log" "log"
"time" "os"
"path/filepath"
"strings"
"sync" "sync"
"time"
) )
type Watcher struct { type Watcher struct {
@ -41,12 +41,12 @@ func (p *Project) Watching() {
var wr sync.WaitGroup var wr sync.WaitGroup
var watcher *fsnotify.Watcher var watcher *fsnotify.Watcher
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if(err != nil){ if err != nil {
Fail(p.Name + ": \t" + err.Error()) Fail(p.Name + ": \t" + err.Error())
} }
channel := make(chan bool,1) channel := make(chan bool, 1)
base, err := os.Getwd() base, err := os.Getwd()
if(err != nil){ if err != nil {
Fail(p.Name + ": \t" + err.Error()) Fail(p.Name + ": \t" + err.Error())
} }
@ -63,12 +63,14 @@ func (p *Project) Watching() {
} }
return nil return nil
} }
routines := func(){ routines := func() {
channel = make(chan bool) channel = make(chan bool)
wr.Add(1) wr.Add(1)
go p.build(); p.install(); p.run(channel, &wr); go p.build()
p.install()
p.run(channel, &wr)
} }
end := func(){ end := func() {
watcher.Close() watcher.Close()
wg.Done() wg.Done()
} }
@ -105,12 +107,12 @@ func (p *Project) Watching() {
select { select {
case event := <-watcher.Events: case event := <-watcher.Events:
if time.Now().Truncate(time.Second).After(p.reload) { if time.Now().Truncate(time.Second).After(p.reload) {
if event.Op & fsnotify.Chmod == fsnotify.Chmod { if event.Op&fsnotify.Chmod == fsnotify.Chmod {
continue continue
} }
if _, err := os.Stat(event.Name); err == nil { if _, err := os.Stat(event.Name); err == nil {
i := strings.Index(event.Name, filepath.Ext(event.Name)) i := strings.Index(event.Name, filepath.Ext(event.Name))
log.Println(green(p.Name + ":"), event.Name[:i]) log.Println(green(p.Name+":"), event.Name[:i])
// stop and run again // stop and run again
close(channel) close(channel)
@ -155,19 +157,19 @@ func (p *Project) build() {
} }
func (p *Project) run(channel chan bool, wr *sync.WaitGroup) { func (p *Project) run(channel chan bool, wr *sync.WaitGroup) {
if p.Run{ if p.Run {
if p.Bin { if p.Bin {
runner := make(chan bool,1) runner := make(chan bool, 1)
LogSuccess(p.Name + ": Running..") LogSuccess(p.Name + ": Running..")
go p.GoRun(channel, runner, wr) go p.GoRun(channel, runner, wr)
for{ for {
select { select {
case <- runner: case <-runner:
LogSuccess(p.Name + ": Runned") LogSuccess(p.Name + ": Runned")
return return
} }
} }
}else{ } else {
LogFail("Set 'app_run' to true for launch run") LogFail("Set 'app_run' to true for launch run")
} }
} }
@ -177,7 +179,7 @@ func (p *Project) run(channel chan bool, wr *sync.WaitGroup) {
func (p *Project) ignore(str string) bool { func (p *Project) ignore(str string) bool {
for _, v := range p.Watcher.Ignore { for _, v := range p.Watcher.Ignore {
v = slash(v) v = slash(v)
if strings.Contains(str, p.base + v) { if strings.Contains(str, p.base+v) {
return true return true
} }
} }
@ -193,15 +195,15 @@ func inArray(str string, list []string) bool {
return false return false
} }
func slash(str string) string{ func slash(str string) string {
if string(str[0]) != "/" { if string(str[0]) != "/" {
str = "/"+str str = "/" + str
} }
if string(str[len(str)-1]) == "/"{ if string(str[len(str)-1]) == "/" {
if(string(str) == "/"){ if string(str) == "/" {
str = "" str = ""
}else { } else {
str = str[0:len(str) - 2] str = str[0 : len(str)-2]
} }
} }
return str return str