Improve the iris run command as requested here https://github.com/kataras/iris/issues/192

This commit is contained in:
Makis Maropoulos 2016-06-24 05:17:22 +03:00
parent 95813bf36b
commit 3e692804f2
5 changed files with 37 additions and 12 deletions

View File

@ -27,6 +27,8 @@ Underline changes, libraries used by iris' base code:
- [Fix or disable colors in iris run](https://github.com/kataras/iris/issues/217).
Improvements to the `iris run` **command**, as requested [here](https://github.com/kataras/iris/issues/192).
[Book](https://kataras.gitbooks.io/iris/content/) and [examples](https://github.com/iris-contrib/examples) are **updated** also.
## 3.0.0-rc.1 -> 3.0.0-rc.2

View File

@ -7,7 +7,7 @@ This package is the command line tool for [../](https://github.com/kataras/iris
[![Iris installed screen](https://raw.githubusercontent.com/iris-contrib/website/gh-pages/assets/iris_cli_screen2.png)](https://raw.githubusercontent.com/iris-contrib/website/gh-pages/assets/iris_cli_screen2.png)
## Install
Current version: 0.0.6
Current version: 0.0.7
```sh
go get -u github.com/kataras/iris/iris

View File

@ -13,6 +13,10 @@ import (
"github.com/kataras/iris/logger"
)
const (
Version = "0.0.7"
)
var (
app *cli.App
printer *logger.Logger
@ -33,7 +37,7 @@ func init() {
defaultInstallDir := workingDir[strings.LastIndexByte(workingDir, os.PathSeparator)+1:]
// init the cli app
app = cli.NewApp("iris", "Command line tool for Iris web framework", "0.0.6")
app = cli.NewApp("iris", "Command line tool for Iris web framework", Version)
// version command
app.Command(cli.Command("version", "\t prints your iris version").Action(func(cli.Flags) error { app.Printf("%s", iris.Version); return nil }))

View File

@ -1,6 +1,7 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -38,6 +39,7 @@ func build(sourcepath string) error {
func run(executablePath string, stdout bool) (*utils.Cmd, error) {
runCmd := utils.CommandBuilder("." + utils.PathSeparator + executablePath)
runCmd.Dir = workingDir
runCmd.Stderr = os.Stderr
if stdout {
runCmd.Stdout = os.Stdout
}
@ -87,10 +89,25 @@ func runAndWatch(flags cli.Flags) error {
}
}
// here(below), we don't return the error because the -help command doesn't help the user for these errors.
subfiles, err := ioutil.ReadDir(workingDir)
if err != nil {
printer.Dangerf(err.Error())
return err
}
var paths []string
paths = append(paths, workingDir)
for _, subfile := range subfiles {
if subfile.IsDir() {
if abspath, err := filepath.Abs(workingDir + utils.PathSeparator + subfile.Name()); err == nil {
paths = append(paths, abspath)
}
}
}
// run the file watcher before all, because the user maybe has a go syntax error before the first run
utils.WatchDirectoryChanges(workingDir, func(fname string) {
utils.WatchDirectoryChanges(paths, func(fname string) {
//remove the working dir from the fname path, printer should only print the relative changed file ( from the project's path)
fname = fname[len(workingDir)+1:]
@ -101,14 +118,17 @@ func runAndWatch(flags cli.Flags) error {
}, printer)
if err := build(programPath); err != nil {
printer.Dangerf(err.Error())
return err
}
runCmd, err := run(executablePath, true)
if err != nil {
printer.Dangerf(err.Error())
return err
}
// here(below), we don't return the error because the -help command doesn't help the user for these errors.
defer func() {
printer.Dangerf("")
printer.Panic(errUnexpected)

View File

@ -340,7 +340,7 @@ func GetParentDir(targetDirectory string) string {
*/
// WatchDirectoryChanges watches a directory and fires the callback with the changed name, receives a logger just to print with red letters any errors, no need for second callback.
func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *logger.Logger) {
func WatchDirectoryChanges(paths []string, evt func(filename string), logger *logger.Logger) {
isWindows := runtime.GOOS == "windows"
watcher, werr := fsnotify.NewWatcher()
if werr != nil {
@ -357,7 +357,7 @@ func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *l
if event.Op&fsnotify.Write == fsnotify.Write {
//this is received two times, the last time is the real changed file, so
i++
if i%2 == 0 || !isWindows { // this 'hack' works for windows but I dont know if works for linux too, we can wait for issue reports here.
if i%2 == 0 || !isWindows { // this 'hack' works for windows & linux but I dont know if works for osx too, we can wait for issue reports here.
if time.Now().After(lastChange.Add(time.Duration(1) * time.Second)) {
lastChange = time.Now()
evt(event.Name)
@ -370,11 +370,10 @@ func WatchDirectoryChanges(rootPath string, evt func(filename string), logger *l
}
}
}()
werr = watcher.Add(rootPath)
if werr != nil {
for _, p := range paths {
if werr = watcher.Add(p); werr != nil {
logger.Dangerf(werr.Error())
}
}
}