realize/realize.go

247 lines
8.4 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.3"
2016-11-01 09:56:12 +00:00
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"
outputs = "outputs.log"
2016-12-18 23:30:58 +00:00
errs = "errors.log"
logs = "logs.log"
2016-11-01 09:56:12 +00:00
host = "localhost"
2016-12-18 23:30:58 +00:00
port = 5001
2016-11-01 09:56:12 +00:00
)
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:"-"`
2016-11-20 16:58:52 +00:00
Projects *[]w.Project `yaml:"projects" json:"projects"`
2016-11-01 09:56:12 +00:00
}
// Realize struct initialization
func init() {
r = realize{
Name: name,
Version: version,
Description: description,
Sync: make(chan string),
Settings: c.Settings{
Resources: c.Resources{
2016-12-18 23:30:58 +00:00
Config: config,
Outputs: outputs,
2016-12-18 23:30:58 +00:00
Logs: logs,
Errors: errs,
2016-11-01 09:56:12 +00:00
},
},
}
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{
{
Name: "run",
Aliases: []string{"r"},
Usage: "Run a toolchain on a project. Can be personalized, used with a single project and without make a realize config file",
2016-08-21 18:15:01 +00:00
Flags: []cli.Flag{
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path"},
&cli.IntFlag{Name: "flimit", Aliases: []string{"f"}, Usage: "Increase files limit"},
&cli.BoolFlag{Name: "legacy", Aliases: []string{"l"}, Value: false, Usage: "Enable legacy watch"},
&cli.IntFlag{Name: "legacy-delay", Aliases: []string{"ld"}, Usage: "Restarting delay for legacy watch"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"},
&cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test"},
&cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate"},
&cli.BoolFlag{Name: "preview", Aliases: []string{"prev"}, Value: false, Usage: "Print each watched file"},
&cli.BoolFlag{Name: "no-run", Aliases: []string{"nr"}, Usage: "Disable go run"},
&cli.BoolFlag{Name: "no-bin", Aliases: []string{"nb"}, Usage: "Disable go install"},
&cli.BoolFlag{Name: "no-fmt", Aliases: []string{"nf"}, Usage: "Disable go fmt"},
&cli.BoolFlag{Name: "no-config", Aliases: []string{"nc"}, Value: false, Usage: "Run ignoring an existing config file"},
&cli.BoolFlag{Name: "no-server", Aliases: []string{"ns"}, Value: false, Usage: "Disable web panel"},
&cli.BoolFlag{Name: "serv-open", Aliases: []string{"so"}, Value: false, Usage: "Open wen panel in a new browser tab"},
&cli.IntFlag{Name: "serv-port", Aliases: []string{"sp"}, Value: port, Usage: "Server port number"},
&cli.StringFlag{Name: "serv-host", Aliases: []string{"sh"}, Value: host, Usage: "Server host"},
2016-08-21 18:15:01 +00:00
},
2016-07-27 09:14:32 +00:00
Action: func(p *cli.Context) error {
r.Settings.Init(p)
if r.Settings.Config.Create || len(r.Blueprint.Projects) <= 0 {
r.Blueprint.Projects = []w.Project{}
handle(r.Blueprint.Add(p))
2016-11-01 09:56:12 +00:00
}
handle(r.Server.Start(p))
2016-11-14 07:59:47 +00:00
handle(r.Blueprint.Run())
2016-11-20 16:58:52 +00:00
handle(r.Record(r))
2016-11-01 09:56:12 +00:00
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
},
{
Name: "config",
Category: "Configuration",
Aliases: []string{"c"},
Usage: "Create/Edit a realize config",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Aliases: []string{"n"}, Value: r.Wdir(), Usage: "Project name"},
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"},
&cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test"},
&cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate"},
&cli.BoolFlag{Name: "preview", Aliases: []string{"prev"}, Value: false, Usage: "Print each watched file"},
&cli.BoolFlag{Name: "no-run", Aliases: []string{"nr"}, Usage: "Disable go run"},
&cli.BoolFlag{Name: "no-bin", Aliases: []string{"nb"}, Usage: "Disable go install"},
&cli.BoolFlag{Name: "no-fmt", Aliases: []string{"nf"}, Usage: "Disable go fmt"},
},
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
},
Before: func(c *cli.Context) error {
return before()
},
},
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 a new project to an existing realize config file",
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"},
&cli.StringFlag{Name: "path", Aliases: []string{"p"}, Value: "", Usage: "Project base path"},
&cli.BoolFlag{Name: "build", Aliases: []string{"b"}, Value: false, Usage: "Enable go build"},
&cli.BoolFlag{Name: "test", Aliases: []string{"t"}, Value: false, Usage: "Enable go test"},
&cli.BoolFlag{Name: "generate", Aliases: []string{"g"}, Value: false, Usage: "Enable go generate"},
&cli.BoolFlag{Name: "preview", Aliases: []string{"prev"}, Value: false, Usage: "Print each watched file"},
&cli.BoolFlag{Name: "no-run", Aliases: []string{"nr"}, Usage: "Disable go run"},
&cli.BoolFlag{Name: "no-bin", Aliases: []string{"nb"}, Usage: "Disable go install"},
&cli.BoolFlag{Name: "no-fmt", Aliases: []string{"nf"}, Usage: "Disable go fmt"},
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."))
2016-11-01 09:56:12 +00:00
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 from a config file",
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
},
{
Name: "clean",
Category: "Configuration",
Aliases: []string{"c"},
Usage: "Remove realize folder",
Action: func(p *cli.Context) error {
handle(r.Settings.Remove())
fmt.Println(r.Green.Bold("Realize folder successfully removed."))
return nil
},
Before: func(c *cli.Context) error {
return before()
},
},
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
}