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

24
main.go
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() {
@ -23,7 +23,7 @@ func main() {
} }
cli := &cli.App{ cli := &cli.App{
Name: app.Name, Name: app.Name,
Version: app.Version, Version: app.Version,
Authors: []*cli.Author{ Authors: []*cli.Author{
&cli.Author{ &cli.Author{
@ -34,7 +34,7 @@ func main() {
Usage: app.Description, Usage: app.Description,
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "run", Name: "run",
Usage: "Build and watch file changes", Usage: "Build and watch file changes",
Action: func(p *cli.Context) error { Action: func(p *cli.Context) error {
y := r.New(p) y := r.New(p)
@ -49,8 +49,8 @@ func main() {
{ {
Name: "start", Name: "start",
Category: "config", Category: "config",
Aliases: []string{"s"}, Aliases: []string{"s"},
Usage: "Create the initial config", Usage: "Create the initial config",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"},
&cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go", Usage: "Project main file \t"}, &cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go", Usage: "Project main file \t"},
@ -71,8 +71,8 @@ func main() {
{ {
Name: "add", Name: "add",
Category: "config", Category: "config",
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "Add another project", Usage: "Add another project",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"}, &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App", Usage: "Project name \t"},
&cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go", Usage: "Project main file \t"}, &cli.StringFlag{Name: "main", Aliases: []string{"m"}, Value: "main.go", Usage: "Project main file \t"},
@ -93,8 +93,8 @@ func main() {
{ {
Name: "remove", Name: "remove",
Category: "config", Category: "config",
Aliases: []string{"r"}, Aliases: []string{"r"},
Usage: "Remove a project", Usage: "Remove a project",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"}, &cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: "Sample App"},
}, },
@ -110,8 +110,8 @@ func main() {
{ {
Name: "list", Name: "list",
Category: "config", Category: "config",
Aliases: []string{"l"}, Aliases: []string{"l"},
Usage: "Projects list", Usage: "Projects list",
Action: func(p *cli.Context) error { Action: func(p *cli.Context) error {
y := r.New(p) y := r.New(p)
return handle(y.List()) return handle(y.List())

View File

@ -1,19 +1,19 @@
package realize package realize
import ( import (
"github.com/fatih/color"
"sync"
"fmt" "fmt"
"github.com/fatih/color"
"log" "log"
"sync"
) )
const ( const (
app_name = "Realize" app_name = "Realize"
app_version = "v1.0" app_version = "v1.0"
app_email = "pracchia@hastega.it" app_email = "pracchia@hastega.it"
app_description = "Run, install or build your applications on file changes. Output preview and multi project support" app_description = "Run, install or build your applications on file changes. Output preview and multi project support"
app_author = "Alessio Pracchia" app_author = "Alessio Pracchia"
app_file = "realize.config.yaml" app_file = "realize.config.yaml"
) )
var wg sync.WaitGroup var wg sync.WaitGroup
@ -33,11 +33,11 @@ type App struct {
func Init() *App { func Init() *App {
return &App{ return &App{
Name: app_name, Name: app_name,
Version: app_version, Version: app_version,
Description: app_description, Description: app_description,
Author: app_author, Author: app_author,
Email: app_email, Email: app_email,
} }
} }
@ -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 {
@ -18,20 +18,20 @@ type Config struct {
// Default value // Default value
func New(params *cli.Context) *Config { func New(params *cli.Context) *Config {
return &Config{ return &Config{
file: app_file, file: app_file,
Version: "1.0", Version: "1.0",
Projects: []Project{ Projects: []Project{
{ {
Name: params.String("name"), Name: params.String("name"),
Main: params.String("main"), Main: params.String("main"),
Path: params.String("base"), Path: params.String("base"),
Run: params.Bool("run"), Run: params.Bool("run"),
Build: params.Bool("build"), Build: params.Bool("build"),
Bin: params.Bool("bin"), Bin: params.Bool("bin"),
Watcher: Watcher{ Watcher: Watcher{
Paths: watcher_paths, Paths: watcher_paths,
Ignore: watcher_ignores, Ignore: watcher_ignores,
Exts: watcher_exts, Exts: watcher_exts,
}, },
}, },
}, },
@ -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
} }
} }
@ -102,14 +102,14 @@ func (h *Config) Create(params *cli.Context) error {
func (h *Config) Add(params *cli.Context) error { func (h *Config) Add(params *cli.Context) error {
if err := h.Read(); err == nil { if err := h.Read(); err == nil {
new := Project{ new := Project{
Name: params.String("name"), Name: params.String("name"),
Main: params.String("main"), Main: params.String("main"),
Path: params.String("base"), Path: params.String("base"),
Run: params.Bool("run"), Run: params.Bool("run"),
Build: params.Bool("build"), Build: params.Bool("build"),
Watcher: Watcher{ Watcher: Watcher{
Paths: watcher_paths, Paths: watcher_paths,
Exts: watcher_exts, Exts: watcher_exts,
Ignore: watcher_ignores, Ignore: watcher_ignores,
}, },
} }
@ -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,40 +1,40 @@
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 {
reload time.Time reload time.Time
base string base string
Name string `yaml:"app_name,omitempty"` Name string `yaml:"app_name,omitempty"`
Path string `yaml:"app_path,omitempty"` Path string `yaml:"app_path,omitempty"`
Main string `yaml:"app_main,omitempty"` Main string `yaml:"app_main,omitempty"`
Run bool `yaml:"app_run,omitempty"` Run bool `yaml:"app_run,omitempty"`
Bin bool `yaml:"app_bin,omitempty"` Bin bool `yaml:"app_bin,omitempty"`
Build bool `yaml:"app_build,omitempty"` Build bool `yaml:"app_build,omitempty"`
Watcher Watcher `yaml:"app_watcher,omitempty"` Watcher Watcher `yaml:"app_watcher,omitempty"`
} }
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 {
@ -18,7 +18,7 @@ type Watcher struct {
Paths []string `yaml:"paths,omitempty"` Paths []string `yaml:"paths,omitempty"`
Ignore []string `yaml:"ignore_paths,omitempty"` Ignore []string `yaml:"ignore_paths,omitempty"`
Exts []string `yaml:"exts,omitempty"` Exts []string `yaml:"exts,omitempty"`
Preview bool `yaml:"preview,omitempty"` Preview bool `yaml:"preview,omitempty"`
} }
func (h *Config) Watch() error { func (h *Config) Watch() error {
@ -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)
@ -154,20 +156,20 @@ func (p *Project) build() {
return return
} }
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