Skip to content

Commit

Permalink
Merge pull request #13 from oneut/fix_command_executes
Browse files Browse the repository at this point in the history
Fix command executes
  • Loading branch information
oneut committed Apr 3, 2018
2 parents 2eaa51e 4ada686 commit 2a47b95
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 53 deletions.
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 77,25 @@ tasks:
aggregate_timeout: 300
commands:
go:
execute: go run main.go
executes:
- "go clean"
- "go build -o main"
- "./main"
needs_restart: true
monitor:
paths:
- ./view
js:
commands:
webpack:
execute: npm start --prefix ./public/assets
executes:
- npm start --prefix ./public/assets
needs_restart: false
watch_stdouts:
- bundle.js
test:
execute: npm test --prefix ./public/assets
executes:
- npm test --prefix ./public/assets
needs_restart: true
monitor:
paths:
Expand Down Expand Up @@ -224,7 229,10 @@ tasks:
aggregate_timeout: 300
commands:
go:
execute: go run main.go
executes:
- "go clean"
- "go build -o main"
- "./main"
needs_restart: true
monitor:
paths:
Expand All @@ -251,20 259,27 @@ tasks:
web:
commands:
go:
execute: go build
executes:
- "go clean"
- "go build -o main"
- "./main"
npm:
execute: npm start
executes:
- npm start
```

## tasks.{task_name}.commands.{command_name}.execute
実行したいコマンドを設定します。
## tasks.{task_name}.commands.{command_name}.executes
実行したいコマンドを設定します。複数設定可能です。タスクは逐次処理です。

```
tasks:
web:
commands:
go:
execute: go build
executes:
- "go clean"
- "go build -o main"
- "./main"
```

## tasks.{task_name}.commands.{command_name}.needs_restart
Expand All @@ -275,7 290,10 @@ tasks:
web:
commands:
go:
execute: go build
executes:
- "go clean"
- "go build -o main"
- "./main"
needs_restart: true
```

Expand All @@ -289,7 307,8 @@ tasks:
web:
commands:
npm:
execute: npm start
executes:
- npm start
needs_restart: false
watch_stdouts:
- bundle.js
Expand Down
75 changes: 39 additions & 36 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 18,7 @@ func NewCommand(name string, commandName string, commandConfig config.Command) C
return &Command{
commandName: commandName,
name: name,
execute: commandConfig.Execute,
executes: commandConfig.Executes,
needsRestart: commandConfig.NeedsRestart,
watchStdouts: commandConfig.WatchStdouts,
}
Expand All @@ -37,7 37,7 @@ type Command struct {
commandName string
name string
callback func(string)
execute string
executes []string
needsRestart bool
watchStdouts []string
}
Expand All @@ -49,43 49,46 @@ func (c *Command) Run(fn func(string)) {
}

func (c *Command) Start() {
args, err := shellwords.Parse(c.execute)
if err != nil {
panic(err)
}

switch len(args) {
case 0:
panic("command.execute is required")
case 1:
c.cmd = exec.Command(args[0])
default:
c.cmd = exec.Command(args[0], args[1:]...)
}

stdout, _ := c.cmd.StdoutPipe()
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
logger.InfoCommandStdout(c.name, c.commandName, line)
c.watchStdout(line)
for _, execute := range c.executes {
args, err := shellwords.Parse(execute)
if err != nil {
panic(err)
}
}()

stderr, _ := c.cmd.StderrPipe()
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
line := scanner.Text()
logger.InfoCommandStdout(c.name, c.commandName, line)

switch len(args) {
case 0:
panic("command.execute is required")
case 1:
c.cmd = exec.Command(args[0])
default:
c.cmd = exec.Command(args[0], args[1:]...)
}
}()

defer c.Kill()
c.sysProcAttr()
c.cmd.Start()
c.cmd.Wait()
stdout, _ := c.cmd.StdoutPipe()
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
logger.InfoCommandStdout(c.name, c.commandName, line)
c.watchStdout(line)
}
}()

stderr, _ := c.cmd.StderrPipe()
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
line := scanner.Text()
logger.InfoCommandStdout(c.name, c.commandName, line)
}
}()

defer c.Kill()
c.sysProcAttr()
c.cmd.Start()
logger.InfoCommand(c.name, c.commandName, "execute: " execute)
c.cmd.Wait()
}
}

func (c *Command) watchStdout(line string) {
Expand Down
10 changes: 8 additions & 2 deletions config/command.go
Original file line number Diff line number Diff line change
@@ -1,11 1,17 @@
package config

type Command struct {
Execute string
Executes []string
NeedsRestart bool `yaml:"needs_restart"`
WatchStdouts []string `yaml:"watch_stdouts"`
}

func (c *Command) IsValid() bool {
return c.Execute != ""
for _, execute := range c.Executes {
if execute != "" {
return true
}
}

return false
}
6 changes: 4 additions & 2 deletions examples/local-web-server-with-webpack/lrp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 6,8 @@ tasks:
webpack:
commands:
watch:
execute: "npm run webpack:start"
executes:
- "npm run webpack:start"
watch_stdouts:
- "Entrypoint main = bundle.js"
local-web-server:
Expand All @@ -15,4 16,5 @@ tasks:
- "./public/index.html"
commands:
http-server:
execute: "npm run hs:start -- -p 8080"
executes:
- "npm run hs:start -- -p 8080"
3 changes: 2 additions & 1 deletion examples/local-web-server/lrp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 13,5 @@ tasks:
- "*~"
commands:
hs:
execute: "npm start -- -p 8080"
executes:
- "npm start -- -p 8080"
5 changes: 4 additions & 1 deletion lrp.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,10 @@ tasks:
aggregate_timeout: 300
commands:
go:
execute: go run main.go
executes:
- "go clean"
- "go build -o main"
- "./main"
needs_restart: true
monitor:
paths:
Expand Down

0 comments on commit 2a47b95

Please sign in to comment.