Skip to content

Commit

Permalink
Merge pull request #35 from tockins/dev
Browse files Browse the repository at this point in the history
1.2.2
  • Loading branch information
Alessio Pracchia committed Dec 18, 2016
2 parents ca6057c + 369b580 commit e18815d
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 197 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ A Go build system with file watchers, output streams and live reload. Run, build
$ realize add
```

It will create a realize.yaml file if it doesn't exist already and adds the working directory as the project.
It will create a realize.yaml file if it doesn't exist already and adds the working directory as project.

Otherwise if a config file already exists it adds the working project to the existing config file.

The add command supports the following custom parameters:
The Add command supports the following custom parameters:

```
--name="Project Name" -> Name, if not specified takes the working directory name
Expand Down
21 changes: 12 additions & 9 deletions realize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const (
version = "1.2.1"
description = "A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths"
config = "realize.yaml"
output = "outputs.log"
log = "logs.log"
streams = "streams.log"
errs = "errors.log"
logs = "logs.log"
host = "localhost"
port = 5000
port = 5001
server = true
open = false
)
Expand Down Expand Up @@ -47,9 +48,10 @@ func init() {
Flimit: 0,
},
Resources: c.Resources{
Config: config,
Output: output,
Log: log,
Config: config,
Streams: streams,
Logs: logs,
Errors: errs,
},
Server: c.Server{
Enabled: server,
Expand Down Expand Up @@ -137,9 +139,10 @@ func main() {
Flimit: 0,
},
Resources: c.Resources{
Config: config,
Output: output,
Log: log,
Config: config,
Streams: streams,
Logs: logs,
Errors: errs,
},
Server: c.Server{
Enabled: server,
Expand Down
12 changes: 6 additions & 6 deletions server/bindata.go

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"strconv"
)

// Server struct contains server informations
// Server settings
type Server struct {
*c.Settings `yaml:"-"`
*w.Blueprint `yaml:"-"`
Sync chan string `yaml:"-"`
}

// Render return a web pages defined in bindata
func render(c echo.Context, path string, mime int) error {
data, err := Asset(path)
if err != nil {
Expand All @@ -45,7 +46,7 @@ func render(c echo.Context, path string, mime int) error {
return nil
}

// Server starting
// Start the web server
func (s *Server) Start(p *cli.Context) (err error) {
if !p.Bool("no-server") && s.Enabled {
e := echo.New()
Expand Down Expand Up @@ -82,8 +83,7 @@ func (s *Server) Start(p *cli.Context) (err error) {
})

//websocket
//e.GET("/ws", echo.WrapHandler(s.projects()))
e.GET("/ws", s.hello)
e.GET("/ws", s.projects)

go e.Start(string(s.Settings.Server.Host) + ":" + strconv.Itoa(s.Settings.Server.Port))
if s.Open || p.Bool("open") {
Expand All @@ -98,22 +98,24 @@ func (s *Server) Start(p *cli.Context) (err error) {
return nil
}

func (s *Server) hello(c echo.Context) error {
func (s *Server) projects(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
msg, _ := json.Marshal(s.Blueprint.Projects)
err := websocket.Message.Send(ws, string(msg))
for {
select {
case <-s.Sync:
msg, _ := json.Marshal(s.Blueprint.Projects)
err = websocket.Message.Send(ws, string(msg))
if err != nil {
//log.Println(err)
break
go func() {
for {
select {
case <-s.Sync:
msg, _ := json.Marshal(s.Blueprint.Projects)
err = websocket.Message.Send(ws, string(msg))
if err != nil {
break
}
}
}

}()
for {
// Read
text := ""
err := websocket.Message.Receive(ws, &text)
Expand All @@ -123,7 +125,6 @@ func (s *Server) hello(c echo.Context) error {
} else {
err := json.Unmarshal([]byte(text), &s.Blueprint.Projects)
if err != nil {
//log.Println(err)
break
}
}
Expand Down
17 changes: 10 additions & 7 deletions server/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var cmd map[string]string
var stderr bytes.Buffer

// Init an associative array with the os supported
func init() {
cmd = map[string]string{
"windows": "start",
Expand All @@ -19,15 +20,17 @@ func init() {
}
}

// Open a url in the default browser
func Open(url string) (io.Writer, error) {
if open, err := cmd[runtime.GOOS]; !err {
open, err := cmd[runtime.GOOS]
if !err {
return nil, errors.New("This operating system is not supported.")
} else {
cmd := exec.Command(open, url)
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return cmd.Stderr, err
}
}
cmd := exec.Command(open, url)
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return cmd.Stderr, err
}

return nil, nil
}
21 changes: 21 additions & 0 deletions settings/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,85 @@ import (
"github.com/fatih/color"
)

// Colors allowed
type Colors struct {
Red
Blue
Yellow
Magenta
Green
}

// Red color
type Red struct{}

// Blue color
type Blue struct{}

// Yellow color
type Yellow struct{}

// Magenta color
type Magenta struct{}

// Green color
type Green struct{}

// Regular font in red
func (c Red) Regular(t ...interface{}) string {
r := color.New(color.FgRed).SprintFunc()
return r(t...)
}

// Bold font in red
func (c Red) Bold(t ...interface{}) string {
r := color.New(color.FgRed, color.Bold).SprintFunc()
return r(t...)
}

// Regular font in blue
func (c Blue) Regular(t ...interface{}) string {
r := color.New(color.FgBlue).SprintFunc()
return r(t...)
}

// Bold font in blue
func (c Blue) Bold(t ...interface{}) string {
r := color.New(color.FgBlue, color.Bold).SprintFunc()
return r(t...)
}

// Regular font in yellow
func (c Yellow) Regular(t ...interface{}) string {
r := color.New(color.FgYellow).SprintFunc()
return r(t...)
}

// Bold font in red
func (c Yellow) Bold(t ...interface{}) string {
r := color.New(color.FgYellow, color.Bold).SprintFunc()
return r(t...)
}

// Regular font in magenta
func (c Magenta) Regular(t ...interface{}) string {
r := color.New(color.FgMagenta).SprintFunc()
return r(t...)
}

// Bold font in magenta
func (c Magenta) Bold(t ...interface{}) string {
r := color.New(color.FgMagenta, color.Bold).SprintFunc()
return r(t...)
}

// Regular font in green
func (c Green) Regular(t ...interface{}) string {
r := color.New(color.FgGreen).SprintFunc()
return r(t...)
}

// Bold font in red
func (c Green) Bold(t ...interface{}) string {
r := color.New(color.FgGreen, color.Bold).SprintFunc()
return r(t...)
Expand Down
2 changes: 1 addition & 1 deletion settings/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
)

// Scan return a byte stream of a given file
// Stream return a byte stream of a given file
func (s Settings) Stream(file string) ([]byte, error) {
_, err := os.Stat(file)
if err == nil {
Expand Down
11 changes: 8 additions & 3 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@ import (
"os"
)

// Settings defines a group of general settings
type Settings struct {
Colors `yaml:"-"`
Resources `yaml:"resources" json:"resources"`
Server `yaml:"server" json:"server"`
Config `yaml:"config" json:"config"`
}

// Config defines structural options
type Config struct {
Flimit uint64 `yaml:"flimit" json:"flimit"`
}

// Server settings, used for the web panel
type Server struct {
Enabled bool `yaml:"enable" json:"enable"`
Open bool `yaml:"open" json:"open"`
Host string `yaml:"host" json:"host"`
Port int `yaml:"port" json:"port"`
}

// Resources defines the files generated by realize
type Resources struct {
Config string `yaml:"-" json:"-"`
Output string `yaml:"output" json:"output"`
Log string `yaml:"log" json:"log"`
Config string `yaml:"-" json:"-"`
Streams string `yaml:"streams" json:"output"`
Logs string `yaml:"logs" json:"log"`
Errors string `yaml:"errors" json:"error"`
}

// Read from config file
Expand Down
3 changes: 3 additions & 0 deletions settings/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import (
"path/filepath"
)

// Wdir return the current working directory
func (s Settings) Wdir() string {
dir, err := os.Getwd()
s.Validate(err)
return filepath.Base(dir)
}

// Validate checks a fatal error
func (s Settings) Validate(err error) error {
if err != nil {
s.Fatal(err, "")
}
return nil
}

// Fatal prints a fatal error with its additional messages
func (s Settings) Fatal(err error, msg ...interface{}) {
if len(msg) > 0 {
log.Fatalln(s.Red.Regular(msg...), err.Error())
Expand Down
7 changes: 3 additions & 4 deletions watcher/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

// Watch method adds the given paths on the Watcher
// Run launches the toolchain for each project
func (h *Blueprint) Run() error {
err := h.check()
if err == nil {
Expand Down Expand Up @@ -68,7 +68,7 @@ func (h *Blueprint) Clean() {
}
}

// Inserts a new project in the list
// Insert a new project in projects list
func (h *Blueprint) Insert(p *cli.Context) error {
err := h.Add(p)
return err
Expand Down Expand Up @@ -134,9 +134,8 @@ func (h *Blueprint) check() error {
if len(h.Projects) > 0 {
h.Clean()
return nil
} else {
return errors.New("There are no projects. The config file is empty.")
}
return errors.New("There are no projects. The config file is empty.")
}

// NameParam check the project name presence. If empty takes the working directory name
Expand Down

0 comments on commit e18815d

Please sign in to comment.