realize/realize.go

218 lines
5.9 KiB
Go
Raw Normal View History

2016-07-12 08:18:02 +00:00
package main
import (
2016-11-01 09:56:12 +00:00
"errors"
"fmt"
s "github.com/tockins/realize/server"
c "github.com/tockins/realize/settings"
w "github.com/tockins/realize/watcher"
2016-08-17 23:35:37 +00:00
"gopkg.in/urfave/cli.v2"
"os"
2016-07-12 08:18:02 +00:00
)
2016-11-01 09:56:12 +00:00
const (
name = "Realize"
version = "1.2"
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 = "realize.log"
host = "localhost"
port = 5000
server = true
open = false
)
var r realize
// Realize struct contains the general app informations
type realize struct {
c.Settings `yaml:"settings,omitempty"`
Name, Description, Author, Email, Host, Version string `yaml:"-"`
Sync chan string `yaml:"-"`
Blueprint w.Blueprint `yaml:"-"`
Server s.Server `yaml:"-"`
Projects *[]w.Project `yaml:"projects"`
}
// Realize struct initialization
func init() {
r = realize{
Name: name,
Version: version,
Description: description,
Sync: make(chan string),
Settings: c.Settings{
Config: c.Config{
Flimit: 0,
},
Resources: c.Resources{
Config: config,
Output: output,
},
Server: c.Server{
Enabled: server,
Open: open,
Host: host,
Port: port,
},
},
}
r.Blueprint = w.Blueprint{
Settings: &r.Settings,
Sync: r.Sync,
}
r.Server = s.Server{
Blueprint: &r.Blueprint,
Settings: &r.Settings,
Sync: r.Sync,
}
r.Projects = &r.Blueprint.Projects
2016-09-01 22:17:19 +00:00
2016-11-01 09:56:12 +00:00
// read if exist
r.Read(&r)
// increase the file limit
if r.Config.Flimit != 0 {
r.Flimit()
}
}
// Before of every exec of a cli method
func before() error {
fmt.Println(r.Blue.Bold(name) + " - " + r.Blue.Bold(version))
fmt.Println(r.Blue.Regular(description) + "\n")
gopath := os.Getenv("GOPATH")
if gopath == "" {
return handle(errors.New("$GOPATH isn't set up properly"))
}
return nil
}
// Handle errors
func handle(err error) error {
if err != nil {
fmt.Println(r.Red.Bold(err.Error()))
os.Exit(1)
}
return nil
}
// Cli commands
2016-07-12 08:18:02 +00:00
func main() {
2016-09-17 23:04:36 +00:00
c := &cli.App{
2016-11-01 09:56:12 +00:00
Name: r.Name,
Version: r.Version,
2016-07-23 22:49:19 +00:00
Authors: []*cli.Author{
2016-08-21 14:35:17 +00:00
{
2016-08-23 13:17:44 +00:00
Name: "Alessio Pracchia",
2016-11-01 09:56:12 +00:00
Email: "pracchia@hastegit",
2016-08-23 13:17:44 +00:00
},
{
Name: "Daniele Conventi",
2016-11-01 09:56:12 +00:00
Email: "conventi@hastegit",
2016-07-23 22:49:19 +00:00
},
},
2016-11-01 09:56:12 +00:00
Usage: r.Description,
2016-07-12 18:03:22 +00:00
Commands: []*cli.Command{
{
2016-08-17 23:35:37 +00:00
Name: "run",
2016-07-12 18:03:22 +00:00
Usage: "Build and watch file changes",
2016-09-18 11:50:15 +00:00
Flags: []cli.Flag{
2016-11-01 09:56:12 +00:00
&cli.BoolFlag{Name: "no-server", Usage: "Disables the web panel"},
&cli.BoolFlag{Name: "open", Usage: "Automatically opens the web panel"},
2016-09-18 11:50:15 +00:00
},
2016-08-21 21:31:16 +00:00
Action: func(p *cli.Context) error {
2016-11-01 09:56:12 +00:00
handle(r.Server.Start(p))
handle(r.Blueprint.Run())
return nil
2016-08-21 21:31:16 +00:00
},
Before: func(c *cli.Context) error {
2016-11-01 09:56:12 +00:00
return before()
2016-08-21 21:31:16 +00:00
},
},
{
Name: "fast",
2016-08-31 12:08:15 +00:00
Usage: "Build and watch file changes for a single project without any Configuration file",
2016-08-21 18:15:01 +00:00
Flags: []cli.Flag{
2016-08-30 17:27:47 +00:00
&cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "", Usage: "Project base path"},
2016-08-22 16:08:41 +00:00
&cli.BoolFlag{Name: "build", Value: false, Usage: "Enables the build"},
&cli.BoolFlag{Name: "no-run", Usage: "Disables the run"},
&cli.BoolFlag{Name: "no-bin", Usage: "Disables the installation"},
&cli.BoolFlag{Name: "no-fmt", Usage: "Disables the fmt (go fmt)"},
2016-09-18 11:50:15 +00:00
&cli.BoolFlag{Name: "no-server", Usage: "Disables the web panel"},
&cli.BoolFlag{Name: "open", Usage: "Automatically opens the web panel"},
2016-09-18 11:50:15 +00:00
&cli.BoolFlag{Name: "test", Value: false, Usage: "Enables the tests"},
&cli.BoolFlag{Name: "config", Value: false, Usage: "Take the defined settings if exist a Configuration file."},
2016-08-21 18:15:01 +00:00
},
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
2016-11-01 09:56:12 +00:00
if !p.Bool("config") {
r.Blueprint.Projects = r.Blueprint.Projects[:0]
}
handle(r.Blueprint.Add(p))
handle(r.Server.Start(p))
handle(r.Blueprint.Fast(p))
return nil
2016-07-12 18:03:22 +00:00
},
2016-07-27 11:42:25 +00:00
Before: func(c *cli.Context) error {
2016-11-01 09:56:12 +00:00
return before()
2016-07-27 11:42:25 +00:00
},
2016-07-12 18:03:22 +00:00
},
2016-07-23 22:49:19 +00:00
{
Name: "add",
2016-08-31 12:08:15 +00:00
Category: "Configuration",
2016-08-17 23:35:37 +00:00
Aliases: []string{"a"},
Usage: "Add another project",
2016-07-23 22:49:19 +00:00
Flags: []cli.Flag{
2016-11-01 09:56:12 +00:00
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: r.Wdir(), Usage: "Project name"},
2016-08-22 16:08:41 +00:00
&cli.StringFlag{Name: "path", Aliases: []string{"b"}, Value: "/", Usage: "Project base path"},
2016-08-27 21:57:26 +00:00
&cli.BoolFlag{Name: "build", Value: false, Usage: "Enable the build"},
&cli.BoolFlag{Name: "no-run", Usage: "Disables the run"},
&cli.BoolFlag{Name: "no-bin", Usage: "Disables the installation"},
&cli.BoolFlag{Name: "no-fmt", Usage: "Disables the fmt (go fmt)"},
2016-09-18 11:50:15 +00:00
&cli.BoolFlag{Name: "test", Value: false, Usage: "Enables the tests"},
2016-07-23 22:49:19 +00:00
},
2016-11-01 09:56:12 +00:00
Action: func(p *cli.Context) (err error) {
handle(r.Blueprint.Insert(p))
handle(r.Record(r))
fmt.Println(r.Green.Bold("Your project was successfully added"))
return nil
2016-07-23 22:49:19 +00:00
},
2016-07-27 11:42:25 +00:00
Before: func(c *cli.Context) error {
2016-11-01 09:56:12 +00:00
return before()
2016-07-27 11:42:25 +00:00
},
2016-07-12 18:03:22 +00:00
},
2016-07-26 17:04:13 +00:00
{
Name: "remove",
2016-08-31 12:08:15 +00:00
Category: "Configuration",
2016-08-17 23:35:37 +00:00
Aliases: []string{"r"},
Usage: "Remove a project",
2016-07-26 17:04:13 +00:00
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: ""},
2016-07-26 17:04:13 +00:00
},
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
2016-11-01 09:56:12 +00:00
handle(r.Blueprint.Remove(p))
handle(r.Record(r))
fmt.Println(r.Green.Bold("Your project was successfully removed."))
return nil
2016-07-26 17:04:13 +00:00
},
2016-07-27 11:42:25 +00:00
Before: func(c *cli.Context) error {
2016-11-01 09:56:12 +00:00
return before()
2016-07-27 11:42:25 +00:00
},
2016-07-26 17:04:13 +00:00
},
{
Name: "list",
2016-08-31 12:08:15 +00:00
Category: "Configuration",
2016-08-17 23:35:37 +00:00
Aliases: []string{"l"},
Usage: "Projects list",
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
2016-11-01 09:56:12 +00:00
return handle(r.Blueprint.List())
2016-07-26 17:04:13 +00:00
},
2016-07-27 11:42:25 +00:00
Before: func(c *cli.Context) error {
2016-11-01 09:56:12 +00:00
return before()
2016-07-27 11:42:25 +00:00
},
2016-07-26 17:04:13 +00:00
},
2016-07-12 18:03:22 +00:00
},
}
2016-09-17 23:04:36 +00:00
c.Run(os.Args)
2016-08-17 23:35:37 +00:00
}