diff options
-rw-r--r-- | main.go | 50 |
1 files changed, 31 insertions, 19 deletions
@@ -1,7 +1,11 @@ -// [PROGRAM] will run a build command and a run command on file system changes. This is so you can +// wbr will run a build command and a run command on file system changes. This is so you can // build&run on save. // You specify a build command and run command as flags and optionally you can filter by extension. +// TODO: +// fix bug when only build command is provided that the output is like this: +// [./build.sh [./build.sh] ./main.c ] + package main import ( @@ -11,6 +15,7 @@ import ( "os/exec" "runtime/debug" "strings" + "time" "github.com/fsnotify/fsnotify" ) @@ -18,7 +23,8 @@ import ( // Colors in escape sequences var ( Reset = "\033[0m" - BoldCyan = "\033[36m" + BoldCyan = "\033[36;1m" + Cyan = "\033[36m" // Red = "\033[31m" // Green = "\033[32m" // Yellow = "\033[33m" @@ -36,8 +42,10 @@ var ( BuildCommand []string // Command run after build RunCommand []string - // Command will only run on files that have the Ext extension - Ext string + // Command will only run on files that have the Extensions extension + Extensions []string + // Layout for timestamp of command + DateLayout string = "15:04:05" // Version tag for wbr Version string ) @@ -54,12 +62,19 @@ func GetVersion() string { } func CompileRun(fileName string) { - if !strings.HasSuffix(fileName, Ext) { + found := false + for _, Ext := range Extensions { + if strings.HasSuffix(fileName, Ext) { + found = true + break + } + } + if !found { return } // Build the program, exit if return if there was an error - { + if len(BuildCommand) > 0 && BuildCommand[0] != "" { var buildCmd *exec.Cmd if len(BuildCommand) > 1 { buildCmd = exec.Command(BuildCommand[0], append(BuildCommand[1:], fileName)...) @@ -69,10 +84,11 @@ func CompileRun(fileName string) { // run the build command out, err := buildCmd.CombinedOutput() + date := time.Now().Format(DateLayout) if len(BuildCommand) > 1 { - fmt.Printf("\n[%s%s %s%s]\n", BoldCyan, BuildCommand[0], strings.Join(append(BuildCommand[1:], fileName), " "), Reset) + fmt.Printf("[%s %s%s %s%s]\n", date, Cyan, BuildCommand[0], strings.Join(append(BuildCommand[1:], fileName), " "), Reset) } else { - fmt.Printf("[%s%s %s %s%s]\n", BoldCyan, BuildCommand[0], BuildCommand, fileName, Reset) + fmt.Printf("[%s %s%s %s %s%s]\n", date, Cyan, BuildCommand[0], BuildCommand, fileName, Reset) } if len(out) > 0 { @@ -88,8 +104,8 @@ func CompileRun(fileName string) { } } - // There was no error in the build so we can execute the run command - { + if len(RunCommand) > 0 && RunCommand[0] != "" { + // There was no error in the build so we can execute the run command var runCmd *exec.Cmd if len(RunCommand) > 1 { runCmd = exec.Command(RunCommand[0], RunCommand[1:]...) @@ -97,11 +113,12 @@ func CompileRun(fileName string) { runCmd = exec.Command(RunCommand[0]) } + date := time.Now().Format(DateLayout) out, err := runCmd.CombinedOutput() if len(RunCommand) > 1 { - fmt.Printf("[%s%s %s%s]\n", BoldCyan, RunCommand[0], strings.Join(append(RunCommand[1:]), " "), Reset) + fmt.Printf("[%s %s%s %s%s]\n", date, Cyan, RunCommand[0], strings.Join(RunCommand[1:], " "), Reset) } else { - fmt.Printf("[%s%s%s]\n", BoldCyan, RunCommand[0], Reset) + fmt.Printf("[%s %s%s%s]\n", date, Cyan, RunCommand[0], Reset) } if len(out) > 0 { @@ -138,17 +155,12 @@ func main() { os.Exit(0) } - if *bc == "" { - fmt.Println("flag is required: -b") - os.Exit(1) - } - BuildCommand = strings.Split(*bc, " ") RunCommand = strings.Split(*rc, " ") - Ext = *e + Extensions = strings.Split(*e, ",") Path = *p - fmt.Printf("[%swatching '%s'%s]", BoldCyan, Path, Reset) + fmt.Printf("[%s watching '%s'%s]\n", Cyan, Path, Reset) go func() { for { |