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
import (
"os"
"gopkg.in/urfave/cli.v2"
r "github.com/tockins/realize/realize"
"gopkg.in/urfave/cli.v2"
"os"
)
func main() {

View File

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

View File

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

View File

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

View File

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