2016-07-12 08:18:02 +00:00
package main
import (
2016-11-01 09:56:12 +00:00
"fmt"
2017-10-24 13:10:54 +00:00
"go/build"
2017-10-08 21:09:45 +00:00
"gopkg.in/urfave/cli.v2"
"log"
2017-09-18 01:05:00 +00:00
"os"
2017-11-19 23:52:31 +00:00
"os/signal"
2017-10-22 16:44:44 +00:00
"path/filepath"
2017-11-19 23:52:31 +00:00
"strings"
"syscall"
2017-09-18 01:05:00 +00:00
"time"
2016-07-12 08:18:02 +00:00
)
2016-11-01 09:56:12 +00:00
const (
2017-11-19 23:52:31 +00:00
RPrefix = "realize"
RVersion = "2.0"
RExt = ".yaml"
RFile = RPrefix + RExt
RDir = "." + RPrefix
RExtWin = ".exe"
2016-11-01 09:56:12 +00:00
)
2017-11-19 23:52:31 +00:00
type (
Realize struct {
Settings Settings ` yaml:"settings" json:"settings" `
Server Server ` yaml:"server" json:"server" `
Schema ` yaml:",inline" `
sync chan string
exit chan os . Signal
}
LogWriter struct { }
)
2017-10-08 21:09:45 +00:00
2017-11-20 08:18:18 +00:00
// init check
2017-11-19 23:52:31 +00:00
func init ( ) {
// custom log
log . SetFlags ( 0 )
log . SetOutput ( LogWriter { } )
if build . Default . GOPATH == "" {
log . Fatal ( "$GOPATH isn't set properly" )
}
if err := os . Setenv ( "GOBIN" , filepath . Join ( build . Default . GOPATH , "bin" ) ) ; err != nil {
log . Fatal ( err )
}
2017-08-27 11:45:06 +00:00
}
2016-11-01 09:56:12 +00:00
2017-11-19 23:52:31 +00:00
// Realize cli commands
2017-08-27 11:45:06 +00:00
func main ( ) {
2017-03-12 22:46:58 +00:00
app := & cli . App {
2017-11-19 23:52:31 +00:00
Name : strings . Title ( RPrefix ) ,
Version : RVersion ,
Description : "Realize is the #1 Golang Task Runner which enhance your workflow by automating the most common tasks and using the best performing Golang live reloading." ,
2016-07-12 18:03:22 +00:00
Commands : [ ] * cli . Command {
{
2017-10-08 21:09:45 +00:00
Name : "start" ,
2017-11-13 21:55:11 +00:00
Aliases : [ ] string { "s" } ,
2017-11-19 23:52:31 +00:00
Description : "Start " + strings . Title ( RPrefix ) + " on a given path. If not exist a config file it creates a new one." ,
2016-08-21 18:15:01 +00:00
Flags : [ ] cli . Flag {
2017-11-12 10:16:45 +00:00
& cli . StringFlag { Name : "path" , Aliases : [ ] string { "p" } , Value : "." , Usage : "Project base path" } ,
2017-10-12 14:27:55 +00:00
& cli . StringFlag { Name : "name" , Aliases : [ ] string { "n" } , Value : "" , Usage : "Run a project by its name" } ,
& cli . BoolFlag { Name : "fmt" , Aliases : [ ] string { "f" } , Value : false , Usage : "Enable go fmt" } ,
& cli . BoolFlag { Name : "vet" , Aliases : [ ] string { "v" } , Value : false , Usage : "Enable go vet" } ,
& 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 : "server" , Aliases : [ ] string { "s" } , Value : false , Usage : "Enable server and open into the default browser" } ,
& cli . BoolFlag { Name : "install" , Aliases : [ ] string { "i" } , Value : false , Usage : "Enable go install" } ,
& cli . BoolFlag { Name : "build" , Aliases : [ ] string { "b" } , Value : false , Usage : "Enable go build" } ,
2017-09-17 21:37:39 +00:00
& cli . BoolFlag { Name : "run" , Aliases : [ ] string { "nr" } , Value : false , Usage : "Enable go run" } ,
2017-10-12 14:27:55 +00:00
& cli . BoolFlag { Name : "no-config" , Aliases : [ ] string { "nc" } , Value : false , Usage : "Ignore existing config and doesn't create a new one" } ,
2016-08-21 18:15:01 +00:00
} ,
2017-11-19 23:52:31 +00:00
Action : func ( c * cli . Context ) error {
return start ( c )
2016-07-12 18:03:22 +00:00
} ,
} ,
2016-12-26 23:14:29 +00:00
{
2017-04-13 13:53:58 +00:00
Name : "add" ,
Category : "Configuration" ,
Aliases : [ ] string { "a" } ,
2017-11-19 23:52:31 +00:00
Description : "Add a project to an existing config or to a new one." ,
2016-12-26 23:14:29 +00:00
Flags : [ ] cli . Flag {
2017-10-22 16:44:44 +00:00
& cli . StringFlag { Name : "path" , Aliases : [ ] string { "p" } , Value : wdir ( ) , Usage : "Project base path" } ,
2017-10-12 14:27:55 +00:00
& cli . BoolFlag { Name : "fmt" , Aliases : [ ] string { "f" } , Value : false , Usage : "Enable go fmt" } ,
& cli . BoolFlag { Name : "vet" , Aliases : [ ] string { "v" } , Value : false , Usage : "Enable go vet" } ,
& 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" } ,
2017-09-17 21:37:39 +00:00
& cli . BoolFlag { Name : "install" , Aliases : [ ] string { "i" } , Value : false , Usage : "Enable go install" } ,
& cli . BoolFlag { Name : "build" , Aliases : [ ] string { "b" } , Value : false , Usage : "Enable go build" } ,
2017-10-12 14:27:55 +00:00
& cli . BoolFlag { Name : "run" , Aliases : [ ] string { "nr" } , Value : false , Usage : "Enable go run" } ,
2016-12-26 23:14:29 +00:00
} ,
2017-11-19 23:52:31 +00:00
Action : func ( c * cli . Context ) error {
return add ( c )
2016-12-26 23:14:29 +00:00
} ,
} ,
2016-07-23 22:49:19 +00:00
{
2017-04-13 13:53:58 +00:00
Name : "init" ,
Category : "Configuration" ,
2017-10-23 12:32:42 +00:00
Aliases : [ ] string { "i" } ,
2017-11-19 23:52:31 +00:00
Description : "Make a new config file step by step." ,
Action : func ( c * cli . Context ) error {
return setup ( c )
2016-07-23 22:49:19 +00:00
} ,
2016-07-12 18:03:22 +00:00
} ,
2016-07-26 17:04:13 +00:00
{
2017-04-13 13:53:58 +00:00
Name : "remove" ,
Category : "Configuration" ,
Aliases : [ ] string { "r" } ,
2017-11-19 23:52:31 +00:00
Description : "Remove a project from an existing config." ,
2016-07-26 17:04:13 +00:00
Flags : [ ] cli . Flag {
2016-08-21 07:16:01 +00:00
& cli . StringFlag { Name : "name" , Aliases : [ ] string { "n" } , Value : "" } ,
2016-07-26 17:04:13 +00:00
} ,
2017-11-19 23:52:31 +00:00
Action : func ( c * cli . Context ) error {
return remove ( c )
2016-07-26 17:04:13 +00:00
} ,
} ,
2016-12-26 23:14:29 +00:00
{
2017-04-13 13:53:58 +00:00
Name : "clean" ,
Category : "Configuration" ,
Aliases : [ ] string { "c" } ,
2017-11-19 23:52:31 +00:00
Description : "Remove " + strings . Title ( RPrefix ) + " folder." ,
Action : func ( c * cli . Context ) error {
return clean ( )
2016-12-26 23:14:29 +00:00
} ,
} ,
2017-10-23 12:32:42 +00:00
{
Name : "version" ,
Aliases : [ ] string { "v" } ,
2017-11-19 23:52:31 +00:00
Description : "Print " + strings . Title ( RPrefix ) + " version." ,
2017-10-23 12:32:42 +00:00
Action : func ( p * cli . Context ) error {
2017-11-19 23:52:31 +00:00
version ( )
2017-10-23 12:32:42 +00:00
return nil
} ,
} ,
2016-07-12 18:03:22 +00:00
} ,
}
2017-04-13 14:09:53 +00:00
if err := app . Run ( os . Args ) ; err != nil {
2017-11-19 23:52:31 +00:00
log . Fatal ( err )
2017-04-13 14:09:53 +00:00
os . Exit ( 1 )
}
2016-08-17 23:35:37 +00:00
}
2017-08-27 11:45:06 +00:00
2017-11-19 23:52:31 +00:00
// Stop realize workflow
func ( r * Realize ) Stop ( ) {
close ( r . exit )
2017-10-12 14:27:55 +00:00
}
2017-11-19 23:52:31 +00:00
// Run realize workflow
func ( r * Realize ) Start ( ) {
r . exit = make ( chan os . Signal , 2 )
signal . Notify ( r . exit , os . Interrupt , syscall . SIGTERM )
for k := range r . Schema . Projects {
r . Schema . Projects [ k ] . parent = r
r . Schema . Projects [ k ] . Setup ( )
go r . Schema . Projects [ k ] . Watch ( r . exit )
}
for {
select {
case <- r . exit :
return
}
2017-08-27 11:45:06 +00:00
}
}
2017-11-19 23:52:31 +00:00
// Prefix a given string with tool name
func ( r * Realize ) Prefix ( input string ) string {
if len ( input ) > 0 {
return fmt . Sprint ( yellow . bold ( "[" ) , strings . ToUpper ( RPrefix ) , yellow . bold ( "]" ) , " : " , input )
2017-08-27 11:45:06 +00:00
}
2017-11-19 23:52:31 +00:00
return input
2017-08-27 11:45:06 +00:00
}
2017-10-08 21:09:45 +00:00
// Rewrite the layout of the log timestamp
2017-11-19 23:52:31 +00:00
func ( w LogWriter ) Write ( bytes [ ] byte ) ( int , error ) {
2017-10-22 16:44:44 +00:00
return fmt . Fprint ( output , yellow . regular ( "[" ) , time . Now ( ) . Format ( "15:04:05" ) , yellow . regular ( "]" ) , string ( bytes ) )
2017-08-27 11:45:06 +00:00
}